Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
Escape references in mounted files when noExpand is set to true
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Y <[email protected]>
  • Loading branch information
ghen committed Oct 5, 2023
1 parent 3681da7 commit b15a744
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
4 changes: 3 additions & 1 deletion internal/humanitec/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func convertFileMountSpec(f *score.FileMountSpec, context *templatesContext, bas
return "", nil, err
}

if !f.NoExpand {
if f.NoExpand {
content = context.Escape(content)
} else {
content = context.Substitute(content)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/humanitec/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,11 @@ func TestScoreConvert(t *testing.T) {
},
"/etc/backend/config.yml": map[string]interface{}{
"mode": "666",
"value": "DEBUG: ${resources.env.DEBUG}",
"value": "DEBUG: $\\{resources.env.DEBUG}",
},
"/etc/backend/config.txt": map[string]interface{}{
"mode": "666",
"value": "Mounted\nFile\nContent\n${resources.env.DEBUG}",
"value": "Mounted\nFile\nContent\n$\\{resources.env.DEBUG}",
},
},
"volume_mounts": map[string]interface{}{
Expand Down
20 changes: 20 additions & 0 deletions internal/humanitec/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,27 @@ func (ctx *templatesContext) Substitute(src string) string {

return ctx.mapVar(matches[2])
})
}

// Escape replaces all matching '${...}' templates in a source string with '$\{...}'
func (ctx *templatesContext) Escape(src string) string {
return placeholderRegEx.ReplaceAllStringFunc(src, func(str string) string {
// WORKAROUND: ReplaceAllStringFunc(..) does not provide match details
// https://github.com/golang/go/issues/5690
var matches = placeholderRegEx.FindStringSubmatch(str)

// SANITY CHECK
if len(matches) != 3 {
log.Printf("Error: could not find a proper match in previously captured string fragment")
return src
}

if strings.HasPrefix(matches[1], "{") {
return fmt.Sprintf("$\\%s", matches[1])
}

return matches[0]
})
}

// MapVar replaces objects and properties references with corresponding values
Expand Down
44 changes: 44 additions & 0 deletions internal/humanitec/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,50 @@ func TestMapVar(t *testing.T) {
assert.Equal(t, "${nil.db.name}", ctx.mapVar("nil.db.name"))
}

func TestEscape(t *testing.T) {
var meta = score.WorkloadMeta{
Name: "test-name",
}

var resources = score.ResourcesSpecs{
"env": score.ResourceSpec{
Type: "environment",
},
"db": score.ResourceSpec{
Type: "postgres",
},
"dns": score.ResourceSpec{
Type: "dns",
},
"service-a": score.ResourceSpec{
Type: "service",
},
}

var ext = extensions.HumanitecResourcesSpecs{
"dns": {Scope: "shared"},
}

ctx, err := buildContext(meta, resources, ext)
assert.NoError(t, err)

assert.Equal(t, "", ctx.Escape(""))
assert.Equal(t, "abc", ctx.Escape("abc"))
assert.Equal(t, "abc $$ abc", ctx.Escape("abc $$ abc"))
assert.Equal(t, "$abc", ctx.Escape("$abc"))
assert.Equal(t, "$${abc}", ctx.Escape("$${abc}"))

assert.Equal(t, "The name is '$\\{metadata.name}'", ctx.Escape("The name is '${metadata.name}'"))
assert.Equal(t, "The name is '$\\{metadata.nil}'", ctx.Escape("The name is '${metadata.nil}'"))

assert.Equal(t, "resources.env.DEBUG", ctx.Escape("resources.env.DEBUG"))

assert.Equal(t, "$\\{resources.db}", ctx.Escape("${resources.db}"))
assert.Equal(t,
"postgresql://$\\{resources.db.user}:$\\{resources.db.password}@$\\{resources.db.host}:$\\{resources.db.port}/$\\{resources.db.name}",
ctx.Escape("postgresql://${resources.db.user}:${resources.db.password}@${resources.db.host}:${resources.db.port}/${resources.db.name}"))
}

func TestSubstitute(t *testing.T) {
var meta = score.WorkloadMeta{
Name: "test-name",
Expand Down

0 comments on commit b15a744

Please sign in to comment.