Skip to content

Commit 8356a6f

Browse files
authored
refactor(authz): replace admin role check with policy-based authorization for default backend access (#2741)
Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
1 parent 46f1bbf commit 8356a6f

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

app/controlplane/internal/service/cascredential.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,23 @@ func (s *CASCredentialsService) Get(ctx context.Context, req *pb.CASCredentialsS
117117
projectIDs[orgID] = []uuid.UUID{*currentAPIToken.ProjectID}
118118
}
119119
mapping, err = s.casMappingUC.FindCASMappingForDownloadByOrg(ctx, req.Digest, []uuid.UUID{orgID}, projectIDs)
120-
}
121-
122-
if err != nil && !biz.IsNotFound(err) {
123-
if biz.IsErrValidation(err) {
124-
return nil, errors.BadRequest("invalid", err.Error())
120+
if err != nil && !biz.IsNotFound(err) {
121+
if biz.IsErrValidation(err) {
122+
return nil, errors.BadRequest("invalid", err.Error())
123+
}
124+
return nil, handleUseCaseErr(err, s.log)
125125
}
126-
return nil, handleUseCaseErr(err, s.log)
127126
}
128127

129128
if mapping != nil {
130129
backend = mapping.CASBackend
131-
} else if authz.Role(currentAuthzSubject).IsAdmin() {
132-
// fallback to default mapping for admins
133-
backend = defaultBackend
130+
} else {
131+
// fallback to default backend if the user or the token is allowed to
132+
if ok, err := s.authzUC.Enforce(ctx, currentAuthzSubject, authz.PolicyDefaultBackendArtifactRead); err != nil {
133+
return nil, handleUseCaseErr(err, s.log)
134+
} else if ok {
135+
backend = defaultBackend
136+
}
134137
}
135138
case casJWT.Uploader:
136139
backend = defaultBackend

app/controlplane/pkg/authz/authz.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const (
6363
ResourceAPIToken = "api_token"
6464
ResourceProjectMembership = "project_membership"
6565
ResourceOrganizationInvitations = "organization_invitations"
66+
ResourceDefaultBackend = "default_backend"
6667

6768
// Top level instance admin role
6869
// this is used to know if an user is a super admin of the chainloop instance
@@ -107,6 +108,8 @@ var (
107108
// Artifact
108109
PolicyArtifactDownload = &Policy{ResourceCASArtifact, ActionRead}
109110
PolicyArtifactUpload = &Policy{ResourceCASArtifact, ActionCreate}
111+
// Being able to read from the default backend
112+
PolicyDefaultBackendArtifactRead = &Policy{ResourceDefaultBackend, ActionRead}
110113
// CAS backend
111114
PolicyCASBackendList = &Policy{ResourceCASBackend, ActionList}
112115
PolicyCASBackendUpdate = &Policy{ResourceCASBackend, ActionUpdate}
@@ -198,6 +201,8 @@ var RolesMap = map[Role][]*Policy{
198201
PolicyArtifactUpload,
199202
// We manually check this policy to be able to know if the user can invite users to the system
200203
PolicyOrganizationInvitationsCreate,
204+
// Being able to read from the default backend
205+
PolicyDefaultBackendArtifactRead,
201206
// + all the policies from the viewer role inherited automatically
202207
},
203208
// RoleViewer is an org-scoped role that provides read-only access to all resources

0 commit comments

Comments
 (0)