Skip to content

Commit

Permalink
fix: The argocd server api-content-type flag does not allow empty con…
Browse files Browse the repository at this point in the history
…tent-type header (#17331)

Signed-off-by: Alexander Matyushentsev <[email protected]>
  • Loading branch information
alexmt authored Feb 28, 2024
1 parent 7aac4ba commit 7901a10
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/argocd-server/commands/argocd_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 11 additions & 2 deletions util/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions util/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"k8s.io/utils/pointer"
)

func TestParseNumFromEnv(t *testing.T) {
Expand Down Expand Up @@ -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)
})
}
Expand Down

0 comments on commit 7901a10

Please sign in to comment.