Skip to content

Commit

Permalink
Fix: SuperAdmin domain policy subjectID
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 committed Jan 24, 2024
1 parent 79ef28a commit 6bcc23f
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ import (
const recoveryDuration = 5 * time.Minute

var (
errRollbackPolicy = errors.New("failed to rollback policy")
errRemoveLocalPolicy = errors.New("failed to remove from local policy copy")
errRemovePolicyEngine = errors.New("failed to remove from policy engine")
)

var (
// ErrFailedToRetrieveMembers failed to retrieve group members.
ErrFailedToRetrieveMembers = errors.New("failed to retrieve group members")

Expand All @@ -48,6 +43,10 @@ var (
errCreateDomainPolicy = errors.New("failed to create domain policy")
errAddPolicies = errors.New("failed to add policies")
errRemovePolicies = errors.New("failed to remove the policies")
errRollbackPolicy = errors.New("failed to rollback policy")
errRemoveLocalPolicy = errors.New("failed to remove from local policy copy")
errRemovePolicyEngine = errors.New("failed to remove from policy engine")
errCheckSuperAdmin = errors.New("failed to check user is super admin")
)

// Authn specifies an API that must be fullfiled by the domain service
Expand Down Expand Up @@ -548,12 +547,35 @@ func (svc service) CreateDomain(ctx context.Context, token string, d Domain) (do

d.CreatedAt = time.Now()

if err := svc.createDomainPolicy(ctx, key.User, domainID, AdministratorRelation); err != nil {
var userSubjectID string
err = svc.Authorize(ctx, PolicyReq{
Subject: key.User,
SubjectType: UserType,
Permission: AdminPermission,
Object: MagistralaObject,
ObjectType: PlatformType,
})
// If user is non-SuperAdmin then subject ID should be domainID_UserID.
// If user is SuperAdmin then subject ID is same as UserID.
// Because SuperAdmin should have access to all domain even SuperAdmin is not a member of domain.
// So for this reason, only userID is used in policy to inherit the access to all domains and domain entities.
switch {
case err == nil:
userSubjectID = key.User
case errors.Contains(errors.ErrDomainAuthorization, err),
errors.Contains(errors.ErrAuthorization, err),
errors.Contains(svcerr.ErrAuthorization, err):
userSubjectID = EncodeDomainUserID(domainID, key.User)
default:
return Domain{}, errors.Wrap(errCheckSuperAdmin, err)
}

if err := svc.createDomainPolicy(ctx, domainID, key.User, userSubjectID, AdministratorRelation); err != nil {
return Domain{}, errors.Wrap(errCreateDomainPolicy, err)
}
defer func() {
if err != nil {
if errRollBack := svc.createDomainPolicyRollback(ctx, key.User, domainID, AdministratorRelation); errRollBack != nil {
if errRollBack := svc.createDomainPolicyRollback(ctx, domainID, key.User, userSubjectID, AdministratorRelation); errRollBack != nil {
err = errors.Wrap(err, errors.Wrap(errRollbackPolicy, errRollBack))
}
}
Expand Down Expand Up @@ -818,10 +840,10 @@ func (svc service) addDomainPolicies(ctx context.Context, domainID, relation str
return nil
}

func (svc service) createDomainPolicy(ctx context.Context, userID, domainID, relation string) (err error) {
func (svc service) createDomainPolicy(ctx context.Context, domainID, userID, userSubjectID, relation string) (err error) {
prs := []PolicyReq{
{
Subject: EncodeDomainUserID(domainID, userID),
Subject: userSubjectID,
SubjectType: UserType,
SubjectKind: UsersKind,
Relation: relation,
Expand Down Expand Up @@ -859,11 +881,11 @@ func (svc service) createDomainPolicy(ctx context.Context, userID, domainID, rel
return err
}

func (svc service) createDomainPolicyRollback(ctx context.Context, userID, domainID, relation string) error {
func (svc service) createDomainPolicyRollback(ctx context.Context, domainID, userID, userSubjectID, relation string) error {
var err error
prs := []PolicyReq{
{
Subject: EncodeDomainUserID(domainID, userID),
Subject: userSubjectID,
SubjectType: UserType,
SubjectKind: UsersKind,
Relation: relation,
Expand Down

0 comments on commit 6bcc23f

Please sign in to comment.