Skip to content

Commit

Permalink
fixed quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
ramondeklein committed Aug 27, 2024
1 parent 7fbd1d4 commit c244afe
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
32 changes: 23 additions & 9 deletions sidecar/pkg/configuration/tenant_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ package configuration
import (
"context"
"fmt"
"log"
"sort"
"strconv"
"strings"

miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2"
Expand Down Expand Up @@ -101,14 +99,9 @@ func parseConfEnvSecret(secret *corev1.Secret) map[string]miniov2.EnvVar {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
name := strings.TrimSpace(parts[0])
value, err := strconv.Unquote(strings.TrimSpace(parts[1]))
if err != nil {
log.Printf("Syntax error for variable %s (skipped): %s", name, err)
continue
}
envMap[name] = miniov2.EnvVar{
Name: name,
Value: value,
Value: unquote(strings.TrimSpace(parts[1])),
}
}
}
Expand Down Expand Up @@ -292,7 +285,28 @@ func envVarsToFileContent(envVars []miniov2.EnvVar, configMaps map[string]*corev
value = &env.Value
}
if value != nil {
sb.WriteString(fmt.Sprintf("export %s=\"%s\"\n", env.Name, *value))
sb.WriteString(fmt.Sprintf("export %s=%q\n", env.Name, *value))
}
}
return sb.String()
}

func unquote(value string) string {
if len(value) < 2 {
return value
}
firstCh := value[0]
lastCh := value[len(value)-1]
if firstCh != lastCh || (firstCh != '\'' && firstCh != '"') {
return value
}
var sb strings.Builder
for i, ch := range value {
if i == 0 || i == len(value)-1 {
continue
}
if ch != '\\' {
sb.WriteRune(ch)
}
}
return sb.String()
Expand Down
41 changes: 41 additions & 0 deletions sidecar/pkg/configuration/tenant_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,47 @@ export MINIO_UPDATE_MINISIGN_PUBKEY="RWTx5Zr1tiHQLwG9keckT0c45M3AGeHD6IvimQHpyRy
export TEST="value"
export TEST_CONFIGMAP="test-configmap-value"
export TEST_SECRET="test-secret-value"
`,
},
{
name: "Quoted variables",
args: args{
tenant: &miniov2.Tenant{
Spec: miniov2.TenantSpec{
Configuration: &corev1.LocalObjectReference{
Name: configSecretName,
},
},
},
secrets: map[string]*corev1.Secret{
configSecretName: {
Data: map[string][]byte{"config.env": []byte(`export MINIO_ROOT_USER="minio"
export MINIO_ROOT_PASSWORD="minio123"
export MINIO_STORAGE_CLASS_STANDARD="EC:2"
export MINIO_BROWSER="on"
export MINIO_TEST1a="on"
export MINIO_TEST1b="\"'on'\""
export MINIO_TEST2a='on'
export MINIO_TEST2b='"\'on\'"'
export MINIO_TEST3=on
`)},
},
},
},
want: `export MINIO_ARGS=""
export MINIO_BROWSER="on"
export MINIO_PROMETHEUS_JOB_ID="minio-job"
export MINIO_ROOT_PASSWORD="minio123"
export MINIO_ROOT_USER="minio"
export MINIO_SERVER_URL="https://minio..svc.cluster.local:443"
export MINIO_STORAGE_CLASS_STANDARD="EC:2"
export MINIO_TEST1a="on"
export MINIO_TEST1b="\"'on'\""
export MINIO_TEST2a="on"
export MINIO_TEST2b="\"'on'\""
export MINIO_TEST3="on"
export MINIO_UPDATE="on"
export MINIO_UPDATE_MINISIGN_PUBKEY="RWTx5Zr1tiHQLwG9keckT0c45M3AGeHD6IvimQHpyRywVWGbP1aVSGav"
`,
},
}
Expand Down

0 comments on commit c244afe

Please sign in to comment.