diff --git a/cmd/argocd-server/commands/argocd_server.go b/cmd/argocd-server/commands/argocd_server.go index 412c0c3141507..a5f5003aa9e8b 100644 --- a/cmd/argocd-server/commands/argocd_server.go +++ b/cmd/argocd-server/commands/argocd_server.go @@ -232,7 +232,7 @@ func NewCommand() *cobra.Command { command.Flags().StringVar(&repoServerAddress, "repo-server", env.StringFromEnv("ARGOCD_SERVER_REPO_SERVER", common.DefaultRepoServerAddr), "Repo server address") command.Flags().StringVar(&dexServerAddress, "dex-server", env.StringFromEnv("ARGOCD_SERVER_DEX_SERVER", common.DefaultDexServerAddr), "Dex server address") command.Flags().BoolVar(&disableAuth, "disable-auth", env.ParseBoolFromEnv("ARGOCD_SERVER_DISABLE_AUTH", false), "Disable client authentication") - command.Flags().StringVar(&contentTypes, "api-content-types", env.StringFromEnv("ARGOCD_API_CONTENT_TYPES", "application/json"), "Semicolon separated list of allowed content types for non GET api requests. Any content type is allowed if empty.") + command.Flags().StringVar(&contentTypes, "api-content-types", env.StringFromEnv("ARGOCD_API_CONTENT_TYPES", "application/json", env.StringFromEnvOpts{AllowEmpty: true}), "Semicolon separated list of allowed content types for non GET api requests. Any content type is allowed if empty.") command.Flags().BoolVar(&enableGZip, "enable-gzip", env.ParseBoolFromEnv("ARGOCD_SERVER_ENABLE_GZIP", true), "Enable GZIP compression") command.AddCommand(cli.NewVersionCmd(cliName)) command.Flags().StringVar(&listenHost, "address", env.StringFromEnv("ARGOCD_SERVER_LISTEN_ADDRESS", common.DefaultAddressAPIServer), "Listen on given address") diff --git a/util/env/env.go b/util/env/env.go index 1b49a0c322065..6808f59d2da75 100644 --- a/util/env/env.go +++ b/util/env/env.go @@ -124,8 +124,17 @@ func ParseDurationFromEnv(env string, defaultValue, min, max time.Duration) time return dur } -func StringFromEnv(env string, defaultValue string) string { - if str := os.Getenv(env); str != "" { +type StringFromEnvOpts struct { + // AllowEmpty allows the value to be empty as long as the environment variable is set. + AllowEmpty bool +} + +func StringFromEnv(env string, defaultValue string, opts ...StringFromEnvOpts) string { + opt := StringFromEnvOpts{} + for _, o := range opts { + opt.AllowEmpty = opt.AllowEmpty || o.AllowEmpty + } + if str, ok := os.LookupEnv(env); opt.AllowEmpty && ok || str != "" { return str } return defaultValue diff --git a/util/env/env_test.go b/util/env/env_test.go index 691d235805b23..18405af65a34a 100644 --- a/util/env/env_test.go +++ b/util/env/env_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "k8s.io/utils/pointer" ) func TestParseNumFromEnv(t *testing.T) { @@ -167,19 +168,25 @@ func TestStringFromEnv(t *testing.T) { testCases := []struct { name string - env string + env *string expected string def string + opts []StringFromEnvOpts }{ - {"Some string", "true", "true", def}, - {"Empty string with default", "", def, def}, - {"Empty string without default", "", "", ""}, + {"Some string", pointer.String("true"), "true", def, nil}, + {"Empty string with default", pointer.String(""), def, def, nil}, + {"Empty string without default", pointer.String(""), "", "", nil}, + {"No env variable with default allow empty", nil, "default", "default", []StringFromEnvOpts{{AllowEmpty: true}}}, + {"Some variable with default allow empty", pointer.String("true"), "true", "default", []StringFromEnvOpts{{AllowEmpty: true}}}, + {"Empty variable with default allow empty", pointer.String(""), "", "default", []StringFromEnvOpts{{AllowEmpty: true}}}, } for _, tt := range testCases { t.Run(tt.name, func(t *testing.T) { - t.Setenv(envKey, tt.env) - b := StringFromEnv(envKey, tt.def) + if tt.env != nil { + t.Setenv(envKey, *tt.env) + } + b := StringFromEnv(envKey, tt.def, tt.opts...) assert.Equal(t, tt.expected, b) }) }