Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow variable escaping in manifest #5516

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions internal/pkg/manifest/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
// Taken from docker/compose.
// Environment variable names consist solely of uppercase letters, digits, and underscore,
// and do not begin with a digit. (https://pubs.opengroup.org/onlinepubs/007904875/basedefs/xbd_chap08.html)
interpolatorEnvVarRegExp = regexp.MustCompile(`\${([_a-zA-Z][_a-zA-Z0-9]*)}`)
interpolatorEnvVarRegExp = regexp.MustCompile(`(\\?)\${([_a-zA-Z][_a-zA-Z0-9]*)}`)
)

// Interpolator substitutes variables in a manifest.
Expand Down Expand Up @@ -106,7 +106,14 @@ func (i *Interpolator) interpolatePart(s string) (string, error) {
replaced := s
for _, match := range matches {
// https://pkg.go.dev/regexp#Regexp.FindAllStringSubmatch
key := match[1]
key := match[2]

if match[1] == "\\" {
// variable is escaped (e.g. \${foo}) -> no substitution is desired, let's just remove the leading backslash
replaced = strings.ReplaceAll(replaced, fmt.Sprintf("\\${%s}", key), fmt.Sprintf("${%s}", key))
continue
}

currSegment := fmt.Sprintf("${%s}", key)
predefinedVal, isPredefined := i.predefinedEnvVars[key]
osVal, isEnvVarSet := os.LookupEnv(key)
Expand Down
17 changes: 17 additions & 0 deletions internal/pkg/manifest/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ network:
- sg-3
`,
},
"should not substitute escaped dollar signs": {
inputStr: "echo \\${name}",
inputEnvVar: map[string]string{
"name": "this variable should not be read",
},
wanted: "echo ${name}\n",
},
"should substitute variables right after one another": {
inputStr: "${a}${b}\\${c}${d}",
inputEnvVar: map[string]string{
"a": "A",
"b": "B",
"c": "C",
"d": "D",
},
wanted: "AB${c}D\n",
},
}

for name, tc := range testCases {
Expand Down
12 changes: 12 additions & 0 deletions site/content/docs/developing/manifest-env-var.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,15 @@ Copilot will substitute `${COPILOT_APPLICATION_NAME}` and `${COPILOT_ENVIRONMENT
$ copilot svc deploy --app my-app --env test
```
to deploy the service to the `test` environment in your `my-app` application, Copilot will resolve `/copilot/${COPILOT_APPLICATION_NAME}/${COPILOT_ENVIRONMENT_NAME}/secrets/db_password` to `/copilot/my-app/test/secrets/db_password`. (For more information of secret injection, see [here](../developing/secrets.en.md)).

## Escaping
If variable substitution is undesired, add a leading backslash:

```yaml
command: echo hello \${name}
# or command: "echo \\${name}"
variable:
name: world
```

In this case Copilot will not attempt to substitute `${name}` with the value of the environment variable `name`.