Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
NOISSUE - Domains status check on Create & List of Things & Groups (#202
Browse files Browse the repository at this point in the history
)

* check domain status on create and list things and groups

Signed-off-by: Arvindh <[email protected]>

* rename internal function name

Signed-off-by: Arvindh <[email protected]>

* fix things test

Signed-off-by: Arvindh <[email protected]>

---------

Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 authored Dec 25, 2023
1 parent 5590695 commit c2f125f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
20 changes: 14 additions & 6 deletions internal/groups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (svc service) CreateGroup(ctx context.Context, token, kind string, g groups
if err != nil {
return groups.Group{}, err
}
// If domain is disabled , then this authorization will fail for all non-admin domain users
if _, err := svc.authorizeKind(ctx, auth.UserType, auth.UsersKind, res.GetId(), auth.MembershipPermission, auth.DomainType, res.GetDomainId()); err != nil {

Check failure on line 50 in internal/groups/service.go

View workflow job for this annotation

GitHub Actions / Build and Push

not enough arguments in call to svc.authorizeKind

Check failure on line 50 in internal/groups/service.go

View workflow job for this annotation

GitHub Actions / Build and Push

not enough arguments in call to svc.authorizeKind
return groups.Group{}, err
}
groupID, err := svc.idProvider.ID()
if err != nil {
return groups.Group{}, err
Expand All @@ -58,7 +62,7 @@ func (svc service) CreateGroup(ctx context.Context, token, kind string, g groups
g.CreatedAt = time.Now()
g.Owner = res.GetDomainId()
if g.Parent != "" {
_, err := svc.authorize(ctx, auth.UserType, token, auth.EditPermission, auth.GroupType, g.Parent)
_, err := svc.authorizeToken(ctx, auth.UserType, token, auth.EditPermission, auth.GroupType, g.Parent)
if err != nil {
return groups.Group{}, errors.Wrap(errParentUnAuthz, err)
}
Expand Down Expand Up @@ -107,7 +111,7 @@ func (svc service) CreateGroup(ctx context.Context, token, kind string, g groups
}

func (svc service) ViewGroup(ctx context.Context, token, id string) (groups.Group, error) {
_, err := svc.authorize(ctx, auth.UserType, token, auth.ViewPermission, auth.GroupType, id)
_, err := svc.authorizeToken(ctx, auth.UserType, token, auth.ViewPermission, auth.GroupType, id)
if err != nil {
return groups.Group{}, err
}
Expand Down Expand Up @@ -219,6 +223,10 @@ func (svc service) ListGroups(ctx context.Context, token, memberKind, memberID s
}
gm.PageMeta.OwnerID = res.GetDomainId()
default:
// If domain is disabled , then this authorization will fail for all non-admin domain users
if _, err := svc.authorizeKind(ctx, auth.UserType, auth.UsersKind, res.GetId(), auth.MembershipPermission, auth.DomainType, res.GetDomainId()); err != nil {

Check failure on line 227 in internal/groups/service.go

View workflow job for this annotation

GitHub Actions / Build and Push

not enough arguments in call to svc.authorizeKind

Check failure on line 227 in internal/groups/service.go

View workflow job for this annotation

GitHub Actions / Build and Push

not enough arguments in call to svc.authorizeKind
return groups.Page{}, err
}
ids, err = svc.listAllGroupsOfUserID(ctx, res.GetId(), gm.Permission)
if err != nil {
return groups.Page{}, err
Expand Down Expand Up @@ -294,7 +302,7 @@ func (svc service) checkSuperAdmin(ctx context.Context, userID string) error {

// IMPROVEMENT NOTE: remove this function and all its related auxiliary function, ListMembers are moved to respective service.
func (svc service) ListMembers(ctx context.Context, token, groupID, permission, memberKind string) (groups.MembersPage, error) {
_, err := svc.authorize(ctx, auth.UserType, token, auth.ViewPermission, auth.GroupType, groupID)
_, err := svc.authorizeToken(ctx, auth.UserType, token, auth.ViewPermission, auth.GroupType, groupID)
if err != nil {
return groups.MembersPage{}, err
}
Expand Down Expand Up @@ -355,7 +363,7 @@ func (svc service) ListMembers(ctx context.Context, token, groupID, permission,
}

func (svc service) UpdateGroup(ctx context.Context, token string, g groups.Group) (groups.Group, error) {
id, err := svc.authorize(ctx, auth.UserType, token, auth.EditPermission, auth.GroupType, g.ID)
id, err := svc.authorizeToken(ctx, auth.UserType, token, auth.EditPermission, auth.GroupType, g.ID)
if err != nil {
return groups.Group{}, err
}
Expand Down Expand Up @@ -685,7 +693,7 @@ func (svc service) listAllGroupsOfUserID(ctx context.Context, userID, permission
}

func (svc service) changeGroupStatus(ctx context.Context, token string, group groups.Group) (groups.Group, error) {
id, err := svc.authorize(ctx, auth.UserType, token, auth.EditPermission, auth.GroupType, group.ID)
id, err := svc.authorizeToken(ctx, auth.UserType, token, auth.EditPermission, auth.GroupType, group.ID)
if err != nil {
return groups.Group{}, err
}
Expand All @@ -712,7 +720,7 @@ func (svc service) identify(ctx context.Context, token string) (*magistrala.Iden
return res, nil
}

func (svc service) authorize(ctx context.Context, subjectType, subject, permission, objectType, object string) (string, error) {
func (svc service) authorizeToken(ctx context.Context, subjectType, subject, permission, objectType, object string) (string, error) {
req := &magistrala.AuthorizeReq{
SubjectType: subjectType,
SubjectKind: auth.TokenKind,
Expand Down
9 changes: 9 additions & 0 deletions things/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (svc service) CreateThings(ctx context.Context, token string, cls ...mgclie
if err != nil {
return []mgclients.Client{}, err
}
// If domain is disabled , then this authorization will fail for all non-admin domain users
if _, err := svc.authorize(ctx, auth.UserType, auth.UsersKind, user.GetId(), auth.MembershipPermission, auth.DomainType, user.GetDomainId()); err != nil {
return []mgclients.Client{}, err
}

var clients []mgclients.Client
for _, c := range cls {
if c.ID == "" {
Expand Down Expand Up @@ -185,6 +190,10 @@ func (svc service) ListClients(ctx context.Context, token, reqUserID string, pm
}
pm.Owner = res.GetDomainId()
default:
// If domain is disabled , then this authorization will fail for all non-admin domain users
if _, err := svc.authorize(ctx, auth.UserType, auth.UsersKind, res.GetId(), auth.MembershipPermission, auth.DomainType, res.GetDomainId()); err != nil {
return mgclients.ClientsPage{}, err
}
ids, err = svc.listClientIDs(ctx, res.GetId(), pm.Permission)
if err != nil {
return mgclients.ClientsPage{}, errors.Wrap(repoerr.ErrNotFound, err)
Expand Down
4 changes: 3 additions & 1 deletion things/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ func TestRegisterClient(t *testing.T) {
for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID, DomainId: testsutil.GenerateUUID(t)}, nil)
repoCall1 := auth.On("AddPolicies", mock.Anything, mock.Anything).Return(&magistrala.AddPoliciesRes{Authorized: true}, nil)
repoCall2 := cRepo.On("Save", context.Background(), mock.Anything).Return([]mgclients.Client{tc.client}, tc.err)
repoCall2 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall3 := cRepo.On("Save", context.Background(), mock.Anything).Return([]mgclients.Client{tc.client}, tc.err)
expected, err := svc.CreateThings(context.Background(), tc.token, tc.client)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
if err == nil {
Expand All @@ -256,6 +257,7 @@ func TestRegisterClient(t *testing.T) {
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
repoCall3.Unset()
}
}

Expand Down

0 comments on commit c2f125f

Please sign in to comment.