From 4290f85dc9067b6b3201fd2d86751d23e8fb6434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20B=C3=A4hler?= Date: Wed, 24 Jan 2024 15:38:19 +0100 Subject: [PATCH] feat: add secret name conversion --- argocd-cmp/cmp.yaml | 4 ---- pkg/config/config.go | 38 ++++++++++++++++++++++++-------------- pkg/config/utils.go | 17 +++++++++++++++++ subst/cmd/render.go | 2 ++ 4 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 pkg/config/utils.go diff --git a/argocd-cmp/cmp.yaml b/argocd-cmp/cmp.yaml index 45557be..2732cc0 100644 --- a/argocd-cmp/cmp.yaml +++ b/argocd-cmp/cmp.yaml @@ -15,10 +15,6 @@ spec: args: - render - "." - - --secret-name - - "${ARGOCD_APP_NAME}" - - --secret-namespace - - "${ARGOCD_APP_NAMESPACE}" - --env-regex - "^ARGOCD_ENV_.*$" - --must-decrypt diff --git a/pkg/config/config.go b/pkg/config/config.go index c86eb24..627e462 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -15,18 +15,19 @@ import ( ) type Configuration struct { - EnvRegex string `mapstructure:"env-regex"` - RootDirectory string `mapstructure:"root-dir"` - FileRegex string `mapstructure:"file-regex"` - SecretName string `mapstructure:"secret-name"` - SecretNamespace string `mapstructure:"secret-namespace"` - EjsonKey []string `mapstructure:"ejson-key"` - SkipDecrypt bool `mapstructure:"skip-decrypt"` - MustDecrypt bool `mapstructure:"must-decrypt"` - KubectlTimeout time.Duration `mapstructure:"kubectl-timeout"` - Kubeconfig string `mapstructure:"kubeconfig"` - KubeAPI string `mapstructure:"kube-api"` - Output string `mapstructure:"output"` + EnvRegex string `mapstructure:"env-regex"` + RootDirectory string `mapstructure:"root-dir"` + FileRegex string `mapstructure:"file-regex"` + SecretName string `mapstructure:"secret-name"` + SecretNamespace string `mapstructure:"secret-namespace"` + EjsonKey []string `mapstructure:"ejson-key"` + SkipDecrypt bool `mapstructure:"skip-decrypt"` + MustDecrypt bool `mapstructure:"must-decrypt"` + KubectlTimeout time.Duration `mapstructure:"kubectl-timeout"` + Kubeconfig string `mapstructure:"kubeconfig"` + KubeAPI string `mapstructure:"kube-api"` + Output string `mapstructure:"output"` + ConvertSecretname bool `mapstructure:"convert-secret-name"` } var ( @@ -82,8 +83,17 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command, directory string) (*C } if cfg.SecretName != "" { - regex := regexp.MustCompile(`[^a-zA-Z0-9]+`) - cfg.SecretName = regex.ReplaceAllString(cfg.SecretName, "-") + if cfg.ConvertSecretname { + cfg.SecretName = getValueAfterUnderscore(cfg.SecretName) + + } else { + regex := regexp.MustCompile(`[^a-zA-Z0-9]+`) + cfg.SecretName = regex.ReplaceAllString(cfg.SecretName, "-") + } + } + + if cfg.SecretNamespace == "" { + cfg.SecretNamespace = os.Getenv("ARGOCD_APP_NAMESPACE") } if cfg.SecretName != "" && cfg.SecretNamespace == "" { diff --git a/pkg/config/utils.go b/pkg/config/utils.go new file mode 100644 index 0000000..1453898 --- /dev/null +++ b/pkg/config/utils.go @@ -0,0 +1,17 @@ +package config + +import ( + "regexp" +) + +func getValueAfterUnderscore(input string) string { + re, _ := regexp.Compile("_(.+)") + + matches := re.FindStringSubmatch(input) + if len(matches) < 2 { + // No match found or the part after underscore is missing + return input + } + + return matches[1] +} diff --git a/subst/cmd/render.go b/subst/cmd/render.go index 15f1f5c..4e25a95 100644 --- a/subst/cmd/render.go +++ b/subst/cmd/render.go @@ -38,6 +38,8 @@ func addRenderFlags(flags *flag.FlagSet) { if flags.Lookup("kube-api") == nil { flags.String("kube-api", "", "Kubernetes API Url") } + flags.Bool("convert-secret-name", true, heredoc.Doc(` + Assuming the secret name is derived from ARGOCD_APP_NAME, this option will only use the application name (without project-name_)`)) flags.String("secret-name", "", heredoc.Doc(` Specify Secret name (each key within the secret will be used as a decryption key)`)) flags.String("secret-namespace", "", heredoc.Doc(`