Skip to content

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

chore(backend): validate iam policy expression with maximum role expiration (#12178)

boojackf65b3409d8ae
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 2237abe634..08d7af4b28 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -5,17 +5,20 @@ import (
"fmt"
"log/slog"
"reflect"
+ "slices"
"strconv"
"strings"
"time"
"github.com/google/cel-go/cel"
+ celast "github.com/google/cel-go/common/ast"
"github.com/gosimple/slug"
"github.com/pkg/errors"
"google.golang.org/genproto/googleapis/type/expr"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
+ "google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/bytebase/bytebase/backend/common"
@@ -370,7 +373,7 @@ func (s *ProjectService) SetIamPolicy(ctx context.Context, request *v1pb.SetIamP
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to convert to roles: %v", err)
}
- if err := s.validateIAMPolicy(request.Policy, roles); err != nil {
+ if err := s.validateIAMPolicy(ctx, request.Policy, roles); err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
@@ -2023,14 +2026,22 @@ func convertToStoreLabelSelectorOperator(operator v1pb.OperatorType) (store.Oper
return store.OperatorType(""), errors.Errorf("invalid operator type: %v", operator)
}
-func (s *ProjectService) validateIAMPolicy(policy *v1pb.IamPolicy, roles []*v1pb.Role) error {
+func (s *ProjectService) validateIAMPolicy(ctx context.Context, policy *v1pb.IamPolicy, roles []*v1pb.Role) error {
if policy == nil {
return errors.Errorf("IAM Policy is required")
}
- return s.validateBindings(policy.Bindings, roles)
+ generalSetting, err := s.store.GetWorkspaceGeneralSetting(ctx)
+ if err != nil {
+ return errors.Wrap(err, "failed to get workspace general setting")
+ }
+ var maximumRoleExpiration *durationpb.Duration
+ if generalSetting != nil {
+ maximumRoleExpiration = generalSetting.MaximumRoleExpiration
+ }
+ return s.validateBindings(policy.Bindings, roles, maximumRoleExpiration)
}
-func (*ProjectService) validateBindings(bindings []*v1pb.Binding, roles []*v1pb.Role) error {
+func (*ProjectService) validateBindings(bindings []*v1pb.Binding, roles []*v1pb.Role, maximumRoleExpiration *durationpb.Duration) error {
if len(bindings) == 0 {
return errors.Errorf("IAM Binding is required")
}
@@ -2061,10 +2072,16 @@ func (*ProjectService) validateBindings(bindings []*v1pb.Binding, roles []*v1pb.
}
projectRoleMap[binding.Role] = true
- // TODO(steven): validate the request time with maximum role expiration.
if _, err := common.ValidateProjectMemberCELExpr(binding.Condition); err != nil {
return err
}
+
+ rolesToValidate := []string{fmt.Sprintf("roles/%s", api.ProjectQuerier), fmt.Sprintf("roles/%s", api.ProjectExporter)}
+ if binding.Condition != nil && binding.Condition.Expression != "" && slices.Contains(rolesToValidate, binding.Role) {
+ if err := validateIAMPolicyExpression(binding.Condition.Expression, maximumRoleExpiration); err != nil {
+ return err
+ }
+ }
}
// Must contain one owner binding.
if _, ok := projectRoleMap[common.FormatRole(api.ProjectOwner.String())]; !ok {
@@ -2073,6 +2090,99 @@ func (*ProjectService) validateBindings(bindings []*v1pb.Binding, roles []*v1pb.
return nil
}
+// validateIAMPolicyExpression validates the IAM policy expression.
+// Currently only validate the following expression:
+// * request.time < timestamp("2021-01-01T00:00:00Z")
+//
+// Other expressions will be ignored.
+func validateIAMPolicyExpression(expr string, maximumRoleExpiration *durationpb.Duration) error {
+ e, err := cel.NewEnv()
+ if err != nil {
+ return errors.Wrap(err, "failed to create cel environment")
+ }
+ ast, iss := e.Parse(expr)
+ if iss != nil {
+ return errors.Wrap(iss.Err(), "failed to parse expression")
+ }
+
+ var validator func(expr celast.Expr) error
+
+ validator = func(expr celast.Expr) error {
+ switch expr.Kind() {
+ case celast.CallKind:
+ functionName := expr.AsCall().FunctionName()
+ switch functionName {
+ case "_||_":
+ for _, arg := range expr.AsCall().Args() {
+ err := validator(arg)
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+ case "_&&_":
+ for _, arg := range expr.AsCall().Args() {
+ err := validator(arg)
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+ // Only handle "request.time < timestamp("2021-01-01T00:00:00Z").
+ case "_<_":
+ if maximumRoleExpiration == nil {
+ return nil
+ }
+
+ var value string
+ for _, arg := range expr.AsCall().Args() {
+ switch arg.Kind() {
+ case celast.SelectKind:
+ variable := fmt.Sprintf("%s.%s", arg.AsSelect().Operand().AsIdent(), arg.AsSelect().FieldName())
+ if variable != "request.time" {
+ return errors.Errorf("unexpected variable %v", variable)
+ }
+ case celast.CallKind:
+ functionName := arg.AsCall().FunctionName()
+ if functionName != "timestamp" {
+ return errors.Errorf("unexpected function %v", functionName)
+ }
+ if len(arg.AsCall().Args()) != 1 {
+ return errors.Errorf("unexpected number of arguments %d", len(arg.AsCall().Args()))
+ }
+ valueArg := arg.AsCall().Args()[0]
+ if valueArg.Kind() != celast.LiteralKind {
+ return errors.Errorf("unexpected argument kind %v", valueArg.Kind())
+ }
+ lit, ok := valueArg.AsLiteral().Value().(string)
+ if !ok {
+ return errors.Errorf("expect string, got %T, hint: filter literals should be string", arg.AsLiteral().Value())
+ }
+ value = lit
+ }
+ }
+
+ t, err := time.Parse(time.RFC3339, value)
+ if err != nil {
+ return errors.Errorf("failed to parse time %v, error: %v", value, err)
+ }
+ maxExpirationTime := time.Now().Add(maximumRoleExpiration.AsDuration())
+ if t.After(maxExpirationTime) {
+ return errors.Errorf("time %s exceeds maximum role expiration %s", t.Format(time.DateTime), maxExpirationTime.Format(time.DateTime))
+ }
+ return nil
+ default:
+ // Ignore other functions.
+ return nil
+ }
+ default:
+ return errors.Errorf("unexpected expr kind %v", expr.Kind())
+ }
+ }
+
+ return validator(ast.NativeRep().Expr())
+}
+
func uniqueBindingMembers(members []string) []string {
temp := []string{}
flag := make(map[string]bool)
diff --git a/backend/api/v1/project_service_test.go b/backend/api/v1/project_service_test.go
index fe5b4b6a6c..c0c1f011a3 100644
--- a/backend/api/v1/project_service_test.go
+++ b/backend/api/v1/project_service_test.go
@@ -1,9 +1,12 @@
package v1
import (
+ "fmt"
"testing"
+ "time"
"github.com/stretchr/testify/require"
+ "google.golang.org/protobuf/types/known/durationpb"
"github.com/bytebase/bytebase/backend/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
@@ -175,7 +178,7 @@ func TestValidateBindings(t *testing.T) {
// Mock an empty project service to test the validateBindings function.
projectService := NewProjectService(nil, nil, nil, nil)
for _, tt := range tests {
- err := projectService.validateBindings(tt.bindings, tt.roles)
+ err := projectService.validateBindings(tt.bindings, tt.roles, nil)
if tt.wantErr {
a.Error(err)
} else {
@@ -184,6 +187,34 @@ func TestValidateBindings(t *testing.T) {
}
}
+func TestValidateIAMPolicyExpression(t *testing.T) {
+ timeNow := time.Now()
+ tests := []struct {
+ expr string
+ maximumRoleExpiration *durationpb.Duration
+ wantErr bool
+ }{
+ {
+ expr: fmt.Sprintf("request.time < timestamp(\"%s\")", timeNow.AddDate(0, 0, 15).Format(time.RFC3339)),
+ maximumRoleExpiration: &durationpb.Duration{Seconds: 60 * 60 * 24 * 30}, // 30 days
+ },
+ {
+ expr: fmt.Sprintf("request.time < timestamp(\"%s\")", timeNow.AddDate(0, 0, 60).Format(time.RFC3339)),
+ maximumRoleExpiration: &durationpb.Duration{Seconds: 60 * 60 * 24 * 30},
+ wantErr: true,
+ },
+ }
+
+ for _, tt := range tests {
+ err := validateIAMPolicyExpression(tt.expr, tt.maximumRoleExpiration)
+ if tt.wantErr {
+ require.Error(t, err)
+ } else {
+ require.NoError(t, err)
+ }
+ }
+}
+
func TestValidateAndConvertToStoreDeploymentSchedule(t *testing.T) {
tests := []struct {
name string
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 3117cf549f..2b5bb44e80 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -662,7 +662,7 @@
},
"maximum-role-expiration": {
"self": "Maximum role expiration",
- "description": "Maximum role expiration is the maximum period a role can be granted to a user.",
+ "description": "Maximum role expiration is the maximum period a role can be granted to a user. (Only for querier and exporter roles)",
"days": "Day(s)",
"never-expires": "Never expires"
},
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 5c04747701..bd7c8219d9 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -3546,4 +3546,4 @@
"self": "Última actualización"
}
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 2f1797dfff..4c6c3c17be 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -3546,4 +3546,4 @@
"self": "最後の更新"
}
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 7b7b4b874d..78c9c18b98 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -662,7 +662,7 @@
},
"maximum-role-expiration": {
"self": "角色最大过期时间",
- "description": "角色最大过期时间是可以赋予用户角色时所能设置的最大过期时间。",
+ "description": "角色最大过期时间是可以赋予用户角色时所能设置的最大过期时间。(包括数据查询者和导出者)",
"days": "天数",
"never-expires": "永不过期"
},