From bf334e6b69622f053171fc8e06a924d3d45dad37 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 22 May 2024 13:06:53 -0600 Subject: [PATCH 01/30] feat: add file type variables to bundles --- .../07-helm-overrides/variable-files/fake_key | 38 +++++++++++++++++++ .../variable-files/sec_ctx.yaml | 3 ++ .../variable-files/uds-bundle.yaml | 25 ++++++++++++ .../variable-files/uds-config.yaml | 5 +++ src/test/e2e/variable_test.go | 28 ++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 src/test/bundles/07-helm-overrides/variable-files/fake_key create mode 100644 src/test/bundles/07-helm-overrides/variable-files/sec_ctx.yaml create mode 100644 src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml create mode 100644 src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml diff --git a/src/test/bundles/07-helm-overrides/variable-files/fake_key b/src/test/bundles/07-helm-overrides/variable-files/fake_key new file mode 100644 index 00000000..4b02b16d --- /dev/null +++ b/src/test/bundles/07-helm-overrides/variable-files/fake_key @@ -0,0 +1,38 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn +NhAAAAAwEAAQAAAYEArtDAntBK1ij7YfCqmgcmmXuVupMzyBw6D6Mt9GualhC9uJREifaf +IsW2pvauetxiy5i8qjowhugjVbuyS7PeVRKYxFgeD2PzaSHl+RmBtfLurwGsf7BQYF1j5j +FtmIk4QUD2ijJn9rrHV2lpDOizvWFTc9L5q4NxQJGTDuQ2ngBNmWWEu1Z1BXUPzqDFj1ly +muSA1HHJw8To9MK7mUQwwwWIBvu9PMm4F5uABDiU+55rIIwdXJxqt4oTttehDmAojj8k3F +Ehrq0uTdpGBenHiNPJFE5+aGuZvD+dT3ek5bVFHOqTeHstCSYzX8G6jEYfS+HUElntNTW3 +VGLTtmbPzzL4mpWLifY/zBiMEVMG3tYHH2ZB8uC0xdpzFXZ9taESpSwXb6xurArfpufNKw +1YhGv8Aov+eWEx43+6pdxWCOHrglqryJX1Rgz4NGTRjzAaDD9lIe6d9+lGXRJhnzTgKkpB +cKxHpM/rRriYgzA1n2HZ9LGXiUD8b1Z5iofZImx/AAAFkJCBuqSQgbqkAAAAB3NzaC1yc2 +EAAAGBAK7QwJ7QStYo+2HwqpoHJpl7lbqTM8gcOg+jLfRrmpYQvbiURIn2nyLFtqb2rnrc +YsuYvKo6MIboI1W7skuz3lUSmMRYHg9j82kh5fkZgbXy7q8BrH+wUGBdY+YxbZiJOEFA9o +oyZ/a6x1dpaQzos71hU3PS+auDcUCRkw7kNp4ATZllhLtWdQV1D86gxY9ZcprkgNRxycPE +6PTCu5lEMMMFiAb7vTzJuBebgAQ4lPueayCMHVycareKE7bXoQ5gKI4/JNxRIa6tLk3aRg +Xpx4jTyRROfmhrmbw/nU93pOW1RRzqk3h7LQkmM1/BuoxGH0vh1BJZ7TU1t1Ri07Zmz88y ++JqVi4n2P8wYjBFTBt7WBx9mQfLgtMXacxV2fbWhEqUsF2+sbqwK36bnzSsNWIRr/AKL/n +lhMeN/uqXcVgjh64Jaq8iV9UYM+DRk0Y8wGgw/ZSHunffpRl0SYZ804CpKQXCsR6TP60a4 +mIMwNZ9h2fSxl4lA/G9WeYqH2SJsfwAAAAMBAAEAAAGAGudZnZdkzQzFk08cklPjZt+ULY +CWCYf+j4WHfwRIaCvvZsa6vZjM6OcQsyVnuNTOB5NcBb6q1ThIp9sZF3l5EBKqnB0Lk0bI +oTdsUyfPxJa8kQZq68PQuuPkifq1aTJFDBXZW29ifKBGKXe5iRkPAGhIFZ00e2bhORSuUy +5gEXTufFsZUFN/8vUPF7e6b9NORfRAt3FDofNd/r0xbecGshzZ0GlRuFc7E7cEvqGOnCLH +Td0Cb8xKyDBh1FPfH31Av3U6JLQXYKZnefIyjxVRhdFt+U3bC3p/cmqKzFjMiiId5sui5N +ZCy8ARs3BkBSrm/HU7E6nnfiunaXxsJ4IT8+i3191XPaYcVyfKy9mXtCj1rvE5we1AEEoO +c3IBTO290iqKL6c7lBi9kUxzTVplLjqm8CzmGysoQbkYXvYOvNPCCQu13EmUso/eQWdf2H +w9GgY8Ge9Hn+7QbIESHlRFFbWBmgOWh2M9jRFbmV4RRxxGDeQMoX8SVgkrnPDRWt2BAAAA +wG3cqK7U7mQvAxrZqo1bNUEbd577B5vgRWitYNg62zRkBQiNM16KFW2m4odxHBRr/znoAC +ox777r0Ut+3iyP+cQRsLUkutd6Sm4sOa1syGHkon9G+R8jDqMJp9uoY6msfUUuSLs3Q5/v +XJYwWJs6TWVZfQMPJ50OwXi6ik0voYbpkAPV63Y3wFHpU9Sb8Qpv782K+f19Fl9YLx0zZ5 +hU4RG8LwmygBhZf3ow4COzd5arAi9cXInLO+YZUMBoOVwPbgAAAMEA1dW30EFsJtF4152X +lQNqL18Bq0CwtBtZZA+HQuPE5ntwPxR9vIG1M1MxksisTrdmAxsByy+eHT1zjVGCRVyOa4 +djG2dW3T4PIUqpR3DzTC+3G+SJnSwv5T6FiI9QlJjAV1+K58+lyrmQSRWiBgoLLyH/haPw +UdfBNRHNcv1Wg25eXKsHA/P+me1a9AYmv1yq42GIV6h7lXJznjHm+jnH2+tOGComRE+H/r +k4Xz79fnCYGmQMZPtcUWdEO4fHp97hAAAAwQDRSVqFLU1YZmQuQ0SRFkUbPy2UGws/HMD0 +sRiQkqJYtIad79Rww7gM+228ePFECCVgOU4EO/H92ShJILVlgsgQp651JmkwkY81FJm/xX +ULgCR2DoUi0IpsGp1bWKoVvJA81bpqfDONF823NwUts30I8e7PwavF7/8z5yWybcCnWUjm +l/cBQiXg9DDW143UV3r7GEm5YqyvKBSAZmCROViFvSJdZvVKHuXwQVngymtrrXVfmuTNK2 +KQRCT1v0lgl18AAAAadHJpc3RhbkB0cmlzdGFuLU5VQzEzQU5IaTcB +-----END OPENSSH PRIVATE KEY----- diff --git a/src/test/bundles/07-helm-overrides/variable-files/sec_ctx.yaml b/src/test/bundles/07-helm-overrides/variable-files/sec_ctx.yaml new file mode 100644 index 00000000..6f25c5d4 --- /dev/null +++ b/src/test/bundles/07-helm-overrides/variable-files/sec_ctx.yaml @@ -0,0 +1,3 @@ +runAsUser: 0 +runAsGroup: 0 +runAsNonRoot: false diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml new file mode 100644 index 00000000..1cd67062 --- /dev/null +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml @@ -0,0 +1,25 @@ +kind: UDSBundle +metadata: + name: helm-variables-file + description: testing a bundle with Helm overrides + version: 0.0.1 + +packages: + - name: helm-overrides + path: "../../../packages/helm" + ref: 0.0.1 + + overrides: + podinfo-component: + unicorn-podinfo: + variables: + - name: log_level + path: "podinfo.logLevel" + description: "Set the log level for podinfo" + default: "debug" # not overwritten! + - name: SECURITY_CTX + path: "podinfo.securityContext" + description: "testing a file" + - name: TEST_SECRET + path: "testSecret" + description: "key to place in the test secret resource" diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml new file mode 100644 index 00000000..64f9c519 --- /dev/null +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml @@ -0,0 +1,5 @@ +files: + packages: + helm-overrides: + test_secret: fake_key + SECURITY_CTX: sec_ctx.yaml \ No newline at end of file diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 7ff5be77..bca5f043 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -373,3 +373,31 @@ func TestExportVarsAsGlobalVars(t *testing.T) { remove(t, bundlePath) } + +func TestVariableFilesForOverrides(t *testing.T) { + // precedence rules: env var > uds-config.variables > uds-config.shared > default + deployZarfInit(t) + e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") + e2e.CreateZarfPkg(t, "src/test/packages/helm", false) + e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/output-var", false) + bundleDir := "src/test/bundles/07-helm-overrides/variable-files" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-var-files-%s-0.0.1.tar.zst", e2e.Arch)) + err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) + require.NoError(t, err) + createLocal(t, bundleDir, e2e.Arch) + + // color := "green" + // err = os.Setenv("UDS_UI_COLOR", color) + require.NoError(t, err) + _, stderr := deploy(t, bundlePath) + + t.Run("test fake_key file contents set as value for testSecret and used in test-secret secret", func(t *testing.T) { + + }) + + t.Run("test sec_ctx file contents set as value for podinfo.securityContext in deployment", func(t *testing.T) { + + }) + + remove(t, bundlePath) +} From fbf8229cf6073aaa8a283f08889bb3a8b4019a19 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 22 May 2024 14:41:08 -0600 Subject: [PATCH 02/30] add FileVariables to BundleDeployOptions. --- src/cmd/uds.go | 7 +++++++ .../07-helm-overrides/variable-files/uds-bundle.yaml | 2 +- .../07-helm-overrides/variable-files/uds-config.yaml | 2 +- src/test/e2e/variable_test.go | 6 ++---- src/types/options.go | 1 + 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/cmd/uds.go b/src/cmd/uds.go index 247c3b83..a64c1649 100644 --- a/src/cmd/uds.go +++ b/src/cmd/uds.go @@ -216,6 +216,13 @@ func loadViperConfig() error { bundleCfg.DeployOpts.SharedVariables[strings.ToUpper(varName)] = varValue } + // ensure the DeployOpts.FileVariables vars are uppercase + for varName, varValue := range bundleCfg.DeployOpts.FileVariables { + // delete the lowercase var and replace with uppercase + delete(bundleCfg.DeployOpts.FileVariables, varName) + bundleCfg.DeployOpts.FileVariables[strings.ToUpper(varName)] = varValue + } + return nil } diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml index 1cd67062..d4db7bcd 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml @@ -1,6 +1,6 @@ kind: UDSBundle metadata: - name: helm-variables-file + name: variable-files description: testing a bundle with Helm overrides version: 0.0.1 diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml index 64f9c519..340d2096 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml @@ -2,4 +2,4 @@ files: packages: helm-overrides: test_secret: fake_key - SECURITY_CTX: sec_ctx.yaml \ No newline at end of file + SECURITY_CTX: sec_ctx.yaml diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index bca5f043..f35d2d56 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -375,13 +375,11 @@ func TestExportVarsAsGlobalVars(t *testing.T) { } func TestVariableFilesForOverrides(t *testing.T) { - // precedence rules: env var > uds-config.variables > uds-config.shared > default deployZarfInit(t) e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") e2e.CreateZarfPkg(t, "src/test/packages/helm", false) - e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/output-var", false) bundleDir := "src/test/bundles/07-helm-overrides/variable-files" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-var-files-%s-0.0.1.tar.zst", e2e.Arch)) + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) require.NoError(t, err) createLocal(t, bundleDir, e2e.Arch) @@ -389,7 +387,7 @@ func TestVariableFilesForOverrides(t *testing.T) { // color := "green" // err = os.Setenv("UDS_UI_COLOR", color) require.NoError(t, err) - _, stderr := deploy(t, bundlePath) + deploy(t, bundlePath) t.Run("test fake_key file contents set as value for testSecret and used in test-secret secret", func(t *testing.T) { diff --git a/src/types/options.go b/src/types/options.go index 0c9285ae..697603a1 100644 --- a/src/types/options.go +++ b/src/types/options.go @@ -32,6 +32,7 @@ type BundleDeployOptions struct { SetVariables map[string]string `json:"setVariables" jsonschema:"description=Key-Value map of variable names and their corresponding values that will be used by Zarf packages in a bundle"` // Variables and SharedVariables are read in from uds-config.yaml Variables map[string]map[string]interface{} `yaml:"variables,omitempty"` + FileVariables map[string]map[string]interface{} `yaml:"files,omitempty"` SharedVariables map[string]interface{} `yaml:"shared,omitempty"` Retries int `yaml:"retries"` } From 3a630139838b119686bacce1d13fe55e7e848810 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 22 May 2024 15:12:41 -0600 Subject: [PATCH 03/30] adding loadFileContents() to loadVariables(). --- src/pkg/bundle/deploy.go | 31 +++++++++++++-- .../07-helm-overrides/variable-files/fake_key | 38 ------------------- .../07-helm-overrides/variable-files/test.pub | 1 + .../variable-files/uds-config.yaml | 4 +- src/types/options.go | 2 +- 5 files changed, 32 insertions(+), 44 deletions(-) delete mode 100644 src/test/bundles/07-helm-overrides/variable-files/fake_key create mode 100644 src/test/bundles/07-helm-overrides/variable-files/test.pub diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index db147276..a0e7d921 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -95,7 +95,10 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { publicKeyPath = "" } - pkgVars := b.loadVariables(pkg, bundleExportedVars) + pkgVars, err := b.loadVariables(pkg, bundleExportedVars) + if err != nil { + return err + } opts := zarfTypes.ZarfPackageOptions{ PackageSource: pkgTmp, @@ -149,8 +152,22 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { return nil } +func (b *Bundle) loadFileContents(path string) (string, error) { + // check for file + if helpers.InvalidPath(path) { + return "", fmt.Errorf("unable to find file %s", path) + } + // read file + read, err := os.ReadFile(path) + if err != nil { + return "", err + } + + return string(read), err +} + // loadVariables loads and sets precedence for config-level and imported variables -func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string]map[string]string) map[string]string { +func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string]map[string]string) (map[string]string, error) { pkgVars := make(map[string]string) // load all exported variables @@ -173,6 +190,14 @@ func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string] for name, val := range b.cfg.DeployOpts.Variables[pkg.Name] { pkgVars[strings.ToUpper(name)] = fmt.Sprint(val) } + // file vars + for name, val := range b.cfg.DeployOpts.FileVariables[pkg.Name] { + fileContents, err := b.loadFileContents(val) + if err != nil { + return pkgVars, err + } + pkgVars[strings.ToUpper(name)] = fileContents + } // env vars (vars that start with UDS_) for _, envVar := range os.Environ() { if strings.HasPrefix(envVar, config.EnvVarPrefix) { @@ -193,7 +218,7 @@ func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string] pkgVars[strings.ToUpper(name)] = fmt.Sprint(val) } } - return pkgVars + return pkgVars, nil } // ConfirmBundleDeploy uses Zarf's pterm logging to prompt the user to confirm bundle creation diff --git a/src/test/bundles/07-helm-overrides/variable-files/fake_key b/src/test/bundles/07-helm-overrides/variable-files/fake_key deleted file mode 100644 index 4b02b16d..00000000 --- a/src/test/bundles/07-helm-overrides/variable-files/fake_key +++ /dev/null @@ -1,38 +0,0 @@ ------BEGIN OPENSSH PRIVATE KEY----- -b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn -NhAAAAAwEAAQAAAYEArtDAntBK1ij7YfCqmgcmmXuVupMzyBw6D6Mt9GualhC9uJREifaf -IsW2pvauetxiy5i8qjowhugjVbuyS7PeVRKYxFgeD2PzaSHl+RmBtfLurwGsf7BQYF1j5j -FtmIk4QUD2ijJn9rrHV2lpDOizvWFTc9L5q4NxQJGTDuQ2ngBNmWWEu1Z1BXUPzqDFj1ly -muSA1HHJw8To9MK7mUQwwwWIBvu9PMm4F5uABDiU+55rIIwdXJxqt4oTttehDmAojj8k3F -Ehrq0uTdpGBenHiNPJFE5+aGuZvD+dT3ek5bVFHOqTeHstCSYzX8G6jEYfS+HUElntNTW3 -VGLTtmbPzzL4mpWLifY/zBiMEVMG3tYHH2ZB8uC0xdpzFXZ9taESpSwXb6xurArfpufNKw -1YhGv8Aov+eWEx43+6pdxWCOHrglqryJX1Rgz4NGTRjzAaDD9lIe6d9+lGXRJhnzTgKkpB -cKxHpM/rRriYgzA1n2HZ9LGXiUD8b1Z5iofZImx/AAAFkJCBuqSQgbqkAAAAB3NzaC1yc2 -EAAAGBAK7QwJ7QStYo+2HwqpoHJpl7lbqTM8gcOg+jLfRrmpYQvbiURIn2nyLFtqb2rnrc -YsuYvKo6MIboI1W7skuz3lUSmMRYHg9j82kh5fkZgbXy7q8BrH+wUGBdY+YxbZiJOEFA9o -oyZ/a6x1dpaQzos71hU3PS+auDcUCRkw7kNp4ATZllhLtWdQV1D86gxY9ZcprkgNRxycPE -6PTCu5lEMMMFiAb7vTzJuBebgAQ4lPueayCMHVycareKE7bXoQ5gKI4/JNxRIa6tLk3aRg -Xpx4jTyRROfmhrmbw/nU93pOW1RRzqk3h7LQkmM1/BuoxGH0vh1BJZ7TU1t1Ri07Zmz88y -+JqVi4n2P8wYjBFTBt7WBx9mQfLgtMXacxV2fbWhEqUsF2+sbqwK36bnzSsNWIRr/AKL/n -lhMeN/uqXcVgjh64Jaq8iV9UYM+DRk0Y8wGgw/ZSHunffpRl0SYZ804CpKQXCsR6TP60a4 -mIMwNZ9h2fSxl4lA/G9WeYqH2SJsfwAAAAMBAAEAAAGAGudZnZdkzQzFk08cklPjZt+ULY -CWCYf+j4WHfwRIaCvvZsa6vZjM6OcQsyVnuNTOB5NcBb6q1ThIp9sZF3l5EBKqnB0Lk0bI -oTdsUyfPxJa8kQZq68PQuuPkifq1aTJFDBXZW29ifKBGKXe5iRkPAGhIFZ00e2bhORSuUy -5gEXTufFsZUFN/8vUPF7e6b9NORfRAt3FDofNd/r0xbecGshzZ0GlRuFc7E7cEvqGOnCLH -Td0Cb8xKyDBh1FPfH31Av3U6JLQXYKZnefIyjxVRhdFt+U3bC3p/cmqKzFjMiiId5sui5N -ZCy8ARs3BkBSrm/HU7E6nnfiunaXxsJ4IT8+i3191XPaYcVyfKy9mXtCj1rvE5we1AEEoO -c3IBTO290iqKL6c7lBi9kUxzTVplLjqm8CzmGysoQbkYXvYOvNPCCQu13EmUso/eQWdf2H -w9GgY8Ge9Hn+7QbIESHlRFFbWBmgOWh2M9jRFbmV4RRxxGDeQMoX8SVgkrnPDRWt2BAAAA -wG3cqK7U7mQvAxrZqo1bNUEbd577B5vgRWitYNg62zRkBQiNM16KFW2m4odxHBRr/znoAC -ox777r0Ut+3iyP+cQRsLUkutd6Sm4sOa1syGHkon9G+R8jDqMJp9uoY6msfUUuSLs3Q5/v -XJYwWJs6TWVZfQMPJ50OwXi6ik0voYbpkAPV63Y3wFHpU9Sb8Qpv782K+f19Fl9YLx0zZ5 -hU4RG8LwmygBhZf3ow4COzd5arAi9cXInLO+YZUMBoOVwPbgAAAMEA1dW30EFsJtF4152X -lQNqL18Bq0CwtBtZZA+HQuPE5ntwPxR9vIG1M1MxksisTrdmAxsByy+eHT1zjVGCRVyOa4 -djG2dW3T4PIUqpR3DzTC+3G+SJnSwv5T6FiI9QlJjAV1+K58+lyrmQSRWiBgoLLyH/haPw -UdfBNRHNcv1Wg25eXKsHA/P+me1a9AYmv1yq42GIV6h7lXJznjHm+jnH2+tOGComRE+H/r -k4Xz79fnCYGmQMZPtcUWdEO4fHp97hAAAAwQDRSVqFLU1YZmQuQ0SRFkUbPy2UGws/HMD0 -sRiQkqJYtIad79Rww7gM+228ePFECCVgOU4EO/H92ShJILVlgsgQp651JmkwkY81FJm/xX -ULgCR2DoUi0IpsGp1bWKoVvJA81bpqfDONF823NwUts30I8e7PwavF7/8z5yWybcCnWUjm -l/cBQiXg9DDW143UV3r7GEm5YqyvKBSAZmCROViFvSJdZvVKHuXwQVngymtrrXVfmuTNK2 -KQRCT1v0lgl18AAAAadHJpc3RhbkB0cmlzdGFuLU5VQzEzQU5IaTcB ------END OPENSSH PRIVATE KEY----- diff --git a/src/test/bundles/07-helm-overrides/variable-files/test.pub b/src/test/bundles/07-helm-overrides/variable-files/test.pub new file mode 100644 index 00000000..fee493dd --- /dev/null +++ b/src/test/bundles/07-helm-overrides/variable-files/test.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCKc2+bG21ybCTuC0U88FPveavqadn9SrBjkCGpaHINq2hZ98eVle5yInOOSq1lGMC89Fyx7VClYwGK1DxMKtRW6u+HUWoOjsxJh4GGIclrRG4xGOSWQk7VR2kRqO3zK+xgEAUo4yQo9L+WPp9GMGGuMZuMFLsKypUJn3P2XVUKgy2oSBMTqVuHZjZ4nn4ubXOb9WfOz/0AFYvsIBuJMMq/K3qffWAcyG2z+3fA0PMBFfjg8hi3QLk7rxZWuYxmKGrIGcuv5sXb3yCJMRav6kilxYBlrDENFnPb91P/w1FAXo3ryCSsXT4dXWTOB/ZGA4xSE5FZagaBLHGkRlEwLBOuyHXDChT6B8YhPblSdzvKWHfgMR/ZZqk/+26JDZAEiQ1QrZ6C39cCIJXRnQHyrtdmac7apkPJCbTEnNQJdJ57dKymmPT6kEgGUH9R/2flKGrynw1SCD9rB1OT/fbgjhBldI38M20MAGqtRoCERhBAO7tYWP/0Fju6B/cB4VXFjj0= tristan@tristan-NUC13ANHi7 diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml index 340d2096..545ee29d 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml @@ -1,5 +1,5 @@ files: packages: helm-overrides: - test_secret: fake_key - SECURITY_CTX: sec_ctx.yaml + test_secret: "test.pub" + SECURITY_CTX: "sec_ctx.yaml" diff --git a/src/types/options.go b/src/types/options.go index 697603a1..4f9a1140 100644 --- a/src/types/options.go +++ b/src/types/options.go @@ -32,7 +32,7 @@ type BundleDeployOptions struct { SetVariables map[string]string `json:"setVariables" jsonschema:"description=Key-Value map of variable names and their corresponding values that will be used by Zarf packages in a bundle"` // Variables and SharedVariables are read in from uds-config.yaml Variables map[string]map[string]interface{} `yaml:"variables,omitempty"` - FileVariables map[string]map[string]interface{} `yaml:"files,omitempty"` + FileVariables map[string]map[string]string `yaml:"files,omitempty"` SharedVariables map[string]interface{} `yaml:"shared,omitempty"` Retries int `yaml:"retries"` } From 0b6f21905a22da844ff547478d2ce114c323ee48 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Thu, 23 May 2024 16:35:13 -0600 Subject: [PATCH 04/30] add file handling from loadVariables() --- src/cmd/uds.go | 14 ++++-- src/pkg/bundle/deploy.go | 19 +++++++- .../variable-files/file-not-found-config.yaml | 4 ++ .../variable-files/invalid-config.yaml | 9 ++++ .../variable-files/uds-config.yaml | 7 ++- src/test/e2e/variable_test.go | 47 +++++++++++++++---- src/types/options.go | 1 + 7 files changed, 82 insertions(+), 19 deletions(-) create mode 100644 src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml create mode 100644 src/test/bundles/07-helm-overrides/variable-files/invalid-config.yaml diff --git a/src/cmd/uds.go b/src/cmd/uds.go index a64c1649..721ad7fb 100644 --- a/src/cmd/uds.go +++ b/src/cmd/uds.go @@ -59,7 +59,9 @@ var deployCmd = &cobra.Command{ configureZarf() // load uds-config if it exists - if v.ConfigFileUsed() != "" { + config := v.ConfigFileUsed() + if config != "" { + bundleCfg.DeployOpts.Config = config if err := loadViperConfig(); err != nil { message.Fatalf(err, "Failed to load uds-config: %s", err.Error()) return @@ -217,10 +219,12 @@ func loadViperConfig() error { } // ensure the DeployOpts.FileVariables vars are uppercase - for varName, varValue := range bundleCfg.DeployOpts.FileVariables { - // delete the lowercase var and replace with uppercase - delete(bundleCfg.DeployOpts.FileVariables, varName) - bundleCfg.DeployOpts.FileVariables[strings.ToUpper(varName)] = varValue + for pkgName, pkgVar := range bundleCfg.DeployOpts.FileVariables { + for varName, varValue := range pkgVar { + // delete the lowercase var and replace with uppercase + delete(bundleCfg.DeployOpts.FileVariables[pkgName], varName) + bundleCfg.DeployOpts.FileVariables[pkgName][strings.ToUpper(varName)] = varValue + } } return nil diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index a0e7d921..e7fd6f6d 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -153,11 +153,23 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { } func (b *Bundle) loadFileContents(path string) (string, error) { - // check for file + //check for absolute path ? ... + + if !filepath.IsAbs(path) { + if filepath.Dir(b.cfg.DeployOpts.Config) != filepath.Dir(path) { + path = filepath.Join(filepath.Dir(b.cfg.DeployOpts.Config), path) + } + } + if helpers.InvalidPath(path) { return "", fmt.Errorf("unable to find file %s", path) } - // read file + + _, err := helpers.IsTextFile(path) + if err != nil { + return "", err + } + read, err := os.ReadFile(path) if err != nil { return "", err @@ -192,6 +204,9 @@ func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string] } // file vars for name, val := range b.cfg.DeployOpts.FileVariables[pkg.Name] { + if _, exists := pkgVars[strings.ToUpper(name)]; exists { + return nil, fmt.Errorf("invalid config: variable %s is declared more than once", strings.ToUpper(name)) + } fileContents, err := b.loadFileContents(val) if err != nil { return pkgVars, err diff --git a/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml new file mode 100644 index 00000000..e88cabb2 --- /dev/null +++ b/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml @@ -0,0 +1,4 @@ +files: + helm-overrides: + test_secret: not-there.pub + SECURITY_CTX: sec_ctx.yaml diff --git a/src/test/bundles/07-helm-overrides/variable-files/invalid-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/invalid-config.yaml new file mode 100644 index 00000000..4beb3d39 --- /dev/null +++ b/src/test/bundles/07-helm-overrides/variable-files/invalid-config.yaml @@ -0,0 +1,9 @@ + +variables: + helm-overrides: + test_secret: whatever + +files: + helm-overrides: + test_secret: test.pub + SECURITY_CTX: sec_ctx.yaml diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml index 545ee29d..146bb4d5 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml @@ -1,5 +1,4 @@ files: - packages: - helm-overrides: - test_secret: "test.pub" - SECURITY_CTX: "sec_ctx.yaml" + helm-overrides: + test_secret: test.pub + SECURITY_CTX: sec_ctx.yaml diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index f35d2d56..3ef46156 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -374,28 +374,59 @@ func TestExportVarsAsGlobalVars(t *testing.T) { remove(t, bundlePath) } -func TestVariableFilesForOverrides(t *testing.T) { +func TestVariableFilesInvalidConfig(t *testing.T) { deployZarfInit(t) e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") e2e.CreateZarfPkg(t, "src/test/packages/helm", false) bundleDir := "src/test/bundles/07-helm-overrides/variable-files" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) - err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) + err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "invalid-config.yaml")) require.NoError(t, err) + + createLocal(t, bundleDir, e2e.Arch) + + cmd := strings.Split(fmt.Sprintf("deploy %s --retries 1 --confirm", bundlePath), " ") + _, stderr, _ := e2e.UDS(cmd...) + require.Contains(t, stderr, "invalid config") +} + +func TestVariableFilesFileNotFound(t *testing.T) { + deployZarfInit(t) + e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") + e2e.CreateZarfPkg(t, "src/test/packages/helm", false) + bundleDir := "src/test/bundles/07-helm-overrides/variable-files" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) + err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "file-not-found-config.yaml")) + require.NoError(t, err) + createLocal(t, bundleDir, e2e.Arch) - // color := "green" - // err = os.Setenv("UDS_UI_COLOR", color) + cmd := strings.Split(fmt.Sprintf("deploy %s --retries 1 --confirm", bundlePath), " ") + _, stderr, _ := e2e.UDS(cmd...) + + require.Contains(t, stderr, fmt.Sprintf("unable to find file %s/not-there.pub", bundleDir)) +} + +func TestVariableFiles(t *testing.T) { + deployZarfInit(t) + e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") + e2e.CreateZarfPkg(t, "src/test/packages/helm", false) + bundleDir := "src/test/bundles/07-helm-overrides/variable-files" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) + err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) + require.NoError(t, err) + // createLocal(t, bundleDir, e2e.Arch) + require.NoError(t, err) deploy(t, bundlePath) - t.Run("test fake_key file contents set as value for testSecret and used in test-secret secret", func(t *testing.T) { + // t.Run("test fake_key file contents set as value for testSecret and used in test-secret secret", func(t *testing.T) { - }) + // }) - t.Run("test sec_ctx file contents set as value for podinfo.securityContext in deployment", func(t *testing.T) { + // t.Run("test sec_ctx file contents set as value for podinfo.securityContext in deployment", func(t *testing.T) { - }) + // }) remove(t, bundlePath) } diff --git a/src/types/options.go b/src/types/options.go index 4f9a1140..a6e0e642 100644 --- a/src/types/options.go +++ b/src/types/options.go @@ -27,6 +27,7 @@ type BundleCreateOptions struct { type BundleDeployOptions struct { Resume bool Source string + Config string Packages []string PublicKeyPath string SetVariables map[string]string `json:"setVariables" jsonschema:"description=Key-Value map of variable names and their corresponding values that will be used by Zarf packages in a bundle"` From 1e271cdd1e6bf583c44c46b07412b59b91758652 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Thu, 23 May 2024 16:41:56 -0600 Subject: [PATCH 05/30] fix deploy_test call of loadVariables() --- src/pkg/bundle/deploy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pkg/bundle/deploy_test.go b/src/pkg/bundle/deploy_test.go index 6658f710..b4052063 100644 --- a/src/pkg/bundle/deploy_test.go +++ b/src/pkg/bundle/deploy_test.go @@ -235,7 +235,7 @@ func TestLoadVariablesPrecedence(t *testing.T) { if tc.loadEnvVar { os.Setenv("UDS_FOO", "set using env var") } - actualPkgVars := tc.bundle.loadVariables(tc.pkg, tc.bundleExportVars) + actualPkgVars, _ := tc.bundle.loadVariables(tc.pkg, tc.bundleExportVars) require.Equal(t, tc.expectedPkgVars, actualPkgVars) }) } From 405327e663b392ba213bcf169c4949c648f71835 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Thu, 23 May 2024 21:08:49 -0600 Subject: [PATCH 06/30] test for zarf var setting; adding file handling to processOverrideVariables() (broken) --- src/pkg/bundle/deploy.go | 19 ++++++++-- .../variable-files/domain.txt | 1 + .../variable-files/uds-config.yaml | 1 + src/test/e2e/variable_test.go | 38 +++++++++++++++---- 4 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 src/test/bundles/07-helm-overrides/variable-files/domain.txt diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index e7fd6f6d..944e5c2e 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -153,9 +153,8 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { } func (b *Bundle) loadFileContents(path string) (string, error) { - //check for absolute path ? ... - if !filepath.IsAbs(path) { + // set path relative to config file, unless they are the same if filepath.Dir(b.cfg.DeployOpts.Config) != filepath.Dir(path) { path = filepath.Join(filepath.Dir(b.cfg.DeployOpts.Config), path) } @@ -175,7 +174,7 @@ func (b *Bundle) loadFileContents(path string) (string, error) { return "", err } - return string(read), err + return string(read), nil } // loadVariables loads and sets precedence for config-level and imported variables @@ -415,6 +414,13 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va overrideVal = configFileOverride } else if sharedConfigOverride, existsInSharedConfig := b.cfg.DeployOpts.SharedVariables[v.Name]; existsInSharedConfig { overrideVal = sharedConfigOverride + } else if fileConfigOverride, existsInFileConfig := b.cfg.DeployOpts.FileVariables[pkgName][v.Name]; existsInFileConfig { + // fileContents, err := b.loadFileContents(fileConfigOverride) + // if err != nil { + // return err + // } + // overrideVal = fileContents + overrideVal = fileConfigOverride } else if v.Default != nil { overrideVal = v.Default } else { @@ -480,6 +486,13 @@ func addOverrideValue(overrides map[string]map[string]*values.Options, component templatedVariable := fmt.Sprintf("%v", v) value = setTemplatedVariables(templatedVariable, pkgVars) } + if val, ok := v.(string); ok { + if strings.Contains(val, ".") { + helmVal := fmt.Sprintf("%s=%v", valuePath, value) + overrides[component][chart].Values = append(overrides[component][chart].FileValues, helmVal) + } + } + // handle default case of simple values like strings and numbers helmVal := fmt.Sprintf("%s=%v", valuePath, value) overrides[component][chart].Values = append(overrides[component][chart].Values, helmVal) diff --git a/src/test/bundles/07-helm-overrides/variable-files/domain.txt b/src/test/bundles/07-helm-overrides/variable-files/domain.txt new file mode 100644 index 00000000..d52686ec --- /dev/null +++ b/src/test/bundles/07-helm-overrides/variable-files/domain.txt @@ -0,0 +1 @@ +from-file.dev \ No newline at end of file diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml index 146bb4d5..aaed66fc 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml @@ -1,4 +1,5 @@ files: helm-overrides: + domain: domain.txt test_secret: test.pub SECURITY_CTX: sec_ctx.yaml diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 3ef46156..49c69b96 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -407,7 +407,7 @@ func TestVariableFilesFileNotFound(t *testing.T) { require.Contains(t, stderr, fmt.Sprintf("unable to find file %s/not-there.pub", bundleDir)) } -func TestVariableFiles(t *testing.T) { +func TestVariableFilesSettingZarfVar(t *testing.T) { deployZarfInit(t) e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") e2e.CreateZarfPkg(t, "src/test/packages/helm", false) @@ -415,18 +415,42 @@ func TestVariableFiles(t *testing.T) { bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) require.NoError(t, err) - // createLocal(t, bundleDir, e2e.Arch) + createLocal(t, bundleDir, e2e.Arch) require.NoError(t, err) - deploy(t, bundlePath) + _, stderr := deploy(t, bundlePath) - // t.Run("test fake_key file contents set as value for testSecret and used in test-secret secret", func(t *testing.T) { + require.Contains(t, stderr, "\"###ZARF_VAR_DOMAIN###\": \"from-file.dev\"") - // }) + remove(t, bundlePath) +} - // t.Run("test sec_ctx file contents set as value for podinfo.securityContext in deployment", func(t *testing.T) { +func TestVariableFilesHelmOverrides(t *testing.T) { + deployZarfInit(t) + e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") + e2e.CreateZarfPkg(t, "src/test/packages/helm", false) + bundleDir := "src/test/bundles/07-helm-overrides/variable-files" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) + err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) + require.NoError(t, err) + createLocal(t, bundleDir, e2e.Arch) + + require.NoError(t, err) + deploy(t, bundlePath) + t.Run("test test.pub file contents set as value for testSecret and used in test-secret secret", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-secret -o=jsonpath={.data} | base64 -d", " ") + stdout, _, err := e2e.UDS(cmd...) + require.NoError(t, err) + require.Contains(t, stdout, "ssh-rsa") + }) + + // t.Run("test sec_ctx file contents set as value for podinfo.securityContext in deployment", func(t *testing.T) { + // cmd := strings.Split("zarf tools helm values -n podinfo unicorn-podinfo", " ") + // stdout, _, err := e2e.UDS(cmd...) + // require.NoError(t, err) + // require.Contains(t, stdout, "key file contents here") // }) - remove(t, bundlePath) + // remove(t, bundlePath) } From d5b375b60d739a4967f3b157c943d450f54badb7 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Fri, 24 May 2024 11:44:18 -0600 Subject: [PATCH 07/30] small refactor; attempting helm.FileValues (bad format errors) --- src/pkg/bundle/deploy.go | 26 ++++++++++++------- .../variable-files/domain.txt | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index 944e5c2e..e8f79446 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -152,14 +152,19 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { return nil } -func (b *Bundle) loadFileContents(path string) (string, error) { +func formFullPath(configPath string, path string) string { if !filepath.IsAbs(path) { // set path relative to config file, unless they are the same - if filepath.Dir(b.cfg.DeployOpts.Config) != filepath.Dir(path) { - path = filepath.Join(filepath.Dir(b.cfg.DeployOpts.Config), path) + if filepath.Dir(configPath) != filepath.Dir(path) { + path = filepath.Join(filepath.Dir(configPath), path) } } + return path +} + +func (b *Bundle) loadFileContents(path string) (string, error) { + path = formFullPath(b.cfg.DeployOpts.Config, path) if helpers.InvalidPath(path) { return "", fmt.Errorf("unable to find file %s", path) } @@ -376,7 +381,7 @@ func (b *Bundle) processOverrideNamespaces(overrideMap sources.NamespaceOverride func (b *Bundle) processOverrideValues(overrideMap *map[string]map[string]*values.Options, values *[]types.BundleChartValue, componentName string, chartName string, pkgVars map[string]string) error { for _, v := range *values { // Add the override to the map, or return an error if the path is invalid - if err := addOverrideValue(*overrideMap, componentName, chartName, v.Path, v.Value, pkgVars); err != nil { + if err := b.addOverrideValue(*overrideMap, componentName, chartName, v.Path, v.Value, pkgVars); err != nil { return err } } @@ -429,7 +434,7 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va } // Add the override to the map, or return an error if the path is invalid - if err := addOverrideValue(*overrideMap, componentName, chartName, v.Path, overrideVal, nil); err != nil { + if err := b.addOverrideValue(*overrideMap, componentName, chartName, v.Path, overrideVal, nil); err != nil { return err } @@ -438,7 +443,7 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va } // addOverrideValue adds a value to a PkgOverrideMap -func addOverrideValue(overrides map[string]map[string]*values.Options, component string, chart string, valuePath string, value interface{}, pkgVars map[string]string) error { +func (b *Bundle) addOverrideValue(overrides map[string]map[string]*values.Options, component string, chart string, valuePath string, value interface{}, pkgVars map[string]string) error { // Create the component map if it doesn't exist if _, ok := overrides[component]; !ok { overrides[component] = make(map[string]*values.Options) @@ -486,10 +491,11 @@ func addOverrideValue(overrides map[string]map[string]*values.Options, component templatedVariable := fmt.Sprintf("%v", v) value = setTemplatedVariables(templatedVariable, pkgVars) } - if val, ok := v.(string); ok { - if strings.Contains(val, ".") { - helmVal := fmt.Sprintf("%s=%v", valuePath, value) - overrides[component][chart].Values = append(overrides[component][chart].FileValues, helmVal) + if _, ok := v.(string); ok { + possFile := formFullPath(b.cfg.DeployOpts.Config, value.(string)) + if isFile, _ := helpers.IsTextFile(possFile); isFile { + helmVal := fmt.Sprintf("%s=%v", valuePath, possFile) + overrides[component][chart].FileValues = append(overrides[component][chart].FileValues, helmVal) } } diff --git a/src/test/bundles/07-helm-overrides/variable-files/domain.txt b/src/test/bundles/07-helm-overrides/variable-files/domain.txt index d52686ec..191f7b22 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/domain.txt +++ b/src/test/bundles/07-helm-overrides/variable-files/domain.txt @@ -1 +1 @@ -from-file.dev \ No newline at end of file +from-file.dev From 5c97215df511db68eb56ce5bc9d69ccc99888fcf Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Tue, 28 May 2024 11:36:04 -0600 Subject: [PATCH 08/30] cleanup helm file handling; base64 test pub key. --- src/pkg/bundle/deploy.go | 16 +++++----------- .../variable-files/sec_ctx.yaml | 3 --- .../07-helm-overrides/variable-files/test.pub | 2 +- .../variable-files/uds-bundle.yaml | 5 +---- .../variable-files/uds-config.yaml | 2 +- src/test/e2e/variable_test.go | 16 ++++++---------- 6 files changed, 14 insertions(+), 30 deletions(-) delete mode 100644 src/test/bundles/07-helm-overrides/variable-files/sec_ctx.yaml diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index e8f79446..20d2e7eb 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -152,7 +152,7 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { return nil } -func formFullPath(configPath string, path string) string { +func formFullRelativePath(configPath string, path string) string { if !filepath.IsAbs(path) { // set path relative to config file, unless they are the same if filepath.Dir(configPath) != filepath.Dir(path) { @@ -164,7 +164,7 @@ func formFullPath(configPath string, path string) string { } func (b *Bundle) loadFileContents(path string) (string, error) { - path = formFullPath(b.cfg.DeployOpts.Config, path) + path = formFullRelativePath(b.cfg.DeployOpts.Config, path) if helpers.InvalidPath(path) { return "", fmt.Errorf("unable to find file %s", path) } @@ -420,12 +420,7 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va } else if sharedConfigOverride, existsInSharedConfig := b.cfg.DeployOpts.SharedVariables[v.Name]; existsInSharedConfig { overrideVal = sharedConfigOverride } else if fileConfigOverride, existsInFileConfig := b.cfg.DeployOpts.FileVariables[pkgName][v.Name]; existsInFileConfig { - // fileContents, err := b.loadFileContents(fileConfigOverride) - // if err != nil { - // return err - // } - // overrideVal = fileContents - overrideVal = fileConfigOverride + overrideVal = formFullRelativePath(b.cfg.DeployOpts.Config, fileConfigOverride) } else if v.Default != nil { overrideVal = v.Default } else { @@ -492,9 +487,8 @@ func (b *Bundle) addOverrideValue(overrides map[string]map[string]*values.Option value = setTemplatedVariables(templatedVariable, pkgVars) } if _, ok := v.(string); ok { - possFile := formFullPath(b.cfg.DeployOpts.Config, value.(string)) - if isFile, _ := helpers.IsTextFile(possFile); isFile { - helmVal := fmt.Sprintf("%s=%v", valuePath, possFile) + if isFile, _ := helpers.IsTextFile(value.(string)); isFile { + helmVal := fmt.Sprintf("%s=%v", valuePath, value) overrides[component][chart].FileValues = append(overrides[component][chart].FileValues, helmVal) } } diff --git a/src/test/bundles/07-helm-overrides/variable-files/sec_ctx.yaml b/src/test/bundles/07-helm-overrides/variable-files/sec_ctx.yaml deleted file mode 100644 index 6f25c5d4..00000000 --- a/src/test/bundles/07-helm-overrides/variable-files/sec_ctx.yaml +++ /dev/null @@ -1,3 +0,0 @@ -runAsUser: 0 -runAsGroup: 0 -runAsNonRoot: false diff --git a/src/test/bundles/07-helm-overrides/variable-files/test.pub b/src/test/bundles/07-helm-overrides/variable-files/test.pub index fee493dd..0164f2dd 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/test.pub +++ b/src/test/bundles/07-helm-overrides/variable-files/test.pub @@ -1 +1 @@ -ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCKc2+bG21ybCTuC0U88FPveavqadn9SrBjkCGpaHINq2hZ98eVle5yInOOSq1lGMC89Fyx7VClYwGK1DxMKtRW6u+HUWoOjsxJh4GGIclrRG4xGOSWQk7VR2kRqO3zK+xgEAUo4yQo9L+WPp9GMGGuMZuMFLsKypUJn3P2XVUKgy2oSBMTqVuHZjZ4nn4ubXOb9WfOz/0AFYvsIBuJMMq/K3qffWAcyG2z+3fA0PMBFfjg8hi3QLk7rxZWuYxmKGrIGcuv5sXb3yCJMRav6kilxYBlrDENFnPb91P/w1FAXo3ryCSsXT4dXWTOB/ZGA4xSE5FZagaBLHGkRlEwLBOuyHXDChT6B8YhPblSdzvKWHfgMR/ZZqk/+26JDZAEiQ1QrZ6C39cCIJXRnQHyrtdmac7apkPJCbTEnNQJdJ57dKymmPT6kEgGUH9R/2flKGrynw1SCD9rB1OT/fbgjhBldI38M20MAGqtRoCERhBAO7tYWP/0Fju6B/cB4VXFjj0= tristan@tristan-NUC13ANHi7 +c3NoLXJzYSBBQUFBQjNOemFDMXljMkVBQUFBREFRQUJBQUFCZ1FDS2MyK2JHMjF5YkNUdUMwVTg4RlB2ZWF2cWFkbjlTckJqa0NHcGFISU5xMmhaOThlVmxlNXlJbk9PU3ExbEdNQzg5Rnl4N1ZDbFl3R0sxRHhNS3RSVzZ1K0hVV29PanN4Smg0R0dJY2xyUkc0eEdPU1dRazdWUjJrUnFPM3pLK3hnRUFVbzR5UW85TCtXUHA5R01HR3VNWnVNRkxzS3lwVUpuM1AyWFZVS2d5Mm9TQk1UcVZ1SFpqWjRubjR1YlhPYjlXZk96LzBBRll2c0lCdUpNTXEvSzNxZmZXQWN5RzJ6KzNmQTBQTUJGZmpnOGhpM1FMazdyeFpXdVl4bUtHcklHY3V2NXNYYjN5Q0pNUmF2NmtpbHhZQmxyREVORm5QYjkxUC93MUZBWG8zcnlDU3NYVDRkWFdUT0IvWkdBNHhTRTVGWmFnYUJMSEdrUmxFd0xCT3V5SFhEQ2hUNkI4WWhQYmxTZHp2S1dIZmdNUi9aWnFrLysyNkpEWkFFaVExUXJaNkMzOWNDSUpYUm5RSHlydGRtYWM3YXBrUEpDYlRFbk5RSmRKNTdkS3ltbVBUNmtFZ0dVSDlSLzJmbEtHcnludzFTQ0Q5ckIxT1QvZmJnamhCbGRJMzhNMjBNQUdxdFJvQ0VSaEJBTzd0WVdQLzBGanU2Qi9jQjRWWEZqajA9IHRyaXN0YW5AdHJpc3Rhbi1OVUMxM0FOSGk3 \ No newline at end of file diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml index d4db7bcd..3db8d3da 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml @@ -17,9 +17,6 @@ packages: path: "podinfo.logLevel" description: "Set the log level for podinfo" default: "debug" # not overwritten! - - name: SECURITY_CTX - path: "podinfo.securityContext" - description: "testing a file" - name: TEST_SECRET path: "testSecret" - description: "key to place in the test secret resource" + description: "base64 encoded key to place in the test secret resource" diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml index aaed66fc..f776636f 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml @@ -2,4 +2,4 @@ files: helm-overrides: domain: domain.txt test_secret: test.pub - SECURITY_CTX: sec_ctx.yaml + diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 49c69b96..64daa302 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -5,6 +5,7 @@ package test import ( + "encoding/base64" "fmt" "os" "path/filepath" @@ -439,18 +440,13 @@ func TestVariableFilesHelmOverrides(t *testing.T) { deploy(t, bundlePath) t.Run("test test.pub file contents set as value for testSecret and used in test-secret secret", func(t *testing.T) { - cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-secret -o=jsonpath={.data} | base64 -d", " ") + cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-secret -o=jsonpath={.data.test}", " ") stdout, _, err := e2e.UDS(cmd...) require.NoError(t, err) - require.Contains(t, stdout, "ssh-rsa") + decoded, err := base64.StdEncoding.DecodeString(stdout) + require.NoError(t, err) + require.Contains(t, string(decoded), "ssh-rsa") }) - // t.Run("test sec_ctx file contents set as value for podinfo.securityContext in deployment", func(t *testing.T) { - // cmd := strings.Split("zarf tools helm values -n podinfo unicorn-podinfo", " ") - // stdout, _, err := e2e.UDS(cmd...) - // require.NoError(t, err) - // require.Contains(t, stdout, "key file contents here") - // }) - - // remove(t, bundlePath) + remove(t, bundlePath) } From 9a835a3bc3f099e8a90dea69dd8ba1637497a440 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Tue, 28 May 2024 11:38:56 -0600 Subject: [PATCH 09/30] lint fixes --- src/test/bundles/07-helm-overrides/variable-files/test.pub | 2 +- .../bundles/07-helm-overrides/variable-files/uds-config.yaml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/bundles/07-helm-overrides/variable-files/test.pub b/src/test/bundles/07-helm-overrides/variable-files/test.pub index 0164f2dd..d1c7a393 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/test.pub +++ b/src/test/bundles/07-helm-overrides/variable-files/test.pub @@ -1 +1 @@ -c3NoLXJzYSBBQUFBQjNOemFDMXljMkVBQUFBREFRQUJBQUFCZ1FDS2MyK2JHMjF5YkNUdUMwVTg4RlB2ZWF2cWFkbjlTckJqa0NHcGFISU5xMmhaOThlVmxlNXlJbk9PU3ExbEdNQzg5Rnl4N1ZDbFl3R0sxRHhNS3RSVzZ1K0hVV29PanN4Smg0R0dJY2xyUkc0eEdPU1dRazdWUjJrUnFPM3pLK3hnRUFVbzR5UW85TCtXUHA5R01HR3VNWnVNRkxzS3lwVUpuM1AyWFZVS2d5Mm9TQk1UcVZ1SFpqWjRubjR1YlhPYjlXZk96LzBBRll2c0lCdUpNTXEvSzNxZmZXQWN5RzJ6KzNmQTBQTUJGZmpnOGhpM1FMazdyeFpXdVl4bUtHcklHY3V2NXNYYjN5Q0pNUmF2NmtpbHhZQmxyREVORm5QYjkxUC93MUZBWG8zcnlDU3NYVDRkWFdUT0IvWkdBNHhTRTVGWmFnYUJMSEdrUmxFd0xCT3V5SFhEQ2hUNkI4WWhQYmxTZHp2S1dIZmdNUi9aWnFrLysyNkpEWkFFaVExUXJaNkMzOWNDSUpYUm5RSHlydGRtYWM3YXBrUEpDYlRFbk5RSmRKNTdkS3ltbVBUNmtFZ0dVSDlSLzJmbEtHcnludzFTQ0Q5ckIxT1QvZmJnamhCbGRJMzhNMjBNQUdxdFJvQ0VSaEJBTzd0WVdQLzBGanU2Qi9jQjRWWEZqajA9IHRyaXN0YW5AdHJpc3Rhbi1OVUMxM0FOSGk3 \ No newline at end of file +c3NoLXJzYSBBQUFBQjNOemFDMXljMkVBQUFBREFRQUJBQUFCZ1FDS2MyK2JHMjF5YkNUdUMwVTg4RlB2ZWF2cWFkbjlTckJqa0NHcGFISU5xMmhaOThlVmxlNXlJbk9PU3ExbEdNQzg5Rnl4N1ZDbFl3R0sxRHhNS3RSVzZ1K0hVV29PanN4Smg0R0dJY2xyUkc0eEdPU1dRazdWUjJrUnFPM3pLK3hnRUFVbzR5UW85TCtXUHA5R01HR3VNWnVNRkxzS3lwVUpuM1AyWFZVS2d5Mm9TQk1UcVZ1SFpqWjRubjR1YlhPYjlXZk96LzBBRll2c0lCdUpNTXEvSzNxZmZXQWN5RzJ6KzNmQTBQTUJGZmpnOGhpM1FMazdyeFpXdVl4bUtHcklHY3V2NXNYYjN5Q0pNUmF2NmtpbHhZQmxyREVORm5QYjkxUC93MUZBWG8zcnlDU3NYVDRkWFdUT0IvWkdBNHhTRTVGWmFnYUJMSEdrUmxFd0xCT3V5SFhEQ2hUNkI4WWhQYmxTZHp2S1dIZmdNUi9aWnFrLysyNkpEWkFFaVExUXJaNkMzOWNDSUpYUm5RSHlydGRtYWM3YXBrUEpDYlRFbk5RSmRKNTdkS3ltbVBUNmtFZ0dVSDlSLzJmbEtHcnludzFTQ0Q5ckIxT1QvZmJnamhCbGRJMzhNMjBNQUdxdFJvQ0VSaEJBTzd0WVdQLzBGanU2Qi9jQjRWWEZqajA9IHRyaXN0YW5AdHJpc3Rhbi1OVUMxM0FOSGk3 diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml index f776636f..6f3025a2 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml @@ -2,4 +2,3 @@ files: helm-overrides: domain: domain.txt test_secret: test.pub - From 39d2187829effa970ec7135712048dde40c60618 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Tue, 28 May 2024 20:50:30 -0600 Subject: [PATCH 10/30] fix loadViperConfig duplicate; fix file handling in override add --- src/cmd/uds.go | 35 ----------------------------------- src/pkg/bundle/deploy.go | 10 ++++++---- 2 files changed, 6 insertions(+), 39 deletions(-) diff --git a/src/cmd/uds.go b/src/cmd/uds.go index 01cf56dd..e6f0e7d6 100644 --- a/src/cmd/uds.go +++ b/src/cmd/uds.go @@ -10,7 +10,6 @@ import ( "io" "os" "path/filepath" - "strings" "github.com/AlecAivazis/survey/v2" "github.com/defenseunicorns/uds-cli/src/config" @@ -186,40 +185,6 @@ var logsCmd = &cobra.Command{ }, } -// loadViperConfig reads the config file and unmarshals the relevant config into DeployOpts.Variables -func loadViperConfig() error { - // get config file from Viper - configFile, err := os.ReadFile(v.ConfigFileUsed()) - if err != nil { - return err - } - - // read relevant config into DeployOpts.Variables - // need to use goyaml because Viper doesn't preserve case: https://github.com/spf13/viper/issues/1014 - err = goyaml.Unmarshal(configFile, &bundleCfg.DeployOpts) - if err != nil { - return err - } - - // ensure the DeployOpts.Variables pkg vars are uppercase - for pkgName, pkgVar := range bundleCfg.DeployOpts.Variables { - for varName, varValue := range pkgVar { - // delete the lowercase var and replace with uppercase - delete(bundleCfg.DeployOpts.Variables[pkgName], varName) - bundleCfg.DeployOpts.Variables[pkgName][strings.ToUpper(varName)] = varValue - } - } - - // ensure the DeployOpts.SharedVariables vars are uppercase - for varName, varValue := range bundleCfg.DeployOpts.SharedVariables { - // delete the lowercase var and replace with uppercase - delete(bundleCfg.DeployOpts.SharedVariables, varName) - bundleCfg.DeployOpts.SharedVariables[strings.ToUpper(varName)] = varValue - } - - return nil -} - func init() { initViper() diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index 8d5d0c1f..e2590b0b 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -471,6 +471,7 @@ func (b *Bundle) addOverrideValue(overrides map[string]map[string]*values.Option templatedVariable := fmt.Sprintf("%v", v) value = setTemplatedVariables(templatedVariable, pkgVars) } + if valueType == "file" { verifiedPath, err := b.handleFileVar(value.(string)) if err != nil { @@ -478,11 +479,12 @@ func (b *Bundle) addOverrideValue(overrides map[string]map[string]*values.Option } helmVal := fmt.Sprintf("%s=%v", valuePath, verifiedPath) overrides[component][chart].FileValues = append(overrides[component][chart].FileValues, helmVal) - } + } else { - // handle default case of simple values like strings and numbers - helmVal := fmt.Sprintf("%s=%v", valuePath, value) - overrides[component][chart].Values = append(overrides[component][chart].Values, helmVal) + // handle default case of simple values like strings and numbers + helmVal := fmt.Sprintf("%s=%v", valuePath, value) + overrides[component][chart].Values = append(overrides[component][chart].Values, helmVal) + } } return nil } From cd23c1a4ade9a972d607d835e71bbe81cac1da5f Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 29 May 2024 08:51:52 -0600 Subject: [PATCH 11/30] refactor ChartVariableType; refactor tests. --- .../variable-files/file-not-found-config.yaml | 3 +- .../variable-files/invalid-config.yaml | 9 --- .../variable-files/log-level.txt | 1 + .../variable-files/uds-bundle.yaml | 1 - .../variable-files/uds-config.yaml | 1 - src/test/e2e/variable_test.go | 58 ++++++------------- src/types/bundle.go | 2 +- uds.schema.json | 8 +++ 8 files changed, 30 insertions(+), 53 deletions(-) delete mode 100644 src/test/bundles/07-helm-overrides/variable-files/invalid-config.yaml create mode 100644 src/test/bundles/07-helm-overrides/variable-files/log-level.txt diff --git a/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml index e88cabb2..77f9b11d 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml @@ -1,4 +1,3 @@ -files: +variables: helm-overrides: test_secret: not-there.pub - SECURITY_CTX: sec_ctx.yaml diff --git a/src/test/bundles/07-helm-overrides/variable-files/invalid-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/invalid-config.yaml deleted file mode 100644 index 4beb3d39..00000000 --- a/src/test/bundles/07-helm-overrides/variable-files/invalid-config.yaml +++ /dev/null @@ -1,9 +0,0 @@ - -variables: - helm-overrides: - test_secret: whatever - -files: - helm-overrides: - test_secret: test.pub - SECURITY_CTX: sec_ctx.yaml diff --git a/src/test/bundles/07-helm-overrides/variable-files/log-level.txt b/src/test/bundles/07-helm-overrides/variable-files/log-level.txt new file mode 100644 index 00000000..b2b5d0d0 --- /dev/null +++ b/src/test/bundles/07-helm-overrides/variable-files/log-level.txt @@ -0,0 +1 @@ +debug \ No newline at end of file diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml index 7a3e9da3..be0711e6 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml @@ -16,7 +16,6 @@ packages: - name: log_level path: "podinfo.logLevel" description: "Set the log level for podinfo" - default: "debug" # not overwritten! - name: TEST_SECRET path: "testSecret" description: "base64 encoded key to place in the test secret resource" diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml index 7869b829..5aa77223 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml @@ -1,4 +1,3 @@ variables: helm-overrides: - domain: domain.txt test_secret: test.pub diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 64daa302..9e61c49e 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -375,22 +375,6 @@ func TestExportVarsAsGlobalVars(t *testing.T) { remove(t, bundlePath) } -func TestVariableFilesInvalidConfig(t *testing.T) { - deployZarfInit(t) - e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") - e2e.CreateZarfPkg(t, "src/test/packages/helm", false) - bundleDir := "src/test/bundles/07-helm-overrides/variable-files" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) - err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "invalid-config.yaml")) - require.NoError(t, err) - - createLocal(t, bundleDir, e2e.Arch) - - cmd := strings.Split(fmt.Sprintf("deploy %s --retries 1 --confirm", bundlePath), " ") - _, stderr, _ := e2e.UDS(cmd...) - require.Contains(t, stderr, "invalid config") -} - func TestVariableFilesFileNotFound(t *testing.T) { deployZarfInit(t) e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") @@ -408,38 +392,21 @@ func TestVariableFilesFileNotFound(t *testing.T) { require.Contains(t, stderr, fmt.Sprintf("unable to find file %s/not-there.pub", bundleDir)) } -func TestVariableFilesSettingZarfVar(t *testing.T) { - deployZarfInit(t) - e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") - e2e.CreateZarfPkg(t, "src/test/packages/helm", false) - bundleDir := "src/test/bundles/07-helm-overrides/variable-files" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) - err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) - require.NoError(t, err) - createLocal(t, bundleDir, e2e.Arch) - - require.NoError(t, err) - _, stderr := deploy(t, bundlePath) - - require.Contains(t, stderr, "\"###ZARF_VAR_DOMAIN###\": \"from-file.dev\"") - - remove(t, bundlePath) -} - func TestVariableFilesHelmOverrides(t *testing.T) { deployZarfInit(t) e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") e2e.CreateZarfPkg(t, "src/test/packages/helm", false) bundleDir := "src/test/bundles/07-helm-overrides/variable-files" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) - err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) - require.NoError(t, err) - createLocal(t, bundleDir, e2e.Arch) + os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) + os.Setenv("UDS_DOMAIN", fmt.Sprintf("%s/domain.txt", bundleDir)) + createLocal(t, bundleDir, e2e.Arch) + cmd := strings.Split(fmt.Sprintf("deploy %s --retries 1 --confirm --set helm-overrides.log_level=%s/log-level.txt", bundlePath, bundleDir), " ") + _, stderr, err := e2e.UDS(cmd...) require.NoError(t, err) - deploy(t, bundlePath) - t.Run("test test.pub file contents set as value for testSecret and used in test-secret secret", func(t *testing.T) { + t.Run("test test.pub file contents set by config", func(t *testing.T) { cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-secret -o=jsonpath={.data.test}", " ") stdout, _, err := e2e.UDS(cmd...) require.NoError(t, err) @@ -448,5 +415,18 @@ func TestVariableFilesHelmOverrides(t *testing.T) { require.Contains(t, string(decoded), "ssh-rsa") }) + t.Run("test log-level.txt set by --set", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-secret -o=jsonpath={.data.test}", " ") + stdout, _, err := e2e.UDS(cmd...) + require.NoError(t, err) + decoded, err := base64.StdEncoding.DecodeString(stdout) + require.NoError(t, err) + require.Contains(t, string(decoded), "ssh-rsa") + }) + + t.Run("test domain zarf var set by env variable", func(t *testing.T) { + require.Contains(t, stderr, fmt.Sprintf("\"###ZARF_VAR_DOMAIN###\": \"%s/domain.txt\"", bundleDir)) + }) + remove(t, bundlePath) } diff --git a/src/types/bundle.go b/src/types/bundle.go index 267f859d..6f26ab43 100644 --- a/src/types/bundle.go +++ b/src/types/bundle.go @@ -51,7 +51,7 @@ type BundleChartVariable struct { Name string `json:"name" jsonschema:"name=Name of the variable to set"` Description string `json:"description,omitempty" jsonschema:"name=Description of the variable"` Default interface{} `json:"default,omitempty" jsonschema:"name=The default value to set"` - Type ChartVariableType `json:"type,omitempty" jsonschema:"type=The type of value to be processed"` + Type ChartVariableType `json:"type,omitempty" jsonschema:"description=The type of value to be processed,enum=raw,enum=file"` } // BundleVariableImport represents variables in the bundle diff --git a/uds.schema.json b/uds.schema.json index 1030a301..d335fbcb 100644 --- a/uds.schema.json +++ b/uds.schema.json @@ -68,6 +68,14 @@ }, "default": { "additionalProperties": true + }, + "type": { + "enum": [ + "raw", + "file" + ], + "type": "string", + "description": "The type of value to be processed" } }, "additionalProperties": false, From bec07c982605e2461dcad2296ba3c612373bf5d6 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 29 May 2024 08:52:47 -0600 Subject: [PATCH 12/30] lint fix --- src/test/bundles/07-helm-overrides/variable-files/log-level.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/bundles/07-helm-overrides/variable-files/log-level.txt b/src/test/bundles/07-helm-overrides/variable-files/log-level.txt index b2b5d0d0..d287cd3e 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/log-level.txt +++ b/src/test/bundles/07-helm-overrides/variable-files/log-level.txt @@ -1 +1 @@ -debug \ No newline at end of file +debug From a2fe42030f95fcd08c6f1dfa41023401d3a93484 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 29 May 2024 11:57:44 -0600 Subject: [PATCH 13/30] file path handling made more reusable; fixed tests. --- src/pkg/bundle/deploy.go | 25 ++++++++----------- .../variable-files/domain.txt | 1 - .../variable-files/uds-bundle.yaml | 1 + .../variable-files/uds-config.yaml | 3 +++ src/test/e2e/variable_test.go | 19 +++++++------- src/test/packages/helm/zarf.yaml | 3 +++ 6 files changed, 26 insertions(+), 26 deletions(-) delete mode 100644 src/test/bundles/07-helm-overrides/variable-files/domain.txt diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index e2590b0b..94abe525 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -155,29 +155,24 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { return nil } -func formFullRelativePath(configPath string, path string) string { - if !filepath.IsAbs(path) { - // set path relative to config file, unless they are the same - if filepath.Dir(configPath) != filepath.Dir(path) { - path = filepath.Join(filepath.Dir(configPath), path) +func formAndCheckFilePath(anchorPath string, filePath string) (string, error) { + if !filepath.IsAbs(filePath) { + // set path relative to anchorPath (i.e. cwd or config), unless they are the same + if filepath.Dir(anchorPath) != filepath.Dir(filePath) { + filePath = filepath.Join(filepath.Dir(anchorPath), filePath) } } - return path -} - -func (b *Bundle) handleFileVar(path string) (string, error) { - path = formFullRelativePath(b.cfg.DeployOpts.Config, path) - if helpers.InvalidPath(path) { - return "", fmt.Errorf("unable to find file %s", path) + if helpers.InvalidPath(filePath) { + return "", fmt.Errorf("Unable to find file %s", filePath) } - _, err := helpers.IsTextFile(path) + _, err := helpers.IsTextFile(filePath) if err != nil { return "", err } - return path, nil + return filePath, nil } // loadVariables loads and sets precedence for config-level and imported variables @@ -473,7 +468,7 @@ func (b *Bundle) addOverrideValue(overrides map[string]map[string]*values.Option } if valueType == "file" { - verifiedPath, err := b.handleFileVar(value.(string)) + verifiedPath, err := formAndCheckFilePath(b.cfg.DeployOpts.Config, value.(string)) if err != nil { return err } diff --git a/src/test/bundles/07-helm-overrides/variable-files/domain.txt b/src/test/bundles/07-helm-overrides/variable-files/domain.txt deleted file mode 100644 index 191f7b22..00000000 --- a/src/test/bundles/07-helm-overrides/variable-files/domain.txt +++ /dev/null @@ -1 +0,0 @@ -from-file.dev diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml index be0711e6..fbbca82f 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml @@ -16,6 +16,7 @@ packages: - name: log_level path: "podinfo.logLevel" description: "Set the log level for podinfo" + type: file - name: TEST_SECRET path: "testSecret" description: "base64 encoded key to place in the test secret resource" diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml index 5aa77223..f7daad6a 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml @@ -1,3 +1,6 @@ +options: + log_level: debug + variables: helm-overrides: test_secret: test.pub diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 9e61c49e..efd552ad 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -381,8 +381,7 @@ func TestVariableFilesFileNotFound(t *testing.T) { e2e.CreateZarfPkg(t, "src/test/packages/helm", false) bundleDir := "src/test/bundles/07-helm-overrides/variable-files" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) - err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "file-not-found-config.yaml")) - require.NoError(t, err) + os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "file-not-found-config.yaml")) createLocal(t, bundleDir, e2e.Arch) @@ -398,10 +397,10 @@ func TestVariableFilesHelmOverrides(t *testing.T) { e2e.CreateZarfPkg(t, "src/test/packages/helm", false) bundleDir := "src/test/bundles/07-helm-overrides/variable-files" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) - os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) - os.Setenv("UDS_DOMAIN", fmt.Sprintf("%s/domain.txt", bundleDir)) - createLocal(t, bundleDir, e2e.Arch) + + os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) + os.Setenv("UDS_TEST_FILE", fmt.Sprintf("%s/test-zarf-var-file.txt", bundleDir)) cmd := strings.Split(fmt.Sprintf("deploy %s --retries 1 --confirm --set helm-overrides.log_level=%s/log-level.txt", bundlePath, bundleDir), " ") _, stderr, err := e2e.UDS(cmd...) require.NoError(t, err) @@ -416,16 +415,16 @@ func TestVariableFilesHelmOverrides(t *testing.T) { }) t.Run("test log-level.txt set by --set", func(t *testing.T) { - cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-secret -o=jsonpath={.data.test}", " ") + cmd := strings.Split("zarf tools kubectl get pod -n podinfo -o=jsonpath={.items[0].spec.containers[0].command}", " ") stdout, _, err := e2e.UDS(cmd...) require.NoError(t, err) - decoded, err := base64.StdEncoding.DecodeString(stdout) - require.NoError(t, err) - require.Contains(t, string(decoded), "ssh-rsa") + require.Contains(t, stdout, "--level=debug") }) t.Run("test domain zarf var set by env variable", func(t *testing.T) { - require.Contains(t, stderr, fmt.Sprintf("\"###ZARF_VAR_DOMAIN###\": \"%s/domain.txt\"", bundleDir)) + // checking output of action in the helm-overrides package + // zarf will handle actually parsing the files passed to it + require.Contains(t, stderr, fmt.Sprintf("TEST_FILE set as %s/test-zarf-var-file.txt", bundleDir)) }) remove(t, bundlePath) diff --git a/src/test/packages/helm/zarf.yaml b/src/test/packages/helm/zarf.yaml index 1bd13e2a..8aac84a8 100644 --- a/src/test/packages/helm/zarf.yaml +++ b/src/test/packages/helm/zarf.yaml @@ -7,6 +7,8 @@ metadata: variables: - name: DOMAIN default: uds.dev + - name: TEST_FILE + type: file components: - name: podinfo-component @@ -24,3 +26,4 @@ components: after: - cmd: | echo "shared var in helm-overrides pkg: "${ZARF_VAR_DOMAIN}"" + - cmd: echo "TEST_FILE set as ${ZARF_VAR_TEST_FILE}" From 3a407ec10241fd2287289ad50b2c8ab142902508 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 29 May 2024 13:33:24 -0600 Subject: [PATCH 14/30] fix typo in test --- .../variable-files/file-not-found-config.yaml | 3 +++ src/test/e2e/variable_test.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml index 77f9b11d..0dc695b8 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml @@ -1,3 +1,6 @@ +options: + log_level: debug + variables: helm-overrides: test_secret: not-there.pub diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index efd552ad..5487ebed 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -388,7 +388,7 @@ func TestVariableFilesFileNotFound(t *testing.T) { cmd := strings.Split(fmt.Sprintf("deploy %s --retries 1 --confirm", bundlePath), " ") _, stderr, _ := e2e.UDS(cmd...) - require.Contains(t, stderr, fmt.Sprintf("unable to find file %s/not-there.pub", bundleDir)) + require.Contains(t, stderr, fmt.Sprintf("Unable to find file %s/not-there.pub", bundleDir)) } func TestVariableFilesHelmOverrides(t *testing.T) { From 513ed8e0d8e2c9f473523e9824fd67301b7636d4 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 29 May 2024 13:41:00 -0600 Subject: [PATCH 15/30] lint fix' --- .../07-helm-overrides/variable-files/file-not-found-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml index 0dc695b8..d447b1c8 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml @@ -1,6 +1,6 @@ options: log_level: debug - + variables: helm-overrides: test_secret: not-there.pub From daf9076f442c4137b4c0f135a8694b3a9515cd30 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 29 May 2024 14:46:56 -0600 Subject: [PATCH 16/30] add docs; make test less brittle. --- docs/overrides.md | 42 +++++++++++++++++++++++++++++++++++ src/test/e2e/variable_test.go | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/overrides.md b/docs/overrides.md index 4bf74734..63b9f87b 100644 --- a/docs/overrides.md +++ b/docs/overrides.md @@ -242,6 +242,48 @@ Variable precedence is as follows: 1. `uds-config.yaml` variables 1. Variables `default` in the`uds-bundle.yaml` +#### Variable Types +Variables can be of either type `raw` or `file`. The type will default to raw if not set explicitly. + +> [!WARNING] +> If a variable is set to accept a file as it's value, but is missing the `file` type, then the file will not be processed. + +```yaml +kind: UDSBundle +metadata: + name: example-bundle + version: 0.0.1 + +packages: + - name: helm-overrides-package + path: "../../packages/helm" + ref: 0.0.1 + overrides: + podinfo-component: + unicorn-podinfo: + variables: + - name: test_secret + path: "testSecret" + description: "Key to be set in a secret" + type: file +``` + +**File Paths** + +If a file path is not absolute it will be set as relative to the uds-config.yaml path. + +e.g. the following `uds-config.yaml` is in [`src/test/bundles/07-helm-overrides/variable-files/`](../src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml) +```yaml +variables: + helm-overrides: + test_secret: test.pub +``` + +This means when `test.pub` is evalutated it will first be appended to the config path like so `src/test/bundles/07-helm-overrides/variable-files/test.pub`. + +If the file path is already set to the same relative path as the config, then no merging will take place. + + ### Namespace It's also possible to specify a namespace for a packaged Helm chart to be installed in. For example, to deploy the a chart in the `custom-podinfo` namespace, you can specify the `namespace` in the `overrides` block: diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 5487ebed..a996080d 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -415,7 +415,7 @@ func TestVariableFilesHelmOverrides(t *testing.T) { }) t.Run("test log-level.txt set by --set", func(t *testing.T) { - cmd := strings.Split("zarf tools kubectl get pod -n podinfo -o=jsonpath={.items[0].spec.containers[0].command}", " ") + cmd := strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath={.spec.template.spec.containers[0].command}", " ") stdout, _, err := e2e.UDS(cmd...) require.NoError(t, err) require.Contains(t, stdout, "--level=debug") From 05080fa58dd2089922ec5624f30c510e44048898 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 29 May 2024 15:05:15 -0600 Subject: [PATCH 17/30] add to docs; rename test --- docs/overrides.md | 4 ++++ src/test/e2e/variable_test.go | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/overrides.md b/docs/overrides.md index 63b9f87b..8553454d 100644 --- a/docs/overrides.md +++ b/docs/overrides.md @@ -283,6 +283,10 @@ This means when `test.pub` is evalutated it will first be appended to the config If the file path is already set to the same relative path as the config, then no merging will take place. +> [!NOTE] +> uds-cli does not encrypt or base64 encode any file contents before passing said data to Zarf or Helm. +> So if for example the file contains a key to be used in a kubernetes secret, it must be base64 encoded before given to uds-cli. + ### Namespace It's also possible to specify a namespace for a packaged Helm chart to be installed in. For example, to deploy the a chart in the `custom-podinfo` namespace, you can specify the `namespace` in the `overrides` block: diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index a996080d..4327bc59 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -391,7 +391,7 @@ func TestVariableFilesFileNotFound(t *testing.T) { require.Contains(t, stderr, fmt.Sprintf("Unable to find file %s/not-there.pub", bundleDir)) } -func TestVariableFilesHelmOverrides(t *testing.T) { +func TestVariableFiles(t *testing.T) { deployZarfInit(t) e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") e2e.CreateZarfPkg(t, "src/test/packages/helm", false) @@ -401,11 +401,12 @@ func TestVariableFilesHelmOverrides(t *testing.T) { os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) os.Setenv("UDS_TEST_FILE", fmt.Sprintf("%s/test-zarf-var-file.txt", bundleDir)) + cmd := strings.Split(fmt.Sprintf("deploy %s --retries 1 --confirm --set helm-overrides.log_level=%s/log-level.txt", bundlePath, bundleDir), " ") _, stderr, err := e2e.UDS(cmd...) require.NoError(t, err) - t.Run("test test.pub file contents set by config", func(t *testing.T) { + t.Run("test test_secret helm override set by config", func(t *testing.T) { cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-secret -o=jsonpath={.data.test}", " ") stdout, _, err := e2e.UDS(cmd...) require.NoError(t, err) @@ -414,7 +415,7 @@ func TestVariableFilesHelmOverrides(t *testing.T) { require.Contains(t, string(decoded), "ssh-rsa") }) - t.Run("test log-level.txt set by --set", func(t *testing.T) { + t.Run("test log-level helm override set by --set", func(t *testing.T) { cmd := strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath={.spec.template.spec.containers[0].command}", " ") stdout, _, err := e2e.UDS(cmd...) require.NoError(t, err) From a2b427693119b88a0314a9a7a16aa59788a91147 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Thu, 30 May 2024 07:38:40 -0600 Subject: [PATCH 18/30] move formAndCheckFilePath and annotate --- src/pkg/bundle/deploy.go | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index 94abe525..b0f1b9e1 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -155,26 +155,6 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { return nil } -func formAndCheckFilePath(anchorPath string, filePath string) (string, error) { - if !filepath.IsAbs(filePath) { - // set path relative to anchorPath (i.e. cwd or config), unless they are the same - if filepath.Dir(anchorPath) != filepath.Dir(filePath) { - filePath = filepath.Join(filepath.Dir(anchorPath), filePath) - } - } - - if helpers.InvalidPath(filePath) { - return "", fmt.Errorf("Unable to find file %s", filePath) - } - - _, err := helpers.IsTextFile(filePath) - if err != nil { - return "", err - } - - return filePath, nil -} - // loadVariables loads and sets precedence for config-level and imported variables func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string]map[string]string) (map[string]string, error) { pkgVars := make(map[string]string) @@ -467,6 +447,7 @@ func (b *Bundle) addOverrideValue(overrides map[string]map[string]*values.Option value = setTemplatedVariables(templatedVariable, pkgVars) } + // Check for files else handle default case of simple values like strings and numbers if valueType == "file" { verifiedPath, err := formAndCheckFilePath(b.cfg.DeployOpts.Config, value.(string)) if err != nil { @@ -475,8 +456,6 @@ func (b *Bundle) addOverrideValue(overrides map[string]map[string]*values.Option helmVal := fmt.Sprintf("%s=%v", valuePath, verifiedPath) overrides[component][chart].FileValues = append(overrides[component][chart].FileValues, helmVal) } else { - - // handle default case of simple values like strings and numbers helmVal := fmt.Sprintf("%s=%v", valuePath, value) overrides[component][chart].Values = append(overrides[component][chart].Values, helmVal) } @@ -498,3 +477,24 @@ func setTemplatedVariables(templatedVariables string, pkgVars map[string]string) }) return replacedValue } + +// formAndCheckFilePath merges relative paths together to form full path and checks if the file exists +func formAndCheckFilePath(anchorPath string, filePath string) (string, error) { + if !filepath.IsAbs(filePath) { + // set path relative to anchorPath (i.e. cwd or config), unless they are the same + if filepath.Dir(anchorPath) != filepath.Dir(filePath) { + filePath = filepath.Join(filepath.Dir(anchorPath), filePath) + } + } + + if helpers.InvalidPath(filePath) { + return "", fmt.Errorf("Unable to find file %s", filePath) + } + + _, err := helpers.IsTextFile(filePath) + if err != nil { + return "", err + } + + return filePath, nil +} From 2d11b4636b6a81aba1d26745891cdce2b4918100 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Fri, 31 May 2024 07:24:02 -0600 Subject: [PATCH 19/30] file handling and docs refactors --- docs/overrides.md | 18 ++++--- src/cmd/uds.go | 9 +--- src/pkg/bundle/deploy.go | 50 ++++++++++++------- .../variable-files/{test.pub => test.cert} | 0 .../variable-files/uds-config.yaml | 2 +- src/test/e2e/variable_test.go | 2 +- 6 files changed, 46 insertions(+), 35 deletions(-) rename src/test/bundles/07-helm-overrides/variable-files/{test.pub => test.cert} (100%) diff --git a/docs/overrides.md b/docs/overrides.md index 8553454d..50cf6141 100644 --- a/docs/overrides.md +++ b/docs/overrides.md @@ -246,7 +246,7 @@ Variable precedence is as follows: Variables can be of either type `raw` or `file`. The type will default to raw if not set explicitly. > [!WARNING] -> If a variable is set to accept a file as it's value, but is missing the `file` type, then the file will not be processed. +> If a variable is set to accept a file as its value, but is missing the `file` type, then the file will not be processed. ```yaml kind: UDSBundle @@ -262,30 +262,34 @@ packages: podinfo-component: unicorn-podinfo: variables: + - name: UI_COLOR + path: "ui.color" + description: "variable UI_COLOR accepts a raw value (e.g. a string, int, map) like "purple", which is passed to the ui.color helm path" + type: raw - name: test_secret path: "testSecret" - description: "Key to be set in a secret" + description: "variable TEST_SECRET will resolve to the contents of a file (e.g. test.cert), which gets passed to the testSecret helm path" type: file ``` **File Paths** -If a file path is not absolute it will be set as relative to the uds-config.yaml path. +If a file path is not absolute, it will be set as relative to the `uds-config.yaml` directory. e.g. the following `uds-config.yaml` is in [`src/test/bundles/07-helm-overrides/variable-files/`](../src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml) ```yaml variables: helm-overrides: - test_secret: test.pub + test_secret: test.cert ``` -This means when `test.pub` is evalutated it will first be appended to the config path like so `src/test/bundles/07-helm-overrides/variable-files/test.pub`. +This means when `test.cert` is evalutated it will first be appended to the config path like so `src/test/bundles/07-helm-overrides/variable-files/test.cert`. If the file path is already set to the same relative path as the config, then no merging will take place. > [!NOTE] -> uds-cli does not encrypt or base64 encode any file contents before passing said data to Zarf or Helm. -> So if for example the file contains a key to be used in a kubernetes secret, it must be base64 encoded before given to uds-cli. +> UDS CLI does not encrypt or base64 encode any file contents before passing said data to Zarf or Helm. +> For example, if the file contains a key to be used in a Kubernetes secret, it must be base64 encoded before being ingested by UDS CLI. ### Namespace diff --git a/src/cmd/uds.go b/src/cmd/uds.go index e6f0e7d6..9474247f 100644 --- a/src/cmd/uds.go +++ b/src/cmd/uds.go @@ -56,14 +56,9 @@ var deployCmd = &cobra.Command{ bundleCfg.DeployOpts.Source = chooseBundle(args) configureZarf() - // load uds-config if it exists - config := v.ConfigFileUsed() - if config != "" { + // set DeployOptions.Config if exists + if config := v.ConfigFileUsed(); config != "" { bundleCfg.DeployOpts.Config = config - if err := loadViperConfig(); err != nil { - message.Fatalf(err, "Failed to load uds-config: %s", err.Error()) - return - } } // create new bundle client and deploy diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index b0f1b9e1..8bbe45b4 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -54,10 +54,11 @@ func (b *Bundle) Deploy() error { if len(userSpecifiedPackages) != len(packagesToDeploy) { return fmt.Errorf("invalid zarf packages specified by --packages") } - return deployPackages(packagesToDeploy, resume, b) + } else { + packagesToDeploy = b.bundle.Packages } - return deployPackages(b.bundle.Packages, resume, b) + return deployPackages(packagesToDeploy, resume, b) } func deployPackages(packages []types.Package, resume bool, b *Bundle) error { @@ -343,7 +344,7 @@ func (b *Bundle) processOverrideNamespaces(overrideMap sources.NamespaceOverride func (b *Bundle) processOverrideValues(overrideMap *map[string]map[string]*values.Options, values *[]types.BundleChartValue, componentName string, chartName string, pkgVars map[string]string) error { for _, v := range *values { // Add the override to the map, or return an error if the path is invalid - if err := b.addOverrideValue(*overrideMap, componentName, chartName, v.Path, "raw", v.Value, pkgVars); err != nil { + if err := b.addOverride(*overrideMap, componentName, chartName, v.Path, types.Raw, v.Value, pkgVars); err != nil { return err } } @@ -389,7 +390,7 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va } // Add the override to the map, or return an error if the path is invalid - if err := b.addOverrideValue(*overrideMap, componentName, chartName, v.Path, v.Type, overrideVal, nil); err != nil { + if err := b.addOverride(*overrideMap, componentName, chartName, v.Path, v.Type, overrideVal, nil); err != nil { return err } @@ -397,8 +398,8 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va return nil } -// addOverrideValue adds a value to a PkgOverrideMap -func (b *Bundle) addOverrideValue(overrides map[string]map[string]*values.Options, component string, chart string, valuePath string, valueType types.ChartVariableType, value interface{}, pkgVars map[string]string) error { +// addOverride adds a value or variable to a PkgOverrideMap +func (b *Bundle) addOverride(overrides map[string]map[string]*values.Options, component string, chart string, valuePath string, valueType types.ChartVariableType, value interface{}, pkgVars map[string]string) error { // Create the component map if it doesn't exist if _, ok := overrides[component]; !ok { overrides[component] = make(map[string]*values.Options) @@ -409,6 +410,16 @@ func (b *Bundle) addOverrideValue(overrides map[string]map[string]*values.Option overrides[component][chart] = &values.Options{} } + if valueType == "file" { + if fileVals, err := b.verifyAndAddFileTo(overrides[component][chart].FileValues, value.(string), valuePath); err == nil { + overrides[component][chart].FileValues = fileVals + } else { + return err + } + + return nil + } + // Add the value to the chart map switch v := value.(type) { case []interface{}: @@ -447,18 +458,9 @@ func (b *Bundle) addOverrideValue(overrides map[string]map[string]*values.Option value = setTemplatedVariables(templatedVariable, pkgVars) } - // Check for files else handle default case of simple values like strings and numbers - if valueType == "file" { - verifiedPath, err := formAndCheckFilePath(b.cfg.DeployOpts.Config, value.(string)) - if err != nil { - return err - } - helmVal := fmt.Sprintf("%s=%v", valuePath, verifiedPath) - overrides[component][chart].FileValues = append(overrides[component][chart].FileValues, helmVal) - } else { - helmVal := fmt.Sprintf("%s=%v", valuePath, value) - overrides[component][chart].Values = append(overrides[component][chart].Values, helmVal) - } + // Handle default case of simple values like strings and numbers + helmVal := fmt.Sprintf("%s=%v", valuePath, value) + overrides[component][chart].Values = append(overrides[component][chart].Values, helmVal) } return nil } @@ -478,6 +480,16 @@ func setTemplatedVariables(templatedVariables string, pkgVars map[string]string) return replacedValue } +// verifyAndAddFileTo +func (b *Bundle) verifyAndAddFileTo(helmFileVals []string, filePath string, key string) ([]string, error) { + verifiedPath, err := formAndCheckFilePath(b.cfg.DeployOpts.Config, filePath) + if err != nil { + return nil, err + } + helmVal := fmt.Sprintf("%s=%v", key, verifiedPath) + return append(helmFileVals, helmVal), nil +} + // formAndCheckFilePath merges relative paths together to form full path and checks if the file exists func formAndCheckFilePath(anchorPath string, filePath string) (string, error) { if !filepath.IsAbs(filePath) { @@ -488,7 +500,7 @@ func formAndCheckFilePath(anchorPath string, filePath string) (string, error) { } if helpers.InvalidPath(filePath) { - return "", fmt.Errorf("Unable to find file %s", filePath) + return "", fmt.Errorf("unable to find file %s", filePath) } _, err := helpers.IsTextFile(filePath) diff --git a/src/test/bundles/07-helm-overrides/variable-files/test.pub b/src/test/bundles/07-helm-overrides/variable-files/test.cert similarity index 100% rename from src/test/bundles/07-helm-overrides/variable-files/test.pub rename to src/test/bundles/07-helm-overrides/variable-files/test.cert diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml index f7daad6a..938dec67 100644 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml @@ -3,4 +3,4 @@ options: variables: helm-overrides: - test_secret: test.pub + test_secret: test.cert diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 4327bc59..7bcec9e5 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -388,7 +388,7 @@ func TestVariableFilesFileNotFound(t *testing.T) { cmd := strings.Split(fmt.Sprintf("deploy %s --retries 1 --confirm", bundlePath), " ") _, stderr, _ := e2e.UDS(cmd...) - require.Contains(t, stderr, fmt.Sprintf("Unable to find file %s/not-there.pub", bundleDir)) + require.Contains(t, stderr, fmt.Sprintf("unable to find file %s/not-there.pub", bundleDir)) } func TestVariableFiles(t *testing.T) { From bc7d4d065964cd0d0d241155b5e57c6cfa4da2b0 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Mon, 3 Jun 2024 09:15:22 -0600 Subject: [PATCH 20/30] handle different value source paths. --- src/pkg/bundle/deploy.go | 72 +++++++++++++++++++++++++---------- src/test/e2e/variable_test.go | 4 +- src/types/bundle.go | 14 +++++++ 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index 8bbe45b4..c60e2d51 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -97,7 +97,7 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { publicKeyPath = "" } - pkgVars, err := b.loadVariables(pkg, bundleExportedVars) + pkgVars := b.loadVariables(pkg, bundleExportedVars) if err != nil { return err } @@ -157,7 +157,7 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { } // loadVariables loads and sets precedence for config-level and imported variables -func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string]map[string]string) (map[string]string, error) { +func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string]map[string]string) map[string]string { pkgVars := make(map[string]string) // load all exported variables @@ -200,7 +200,7 @@ func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string] pkgVars[strings.ToUpper(name)] = fmt.Sprint(val) } } - return pkgVars, nil + return pkgVars } // ConfirmBundleDeploy uses Zarf's pterm logging to prompt the user to confirm bundle creation @@ -344,7 +344,7 @@ func (b *Bundle) processOverrideNamespaces(overrideMap sources.NamespaceOverride func (b *Bundle) processOverrideValues(overrideMap *map[string]map[string]*values.Options, values *[]types.BundleChartValue, componentName string, chartName string, pkgVars map[string]string) error { for _, v := range *values { // Add the override to the map, or return an error if the path is invalid - if err := b.addOverride(*overrideMap, componentName, chartName, v.Path, types.Raw, v.Value, pkgVars); err != nil { + if err := addOverride(*overrideMap, componentName, chartName, v, v.Value, pkgVars); err != nil { return err } } @@ -365,32 +365,38 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va setVal := strings.Split(k, ".") if setVal[0] == pkgName && strings.ToUpper(setVal[1]) == v.Name { overrideVal = val + v.ValueSource = b.getSourcePath(types.CLI) } } else if strings.ToUpper(k) == v.Name { overrideVal = val + v.ValueSource = b.getSourcePath(types.CLI) } } // check for override in env vars if not in --set if envVarOverride, exists := os.LookupEnv(strings.ToUpper(config.EnvVarPrefix + v.Name)); overrideVal == nil && exists { overrideVal = envVarOverride + v.ValueSource = b.getSourcePath(types.Env) } // if not in --set or an env var, use the following precedence: configFile, sharedConfig, default if overrideVal == nil { if configFileOverride, existsInConfig := b.cfg.DeployOpts.Variables[pkgName][v.Name]; existsInConfig { overrideVal = configFileOverride + v.ValueSource = b.getSourcePath(types.Config) } else if sharedConfigOverride, existsInSharedConfig := b.cfg.DeployOpts.SharedVariables[v.Name]; existsInSharedConfig { overrideVal = sharedConfigOverride + v.ValueSource = b.getSourcePath(types.Config) } else if v.Default != nil { overrideVal = v.Default + v.ValueSource = b.getSourcePath(types.Bundle) } else { continue } } // Add the override to the map, or return an error if the path is invalid - if err := b.addOverride(*overrideMap, componentName, chartName, v.Path, v.Type, overrideVal, nil); err != nil { + if err := addOverride(*overrideMap, componentName, chartName, v, overrideVal, nil); err != nil { return err } @@ -399,7 +405,7 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va } // addOverride adds a value or variable to a PkgOverrideMap -func (b *Bundle) addOverride(overrides map[string]map[string]*values.Options, component string, chart string, valuePath string, valueType types.ChartVariableType, value interface{}, pkgVars map[string]string) error { +func addOverride[T types.ChartOverride](overrides map[string]map[string]*values.Options, component string, chart string, override T, value interface{}, pkgVars map[string]string) error { // Create the component map if it doesn't exist if _, ok := overrides[component]; !ok { overrides[component] = make(map[string]*values.Options) @@ -410,14 +416,21 @@ func (b *Bundle) addOverride(overrides map[string]map[string]*values.Options, co overrides[component][chart] = &values.Options{} } - if valueType == "file" { - if fileVals, err := b.verifyAndAddFileTo(overrides[component][chart].FileValues, value.(string), valuePath); err == nil { - overrides[component][chart].FileValues = fileVals - } else { - return err - } + var valuePath string - return nil + switch v := any(override).(type) { + case types.BundleChartValue: + valuePath = v.Path + case types.BundleChartVariable: + valuePath = v.Path + if v.Type == types.File { + if fileVals, err := addFileValue(overrides[component][chart].FileValues, value.(string), v); err == nil { + overrides[component][chart].FileValues = fileVals + } else { + return err + } + return nil + } } // Add the value to the chart map @@ -465,6 +478,23 @@ func (b *Bundle) addOverride(overrides map[string]map[string]*values.Options, co return nil } +// getSourcePath returns the path from where a value is set +func (b *Bundle) getSourcePath(pathType types.ValueSources) string { + var sourcePath string + switch pathType { + case types.CLI: + sourcePath, _ = os.Getwd() + case types.Env: + sourcePath, _ = os.Getwd() + case types.Bundle: + sourcePath = b.cfg.DeployOpts.Source + case types.Config: + sourcePath = filepath.Dir(b.cfg.DeployOpts.Config) + } + + return sourcePath +} + // setTemplatedVariables sets the value for the templated variables func setTemplatedVariables(templatedVariables string, pkgVars map[string]string) string { // Use ReplaceAllStringFunc to handle all occurrences of templated variables @@ -480,22 +510,22 @@ func setTemplatedVariables(templatedVariables string, pkgVars map[string]string) return replacedValue } -// verifyAndAddFileTo -func (b *Bundle) verifyAndAddFileTo(helmFileVals []string, filePath string, key string) ([]string, error) { - verifiedPath, err := formAndCheckFilePath(b.cfg.DeployOpts.Config, filePath) +// addFileValue +func addFileValue(helmFileVals []string, filePath string, override types.BundleChartVariable) ([]string, error) { + verifiedPath, err := formFilePath(override.ValueSource, filePath) if err != nil { return nil, err } - helmVal := fmt.Sprintf("%s=%v", key, verifiedPath) + helmVal := fmt.Sprintf("%s=%v", override.Path, verifiedPath) return append(helmFileVals, helmVal), nil } -// formAndCheckFilePath merges relative paths together to form full path and checks if the file exists -func formAndCheckFilePath(anchorPath string, filePath string) (string, error) { +// formFilePath merges relative paths together to form full path and checks if the file exists +func formFilePath(anchorPath string, filePath string) (string, error) { if !filepath.IsAbs(filePath) { // set path relative to anchorPath (i.e. cwd or config), unless they are the same - if filepath.Dir(anchorPath) != filepath.Dir(filePath) { - filePath = filepath.Join(filepath.Dir(anchorPath), filePath) + if anchorPath != filepath.Dir(filePath) { + filePath = filepath.Join(anchorPath, filePath) } } diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 7bcec9e5..0a1f7d64 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -385,7 +385,7 @@ func TestVariableFilesFileNotFound(t *testing.T) { createLocal(t, bundleDir, e2e.Arch) - cmd := strings.Split(fmt.Sprintf("deploy %s --retries 1 --confirm", bundlePath), " ") + cmd := strings.Split(fmt.Sprintf("deploy %s --confirm", bundlePath), " ") _, stderr, _ := e2e.UDS(cmd...) require.Contains(t, stderr, fmt.Sprintf("unable to find file %s/not-there.pub", bundleDir)) @@ -402,7 +402,7 @@ func TestVariableFiles(t *testing.T) { os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) os.Setenv("UDS_TEST_FILE", fmt.Sprintf("%s/test-zarf-var-file.txt", bundleDir)) - cmd := strings.Split(fmt.Sprintf("deploy %s --retries 1 --confirm --set helm-overrides.log_level=%s/log-level.txt", bundlePath, bundleDir), " ") + cmd := strings.Split(fmt.Sprintf("deploy %s --confirm --set helm-overrides.log_level=%s/log-level.txt", bundlePath, bundleDir), " ") _, stderr, err := e2e.UDS(cmd...) require.NoError(t, err) diff --git a/src/types/bundle.go b/src/types/bundle.go index 6f26ab43..e1d8b523 100644 --- a/src/types/bundle.go +++ b/src/types/bundle.go @@ -11,6 +11,15 @@ const ( Raw ChartVariableType = "raw" ) +type ValueSources string + +const ( + Config ValueSources = "config" + Env ValueSources = "env" + CLI ValueSources = "cli" + Bundle ValueSources = "bundle" +) + // UDSBundle is the top-level structure of a UDS bundle type UDSBundle struct { Kind string `json:"kind" jsonschema:"description=The kind of UDS package,enum=UDSBundle"` @@ -41,6 +50,10 @@ type BundleChartOverrides struct { ValuesFiles []string `json:"valuesFiles,omitempty" jsonschema:"description=List of Helm chart value file paths to set statically"` } +type ChartOverride interface { + BundleChartVariable | BundleChartValue +} + type BundleChartValue struct { Path string `json:"path" jsonschema:"name=Path to the Helm chart value to set. The format is , example=controller.service.type"` Value interface{} `json:"value" jsonschema:"name=The value to set"` @@ -52,6 +65,7 @@ type BundleChartVariable struct { Description string `json:"description,omitempty" jsonschema:"name=Description of the variable"` Default interface{} `json:"default,omitempty" jsonschema:"name=The default value to set"` Type ChartVariableType `json:"type,omitempty" jsonschema:"description=The type of value to be processed,enum=raw,enum=file"` + ValueSource string } // BundleVariableImport represents variables in the bundle From 18b13ece62e02ba78ee4c2572b000be97e363b7d Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Mon, 3 Jun 2024 12:16:32 -0600 Subject: [PATCH 21/30] refactor tests. --- src/pkg/bundle/deploy.go | 5 +- src/pkg/bundle/deploy_test.go | 96 ++++++++++++++++++- .../bundles/07-helm-overrides/uds-bundle.yaml | 1 + .../bundles/07-helm-overrides/uds-config.yaml | 1 + .../variable-files/file-not-found-config.yaml | 6 -- .../variable-files/uds-bundle.yaml | 23 ----- .../variable-files/uds-config.yaml | 6 -- src/test/e2e/variable_test.go | 82 ++++------------ .../packages/helm/unicorn-podinfo/values.yaml | 5 + 9 files changed, 124 insertions(+), 101 deletions(-) delete mode 100644 src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml delete mode 100644 src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml delete mode 100644 src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index c60e2d51..0c123964 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -98,9 +98,6 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle) error { } pkgVars := b.loadVariables(pkg, bundleExportedVars) - if err != nil { - return err - } opts := zarfTypes.ZarfPackageOptions{ PackageSource: pkgTmp, @@ -487,7 +484,7 @@ func (b *Bundle) getSourcePath(pathType types.ValueSources) string { case types.Env: sourcePath, _ = os.Getwd() case types.Bundle: - sourcePath = b.cfg.DeployOpts.Source + sourcePath = filepath.Dir(b.cfg.DeployOpts.Source) case types.Config: sourcePath = filepath.Dir(b.cfg.DeployOpts.Config) } diff --git a/src/pkg/bundle/deploy_test.go b/src/pkg/bundle/deploy_test.go index b4052063..7e7b62df 100644 --- a/src/pkg/bundle/deploy_test.go +++ b/src/pkg/bundle/deploy_test.go @@ -1,7 +1,9 @@ package bundle import ( + "fmt" "os" + "path/filepath" "testing" "github.com/defenseunicorns/uds-cli/src/types" @@ -235,7 +237,7 @@ func TestLoadVariablesPrecedence(t *testing.T) { if tc.loadEnvVar { os.Setenv("UDS_FOO", "set using env var") } - actualPkgVars, _ := tc.bundle.loadVariables(tc.pkg, tc.bundleExportVars) + actualPkgVars := tc.bundle.loadVariables(tc.pkg, tc.bundleExportVars) require.Equal(t, tc.expectedPkgVars, actualPkgVars) }) } @@ -429,3 +431,95 @@ func TestHelmOverrideVariablePrecedence(t *testing.T) { }) } } + +func TestFileVariableHandlers(t *testing.T) { + cwd, _ := os.Getwd() + t.Run("addFileValue file not found", func(t *testing.T) { + overrideVar := types.BundleChartVariable{ + Path: "key", + Name: "testVar", + ValueSource: "./", + } + _, err := addFileValue(make([]string, 0), "not-there.txt", overrideVar) + require.Contains(t, err.Error(), "unable to find file") + }) + + t.Run("addFileValue file found", func(t *testing.T) { + valSrcPath := filepath.Join(cwd, "/../../test/bundles/07-helm-overrides/variable-files") + overrideVar := types.BundleChartVariable{ + Path: "key", + Name: "testVar", + ValueSource: valSrcPath, + } + fileVals, err := addFileValue(make([]string, 0), "test.cert", overrideVar) + require.NoError(t, err) + require.Equal(t, fmt.Sprintf("%s=%s/test.cert", overrideVar.Path, valSrcPath), fileVals[0]) + }) + + testCases := []struct { + name string + bundle Bundle + arg types.ValueSources + wanted string + }{ + { + name: "getSourcePath --set", + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{ + Config: "", + Source: "", + }, + }, + }, + arg: types.CLI, + wanted: cwd, + }, + { + name: "getSourcePath UDS_VAR", + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{ + Config: "", + Source: "", + }, + }, + }, + arg: types.CLI, + wanted: cwd, + }, + { + name: "getSourcePath Config", + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{ + Config: "/src/path/to/config/", + Source: "", + }, + }, + }, + arg: types.Config, + wanted: "/src/path/to/config", + }, + { + name: "getSourcePath Bundle", + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{ + Config: "", + Source: "/src/path/to/bundle/", + }, + }, + }, + arg: types.Bundle, + wanted: "/src/path/to/bundle", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + sourcePath := tc.bundle.getSourcePath(tc.arg) + require.Equal(t, tc.wanted, sourcePath) + }) + } +} diff --git a/src/test/bundles/07-helm-overrides/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/uds-bundle.yaml index dd385516..9f980e74 100644 --- a/src/test/bundles/07-helm-overrides/uds-bundle.yaml +++ b/src/test/bundles/07-helm-overrides/uds-bundle.yaml @@ -43,6 +43,7 @@ packages: - name: SECRET_VAL path: "testSecret" description: "testing a secret value" + type: file - name: SECURITY_CTX path: "podinfo.securityContext" description: "testing an object" diff --git a/src/test/bundles/07-helm-overrides/uds-config.yaml b/src/test/bundles/07-helm-overrides/uds-config.yaml index a688023d..5d091f63 100644 --- a/src/test/bundles/07-helm-overrides/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/uds-config.yaml @@ -15,6 +15,7 @@ variables: drop: - SETUID - SETGID + secret_val: ./variable-files/test.cert HOSTS: - host: podinfo.burning.boats paths: diff --git a/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml deleted file mode 100644 index d447b1c8..00000000 --- a/src/test/bundles/07-helm-overrides/variable-files/file-not-found-config.yaml +++ /dev/null @@ -1,6 +0,0 @@ -options: - log_level: debug - -variables: - helm-overrides: - test_secret: not-there.pub diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml deleted file mode 100644 index fbbca82f..00000000 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-bundle.yaml +++ /dev/null @@ -1,23 +0,0 @@ -kind: UDSBundle -metadata: - name: variable-files - description: testing a bundle with Helm overrides - version: 0.0.1 - -packages: - - name: helm-overrides - path: "../../../packages/helm" - ref: 0.0.1 - - overrides: - podinfo-component: - unicorn-podinfo: - variables: - - name: log_level - path: "podinfo.logLevel" - description: "Set the log level for podinfo" - type: file - - name: TEST_SECRET - path: "testSecret" - description: "base64 encoded key to place in the test secret resource" - type: file diff --git a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml b/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml deleted file mode 100644 index 938dec67..00000000 --- a/src/test/bundles/07-helm-overrides/variable-files/uds-config.yaml +++ /dev/null @@ -1,6 +0,0 @@ -options: - log_level: debug - -variables: - helm-overrides: - test_secret: test.cert diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 0a1f7d64..babbf7cd 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -76,11 +76,12 @@ func TestBundleWithHelmOverrides(t *testing.T) { e2e.CreateZarfPkg(t, "src/test/packages/helm", false) bundleDir := "src/test/bundles/07-helm-overrides" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-helm-overrides-%s-0.0.1.tar.zst", e2e.Arch)) - err := os.Setenv("UDS_CONFIG", filepath.Join("src/test/bundles/07-helm-overrides", "uds-config.yaml")) + err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) + os.Setenv("UDS_TEST_FILE", fmt.Sprintf("%s/test-zarf-var-file.txt", bundleDir)) require.NoError(t, err) createLocal(t, bundleDir, e2e.Arch) - deploy(t, bundlePath) + _, stderr := deploy(t, bundlePath) // test values overrides t.Run("check values overrides", func(t *testing.T) { @@ -132,10 +133,10 @@ func TestBundleWithHelmOverrides(t *testing.T) { }) t.Run("check variables overrides, no default and not set in config", func(t *testing.T) { - cmd := strings.Split("zarf tools kubectl get secret test-secret -n podinfo -o jsonpath=\"{.data.test}\"", " ") - secretValue, _, err := e2e.UDS(cmd...) + cmd := strings.Split("zarf tools kubectl get service -n podinfo unicorn-podinfo -o jsonpath='{.spec.type}'", " ") + serviceType, _, err := e2e.UDS(cmd...) // expect the value to be from the underlying chart's values.yaml, no overrides - require.Equal(t, "\"dGVzdC1zZWNyZXQ=\"", secretValue) + require.Equal(t, "'ClusterIP'", serviceType) require.NoError(t, err) }) @@ -155,6 +156,21 @@ func TestBundleWithHelmOverrides(t *testing.T) { require.Contains(t, hosts, "podinfo.unicorns") }) + t.Run("check variables overrides with a file type value", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-secret -o=jsonpath={.data.test}", " ") + stdout, _, err := e2e.UDS(cmd...) + require.NoError(t, err) + decoded, err := base64.StdEncoding.DecodeString(stdout) + require.NoError(t, err) + require.Contains(t, string(decoded), "ssh-rsa") + }) + + t.Run("test domain zarf var set by env variable", func(t *testing.T) { + // checking output of action in the helm-overrides package + // zarf will handle actually parsing and validating the files passed to it + require.Contains(t, stderr, fmt.Sprintf("TEST_FILE set as %s/test-zarf-var-file.txt", bundleDir)) + }) + remove(t, bundlePath) } @@ -374,59 +390,3 @@ func TestExportVarsAsGlobalVars(t *testing.T) { remove(t, bundlePath) } - -func TestVariableFilesFileNotFound(t *testing.T) { - deployZarfInit(t) - e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") - e2e.CreateZarfPkg(t, "src/test/packages/helm", false) - bundleDir := "src/test/bundles/07-helm-overrides/variable-files" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) - os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "file-not-found-config.yaml")) - - createLocal(t, bundleDir, e2e.Arch) - - cmd := strings.Split(fmt.Sprintf("deploy %s --confirm", bundlePath), " ") - _, stderr, _ := e2e.UDS(cmd...) - - require.Contains(t, stderr, fmt.Sprintf("unable to find file %s/not-there.pub", bundleDir)) -} - -func TestVariableFiles(t *testing.T) { - deployZarfInit(t) - e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") - e2e.CreateZarfPkg(t, "src/test/packages/helm", false) - bundleDir := "src/test/bundles/07-helm-overrides/variable-files" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variable-files-%s-0.0.1.tar.zst", e2e.Arch)) - createLocal(t, bundleDir, e2e.Arch) - - os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) - os.Setenv("UDS_TEST_FILE", fmt.Sprintf("%s/test-zarf-var-file.txt", bundleDir)) - - cmd := strings.Split(fmt.Sprintf("deploy %s --confirm --set helm-overrides.log_level=%s/log-level.txt", bundlePath, bundleDir), " ") - _, stderr, err := e2e.UDS(cmd...) - require.NoError(t, err) - - t.Run("test test_secret helm override set by config", func(t *testing.T) { - cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-secret -o=jsonpath={.data.test}", " ") - stdout, _, err := e2e.UDS(cmd...) - require.NoError(t, err) - decoded, err := base64.StdEncoding.DecodeString(stdout) - require.NoError(t, err) - require.Contains(t, string(decoded), "ssh-rsa") - }) - - t.Run("test log-level helm override set by --set", func(t *testing.T) { - cmd := strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath={.spec.template.spec.containers[0].command}", " ") - stdout, _, err := e2e.UDS(cmd...) - require.NoError(t, err) - require.Contains(t, stdout, "--level=debug") - }) - - t.Run("test domain zarf var set by env variable", func(t *testing.T) { - // checking output of action in the helm-overrides package - // zarf will handle actually parsing the files passed to it - require.Contains(t, stderr, fmt.Sprintf("TEST_FILE set as %s/test-zarf-var-file.txt", bundleDir)) - }) - - remove(t, bundlePath) -} diff --git a/src/test/packages/helm/unicorn-podinfo/values.yaml b/src/test/packages/helm/unicorn-podinfo/values.yaml index 776919ee..3ec4f94a 100644 --- a/src/test/packages/helm/unicorn-podinfo/values.yaml +++ b/src/test/packages/helm/unicorn-podinfo/values.yaml @@ -7,3 +7,8 @@ podinfo: # doesn't do anything, just testing more configs ingress: enabled: true + +# for testing when values are not overridden +service: + enabled: true + type: ClusterIP From ee2e768b80f3f8d5d1876466f0b9df28894ed881 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Mon, 3 Jun 2024 12:45:21 -0600 Subject: [PATCH 22/30] update schema and fix test conflicts --- .../values-file/uds-bundle.yaml | 1 + src/test/e2e/variable_test.go | 19 ++++++------------- src/types/bundle.go | 2 +- uds.schema.json | 4 ++++ 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/test/bundles/07-helm-overrides/values-file/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/values-file/uds-bundle.yaml index 46550cfa..edd1a0a5 100644 --- a/src/test/bundles/07-helm-overrides/values-file/uds-bundle.yaml +++ b/src/test/bundles/07-helm-overrides/values-file/uds-bundle.yaml @@ -32,6 +32,7 @@ packages: description: "Set the message for podinfo's UI" - name: SECRET_VAL path: "testSecret" + type: file description: "testing a secret value" - name: SECURITY_CTX path: "podinfo.securityContext" diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index babbf7cd..a6107853 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -262,20 +262,20 @@ func TestBundleWithEnvVarHelmOverrides(t *testing.T) { deployZarfInit(t) e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") e2e.CreateZarfPkg(t, "src/test/packages/helm", false) + + // create and deploy bundle + bundleDir := "src/test/bundles/07-helm-overrides" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-helm-overrides-%s-0.0.1.tar.zst", e2e.Arch)) + color := "purple" - b64Secret := "dGhhdCBhaW50IG15IHRydWNr" - err := os.Setenv("UDS_CONFIG", filepath.Join("src/test/bundles/07-helm-overrides", "uds-config.yaml")) + err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) require.NoError(t, err) err = os.Setenv("UDS_UI_COLOR", color) require.NoError(t, err) err = os.Setenv("UDS_UI_MSG", "im set by an env var") require.NoError(t, err) - err = os.Setenv("UDS_SECRET_VAL", b64Secret) require.NoError(t, err) - // create and deploy bundle - bundleDir := "src/test/bundles/07-helm-overrides" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-helm-overrides-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) deploy(t, bundlePath) @@ -286,13 +286,6 @@ func TestBundleWithEnvVarHelmOverrides(t *testing.T) { require.NoError(t, err) }) - t.Run("check override secret val", func(t *testing.T) { - cmd := strings.Split("z tools kubectl get secret test-secret -n podinfo -o jsonpath=\"{.data.test}\"", " ") - secretValue, _, err := e2e.UDS(cmd...) - require.Equal(t, fmt.Sprintf("\"%s\"", b64Secret), secretValue) - require.NoError(t, err) - }) - t.Run("ensure --set overrides take precedence over env vars", func(t *testing.T) { deployCmd := fmt.Sprintf("deploy %s --set UI_COLOR=orange --set helm-overrides.ui_msg=foo --confirm", bundlePath) _, _, err := e2e.UDS(strings.Split(deployCmd, " ")...) diff --git a/src/types/bundle.go b/src/types/bundle.go index e1d8b523..99552700 100644 --- a/src/types/bundle.go +++ b/src/types/bundle.go @@ -65,7 +65,7 @@ type BundleChartVariable struct { Description string `json:"description,omitempty" jsonschema:"name=Description of the variable"` Default interface{} `json:"default,omitempty" jsonschema:"name=The default value to set"` Type ChartVariableType `json:"type,omitempty" jsonschema:"description=The type of value to be processed,enum=raw,enum=file"` - ValueSource string + ValueSource string `json:"source,omitempty" jsonschema:"description=Where the value is set from"` } // BundleVariableImport represents variables in the bundle diff --git a/uds.schema.json b/uds.schema.json index d335fbcb..96c03734 100644 --- a/uds.schema.json +++ b/uds.schema.json @@ -76,6 +76,10 @@ ], "type": "string", "description": "The type of value to be processed" + }, + "source": { + "type": "string", + "description": "Where the value is set from" } }, "additionalProperties": false, From 3c420ce453a988300851fa96d5464a194a420e87 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Mon, 3 Jun 2024 13:02:28 -0600 Subject: [PATCH 23/30] change ValueSource to Source to remove confusion with type. --- src/pkg/bundle/deploy.go | 16 ++++++++-------- src/types/bundle.go | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index 0c123964..500a45b6 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -362,31 +362,31 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va setVal := strings.Split(k, ".") if setVal[0] == pkgName && strings.ToUpper(setVal[1]) == v.Name { overrideVal = val - v.ValueSource = b.getSourcePath(types.CLI) + v.Source = b.getSourcePath(types.CLI) } } else if strings.ToUpper(k) == v.Name { overrideVal = val - v.ValueSource = b.getSourcePath(types.CLI) + v.Source = b.getSourcePath(types.CLI) } } // check for override in env vars if not in --set if envVarOverride, exists := os.LookupEnv(strings.ToUpper(config.EnvVarPrefix + v.Name)); overrideVal == nil && exists { overrideVal = envVarOverride - v.ValueSource = b.getSourcePath(types.Env) + v.Source = b.getSourcePath(types.Env) } // if not in --set or an env var, use the following precedence: configFile, sharedConfig, default if overrideVal == nil { if configFileOverride, existsInConfig := b.cfg.DeployOpts.Variables[pkgName][v.Name]; existsInConfig { overrideVal = configFileOverride - v.ValueSource = b.getSourcePath(types.Config) + v.Source = b.getSourcePath(types.Config) } else if sharedConfigOverride, existsInSharedConfig := b.cfg.DeployOpts.SharedVariables[v.Name]; existsInSharedConfig { overrideVal = sharedConfigOverride - v.ValueSource = b.getSourcePath(types.Config) + v.Source = b.getSourcePath(types.Config) } else if v.Default != nil { overrideVal = v.Default - v.ValueSource = b.getSourcePath(types.Bundle) + v.Source = b.getSourcePath(types.Bundle) } else { continue } @@ -507,9 +507,9 @@ func setTemplatedVariables(templatedVariables string, pkgVars map[string]string) return replacedValue } -// addFileValue +// addFileValue adds a key=filepath string to helm FileValues func addFileValue(helmFileVals []string, filePath string, override types.BundleChartVariable) ([]string, error) { - verifiedPath, err := formFilePath(override.ValueSource, filePath) + verifiedPath, err := formFilePath(override.Source, filePath) if err != nil { return nil, err } diff --git a/src/types/bundle.go b/src/types/bundle.go index 99552700..a64496b4 100644 --- a/src/types/bundle.go +++ b/src/types/bundle.go @@ -65,7 +65,7 @@ type BundleChartVariable struct { Description string `json:"description,omitempty" jsonschema:"name=Description of the variable"` Default interface{} `json:"default,omitempty" jsonschema:"name=The default value to set"` Type ChartVariableType `json:"type,omitempty" jsonschema:"description=The type of value to be processed,enum=raw,enum=file"` - ValueSource string `json:"source,omitempty" jsonschema:"description=Where the value is set from"` + Source string `json:"source,omitempty" jsonschema:"description=Where the value is set from"` } // BundleVariableImport represents variables in the bundle From 6d2914a157d1bec34dcc47ae4ef93785e51ec585 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Mon, 3 Jun 2024 13:07:34 -0600 Subject: [PATCH 24/30] fix wrong property name in unit test --- src/pkg/bundle/deploy_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/pkg/bundle/deploy_test.go b/src/pkg/bundle/deploy_test.go index 7e7b62df..56e76e7a 100644 --- a/src/pkg/bundle/deploy_test.go +++ b/src/pkg/bundle/deploy_test.go @@ -436,9 +436,9 @@ func TestFileVariableHandlers(t *testing.T) { cwd, _ := os.Getwd() t.Run("addFileValue file not found", func(t *testing.T) { overrideVar := types.BundleChartVariable{ - Path: "key", - Name: "testVar", - ValueSource: "./", + Path: "key", + Name: "testVar", + Source: "./", } _, err := addFileValue(make([]string, 0), "not-there.txt", overrideVar) require.Contains(t, err.Error(), "unable to find file") @@ -447,9 +447,9 @@ func TestFileVariableHandlers(t *testing.T) { t.Run("addFileValue file found", func(t *testing.T) { valSrcPath := filepath.Join(cwd, "/../../test/bundles/07-helm-overrides/variable-files") overrideVar := types.BundleChartVariable{ - Path: "key", - Name: "testVar", - ValueSource: valSrcPath, + Path: "key", + Name: "testVar", + Source: valSrcPath, } fileVals, err := addFileValue(make([]string, 0), "test.cert", overrideVar) require.NoError(t, err) From d72cde950c0bd8decba8a62e3c260661ed24b8b2 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:14:04 -0600 Subject: [PATCH 25/30] revert e2e secret test removals --- .../bundles/07-helm-overrides/uds-bundle.yaml | 5 +++- .../bundles/07-helm-overrides/uds-config.yaml | 2 +- src/test/e2e/variable_test.go | 27 ++++++++++++------- .../templates/file-secret.yaml | 7 +++++ .../packages/helm/unicorn-podinfo/values.yaml | 1 + 5 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 src/test/packages/helm/unicorn-podinfo/templates/file-secret.yaml diff --git a/src/test/bundles/07-helm-overrides/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/uds-bundle.yaml index 9f980e74..3dd87610 100644 --- a/src/test/bundles/07-helm-overrides/uds-bundle.yaml +++ b/src/test/bundles/07-helm-overrides/uds-bundle.yaml @@ -43,7 +43,6 @@ packages: - name: SECRET_VAL path: "testSecret" description: "testing a secret value" - type: file - name: SECURITY_CTX path: "podinfo.securityContext" description: "testing an object" @@ -53,3 +52,7 @@ packages: - name: HOSTS path: "podinfo.ingress.hosts" description: "just testing a a list of objects (doesn't actually do ingress things)" + - name: SECRET_FILE_VAL + path: "testFileSecret" + description: "testing setting secret with file" + type: "file" diff --git a/src/test/bundles/07-helm-overrides/uds-config.yaml b/src/test/bundles/07-helm-overrides/uds-config.yaml index 5d091f63..299679dc 100644 --- a/src/test/bundles/07-helm-overrides/uds-config.yaml +++ b/src/test/bundles/07-helm-overrides/uds-config.yaml @@ -15,7 +15,6 @@ variables: drop: - SETUID - SETGID - secret_val: ./variable-files/test.cert HOSTS: - host: podinfo.burning.boats paths: @@ -25,3 +24,4 @@ variables: paths: - path: "/" pathType: "Prefix" + secret_file_val: "./variable-files/test.cert" diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index a6107853..335a2b5e 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -133,10 +133,10 @@ func TestBundleWithHelmOverrides(t *testing.T) { }) t.Run("check variables overrides, no default and not set in config", func(t *testing.T) { - cmd := strings.Split("zarf tools kubectl get service -n podinfo unicorn-podinfo -o jsonpath='{.spec.type}'", " ") - serviceType, _, err := e2e.UDS(cmd...) + cmd := strings.Split("zarf tools kubectl get secret test-secret -n podinfo -o jsonpath=\"{.data.test}\"", " ") + secretValue, _, err := e2e.UDS(cmd...) // expect the value to be from the underlying chart's values.yaml, no overrides - require.Equal(t, "'ClusterIP'", serviceType) + require.Equal(t, "\"dGVzdC1zZWNyZXQ=\"", secretValue) require.NoError(t, err) }) @@ -157,7 +157,7 @@ func TestBundleWithHelmOverrides(t *testing.T) { }) t.Run("check variables overrides with a file type value", func(t *testing.T) { - cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-secret -o=jsonpath={.data.test}", " ") + cmd := strings.Split("zarf tools kubectl get secret -n podinfo test-file-secret -o=jsonpath={.data.test}", " ") stdout, _, err := e2e.UDS(cmd...) require.NoError(t, err) decoded, err := base64.StdEncoding.DecodeString(stdout) @@ -262,20 +262,20 @@ func TestBundleWithEnvVarHelmOverrides(t *testing.T) { deployZarfInit(t) e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") e2e.CreateZarfPkg(t, "src/test/packages/helm", false) - - // create and deploy bundle - bundleDir := "src/test/bundles/07-helm-overrides" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-helm-overrides-%s-0.0.1.tar.zst", e2e.Arch)) - color := "purple" - err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) + b64Secret := "dGhhdCBhaW50IG15IHRydWNr" + err := os.Setenv("UDS_CONFIG", filepath.Join("src/test/bundles/07-helm-overrides", "uds-config.yaml")) require.NoError(t, err) err = os.Setenv("UDS_UI_COLOR", color) require.NoError(t, err) err = os.Setenv("UDS_UI_MSG", "im set by an env var") require.NoError(t, err) + err = os.Setenv("UDS_SECRET_VAL", b64Secret) require.NoError(t, err) + // create and deploy bundle + bundleDir := "src/test/bundles/07-helm-overrides" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-helm-overrides-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) deploy(t, bundlePath) @@ -286,6 +286,13 @@ func TestBundleWithEnvVarHelmOverrides(t *testing.T) { require.NoError(t, err) }) + t.Run("check override secret val", func(t *testing.T) { + cmd := strings.Split("z tools kubectl get secret test-secret -n podinfo -o jsonpath=\"{.data.test}\"", " ") + secretValue, _, err := e2e.UDS(cmd...) + require.Equal(t, fmt.Sprintf("\"%s\"", b64Secret), secretValue) + require.NoError(t, err) + }) + t.Run("ensure --set overrides take precedence over env vars", func(t *testing.T) { deployCmd := fmt.Sprintf("deploy %s --set UI_COLOR=orange --set helm-overrides.ui_msg=foo --confirm", bundlePath) _, _, err := e2e.UDS(strings.Split(deployCmd, " ")...) diff --git a/src/test/packages/helm/unicorn-podinfo/templates/file-secret.yaml b/src/test/packages/helm/unicorn-podinfo/templates/file-secret.yaml new file mode 100644 index 00000000..03a8951d --- /dev/null +++ b/src/test/packages/helm/unicorn-podinfo/templates/file-secret.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: Secret +metadata: + name: test-file-secret +type: Opaque +data: + test: {{ .Values.testFileSecret }} diff --git a/src/test/packages/helm/unicorn-podinfo/values.yaml b/src/test/packages/helm/unicorn-podinfo/values.yaml index 3ec4f94a..140c8a9f 100644 --- a/src/test/packages/helm/unicorn-podinfo/values.yaml +++ b/src/test/packages/helm/unicorn-podinfo/values.yaml @@ -1,4 +1,5 @@ testSecret: "dGVzdC1zZWNyZXQ=" # test-secret +testFileSecret: "" podinfo: ui: color: "purple" From 33daef155957df6d05fd1fe53f68d9437183297e Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Mon, 3 Jun 2024 19:34:01 -0600 Subject: [PATCH 26/30] clean up tests from previous iterations --- .../07-helm-overrides/values-file/uds-bundle.yaml | 1 - .../07-helm-overrides/variable-files/log-level.txt | 1 - src/test/e2e/variable_test.go | 10 +--------- src/test/packages/helm/unicorn-podinfo/values.yaml | 5 ----- src/test/packages/helm/zarf.yaml | 3 --- 5 files changed, 1 insertion(+), 19 deletions(-) delete mode 100644 src/test/bundles/07-helm-overrides/variable-files/log-level.txt diff --git a/src/test/bundles/07-helm-overrides/values-file/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/values-file/uds-bundle.yaml index edd1a0a5..46550cfa 100644 --- a/src/test/bundles/07-helm-overrides/values-file/uds-bundle.yaml +++ b/src/test/bundles/07-helm-overrides/values-file/uds-bundle.yaml @@ -32,7 +32,6 @@ packages: description: "Set the message for podinfo's UI" - name: SECRET_VAL path: "testSecret" - type: file description: "testing a secret value" - name: SECURITY_CTX path: "podinfo.securityContext" diff --git a/src/test/bundles/07-helm-overrides/variable-files/log-level.txt b/src/test/bundles/07-helm-overrides/variable-files/log-level.txt deleted file mode 100644 index d287cd3e..00000000 --- a/src/test/bundles/07-helm-overrides/variable-files/log-level.txt +++ /dev/null @@ -1 +0,0 @@ -debug diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 335a2b5e..c348e8d2 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -77,11 +77,10 @@ func TestBundleWithHelmOverrides(t *testing.T) { bundleDir := "src/test/bundles/07-helm-overrides" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-helm-overrides-%s-0.0.1.tar.zst", e2e.Arch)) err := os.Setenv("UDS_CONFIG", filepath.Join(bundleDir, "uds-config.yaml")) - os.Setenv("UDS_TEST_FILE", fmt.Sprintf("%s/test-zarf-var-file.txt", bundleDir)) require.NoError(t, err) createLocal(t, bundleDir, e2e.Arch) - _, stderr := deploy(t, bundlePath) + deploy(t, bundlePath) // test values overrides t.Run("check values overrides", func(t *testing.T) { @@ -107,7 +106,6 @@ func TestBundleWithHelmOverrides(t *testing.T) { require.Contains(t, tolerations, "\"key\":\"unicorn\"") require.Contains(t, tolerations, "\"effect\":\"NoSchedule\"") require.NoError(t, err) - }) // test variables overrides @@ -165,12 +163,6 @@ func TestBundleWithHelmOverrides(t *testing.T) { require.Contains(t, string(decoded), "ssh-rsa") }) - t.Run("test domain zarf var set by env variable", func(t *testing.T) { - // checking output of action in the helm-overrides package - // zarf will handle actually parsing and validating the files passed to it - require.Contains(t, stderr, fmt.Sprintf("TEST_FILE set as %s/test-zarf-var-file.txt", bundleDir)) - }) - remove(t, bundlePath) } diff --git a/src/test/packages/helm/unicorn-podinfo/values.yaml b/src/test/packages/helm/unicorn-podinfo/values.yaml index 140c8a9f..0085e8d6 100644 --- a/src/test/packages/helm/unicorn-podinfo/values.yaml +++ b/src/test/packages/helm/unicorn-podinfo/values.yaml @@ -8,8 +8,3 @@ podinfo: # doesn't do anything, just testing more configs ingress: enabled: true - -# for testing when values are not overridden -service: - enabled: true - type: ClusterIP diff --git a/src/test/packages/helm/zarf.yaml b/src/test/packages/helm/zarf.yaml index 8aac84a8..1bd13e2a 100644 --- a/src/test/packages/helm/zarf.yaml +++ b/src/test/packages/helm/zarf.yaml @@ -7,8 +7,6 @@ metadata: variables: - name: DOMAIN default: uds.dev - - name: TEST_FILE - type: file components: - name: podinfo-component @@ -26,4 +24,3 @@ components: after: - cmd: | echo "shared var in helm-overrides pkg: "${ZARF_VAR_DOMAIN}"" - - cmd: echo "TEST_FILE set as ${ZARF_VAR_TEST_FILE}" From 861f15f5a922b7e682b2e50f990c95394f527043 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Mon, 3 Jun 2024 20:30:32 -0600 Subject: [PATCH 27/30] lifted unit tests up to processOverrideVariables --- src/pkg/bundle/deploy_test.go | 178 ++++++++++++++++++++++++---------- 1 file changed, 129 insertions(+), 49 deletions(-) diff --git a/src/pkg/bundle/deploy_test.go b/src/pkg/bundle/deploy_test.go index 56e76e7a..b65a8346 100644 --- a/src/pkg/bundle/deploy_test.go +++ b/src/pkg/bundle/deploy_test.go @@ -3,7 +3,6 @@ package bundle import ( "fmt" "os" - "path/filepath" "testing" "github.com/defenseunicorns/uds-cli/src/types" @@ -433,93 +432,174 @@ func TestHelmOverrideVariablePrecedence(t *testing.T) { } func TestFileVariableHandlers(t *testing.T) { - cwd, _ := os.Getwd() - t.Run("addFileValue file not found", func(t *testing.T) { - overrideVar := types.BundleChartVariable{ - Path: "key", - Name: "testVar", - Source: "./", - } - _, err := addFileValue(make([]string, 0), "not-there.txt", overrideVar) - require.Contains(t, err.Error(), "unable to find file") - }) - - t.Run("addFileValue file found", func(t *testing.T) { - valSrcPath := filepath.Join(cwd, "/../../test/bundles/07-helm-overrides/variable-files") - overrideVar := types.BundleChartVariable{ - Path: "key", - Name: "testVar", - Source: valSrcPath, - } - fileVals, err := addFileValue(make([]string, 0), "test.cert", overrideVar) - require.NoError(t, err) - require.Equal(t, fmt.Sprintf("%s=%s/test.cert", overrideVar.Path, valSrcPath), fileVals[0]) - }) + const ( + componentName = "test-component" + chartName = "test-chart" + pkgName = "test-package" + varName = "cert" + path = "test.Cert" + relativePath = "../../../src/test/bundles/07-helm-overrides/variable-files/" + ) + type args struct { + pkgName string + variables *[]types.BundleChartVariable + componentName string + chartName string + } testCases := []struct { - name string - bundle Bundle - arg types.ValueSources - wanted string + name string + bundle Bundle + args args + loadEnv bool + requireNoErr bool }{ { - name: "getSourcePath --set", + name: "with --set", bundle: Bundle{ cfg: &types.BundleConfig{ DeployOpts: types.BundleDeployOptions{ - Config: "", - Source: "", + SetVariables: map[string]string{ + varName: fmt.Sprintf("%s/test.cert", relativePath), + }, + }, + }, + }, + args: args{ + pkgName: pkgName, + variables: &[]types.BundleChartVariable{ + { + Name: varName, + Path: path, + Type: types.File, + Description: "set the var from cli, so source path is current working directory (eg. /home/user/repos/uds-cli/...)", + }, + }, + componentName: componentName, + chartName: chartName, + }, + requireNoErr: true, + }, + { + name: "with UDS_VAR", + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{}, + }, + }, + args: args{ + pkgName: pkgName, + variables: &[]types.BundleChartVariable{ + { + Name: varName, + Path: path, + Type: types.File, + Description: "set the var from env, so source path is current working directory (eg. /home/user/repos/uds-cli/...)", }, }, + componentName: componentName, + chartName: chartName, }, - arg: types.CLI, - wanted: cwd, + loadEnv: true, + requireNoErr: true, }, { - name: "getSourcePath UDS_VAR", + name: "with Config", bundle: Bundle{ cfg: &types.BundleConfig{ DeployOpts: types.BundleDeployOptions{ - Config: "", - Source: "", + Config: fmt.Sprintf("%s/uds-config.yaml", relativePath), + Variables: map[string]map[string]interface{}{ + pkgName: { + varName: "test.cert", + }, + }, + }, + }, + }, + args: args{ + pkgName: pkgName, + variables: &[]types.BundleChartVariable{ + { + Name: varName, + Path: path, + Type: types.File, + Description: "set the var from config, so source path is config directory", }, }, + componentName: componentName, + chartName: chartName, }, - arg: types.CLI, - wanted: cwd, + requireNoErr: true, }, { - name: "getSourcePath Config", + name: "with Bundle", bundle: Bundle{ cfg: &types.BundleConfig{ DeployOpts: types.BundleDeployOptions{ - Config: "/src/path/to/config/", - Source: "", + + Source: fmt.Sprintf("%s/uds-bundle-helm-overrides-amd64-0.0.1.tar.zst", relativePath), }, }, }, - arg: types.Config, - wanted: "/src/path/to/config", + args: args{ + pkgName: pkgName, + variables: &[]types.BundleChartVariable{ + { + Name: varName, + Path: path, + Type: types.File, + Description: "set the var from bundle default, so source path is bundle directory", + Default: "test.cert", + }, + }, + componentName: componentName, + chartName: chartName, + }, + requireNoErr: true, }, { - name: "getSourcePath Bundle", + name: "file not found", bundle: Bundle{ cfg: &types.BundleConfig{ DeployOpts: types.BundleDeployOptions{ - Config: "", - Source: "/src/path/to/bundle/", + Source: fmt.Sprintf("%s/uds-bundle-helm-overrides-amd64-0.0.1.tar.zst", relativePath), }, }, }, - arg: types.Bundle, - wanted: "/src/path/to/bundle", + args: args{ + pkgName: pkgName, + variables: &[]types.BundleChartVariable{ + { + Name: varName, + Path: path, + Type: types.File, + Description: "set the var from bundle default, so source path is bundle directory", + Default: "not-there-test.cert", + }, + }, + componentName: componentName, + chartName: chartName, + }, + requireNoErr: false, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - sourcePath := tc.bundle.getSourcePath(tc.arg) - require.Equal(t, tc.wanted, sourcePath) + os.Unsetenv("UDS_CERT") + if tc.loadEnv { + os.Setenv("UDS_CERT", fmt.Sprintf("%s/test.cert", relativePath)) + } + + overrideMap := map[string]map[string]*values.Options{} + err := tc.bundle.processOverrideVariables(&overrideMap, tc.args.pkgName, tc.args.variables, tc.args.componentName, tc.args.chartName) + + if tc.requireNoErr { + require.NoError(t, err) + } else { + require.Contains(t, err.Error(), "unable to find") + } }) } } From 85a5ad379590e85c5a5dc733220367ae58f97549 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Tue, 4 Jun 2024 12:06:19 -0600 Subject: [PATCH 28/30] check override map in unit test for correct FileValues --- src/pkg/bundle/deploy_test.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/pkg/bundle/deploy_test.go b/src/pkg/bundle/deploy_test.go index b65a8346..491e4f9d 100644 --- a/src/pkg/bundle/deploy_test.go +++ b/src/pkg/bundle/deploy_test.go @@ -3,6 +3,7 @@ package bundle import ( "fmt" "os" + "path/filepath" "testing" "github.com/defenseunicorns/uds-cli/src/types" @@ -432,11 +433,12 @@ func TestHelmOverrideVariablePrecedence(t *testing.T) { } func TestFileVariableHandlers(t *testing.T) { + cwd, _ := os.Getwd() const ( componentName = "test-component" chartName = "test-chart" pkgName = "test-package" - varName = "cert" + varName = "CERT" path = "test.Cert" relativePath = "../../../src/test/bundles/07-helm-overrides/variable-files/" ) @@ -453,6 +455,7 @@ func TestFileVariableHandlers(t *testing.T) { args args loadEnv bool requireNoErr bool + expected string }{ { name: "with --set", @@ -479,6 +482,7 @@ func TestFileVariableHandlers(t *testing.T) { chartName: chartName, }, requireNoErr: true, + expected: fmt.Sprintf("%s=%s", path, filepath.Join(cwd, fmt.Sprintf("%s/test.cert", relativePath))), }, { name: "with UDS_VAR", @@ -502,6 +506,7 @@ func TestFileVariableHandlers(t *testing.T) { }, loadEnv: true, requireNoErr: true, + expected: fmt.Sprintf("%s=%s", path, filepath.Join(cwd, fmt.Sprintf("%s/test.cert", relativePath))), }, { name: "with Config", @@ -531,6 +536,7 @@ func TestFileVariableHandlers(t *testing.T) { chartName: chartName, }, requireNoErr: true, + expected: fmt.Sprintf("%s=%s", path, fmt.Sprintf("%stest.cert", relativePath)), }, { name: "with Bundle", @@ -557,6 +563,7 @@ func TestFileVariableHandlers(t *testing.T) { chartName: chartName, }, requireNoErr: true, + expected: fmt.Sprintf("%s=%s", path, fmt.Sprintf("%stest.cert", relativePath)), }, { name: "file not found", @@ -582,6 +589,7 @@ func TestFileVariableHandlers(t *testing.T) { chartName: chartName, }, requireNoErr: false, + expected: "", }, } @@ -597,6 +605,7 @@ func TestFileVariableHandlers(t *testing.T) { if tc.requireNoErr { require.NoError(t, err) + require.Contains(t, tc.expected, overrideMap[componentName][chartName].FileValues[0]) } else { require.Contains(t, err.Error(), "unable to find") } From e995e65af12fd137c1bab591a088d3c004be9ded Mon Sep 17 00:00:00 2001 From: Tristan Holaday <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 5 Jun 2024 08:05:30 -0600 Subject: [PATCH 29/30] Update src/pkg/bundle/deploy_test.go Co-authored-by: UncleGedd <42304551+UncleGedd@users.noreply.github.com> --- src/pkg/bundle/deploy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pkg/bundle/deploy_test.go b/src/pkg/bundle/deploy_test.go index 491e4f9d..d203b527 100644 --- a/src/pkg/bundle/deploy_test.go +++ b/src/pkg/bundle/deploy_test.go @@ -605,7 +605,7 @@ func TestFileVariableHandlers(t *testing.T) { if tc.requireNoErr { require.NoError(t, err) - require.Contains(t, tc.expected, overrideMap[componentName][chartName].FileValues[0]) + require.Equals(t, tc.expected, overrideMap[componentName][chartName].FileValues[0]) } else { require.Contains(t, err.Error(), "unable to find") } From 9b457c92e7d0ef9f484f005c4a22fa1fc4f285f1 Mon Sep 17 00:00:00 2001 From: TristanHoladay <40547442+TristanHoladay@users.noreply.github.com> Date: Wed, 5 Jun 2024 08:07:28 -0600 Subject: [PATCH 30/30] switch Equals to Equal --- src/pkg/bundle/deploy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pkg/bundle/deploy_test.go b/src/pkg/bundle/deploy_test.go index d203b527..b7b2a909 100644 --- a/src/pkg/bundle/deploy_test.go +++ b/src/pkg/bundle/deploy_test.go @@ -605,7 +605,7 @@ func TestFileVariableHandlers(t *testing.T) { if tc.requireNoErr { require.NoError(t, err) - require.Equals(t, tc.expected, overrideMap[componentName][chartName].FileValues[0]) + require.Equal(t, tc.expected, overrideMap[componentName][chartName].FileValues[0]) } else { require.Contains(t, err.Error(), "unable to find") }