Skip to content

Commit

Permalink
add new flags
Browse files Browse the repository at this point in the history
  • Loading branch information
betorvs committed Mar 19, 2021
1 parent 0dc98ee commit 682a6d5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ var (
NewLabels string
// NewAnnotations string
NewAnnotations string
// DisabledLabel string
DisabledLabel string
// MiddleName string
MiddleName string
// Debug bool
Debug bool
)
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
40 changes: 40 additions & 0 deletions usecase/secret_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ".") {
Expand All @@ -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]
}
Expand All @@ -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 {
Expand All @@ -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
}

0 comments on commit 682a6d5

Please sign in to comment.