Skip to content

Commit

Permalink
when the role is 'root', return * perm.
Browse files Browse the repository at this point in the history
add ut for enhancement.

format tune.

revert betesting

define []byte("*") as allKeys, reuse it.
  • Loading branch information
horizonzy committed May 17, 2021
1 parent 9501e8e commit aa4341f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions server/auth/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ var (
authEnabled = []byte{1}
authDisabled = []byte{0}

allKeys = []byte("*")
rootPerm = authpb.Permission{PermType: authpb.READWRITE, Key: allKeys, RangeEnd: []byte{0}}

revisionKey = []byte("authRevision")

authBucketName = []byte("auth")
Expand Down Expand Up @@ -624,6 +627,10 @@ func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUs
}

func (as *authStore) RoleGet(r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, error) {
if rootRole == r.Role {
return &pb.AuthRoleGetResponse{Header: &pb.ResponseHeader{}, Perm: []*authpb.Permission{&rootPerm}}, nil
}

tx := as.be.BatchTx()
tx.Lock()
defer tx.Unlock()
Expand Down
22 changes: 22 additions & 0 deletions server/auth/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,28 @@ func TestUserGrant(t *testing.T) {
}
}

func TestGetRootRole(t *testing.T) {
as, tearDown := setupAuthStore(t)
defer tearDown(t)

perm := &authpb.Permission{
PermType: authpb.READWRITE,
Key: allKeys,
RangeEnd: []byte{0},
}

//get root role
r, err := as.RoleGet(&pb.AuthRoleGetRequest{Role: "root"})
if err != nil {
t.Fatal(err)
}

//check the role is root
if !reflect.DeepEqual(perm, r.Perm[0]) {
t.Errorf("expected %v, got %v", perm, r.Perm[0])
}
}

func TestHasRole(t *testing.T) {
as, tearDown := setupAuthStore(t)
defer tearDown(t)
Expand Down

0 comments on commit aa4341f

Please sign in to comment.