diff --git a/config/environment.go b/config/environment.go index 74cfe92..67bc9cc 100644 --- a/config/environment.go +++ b/config/environment.go @@ -41,6 +41,10 @@ var ( NewLabels string // NewAnnotations string NewAnnotations string + // DisabledLabel string + DisabledLabel string + // MiddleName string + MiddleName string // Debug bool Debug bool ) diff --git a/main.go b/main.go index c1a1bac..e01eae1 100644 --- a/main.go +++ b/main.go @@ -222,6 +222,8 @@ func initCommands() { scanSecretsValuesCmd.Flags().StringVar(&config.MatchKey, "matchKey", os.Getenv("MATCH_KEY"), "Key inside Secret to be exported to Secret Receiver") scanSecretsValuesCmd.Flags().StringVar(&config.NewLabels, "newLabels", os.Getenv("NEW_LABELS"), "New Labels to be exported to Secret Receiver") scanSecretsValuesCmd.Flags().StringVar(&config.NewAnnotations, "newAnnotations", os.Getenv("NEW_ANNOTATIONS"), "New Annotations to be exported to Secret Receiver") + scanSecretsValuesCmd.Flags().StringVar(&config.DisabledLabel, "disabledLabel", os.Getenv("DISABLED_LABEL"), "Label to not export to Secret Receiver") + scanSecretsValuesCmd.Flags().StringVar(&config.MiddleName, "middleName", os.Getenv("MIDDLE_NAME"), "Add middle name in secret data name before sending to Secret Receiver") } func main() { diff --git a/usecase/secret_usecase.go b/usecase/secret_usecase.go index 75463ea..ae480f4 100644 --- a/usecase/secret_usecase.go +++ b/usecase/secret_usecase.go @@ -244,6 +244,12 @@ func ScanSubvalueSecret(labels string) (string, error) { var countErrors int var countErrorsNames []string for _, item := range res.Items { + if config.DisabledLabel != "" { + if searchLabels(config.DisabledLabel, item.Labels) { + fmt.Printf("Skiping secret %s \n", item.Name) + continue + } + } data := make(map[string]string) var suffixName, key, subkey string if strings.Contains(config.MatchKey, ".") { @@ -256,12 +262,15 @@ func ScanSubvalueSecret(labels string) (string, error) { suffixName = string(v) } if k == key { + // fmt.Println(k) temp := make(map[string]string) err := yaml.Unmarshal(v, &temp) if err != nil { fmt.Println("fail in Unmarshal") countErrors++ } + // fmt.Println(temp) + // if subkey is not empty if subkey != "" { data[subkey] = temp[subkey] } @@ -288,9 +297,14 @@ func ScanSubvalueSecret(labels string) (string, error) { } name := fmt.Sprintf("%s-%s-%s", item.Name, subkey, suffixName) localName := fmt.Sprintf("%s-%s", subkey, suffixName) + if config.MiddleName != "" { + localName = fmt.Sprintf("%s-%s-%s", subkey, config.MiddleName, suffixName) + } + fmt.Println(localName) localData := map[string]string{ localName: data[subkey], } + fmt.Println(localData) newSecret := rewriteSecret(name, destination, localData, labels, annotations) err := ManageSecret(name, newSecret) if err != nil { @@ -303,3 +317,29 @@ func ScanSubvalueSecret(labels string) (string, error) { } return "OK", nil } + +func searchLabels(label string, labels map[string]string) bool { + var key, value string + if strings.Contains(label, "=") { + splited := strings.Split(label, "=") + key = splited[0] + value = splited[1] + } else { + key = label + } + if len(labels) == 0 { + return false + } + for k, v := range labels { + if value != "" && k == key && v == value { + return true + } + if value == "" { + if k == key { + return true + } + } + + } + return false +}