Skip to content

Commit

Permalink
pillar/containerd: Avoid double quoted values for environment variables
Browse files Browse the repository at this point in the history
When processing variables for containers defined at the controller
level, pillar creates the environment file with the proper shell
key/value pair, i.e. varname="value". However, if the value is already
enclosed in double quotes, the final result should be something like
varname=""value"", which will be misinterpreted by the shell will break
the shim VM initrd when initializing the container. This commit fixes
this issue by removing leading and trailing spaces and double quotes, so
values will not be quoted twice.

Signed-off-by: Renê de Souza Pinto <rene@renesp.com.br>
  • Loading branch information
rene authored and eriknordmark committed Jan 18, 2024
1 parent 3db87c6 commit c95563d
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/pillar/containerd/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,13 @@ func (s *ociSpec) AddLoader(volume string) error {
for _, e := range s.Process.Env {
keyAndValueSlice := strings.SplitN(e, "=", 2)
if len(keyAndValueSlice) == 2 {
//handles Key=Value case
envContent = envContent + fmt.Sprintf("export %s=\"%s\"\n", keyAndValueSlice[0], keyAndValueSlice[1])
// handles Key=Value case
// Trim off (i.e., remove leading and trailing) spaces and
// double quotes, so we don't quote the value twice
val := strings.Trim(keyAndValueSlice[1], " \"")
envContent = envContent + fmt.Sprintf("export %s=\"%s\"\n", keyAndValueSlice[0], val)
} else {
//handles Key= case
// handles Key= case
envContent = envContent + fmt.Sprintf("export %s\n", e)
}
}
Expand Down Expand Up @@ -503,7 +506,13 @@ func (s *ociSpec) UpdateEnvVar(envVars map[string]string) {
switch k {
case eveECOCMDOverride:
if len(v) != 0 {
s.Process.Args = strings.Fields(v)
// Command string might contain several parameters
// separated by space. However, if the variable was defined
// with quotation marks, it will split wrongly, so it must
// be trimmed off first, i.e., remove leading and trailing
// spaces and double quotes
tstr := strings.Trim(v, " \"")
s.Process.Args = strings.Fields(tstr)
}
}
}
Expand Down

0 comments on commit c95563d

Please sign in to comment.