Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Add auth action validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ypt committed Dec 21, 2020
1 parent a2c5cda commit 7969554
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 5 additions & 7 deletions pulsar/resource_pulsar_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,8 @@ func resourcePulsarNamespace() *schema.Resource {
Required: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
// TODO: validation
// ValidateFunc: validation.StringInSlice(pulsar.ValidAuthActions, false),
Type: schema.TypeString,
ValidateFunc: validateAuthAction,
},
},
},
Expand Down Expand Up @@ -381,9 +380,9 @@ func resourcePulsarNamespaceRead(d *schema.ResourceData, meta interface{}) error
}

permissionGrants := []interface{}{}
for role := range grants {
for role, roleActions := range grants {
actions := []string{}
for _, action := range grants[role] {
for _, action := range roleActions {
actions = append(actions, action.String())
}
sort.Strings(actions)
Expand Down Expand Up @@ -495,7 +494,6 @@ func resourcePulsarNamespaceUpdate(d *schema.ResourceData, meta interface{}) err
errs = multierror.Append(errs, fmt.Errorf("unmarshalPermissionGrants: %w", err))
} else {
for _, grant := range permissionGrants {
// TODO: revisit interaction w/ error cases above
if err = client.GrantNamespacePermission(*nsName, grant.Role, grant.Actions); err != nil {
errs = multierror.Append(errs, fmt.Errorf("GrantNamespacePermission: %w", err))
}
Expand Down Expand Up @@ -753,7 +751,7 @@ func unmarshalPermissionGrants(v []interface{}) ([]*types.PermissionGrant, error
for _, action := range data["actions"].(*schema.Set).List() {
authAction, err := common.ParseAuthAction(action.(string))
if err != nil {
return permissionGrants, fmt.Errorf("ERROR_INVALID_AUTH_ACTION: %w", err)
return nil, fmt.Errorf("ERROR_INVALID_AUTH_ACTION: %w", err)
}
actions = append(actions, authAction)
}
Expand Down
10 changes: 10 additions & 0 deletions pulsar/validate_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"strings"

"github.com/streamnative/pulsarctl/pkg/pulsar/common"
"github.com/streamnative/pulsarctl/pkg/pulsar/utils"
)

Expand Down Expand Up @@ -41,3 +42,12 @@ func validateTopicType(val interface{}, key string) (warns []string, errs []erro
}
return
}

func validateAuthAction(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
_, err := common.ParseAuthAction(v)
if err != nil {
errs = append(errs, fmt.Errorf("%q must be a valid auth action (got: %s): %w", key, v, err))
}
return
}

0 comments on commit 7969554

Please sign in to comment.