Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
feat: add secret name conversion (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbaehler committed Jan 24, 2024
1 parent ed77294 commit 908d191
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
4 changes: 0 additions & 4 deletions argocd-cmp/cmp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ spec:
args:
- render
- "."
- --secret-name
- "${ARGOCD_APP_NAME}"
- --secret-namespace
- "${ARGOCD_APP_NAMESPACE}"
- --env-regex
- "^ARGOCD_ENV_.*$"
- --must-decrypt
Expand Down
38 changes: 24 additions & 14 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 == "" {
Expand Down
17 changes: 17 additions & 0 deletions pkg/config/utils.go
Original file line number Diff line number Diff line change
@@ -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]
}
2 changes: 2 additions & 0 deletions subst/cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down

0 comments on commit 908d191

Please sign in to comment.