MCPcopy Create free account
hub / github.com/bytebase/bytebase / doIAMPermissionCheck

Function doIAMPermissionCheck

backend/api/v1/acl.go:239–293  ·  view source on GitHub ↗
(ctx context.Context, iamManager *iam.Manager, fullMethod string, user *store.UserMessage, authContext *common.AuthContext)

Source from the content-addressed store, hash-verified

237}
238
239func doIAMPermissionCheck(ctx context.Context, iamManager *iam.Manager, fullMethod string, user *store.UserMessage, authContext *common.AuthContext) (bool, []string, error) {
240 if auth.IsAuthenticationSkipped(fullMethod, authContext) {
241 return true, nil, nil
242 }
243 if authContext.AuthMethod != common.AuthMethodIAM {
244 return true, nil, nil
245 }
246 // Handle GetProject() error status.
247 if len(authContext.Resources) == 0 {
248 return false, nil, errors.Errorf("no resource found for IAM auth method")
249 }
250
251 var hasWorkspaceResource bool
252 projectIDMap := make(map[string]bool)
253 workspaceID := common.GetWorkspaceIDFromContext(ctx)
254 for _, resource := range authContext.Resources {
255 switch resource.Type {
256 case common.ResourceTypeWorkspace:
257 hasWorkspaceResource = true
258 case common.ResourceTypeProject:
259 projectIDMap[resource.ID] = true
260 default:
261 return false, nil, errors.Errorf("unknown resource type %v", resource.Type)
262 }
263 }
264
265 if hasWorkspaceResource {
266 ok, err := iamManager.CheckPermission(ctx, authContext.Permission, user, workspaceID)
267 if err != nil {
268 return false, nil, err
269 }
270 if !ok {
271 return false, nil, nil
272 }
273 }
274 if len(projectIDMap) > 0 {
275 var projectIDs []string
276 for projectID := range projectIDMap {
277 projectIDs = append(projectIDs, projectID)
278 }
279 ok, err := iamManager.CheckPermission(ctx, authContext.Permission, user, workspaceID, projectIDs...)
280 if err != nil {
281 return false, nil, err
282 }
283 if ok {
284 return true, nil, nil
285 }
286 projectResources := []string{}
287 for _, id := range projectIDs {
288 projectResources = append(projectResources, common.FormatProject(id))
289 }
290 return false, projectResources, nil
291 }
292 return true, nil, nil
293}
294
295var workspaceRegex = regexp.MustCompile(`^workspaces/[^/]+`)
296var projectRegex = regexp.MustCompile(`^projects/[^/]+`)

Callers 1

doACLCheckMethod · 0.85

Calls 5

IsAuthenticationSkippedFunction · 0.92
FormatProjectFunction · 0.92
ErrorfMethod · 0.80
CheckPermissionMethod · 0.80

Tested by

no test coverage detected