From ccaf0aeca61128b1de9d923e4354b50bd2e495cf Mon Sep 17 00:00:00 2001 From: Jimmi Dyson Date: Mon, 5 Jun 2023 12:02:06 +0100 Subject: [PATCH] fix: Unset REGISTRY_ prefixed env vars The Docker registry that is embedded in mindthegap reads `REGISTRY_` prefixed environment variables, which could lead to unexpected behaviour dependin on the environment. This commit unsets these environment variables to ensure consistent behaviour independent of environment. Fixes D2IQ-97438. --- .golangci.yml | 10 +++++----- cmd/mindthegap/root/root.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 9c702c06..a798c5ec 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -42,11 +42,11 @@ linters: linters-settings: depguard: - list-type: blacklist - packages: - - k8s.io/kubernetes - packages-with-error-messages: - k8s.io/kubernetes: "do not use k8s.io/kubernetes directly" + rules: + main: + deny: + - pkg: k8s.io/kubernetes + desc: "do not use k8s.io/kubernetes directly" errcheck: exclude-functions: - encoding/json.Marshal diff --git a/cmd/mindthegap/root/root.go b/cmd/mindthegap/root/root.go index cc49de5e..bbc8cdd0 100644 --- a/cmd/mindthegap/root/root.go +++ b/cmd/mindthegap/root/root.go @@ -6,6 +6,7 @@ package root import ( "io" "os" + "strings" "github.com/spf13/cobra" @@ -21,6 +22,24 @@ import ( func NewCommand(in io.Reader, out, errOut io.Writer) (*cobra.Command, output.Output) { rootCmd, rootOpts := root.NewCommand(out, errOut) + originalPreRun := rootCmd.PersistentPreRunE + rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + if err := originalPreRun(cmd, args); err != nil { + return err + } + + for _, env := range os.Environ() { + envKey, _, _ := strings.Cut(env, "=") + if strings.HasPrefix(envKey, "REGISTRY_") { + if err := os.Unsetenv(envKey); err != nil { + return err + } + } + } + + return nil + } + rootCmd.AddCommand(create.NewCommand(rootOpts.Output)) rootCmd.AddCommand(push.NewCommand(rootOpts.Output)) rootCmd.AddCommand(serve.NewCommand(rootOpts.Output))