Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: optimize checks (#13006)

Danny Xu87cae8b8f5f6
diff --git a/backend/api/v1/acl.go b/backend/api/v1/acl.go
index 66b4121709..607e7ee0fd 100644
--- a/backend/api/v1/acl.go
+++ b/backend/api/v1/acl.go
@@ -210,24 +210,23 @@ func (in *ACLInterceptor) doIAMPermissionCheck(ctx context.Context, fullMethod s
if len(authContext.Resources) == 0 {
return false, nil, errors.Errorf("no resource found for IAM auth method")
}
- for _, resource := range authContext.Resources {
- slog.Debug("IAM auth method", slog.String("method", fullMethod), slog.String("permission", authContext.Permission), slog.String("project", resource.ProjectID), slog.Bool("workspace", resource.Workspace))
- if resource.Workspace {
- ok, err := in.iamManager.CheckPermission(ctx, authContext.Permission, user)
- if err != nil {
- return false, nil, err
- }
- if !ok {
- return false, nil, nil
- }
- } else {
- ok, err := in.iamManager.CheckPermission(ctx, authContext.Permission, user, resource.ProjectID)
- if err != nil {
- return false, []string{resource.ProjectID}, err
- }
- if !ok {
- return false, []string{resource.ProjectID}, nil
- }
+ if authContext.HasWorkspaceResource() {
+ ok, err := in.iamManager.CheckPermission(ctx, authContext.Permission, user)
+ if err != nil {
+ return false, nil, err
+ }
+ if !ok {
+ return false, nil, nil
+ }
+ }
+ projectIDs := authContext.GetProjectResources()
+ if len(projectIDs) > 0 {
+ ok, err := in.iamManager.CheckPermission(ctx, authContext.Permission, user, projectIDs...)
+ if err != nil {
+ return false, projectIDs, err
+ }
+ if !ok {
+ return false, projectIDs, nil
}
}
return true, nil, nil
diff --git a/backend/common/context.go b/backend/common/context.go
index f5d93ae027..e723df889c 100644
--- a/backend/common/context.go
+++ b/backend/common/context.go
@@ -48,3 +48,26 @@ type AuthContext struct {
AuthMethod AuthMethod
Resources []*Resource
}
+
+func (c *AuthContext) HasWorkspaceResource() bool {
+ for _, r := range c.Resources {
+ if r.Workspace {
+ return true
+ }
+ }
+ return false
+}
+
+func (c *AuthContext) GetProjectResources() []string {
+ projectIDMap := make(map[string]bool)
+ for _, r := range c.Resources {
+ if r.ProjectID != "" {
+ projectIDMap[r.ProjectID] = true
+ }
+ }
+ var projectIDs []string
+ for projectID := range projectIDMap {
+ projectIDs = append(projectIDs, projectID)
+ }
+ return projectIDs
+}