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

Switch policies in AppRole to TypeCommaStringSlice #3163

Merged
merged 2 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions builtin/credential/approle/path_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func rolePaths(b *backend) []*framework.Path {
addresses which can perform the login operation`,
},
"policies": &framework.FieldSchema{
Type: framework.TypeString,
Type: framework.TypeCommaStringSlice,
Default: "default",
Description: "Comma separated list of policies on the role.",
},
Expand Down Expand Up @@ -172,7 +172,7 @@ TTL will be set to the value of this parameter.`,
Description: "Name of the role.",
},
"policies": &framework.FieldSchema{
Type: framework.TypeString,
Type: framework.TypeCommaStringSlice,
Default: "default",
Description: "Comma separated list of policies on the role.",
},
Expand Down Expand Up @@ -768,9 +768,9 @@ func (b *backend) pathRoleCreateUpdate(req *logical.Request, data *framework.Fie
}

if policiesRaw, ok := data.GetOk("policies"); ok {
role.Policies = policyutil.ParsePolicies(policiesRaw.(string))
role.Policies = policyutil.ParsePolicies(policiesRaw)
} else if req.Operation == logical.CreateOperation {
role.Policies = policyutil.ParsePolicies(data.Get("policies").(string))
role.Policies = policyutil.ParsePolicies(data.Get("policies"))
}

periodRaw, ok := data.GetOk("period")
Expand Down Expand Up @@ -1306,8 +1306,8 @@ func (b *backend) pathRolePoliciesUpdate(req *logical.Request, data *framework.F
return nil, nil
}

policies := strings.TrimSpace(data.Get("policies").(string))
if policies == "" {
policiesRaw, ok := data.GetOk("policies")
if !ok {
return logical.ErrorResponse("missing policies"), nil
}

Expand All @@ -1316,7 +1316,7 @@ func (b *backend) pathRolePoliciesUpdate(req *logical.Request, data *framework.F
lock.Lock()
defer lock.Unlock()

role.Policies = policyutil.ParsePolicies(policies)
role.Policies = policyutil.ParsePolicies(policiesRaw)

return nil, b.setRoleEntry(req.Storage, roleName, role, "")
}
Expand Down Expand Up @@ -1359,7 +1359,7 @@ func (b *backend) pathRolePoliciesDelete(req *logical.Request, data *framework.F
lock.Lock()
defer lock.Unlock()

role.Policies = policyutil.ParsePolicies(data.GetDefaultOrZero("policies").(string))
role.Policies = policyutil.ParsePolicies(data.GetDefaultOrZero("policies"))

return nil, b.setRoleEntry(req.Storage, roleName, role, "")
}
Expand Down
15 changes: 12 additions & 3 deletions helper/policyutil/policyutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ const (
// all other policies will be ignored, the result will contain
// just the 'root'. In cases where 'root' is not present, if
// 'default' policy is not already present, it will be added.
func ParsePolicies(policiesRaw string) []string {
if policiesRaw == "" {
func ParsePolicies(policiesRaw interface{}) []string {
if policiesRaw == nil {
return []string{"default"}
}

policies := strings.Split(policiesRaw, ",")
var policies []string
switch policiesRaw.(type) {
case string:
if policiesRaw.(string) == "" {
return []string{"default"}
}
policies = strings.Split(policiesRaw, ",")
case []string:
policies = policiesRaw.([]string)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an empty string is supplied as policies, do we want to add the default policy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't do it here and I'm afraid things will break (especially, in the token store). We already have defaults in approle's schema.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

}

return SanitizePolicies(policies, true)
}
Expand Down