Skip to content

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

feat(group): support instance_id as cel input (#6684)

h3n4l3edff2b57b3c
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 328a5c67b0..c9363b1e53 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -1394,7 +1394,7 @@ func (s *ProjectService) CreateDatabaseGroup(ctx context.Context, request *v1pb.
if request.DatabaseGroup.DatabaseExpr.Expression == "" {
return nil, status.Errorf(codes.InvalidArgument, "database group database expression is required")
}
- if _, err := validateGroupCELExpr(request.DatabaseGroup.DatabaseExpr.Expression); err != nil {
+ if _, err := common.ValidateGroupCELExpr(request.DatabaseGroup.DatabaseExpr.Expression); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid database group expression: %v", err)
}
@@ -1463,7 +1463,7 @@ func (s *ProjectService) UpdateDatabaseGroup(ctx context.Context, request *v1pb.
if request.DatabaseGroup.DatabaseExpr.Expression == "" {
return nil, status.Errorf(codes.InvalidArgument, "database group expr is required")
}
- if _, err := validateGroupCELExpr(request.DatabaseGroup.DatabaseExpr.Expression); err != nil {
+ if _, err := common.ValidateGroupCELExpr(request.DatabaseGroup.DatabaseExpr.Expression); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid database group expression: %v", err)
}
updateDatabaseGroup.Expression = request.DatabaseGroup.DatabaseExpr
@@ -1652,7 +1652,7 @@ func (s *ProjectService) CreateSchemaGroup(ctx context.Context, request *v1pb.Cr
if request.SchemaGroup.TableExpr.Expression == "" {
return nil, status.Errorf(codes.InvalidArgument, "database group database expression is required")
}
- if _, err := validateGroupCELExpr(request.SchemaGroup.TableExpr.Expression); err != nil {
+ if _, err := common.ValidateGroupCELExpr(request.SchemaGroup.TableExpr.Expression); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid schema group table expression: %v", err)
}
@@ -1731,7 +1731,7 @@ func (s *ProjectService) UpdateSchemaGroup(ctx context.Context, request *v1pb.Up
if request.SchemaGroup.TableExpr.Expression == "" {
return nil, status.Errorf(codes.InvalidArgument, "schema group table expression is required")
}
- if _, err := validateGroupCELExpr(request.SchemaGroup.TableExpr.Expression); err != nil {
+ if _, err := common.ValidateGroupCELExpr(request.SchemaGroup.TableExpr.Expression); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid schema group table expression: %v", err)
}
updateSchemaGroup.Expression = request.SchemaGroup.TableExpr
@@ -1881,66 +1881,20 @@ func (s *ProjectService) GetSchemaGroup(ctx context.Context, request *v1pb.GetSc
return s.convertStoreToAPISchemaGroupFull(ctx, schemaGroup, databaseGroup, projectResourceID)
}
-func (s *ProjectService) convertStoreToAPIDatabaseGroupFull(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) (*v1pb.DatabaseGroup, error) {
- matches, unmatches, err := s.getMatchedAndUnmatchedDatabases(ctx, databaseGroup, projectResourceID)
- if err != nil {
- return nil, err
- }
- ret := &v1pb.DatabaseGroup{
- Name: fmt.Sprintf("%s%s/%s%s", projectNamePrefix, projectResourceID, databaseGroupNamePrefix, databaseGroup.ResourceID),
- DatabasePlaceholder: databaseGroup.Placeholder,
- DatabaseExpr: databaseGroup.Expression,
- }
- for _, database := range matches {
- ret.MatchedDatabases = append(ret.MatchedDatabases, &v1pb.DatabaseGroup_Database{
- Name: fmt.Sprintf("%s%s/%s%s", instanceNamePrefix, database.InstanceID, databaseIDPrefix, database.DatabaseName),
- })
- }
- for _, database := range unmatches {
- ret.UnmatchedDatabases = append(ret.UnmatchedDatabases, &v1pb.DatabaseGroup_Database{
- Name: fmt.Sprintf("%s%s/%s%s", instanceNamePrefix, database.InstanceID, databaseIDPrefix, database.DatabaseName),
- })
- }
- return ret, nil
-}
-
-func validateGroupCELExpr(expr string) (cel.Program, error) {
- e, err := cel.NewEnv(
- cel.Variable("resource", cel.MapType(cel.StringType, cel.AnyType)),
- )
- if err != nil {
- return nil, status.Errorf(codes.Internal, err.Error())
- }
- ast, issues := e.Parse(expr)
- if issues != nil && issues.Err() != nil {
- return nil, status.Errorf(codes.InvalidArgument, issues.Err().Error())
- }
- prog, err := e.Program(ast)
- if err != nil {
- return nil, status.Errorf(codes.InvalidArgument, err.Error())
- }
- return prog, nil
-}
-
-func (s *ProjectService) getMatchedAndUnmatchedDatabases(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) ([]*store.DatabaseMessage, []*store.DatabaseMessage, error) {
- databases, err := s.store.ListDatabases(ctx, &store.FindDatabaseMessage{
- ProjectID: &projectResourceID,
- })
- if err != nil {
- return nil, nil, status.Errorf(codes.Internal, err.Error())
- }
- prog, err := validateGroupCELExpr(databaseGroup.Expression.Expression)
+func getMatchedAndUnmatchedDatabasesInDatabaseGroup(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, allDatabases []*store.DatabaseMessage) ([]*store.DatabaseMessage, []*store.DatabaseMessage, error) {
+ prog, err := common.ValidateGroupCELExpr(databaseGroup.Expression.Expression)
if err != nil {
return nil, nil, err
}
var matches []*store.DatabaseMessage
var unmatches []*store.DatabaseMessage
- for _, database := range databases {
+ for _, database := range allDatabases {
res, _, err := prog.ContextEval(ctx, map[string]any{
"resource": map[string]any{
"database_name": database.DatabaseName,
"environment_name": fmt.Sprintf("%s%s", environmentNamePrefix, database.EnvironmentID),
+ "instance_id": database.InstanceID,
},
})
if err != nil {
@@ -1960,6 +1914,35 @@ func (s *ProjectService) getMatchedAndUnmatchedDatabases(ctx context.Context, da
return matches, unmatches, nil
}
+func (s *ProjectService) convertStoreToAPIDatabaseGroupFull(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) (*v1pb.DatabaseGroup, error) {
+ databases, err := s.store.ListDatabases(ctx, &store.FindDatabaseMessage{
+ ProjectID: &projectResourceID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ matches, unmatches, err := getMatchedAndUnmatchedDatabasesInDatabaseGroup(ctx, databaseGroup, databases)
+ if err != nil {
+ return nil, err
+ }
+ ret := &v1pb.DatabaseGroup{
+ Name: fmt.Sprintf("%s%s/%s%s", projectNamePrefix, projectResourceID, databaseGroupNamePrefix, databaseGroup.ResourceID),
+ DatabasePlaceholder: databaseGroup.Placeholder,
+ DatabaseExpr: databaseGroup.Expression,
+ }
+ for _, database := range matches {
+ ret.MatchedDatabases = append(ret.MatchedDatabases, &v1pb.DatabaseGroup_Database{
+ Name: fmt.Sprintf("%s%s/%s%s", instanceNamePrefix, database.InstanceID, databaseIDPrefix, database.DatabaseName),
+ })
+ }
+ for _, database := range unmatches {
+ ret.UnmatchedDatabases = append(ret.UnmatchedDatabases, &v1pb.DatabaseGroup_Database{
+ Name: fmt.Sprintf("%s%s/%s%s", instanceNamePrefix, database.InstanceID, databaseIDPrefix, database.DatabaseName),
+ })
+ }
+ return ret, nil
+}
+
func convertStoreToAPIDatabaseGroupBasic(databaseGroup *store.DatabaseGroupMessage, projectResourceID string) *v1pb.DatabaseGroup {
return &v1pb.DatabaseGroup{
Name: fmt.Sprintf("%s%s/%s%s", projectNamePrefix, projectResourceID, databaseGroupNamePrefix, databaseGroup.ResourceID),
@@ -1984,12 +1967,18 @@ func (s *ProjectService) convertStoreToAPISchemaGroupFull(ctx context.Context, s
}
func (s *ProjectService) getMatchesAndUnmatchedTables(ctx context.Context, schemaGroup *store.SchemaGroupMessage, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) ([]*v1pb.SchemaGroup_Table, []*v1pb.SchemaGroup_Table, error) {
- matchesDatabases, _, err := s.getMatchedAndUnmatchedDatabases(ctx, databaseGroup, projectResourceID)
+ allDatabases, err := s.store.ListDatabases(ctx, &store.FindDatabaseMessage{
+ ProjectID: &projectResourceID,
+ })
+ if err != nil {
+ return nil, nil, status.Errorf(codes.Internal, err.Error())
+ }
+ matchesDatabases, _, err := getMatchedAndUnmatchedDatabasesInDatabaseGroup(ctx, databaseGroup, allDatabases)
if err != nil {
return nil, nil, err
}
- prog, err := validateGroupCELExpr(schemaGroup.Expression.Expression)
+ prog, err := common.ValidateGroupCELExpr(schemaGroup.Expression.Expression)
if err != nil {
return nil, nil, err
}
diff --git a/backend/common/convert.go b/backend/common/convert.go
index 29aeb0d822..54107d751b 100644
--- a/backend/common/convert.go
+++ b/backend/common/convert.go
@@ -101,3 +101,22 @@ func ConvertUnparsedApproval(expression *expr.Expr) (*v1alpha1.ParsedExpr, error
}
return expr, nil
}
+
+// ValidateGroupCELExpr validates group expr.
+func ValidateGroupCELExpr(expr string) (cel.Program, error) {
+ e, err := cel.NewEnv(
+ cel.Variable("resource", cel.MapType(cel.StringType, cel.AnyType)),
+ )
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ ast, issues := e.Parse(expr)
+ if issues != nil && issues.Err() != nil {
+ return nil, status.Errorf(codes.InvalidArgument, issues.Err().Error())
+ }
+ prog, err := e.Program(ast)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ return prog, nil
+}
diff --git a/backend/server/issue.go b/backend/server/issue.go
index 8c0881c0ef..c6c7ff79d4 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -12,7 +12,6 @@ import (
"strconv"
"strings"
- "github.com/google/cel-go/cel"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
@@ -1913,21 +1912,10 @@ func convertDatabaseLabels(labelsJSON string) ([]*api.DatabaseLabel, error) {
// TODO(zp): keep this function as same as the one in the project_service.go.
func getMatchedAndUnmatchedDatabases(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, allDatabases []*store.DatabaseMessage) ([]*store.DatabaseMessage, []*store.DatabaseMessage, error) {
- e, err := cel.NewEnv(
- cel.Variable("resource", cel.MapType(cel.StringType, cel.AnyType)),
- )
+ prog, err := common.ValidateGroupCELExpr(databaseGroup.Expression.Expression)
if err != nil {
- return nil, nil, status.Errorf(codes.Internal, err.Error())
+ return nil, nil, err
}
- ast, issues := e.Parse(databaseGroup.Expression.Expression)
- if issues != nil && issues.Err() != nil {
- return nil, nil, status.Errorf(codes.InvalidArgument, issues.Err().Error())
- }
- prog, err := e.Program(ast)
- if err != nil {
- return nil, nil, status.Errorf(codes.InvalidArgument, err.Error())
- }
-
var matches []*store.DatabaseMessage
var unmatches []*store.DatabaseMessage
@@ -1936,6 +1924,7 @@ func getMatchedAndUnmatchedDatabases(ctx context.Context, databaseGroup *store.D
"resource": map[string]any{
"database_name": database.DatabaseName,
"environment_name": fmt.Sprintf("%s%s", "environments/", database.EnvironmentID),
+ "instance_id": database.InstanceID,
},
})
if err != nil {
@@ -1956,19 +1945,9 @@ func getMatchedAndUnmatchedDatabases(ctx context.Context, databaseGroup *store.D
}
func getMatchesAndUnmatchedTables(ctx context.Context, dbSchema *store.DBSchema, schemaGroup *store.SchemaGroupMessage) ([]string, []string, error) {
- e, err := cel.NewEnv(
- cel.Variable("resource", cel.MapType(cel.StringType, cel.AnyType)),
- )
- if err != nil {
- return nil, nil, status.Errorf(codes.Internal, err.Error())
- }
- ast, issues := e.Parse(schemaGroup.Expression.Expression)
- if issues != nil && issues.Err() != nil {
- return nil, nil, status.Errorf(codes.InvalidArgument, issues.Err().Error())
- }
- prog, err := e.Program(ast)
+ prog, err := common.ValidateGroupCELExpr(schemaGroup.Expression.Expression)
if err != nil {
- return nil, nil, status.Errorf(codes.InvalidArgument, err.Error())
+ return nil, nil, err
}
var matched []string