Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: uds config validation #618

Merged
merged 18 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/cmd/uds.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/defenseunicorns/uds-cli/src/config"
"github.com/defenseunicorns/uds-cli/src/config/lang"
"github.com/defenseunicorns/uds-cli/src/pkg/bundle"
"github.com/defenseunicorns/uds-cli/src/types"
"github.com/defenseunicorns/zarf/src/pkg/message"
goyaml "github.com/goccy/go-yaml"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -193,9 +194,7 @@ func loadViperConfig() error {
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.UnmarshalWithOptions(configFile, &bundleCfg.DeployOpts, goyaml.Strict())
err = UnmarshalAndValidateConfig(configFile, &bundleCfg)
decleaver marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down Expand Up @@ -287,3 +286,19 @@ func chooseBundle(args []string) string {

return path
}

func UnmarshalAndValidateConfig(configFile []byte, bundleCfg *types.BundleConfig) error {
// 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.UnmarshalWithOptions(configFile, &bundleCfg.DeployOpts, goyaml.Strict())
if err != nil {
return err
}
// validate config options
for optionName := range bundleCfg.DeployOpts.Options {
if !types.IsValidConfigOption(optionName) {
decleaver marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("invalid config option: %s", optionName)
}
}
return nil
}
61 changes: 61 additions & 0 deletions src/cmd/uds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors

// Package cmd contains the CLI commands for UDS.
package cmd

import (
"testing"

"github.com/defenseunicorns/uds-cli/src/types"
"github.com/stretchr/testify/require"
)

func TestUnmarshalAndValidateConfig(t *testing.T) {
type args struct {
configFile []byte
bundleCfg *types.BundleConfig
}
tests := []struct {
name string
args args
wantErr bool
errContains string
}{
{
name: "Invalid option key",
args: args{
configFile: []byte(`
options:
log_levelx: debug
`),
bundleCfg: &types.BundleConfig{},
},
wantErr: true,
errContains: "invalid config option: log_levelx",
},
{
name: "Option typo",
args: args{
configFile: []byte(`
optionx:
log_level: debug
`),
bundleCfg: &types.BundleConfig{},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := UnmarshalAndValidateConfig(tt.args.configFile, tt.args.bundleCfg)
if tt.wantErr {
require.NotNil(t, err, "Expected error")
require.Contains(t, err.Error(), tt.errContains, "Error message should contain the expected string")
} else {
require.Nil(t, err, "Expected no error")
}
})
}
}
7 changes: 4 additions & 3 deletions src/test/bundles/07-helm-overrides/uds-config-invalid.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
options:
log_level: debug
log_levelx: debug

# helm-overrides is an invalid top level key. Missing `variables` parent key.
decleaver marked this conversation as resolved.
Show resolved Hide resolved
helm-overrides:
ui_color: "orange"
variables:
helm-overrides:
ui_color: "orange"
3 changes: 1 addition & 2 deletions src/test/e2e/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,13 @@ func TestBundleTmpDir(t *testing.T) {

func TestInvalidConfig(t *testing.T) {
os.Setenv("UDS_CONFIG", filepath.Join("src/test/bundles/07-helm-overrides", "uds-config-invalid.yaml"))
deployZarfInit(t)
zarfPkgPath := "src/test/packages/helm"
e2e.HelmDepUpdate(t, fmt.Sprintf("%s/unicorn-podinfo", zarfPkgPath))
e2e.CreateZarfPkg(t, zarfPkgPath, false)
bundleDir := "src/test/bundles/07-helm-overrides"
createLocal(t, bundleDir, e2e.Arch)
bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-helm-overrides-%s-0.0.1.tar.zst", e2e.Arch))
_, stderr := deployWithError(t, bundlePath)
require.Contains(t, stderr, "unknown field")
require.Contains(t, stderr, "invalid config option: log_levelx")
os.Unsetenv("UDS_CONFIG")
}
22 changes: 22 additions & 0 deletions src/types/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ type BundleDeployOptions struct {
Options map[string]interface{} `yaml:"options,omitempty"`
}

type ConfigOption string

const (
Confirm ConfigOption = "confirm"
Insecure ConfigOption = "insecure"
CachePath ConfigOption = "uds_cache"
TempDirectory ConfigOption = "tmp_dir"
LogLevel ConfigOption = "log_level"
Architecture ConfigOption = "architecture"
NoLogFile ConfigOption = "no_log_file"
NoProgress ConfigOption = "no_progress"
)

func IsValidConfigOption(str string) bool {
switch ConfigOption(str) {
case Confirm, Insecure, CachePath, TempDirectory, LogLevel, Architecture, NoLogFile, NoProgress:
return true
default:
return false
}
}

// BundleInspectOptions is the options for the bundler.Inspect() function
type BundleInspectOptions struct {
PublicKeyPath string
Expand Down