From 872411c05679f6979c02dc7d27ede2b79857bafd Mon Sep 17 00:00:00 2001 From: Nghia Tran Date: Tue, 8 Feb 2022 10:32:22 -0800 Subject: [PATCH 1/2] Validate KO_CONFIG_PATH to avoid ignored options --- pkg/commands/options/build.go | 15 ++++++- pkg/commands/options/build_test.go | 40 +++++++++++++++++++ .../testdata/bad-config/.ko.yaml/.gitignore | 3 ++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 pkg/commands/options/testdata/bad-config/.ko.yaml/.gitignore diff --git a/pkg/commands/options/build.go b/pkg/commands/options/build.go index 0bfd69b69e..44f0373b25 100644 --- a/pkg/commands/options/build.go +++ b/pkg/commands/options/build.go @@ -91,14 +91,27 @@ func (bo *BuildOptions) LoadConfig() error { } // If omitted, use this base image. v.SetDefault("defaultBaseImage", configDefaultBaseImage) - v.SetConfigName(".ko") // .yaml is implicit + const configName = ".ko" + + v.SetConfigName(configName) // .yaml is implicit v.SetEnvPrefix("KO") v.AutomaticEnv() if override := os.Getenv("KO_CONFIG_PATH"); override != "" { + path := filepath.Join(override, configName+".yaml") + file, err := os.Stat(path) + if err != nil { + return fmt.Errorf("error looking for config file: %w", err) + } + if !file.Mode().IsRegular() { + return fmt.Errorf("config file %s is not a regular file", path) + } v.AddConfigPath(override) } + if bo.WorkingDirectory == "" { + bo.WorkingDirectory = "." + } v.AddConfigPath(bo.WorkingDirectory) if err := v.ReadInConfig(); err != nil { diff --git a/pkg/commands/options/build_test.go b/pkg/commands/options/build_test.go index 145fd8599f..3d30350041 100644 --- a/pkg/commands/options/build_test.go +++ b/pkg/commands/options/build_test.go @@ -15,6 +15,8 @@ package options import ( + "os" + "strings" "testing" "github.com/google/ko/pkg/build" @@ -97,3 +99,41 @@ func TestAddBuildOptionsSetsDefaultsForNonFlagOptions(t *testing.T) { t.Error("expected Trimpath=true") } } + +func TestOverrideConfigPath(t *testing.T) { + const envName = "KO_CONFIG_PATH" + bo := &BuildOptions{} + for _, tc := range []struct { + name string + koConfigPath string + err string + }{{ + name: ".ko.yaml does not exist", + koConfigPath: "testdata", + err: "testdata/.ko.yaml: no such file or directory", + }, { + name: ".ko.yaml is dir", + koConfigPath: "testdata/bad-config", + err: "testdata/bad-config/.ko.yaml is not a regular file", + }, { + name: "good", + koConfigPath: "testdata/config", + }} { + t.Run(tc.name, func(t *testing.T) { + oldEnv := os.Getenv(envName) + defer os.Setenv(envName, oldEnv) + + os.Setenv(envName, tc.koConfigPath) + err := bo.LoadConfig() + if err == nil { + if tc.err == "" { + return + } + t.Fatalf("expected error %q, saw nil", tc.err) + } + if !strings.Contains(err.Error(), tc.err) { + t.Errorf("expected error to contain %q, saw: %v", tc.err, err) + } + }) + } +} diff --git a/pkg/commands/options/testdata/bad-config/.ko.yaml/.gitignore b/pkg/commands/options/testdata/bad-config/.ko.yaml/.gitignore new file mode 100644 index 0000000000..a2458976c2 --- /dev/null +++ b/pkg/commands/options/testdata/bad-config/.ko.yaml/.gitignore @@ -0,0 +1,3 @@ +.# We just want to have a practically empty directory. +* +!.gitignore From 09f79f9eb507ddd03c8d6953ab23d3f99e6c2ebb Mon Sep 17 00:00:00 2001 From: Nghia Tran Date: Tue, 8 Feb 2022 11:02:38 -0800 Subject: [PATCH 2/2] Remove dup --- pkg/commands/options/build.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/commands/options/build.go b/pkg/commands/options/build.go index 44f0373b25..4f2fec976b 100644 --- a/pkg/commands/options/build.go +++ b/pkg/commands/options/build.go @@ -108,10 +108,6 @@ func (bo *BuildOptions) LoadConfig() error { } v.AddConfigPath(override) } - - if bo.WorkingDirectory == "" { - bo.WorkingDirectory = "." - } v.AddConfigPath(bo.WorkingDirectory) if err := v.ReadInConfig(); err != nil {