From 671802970350fec4bdc1b812856216369705efb9 Mon Sep 17 00:00:00 2001 From: Alexandre Gaudreault Date: Tue, 20 Aug 2024 17:28:36 -0400 Subject: [PATCH 1/3] feat(dex): set logger based on argo config Signed-off-by: Alexandre Gaudreault --- util/dex/config.go | 37 ++++++++++++++++++ util/dex/dex_test.go | 91 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 127 insertions(+), 1 deletion(-) diff --git a/util/dex/config.go b/util/dex/config.go index 3c68b11a55e9d..1b2f0fd02dfae 100644 --- a/util/dex/config.go +++ b/util/dex/config.go @@ -2,11 +2,14 @@ package dex import ( "fmt" + "os" "sigs.k8s.io/yaml" "github.com/argoproj/argo-cd/v2/common" "github.com/argoproj/argo-cd/v2/util/settings" + + log "github.com/sirupsen/logrus" ) func GenerateDexConfigYAML(argocdSettings *settings.ArgoCDSettings, disableTls bool) ([]byte, error) { @@ -38,6 +41,20 @@ func GenerateDexConfigYAML(argocdSettings *settings.ArgoCDSettings, disableTls b } } + if loggerCfg, found := dexCfg["logger"].(map[string]interface{}); found { + if _, found := loggerCfg["level"]; !found { + loggerCfg["level"] = slogLevelFromLogrus(os.Getenv(common.EnvLogLevel)) + } + if _, found := loggerCfg["format"]; !found { + loggerCfg["format"] = os.Getenv(common.EnvLogFormat) + } + } else { + dexCfg["logger"] = map[string]interface{}{ + "level": slogLevelFromLogrus(os.Getenv(common.EnvLogLevel)), + "format": os.Getenv(common.EnvLogFormat), + } + } + dexCfg["grpc"] = map[string]interface{}{ "addr": "0.0.0.0:5557", } @@ -130,3 +147,23 @@ func needsRedirectURI(connectorType string) bool { } return false } + +func slogLevelFromLogrus(level string) string { + logrusLevel, err := log.ParseLevel(level) + if err != nil { + return level + } + + switch logrusLevel { + case log.DebugLevel, log.TraceLevel: + return "DEBUG" + case log.InfoLevel: + return "INFO" + case log.WarnLevel: + return "WARN" + case log.ErrorLevel, log.FatalLevel, log.PanicLevel: + return "ERROR" + } + // return the logrus level and let slog parse it + return level +} diff --git a/util/dex/dex_test.go b/util/dex/dex_test.go index 8d54dc2a6120e..63d78c61a97e8 100644 --- a/util/dex/dex_test.go +++ b/util/dex/dex_test.go @@ -13,8 +13,11 @@ import ( "github.com/stretchr/testify/require" "sigs.k8s.io/yaml" - // "github.com/argoproj/argo-cd/common" + "github.com/argoproj/argo-cd/v2/common" "github.com/argoproj/argo-cd/v2/util/settings" + + utillog "github.com/argoproj/argo-cd/v2/util/log" + log "github.com/sirupsen/logrus" ) const invalidURL = ":://localhost/foo/bar" @@ -142,6 +145,33 @@ connectors: nameAttr: cn ` +var goodDexConfigWithLogger = ` +logger: + level: debug + other: value +connectors: +# GitHub example +- type: github + id: github + name: GitHub + config: + clientID: aabbccddeeff00112233 + clientSecret: $dex.github.clientSecret + orgs: + - name: your-github-org + +# GitHub enterprise example +- type: github + id: acme-github + name: Acme GitHub + config: + hostName: github.acme.example.com + clientID: abcdefghijklmnopqrst + clientSecret: $dex.acme.clientSecret + orgs: + - name: your-github-org +` + var goodSecrets = map[string]string{ "dex.github.clientSecret": "foobar", "dex.acme.clientSecret": "barfoo", @@ -272,6 +302,65 @@ func Test_GenerateDexConfig(t *testing.T) { } }) + t.Run("Logging level", func(t *testing.T) { + s := settings.ArgoCDSettings{ + URL: "http://localhost", + DexConfig: goodDexConfig, + } + t.Setenv(common.EnvLogLevel, log.WarnLevel.String()) + t.Setenv(common.EnvLogFormat, utillog.JsonFormat) + + config, err := GenerateDexConfigYAML(&s, false) + require.NoError(t, err) + assert.NotNil(t, config) + var dexCfg map[string]interface{} + err = yaml.Unmarshal(config, &dexCfg) + if err != nil { + panic(err.Error()) + } + loggerCfg, ok := dexCfg["logger"].(map[string]interface{}) + assert.True(t, ok) + + level, ok := loggerCfg["level"].(string) + assert.True(t, ok) + assert.Equal(t, "WARN", level) + + format, ok := loggerCfg["format"].(string) + assert.True(t, ok) + assert.Equal(t, "json", format) + }) + + t.Run("Logging level with config", func(t *testing.T) { + s := settings.ArgoCDSettings{ + URL: "http://localhost", + DexConfig: goodDexConfigWithLogger, + } + t.Setenv(common.EnvLogLevel, log.WarnLevel.String()) + t.Setenv(common.EnvLogFormat, utillog.JsonFormat) + + config, err := GenerateDexConfigYAML(&s, false) + require.NoError(t, err) + assert.NotNil(t, config) + var dexCfg map[string]interface{} + err = yaml.Unmarshal(config, &dexCfg) + if err != nil { + panic(err.Error()) + } + loggerCfg, ok := dexCfg["logger"].(map[string]interface{}) + assert.True(t, ok) + + level, ok := loggerCfg["level"].(string) + assert.True(t, ok) + assert.Equal(t, "debug", level) + + format, ok := loggerCfg["format"].(string) + assert.True(t, ok) + assert.Equal(t, "json", format) + + _, ok = loggerCfg["other"].(string) + assert.True(t, ok) + }) + t.Run("Redirect config", func(t *testing.T) { types := []string{"oidc", "saml", "microsoft", "linkedin", "gitlab", "github", "bitbucket-cloud", "openshift", "gitea", "google", "oauth"} for _, c := range types { From 12635dba591983299d759faa5563e355bad00ab0 Mon Sep 17 00:00:00 2001 From: Alexandre Gaudreault Date: Tue, 20 Aug 2024 18:12:09 -0400 Subject: [PATCH 2/3] add env config Signed-off-by: Alexandre Gaudreault --- cmd/argocd-dex/commands/argocd_dex.go | 8 ++++---- docs/operator-manual/argocd-cmd-params-cm.yaml | 5 +++++ manifests/base/dex/argocd-dex-server-deployment.yaml | 12 ++++++++++++ manifests/ha/install.yaml | 12 ++++++++++++ manifests/ha/namespace-install.yaml | 12 ++++++++++++ manifests/install.yaml | 12 ++++++++++++ manifests/namespace-install.yaml | 12 ++++++++++++ 7 files changed, 69 insertions(+), 4 deletions(-) diff --git a/cmd/argocd-dex/commands/argocd_dex.go b/cmd/argocd-dex/commands/argocd_dex.go index 55b628ba96dc1..43efbbb050dd5 100644 --- a/cmd/argocd-dex/commands/argocd_dex.go +++ b/cmd/argocd-dex/commands/argocd_dex.go @@ -136,8 +136,8 @@ func NewRunDexCommand() *cobra.Command { } clientConfig = cli.AddKubectlFlagsToCmd(&command) - command.Flags().StringVar(&cmdutil.LogFormat, "logformat", "text", "Set the logging format. One of: text|json") - command.Flags().StringVar(&cmdutil.LogLevel, "loglevel", "info", "Set the logging level. One of: debug|info|warn|error") + command.Flags().StringVar(&cmdutil.LogFormat, "logformat", env.StringFromEnv("ARGOCD_DEX_SERVER_LOGFORMAT", "text"), "Set the logging format. One of: text|json") + command.Flags().StringVar(&cmdutil.LogLevel, "loglevel", env.StringFromEnv("ARGOCD_DEX_SERVER_LOGLEVEL", "info"), "Set the logging level. One of: debug|info|warn|error") command.Flags().BoolVar(&disableTLS, "disable-tls", env.ParseBoolFromEnv("ARGOCD_DEX_SERVER_DISABLE_TLS", false), "Disable TLS on the HTTP endpoint") return &command } @@ -204,8 +204,8 @@ func NewGenDexConfigCommand() *cobra.Command { } clientConfig = cli.AddKubectlFlagsToCmd(&command) - command.Flags().StringVar(&cmdutil.LogFormat, "logformat", "text", "Set the logging format. One of: text|json") - command.Flags().StringVar(&cmdutil.LogLevel, "loglevel", "info", "Set the logging level. One of: debug|info|warn|error") + command.Flags().StringVar(&cmdutil.LogFormat, "logformat", env.StringFromEnv("ARGOCD_DEX_SERVER_LOGFORMAT", "text"), "Set the logging format. One of: text|json") + command.Flags().StringVar(&cmdutil.LogLevel, "loglevel", env.StringFromEnv("ARGOCD_DEX_SERVER_LOGLEVEL", "info"), "Set the logging level. One of: debug|info|warn|error") command.Flags().StringVarP(&out, "out", "o", "", "Output to the specified file instead of stdout") command.Flags().BoolVar(&disableTLS, "disable-tls", env.ParseBoolFromEnv("ARGOCD_DEX_SERVER_DISABLE_TLS", false), "Disable TLS on the HTTP endpoint") return &command diff --git a/docs/operator-manual/argocd-cmd-params-cm.yaml b/docs/operator-manual/argocd-cmd-params-cm.yaml index 80fc27b9ad7f2..8b5e18a5fa6ef 100644 --- a/docs/operator-manual/argocd-cmd-params-cm.yaml +++ b/docs/operator-manual/argocd-cmd-params-cm.yaml @@ -188,6 +188,11 @@ data: # Include hidden directories from Git reposerver.include.hidden.directories: "false" + + # Set the logging format. One of: text|json (default "text") + dexserver.log.format: "text" + # Set the logging level. One of: debug|info|warn|error (default "info") + dexserver.log.level: "info" # Disable TLS on the HTTP endpoint dexserver.disable.tls: "false" diff --git a/manifests/base/dex/argocd-dex-server-deployment.yaml b/manifests/base/dex/argocd-dex-server-deployment.yaml index 42d2b705c9fbc..f2d77c6ac1f6a 100644 --- a/manifests/base/dex/argocd-dex-server-deployment.yaml +++ b/manifests/base/dex/argocd-dex-server-deployment.yaml @@ -41,6 +41,18 @@ spec: imagePullPolicy: Always command: [/shared/argocd-dex, rundex] env: + - name: ARGOCD_DEX_SERVER_LOGFORMAT + valueFrom: + configMapKeyRef: + key: dexserver.log.format + name: argocd-cmd-params-cm + optional: true + - name: ARGOCD_DEX_SERVER_LOGLEVEL + valueFrom: + configMapKeyRef: + key: dexserver.log.level + name: argocd-cmd-params-cm + optional: true - name: ARGOCD_DEX_SERVER_DISABLE_TLS valueFrom: configMapKeyRef: diff --git a/manifests/ha/install.yaml b/manifests/ha/install.yaml index df2d55bc516c3..952c50ce433de 100644 --- a/manifests/ha/install.yaml +++ b/manifests/ha/install.yaml @@ -23948,6 +23948,18 @@ spec: - /shared/argocd-dex - rundex env: + - name: ARGOCD_DEX_SERVER_LOGFORMAT + valueFrom: + configMapKeyRef: + key: dexserver.log.format + name: argocd-cmd-params-cm + optional: true + - name: ARGOCD_DEX_SERVER_LOGLEVEL + valueFrom: + configMapKeyRef: + key: dexserver.log.level + name: argocd-cmd-params-cm + optional: true - name: ARGOCD_DEX_SERVER_DISABLE_TLS valueFrom: configMapKeyRef: diff --git a/manifests/ha/namespace-install.yaml b/manifests/ha/namespace-install.yaml index 1376ffd4fea5f..c125e96c4a29e 100644 --- a/manifests/ha/namespace-install.yaml +++ b/manifests/ha/namespace-install.yaml @@ -1782,6 +1782,18 @@ spec: - /shared/argocd-dex - rundex env: + - name: ARGOCD_DEX_SERVER_LOGFORMAT + valueFrom: + configMapKeyRef: + key: dexserver.log.format + name: argocd-cmd-params-cm + optional: true + - name: ARGOCD_DEX_SERVER_LOGLEVEL + valueFrom: + configMapKeyRef: + key: dexserver.log.level + name: argocd-cmd-params-cm + optional: true - name: ARGOCD_DEX_SERVER_DISABLE_TLS valueFrom: configMapKeyRef: diff --git a/manifests/install.yaml b/manifests/install.yaml index 969731f003336..8f3b3d2721886 100644 --- a/manifests/install.yaml +++ b/manifests/install.yaml @@ -23065,6 +23065,18 @@ spec: - /shared/argocd-dex - rundex env: + - name: ARGOCD_DEX_SERVER_LOGFORMAT + valueFrom: + configMapKeyRef: + key: dexserver.log.format + name: argocd-cmd-params-cm + optional: true + - name: ARGOCD_DEX_SERVER_LOGLEVEL + valueFrom: + configMapKeyRef: + key: dexserver.log.level + name: argocd-cmd-params-cm + optional: true - name: ARGOCD_DEX_SERVER_DISABLE_TLS valueFrom: configMapKeyRef: diff --git a/manifests/namespace-install.yaml b/manifests/namespace-install.yaml index 9bd3e25384635..eaf86aa33ad48 100644 --- a/manifests/namespace-install.yaml +++ b/manifests/namespace-install.yaml @@ -899,6 +899,18 @@ spec: - /shared/argocd-dex - rundex env: + - name: ARGOCD_DEX_SERVER_LOGFORMAT + valueFrom: + configMapKeyRef: + key: dexserver.log.format + name: argocd-cmd-params-cm + optional: true + - name: ARGOCD_DEX_SERVER_LOGLEVEL + valueFrom: + configMapKeyRef: + key: dexserver.log.level + name: argocd-cmd-params-cm + optional: true - name: ARGOCD_DEX_SERVER_DISABLE_TLS valueFrom: configMapKeyRef: From b2e716a209046b8cad505af0b55f4970696f5e3c Mon Sep 17 00:00:00 2001 From: Alexandre Gaudreault Date: Tue, 20 Aug 2024 18:50:11 -0400 Subject: [PATCH 3/3] organize import bug? Signed-off-by: Alexandre Gaudreault --- util/dex/dex_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/util/dex/dex_test.go b/util/dex/dex_test.go index 63d78c61a97e8..c4413b626eea6 100644 --- a/util/dex/dex_test.go +++ b/util/dex/dex_test.go @@ -9,15 +9,14 @@ import ( "strings" "testing" + log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "sigs.k8s.io/yaml" "github.com/argoproj/argo-cd/v2/common" - "github.com/argoproj/argo-cd/v2/util/settings" - utillog "github.com/argoproj/argo-cd/v2/util/log" - log "github.com/sirupsen/logrus" + "github.com/argoproj/argo-cd/v2/util/settings" ) const invalidURL = ":://localhost/foo/bar"