From c244afe12ab6d060183f0d058265bff494adc721 Mon Sep 17 00:00:00 2001 From: Ramon de Klein Date: Tue, 27 Aug 2024 14:51:19 +0200 Subject: [PATCH] fixed quoting --- .../pkg/configuration/tenant_configuration.go | 32 +++++++++++---- .../tenant_configuration_test.go | 41 +++++++++++++++++++ 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/sidecar/pkg/configuration/tenant_configuration.go b/sidecar/pkg/configuration/tenant_configuration.go index 60584b66d82..ddbea480603 100644 --- a/sidecar/pkg/configuration/tenant_configuration.go +++ b/sidecar/pkg/configuration/tenant_configuration.go @@ -19,9 +19,7 @@ package configuration import ( "context" "fmt" - "log" "sort" - "strconv" "strings" miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2" @@ -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])), } } } @@ -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() diff --git a/sidecar/pkg/configuration/tenant_configuration_test.go b/sidecar/pkg/configuration/tenant_configuration_test.go index daa6afd1a9b..3d9560e7edb 100644 --- a/sidecar/pkg/configuration/tenant_configuration_test.go +++ b/sidecar/pkg/configuration/tenant_configuration_test.go @@ -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" `, }, }