Skip to content

Commit

Permalink
authz: support empty principals and fix rbac authenticated matcher (#…
Browse files Browse the repository at this point in the history
…4883)

* authz: support empty principals in SDK and fixes to rbac authenticated
matcher.

* Minor formatting

* Remove pointer from principals fields

* resolving comments
  • Loading branch information
ashithasantosh authored Oct 21, 2021
1 parent f00baa6 commit 4f21cde
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 31 deletions.
20 changes: 13 additions & 7 deletions authz/rbac_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,20 @@ func parsePrincipalNames(principalNames []string) []*v3rbacpb.Principal {
}

func parsePeer(source peer) (*v3rbacpb.Principal, error) {
if len(source.Principals) > 0 {
return principalOr(parsePrincipalNames(source.Principals)), nil
if source.Principals == nil {
return &v3rbacpb.Principal{
Identifier: &v3rbacpb.Principal_Any{
Any: true,
},
}, nil
}
return &v3rbacpb.Principal{
Identifier: &v3rbacpb.Principal_Any{
Any: true,
},
}, nil
if len(source.Principals) == 0 {
return &v3rbacpb.Principal{
Identifier: &v3rbacpb.Principal_Authenticated_{
Authenticated: &v3rbacpb.Principal_Authenticated{},
}}, nil
}
return principalOr(parsePrincipalNames(source.Principals)), nil
}

func parsePaths(paths []string) []*v3rbacpb.Permission {
Expand Down
26 changes: 26 additions & 0 deletions authz/rbac_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,32 @@ func TestTranslatePolicy(t *testing.T) {
},
},
},
"empty principal field": {
authzPolicy: `{
"name": "authz",
"allow_rules": [{
"name": "allow_authenticated",
"source": {"principals":[]}
}]
}`,
wantPolicies: []*v3rbacpb.RBAC{
{
Action: v3rbacpb.RBAC_ALLOW,
Policies: map[string]*v3rbacpb.Policy{
"authz_allow_authenticated": {
Principals: []*v3rbacpb.Principal{
{Identifier: &v3rbacpb.Principal_Authenticated_{
Authenticated: &v3rbacpb.Principal_Authenticated{},
}},
},
Permissions: []*v3rbacpb.Permission{
{Rule: &v3rbacpb.Permission_Any{Any: true}},
},
},
},
},
},
},
"unknown field": {
authzPolicy: `{"random": 123}`,
wantErr: "failed to unmarshal policy",
Expand Down
Loading

0 comments on commit 4f21cde

Please sign in to comment.