AuthN: Introduce DefaultOrgID function for managed service accounts (#93432)

* Managed Service Accounts: Use AutoAssignOrgID

* Fix the IsExternalServiceAccount function

* Reassign service account role

* Account for AutoAssignOrg

* Update pkg/services/serviceaccounts/models.go

* Simplify IsExternalServiceAccount function

* Add tests

* Easier to understand test

* Revert small change
This commit is contained in:
Gabriel MABILLE
2024-09-20 14:43:29 +02:00
committed by GitHub
parent bb69afed7c
commit 8d84517103
12 changed files with 192 additions and 122 deletions
@@ -240,6 +240,15 @@ func (*AccessControlStore) saveUserAssignment(ctx context.Context, sess *db.Sess
return errGetAssigns
}
// Revoke assignment if it's assigned to another user or service account
if len(assignments) > 0 && assignments[0].UserID != assignment.UserID {
if _, errDel := sess.Where("role_id = ?", assignment.RoleID).Delete(&accesscontrol.UserRole{}); errDel != nil {
return errDel
}
assignments = nil
}
// If no assignment exists, insert a new one.
if len(assignments) == 0 {
if _, errInsert := sess.Insert(&assignment); errInsert != nil {
return errInsert
@@ -247,11 +256,6 @@ func (*AccessControlStore) saveUserAssignment(ctx context.Context, sess *db.Sess
return nil
}
// Ensure the role was assigned only to this service account
if len(assignments) > 1 || assignments[0].UserID != assignment.UserID {
return errors.New("external service role assigned to another user or service account")
}
// Ensure the assignment is in the correct organization
_, errUpdate := sess.Where("role_id = ? AND user_id = ?", assignment.RoleID, assignment.UserID).MustCols("org_id").Update(&assignment)
return errUpdate
@@ -103,10 +103,10 @@ func TestAccessControlStore_SaveExternalServiceRole(t *testing.T) {
{
cmd: accesscontrol.SaveExternalServiceRoleCommand{
ExternalServiceID: "app1",
AssignmentOrgID: 1,
AssignmentOrgID: 2,
ServiceAccountID: 2,
},
wantErr: true,
wantErr: false,
},
},
},