Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GROW-2831): correct handling for resourceTags in policy exceptions #1605

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions cli/cmd/policy_exceptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func promptAddExceptionConstraints(policy api.Policy) ([]api.PolicyExceptionCons
answer, err := promptAskConstraintsQuestion(q)

if err != nil {
return []api.PolicyExceptionConstraint{}, nil
return []api.PolicyExceptionConstraint{}, err
}

if answer != nil {
Expand Down Expand Up @@ -361,10 +361,15 @@ func promptAskConstraintsQuestion(constraintQuestion PolicyExceptionSurveyQuesti
}
case "KVTagPair":
if constraintQuestion.constraint.MultiValue {
//prompt kv tag list
answer, err = promptAddExceptionConstraintMapList(
constraintQuestion.constraint.FieldKey, constraintQuestion.questions,
)
// workaround for the unique shape of resourceTags data, see GROW-2831
if constraintQuestion.constraint.FieldKey == "resourceTags" {
answer, err = promptAddExceptionConstraintResourceTags(constraintQuestion.constraint.FieldKey)
} else {
//prompt kv tag list
answer, err = promptAddExceptionConstraintMapList(
constraintQuestion.constraint.FieldKey, constraintQuestion.questions,
)
}
} else {
//prompt kv tag
mapAnswers, err := promptAddExceptionConstraintMap(constraintQuestion.questions)
Expand Down Expand Up @@ -548,11 +553,44 @@ func promptAddExceptionConstraintMap(mapQuestions []*survey.Question) (constrain
return mapAnswer, nil
}

type constraintMapAnswer struct {
// resourceTags requires special handling, see GROW-2831
type resourceTagsConstraintAnswer struct {
Key string `json:"key"`
Value string `json:"value"`
}

// resourceTags requires special handling, see GROW-2831
func promptAddExceptionConstraintResourceTags(key string) (*api.PolicyExceptionConstraint, error) {
questions := []*survey.Question{{
Name: "key",
Prompt: &survey.Input{Message: "tag name:"},
Validate: survey.Required,
}, {
Name: "value",
Prompt: &survey.Input{Message: "values:", Help: "Supply a comma seperated list for multiple"},
Validate: survey.Required,
}}

var answer resourceTagsConstraintAnswer
if err := survey.Ask(questions, &answer); err != nil {
return nil, err
}

finalAnswer := strings.Split(answer.Value, ",")
return &api.PolicyExceptionConstraint{
FieldKey: key,
FieldValues: []any{
map[string]interface{}{"key": answer.Key, "value": finalAnswer},
},
}, nil

}

type constraintMapAnswer struct {
Key string `json:"key"`
Value []string `json:"value"`
}

func promptAddExceptionConstraintMapList(
key string, mapQuestions []*survey.Question,
) (*api.PolicyExceptionConstraint, error) {
Expand Down
Loading