Skip to content

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

chore: add feature guard (#5261)

p0nyad9d274d8dc3
diff --git a/backend/api/v1/risk_service.go b/backend/api/v1/risk_service.go
index 8916691987..70bc676514 100644
--- a/backend/api/v1/risk_service.go
+++ b/backend/api/v1/risk_service.go
@@ -9,6 +9,7 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
"github.com/bytebase/bytebase/backend/common"
+ enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"
api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
@@ -17,13 +18,15 @@ import (
// RiskService implements the risk service.
type RiskService struct {
v1pb.UnimplementedRiskServiceServer
- store *store.Store
+ store *store.Store
+ licenseService enterpriseAPI.LicenseService
}
// NewRiskService creates a new RiskService.
-func NewRiskService(store *store.Store) *RiskService {
+func NewRiskService(store *store.Store, licenseService enterpriseAPI.LicenseService) *RiskService {
return &RiskService{
- store: store,
+ store: store,
+ licenseService: licenseService,
}
}
@@ -54,6 +57,9 @@ func (s *RiskService) ListRisks(ctx context.Context, _ *v1pb.ListRisksRequest) (
// CreateRisk creates a risk.
func (s *RiskService) CreateRisk(ctx context.Context, request *v1pb.CreateRiskRequest) (*v1pb.Risk, error) {
+ if !s.licenseService.IsFeatureEnabled(api.FeatureCustomApproval) {
+ return nil, status.Errorf(codes.PermissionDenied, api.FeatureCustomApproval.AccessErrorMessage())
+ }
principalID := ctx.Value(common.PrincipalIDContextKey).(int)
risk, err := s.store.CreateRisk(ctx, &store.RiskMessage{
Source: convertSource(request.Risk.Source),
@@ -70,6 +76,9 @@ func (s *RiskService) CreateRisk(ctx context.Context, request *v1pb.CreateRiskRe
// UpdateRisk updates a risk.
func (s *RiskService) UpdateRisk(ctx context.Context, request *v1pb.UpdateRiskRequest) (*v1pb.Risk, error) {
+ if !s.licenseService.IsFeatureEnabled(api.FeatureCustomApproval) {
+ return nil, status.Errorf(codes.PermissionDenied, api.FeatureCustomApproval.AccessErrorMessage())
+ }
principalID := ctx.Value(common.PrincipalIDContextKey).(int)
if request.UpdateMask == nil {
return nil, status.Errorf(codes.InvalidArgument, "update_mask must be set")
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.go
index d3009c24b4..8f77b29416 100644
--- a/backend/api/v1/setting_service.go
+++ b/backend/api/v1/setting_service.go
@@ -9,6 +9,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/component/config"
+ enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"
api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/store"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -18,15 +19,17 @@ import (
// SettingService implements the setting service.
type SettingService struct {
v1pb.UnimplementedSettingServiceServer
- store *store.Store
- profile *config.Profile
+ store *store.Store
+ profile *config.Profile
+ licenseService enterpriseAPI.LicenseService
}
// NewSettingService creates a new setting service.
-func NewSettingService(store *store.Store, profile *config.Profile) *SettingService {
+func NewSettingService(store *store.Store, profile *config.Profile, licenseService enterpriseAPI.LicenseService) *SettingService {
return &SettingService{
- store: store,
- profile: profile,
+ store: store,
+ profile: profile,
+ licenseService: licenseService,
}
}
@@ -101,6 +104,9 @@ func (s *SettingService) SetSetting(ctx context.Context, request *v1pb.SetSettin
}
settingValue = string(bytes)
}
+ if apiSettingName == api.SettingWorkspaceApproval && !s.licenseService.IsFeatureEnabled(api.FeatureCustomApproval) {
+ return nil, status.Errorf(codes.PermissionDenied, api.FeatureCustomApproval.AccessErrorMessage())
+ }
setting, err := s.store.UpsertSettingV2(ctx, &store.SetSettingMessage{
Name: apiSettingName,
diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.go
index 1da471c815..037924de97 100644
--- a/backend/runner/approval/runner.go
+++ b/backend/runner/approval/runner.go
@@ -20,7 +20,9 @@ import (
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/config"
"github.com/bytebase/bytebase/backend/component/dbfactory"
+ enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"
api "github.com/bytebase/bytebase/backend/legacyapi"
+
"github.com/bytebase/bytebase/backend/store"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -61,17 +63,19 @@ var issueTypeToRiskSource = map[api.IssueType]store.RiskSource{
// Runner is the runner for finding approval templates for issues.
type Runner struct {
- store *store.Store
- dbFactory *dbfactory.DBFactory
- profile config.Profile
+ store *store.Store
+ dbFactory *dbfactory.DBFactory
+ profile config.Profile
+ licenseService enterpriseAPI.LicenseService
}
// NewRunner creates a new runner.
-func NewRunner(store *store.Store, dbFactory *dbfactory.DBFactory, profile config.Profile) *Runner {
+func NewRunner(store *store.Store, dbFactory *dbfactory.DBFactory, profile config.Profile, licenseService enterpriseAPI.LicenseService) *Runner {
return &Runner{
- store: store,
- dbFactory: dbFactory,
- profile: profile,
+ store: store,
+ dbFactory: dbFactory,
+ profile: profile,
+ licenseService: licenseService,
}
}
@@ -110,10 +114,11 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {
}
// no need to find if
+ // - feature is not enabled
// - risk source is RiskSourceUnknown
// - risks are empty
// - approval setting rules are empty
- if issueTypeToRiskSource[issue.Type] == store.RiskSourceUnknown || len(risks) == 0 || len(approvalSetting.Rules) == 0 {
+ if !r.licenseService.IsFeatureEnabled(api.FeatureCustomApproval) || issueTypeToRiskSource[issue.Type] == store.RiskSourceUnknown || len(risks) == 0 || len(approvalSetting.Rules) == 0 {
payload := &storepb.IssuePayload{
Approval: &storepb.IssuePayloadApproval{
ApprovalFindingDone: true,
diff --git a/backend/server/issue.go b/backend/server/issue.go
index c046799506..c4e10cf48b 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -421,10 +421,15 @@ func (s *Server) createIssue(ctx context.Context, issueCreate *api.IssueCreate,
return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("assignee %d not found", issueCreate.AssigneeID))
}
// TODO(p0ny): remove issueCreate.Payload
- issueCreatePayload := &storepb.IssuePayload{}
- issueCreatePayload.Approval = &storepb.IssuePayloadApproval{
- ApprovalFindingDone: false,
+ issueCreatePayload := &storepb.IssuePayload{
+ Approval: &storepb.IssuePayloadApproval{
+ ApprovalFindingDone: false,
+ },
+ }
+ if !s.licenseService.IsFeatureEnabled(api.FeatureCustomApproval) {
+ issueCreatePayload.Approval.ApprovalFindingDone = true
}
+
issueCreatePayloadBytes, err := protojson.Marshal(issueCreatePayload)
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal issue payload").SetInternal(err)
diff --git a/backend/server/server.go b/backend/server/server.go
index 658508a054..54e9f60256 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -399,7 +399,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
s.ApplicationRunner = apprun.NewRunner(storeInstance, s.ActivityManager, s.feishuProvider, profile)
s.BackupRunner = backuprun.NewRunner(storeInstance, s.dbFactory, s.s3Client, s.stateCfg, &profile)
s.RollbackRunner = rollbackrun.NewRunner(storeInstance, s.dbFactory, s.stateCfg)
- s.ApprovalRunner = approval.NewRunner(storeInstance, s.dbFactory, profile)
+ s.ApprovalRunner = approval.NewRunner(storeInstance, s.dbFactory, profile, s.licenseService)
s.TaskScheduler = taskrun.NewScheduler(storeInstance, s.ApplicationRunner, s.SchemaSyncer, s.ActivityManager, s.licenseService, s.stateCfg, profile, s.MetricReporter)
s.TaskScheduler.Register(api.TaskGeneral, taskrun.NewDefaultExecutor())
@@ -551,11 +551,11 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
v1pb.RegisterInstanceRoleServiceServer(s.grpcServer, v1.NewInstanceRoleService(s.store, s.dbFactory))
v1pb.RegisterOrgPolicyServiceServer(s.grpcServer, v1.NewOrgPolicyService(s.store, s.licenseService))
v1pb.RegisterIdentityProviderServiceServer(s.grpcServer, v1.NewIdentityProviderService(s.store, s.licenseService))
- v1pb.RegisterSettingServiceServer(s.grpcServer, v1.NewSettingService(s.store, &s.profile))
+ v1pb.RegisterSettingServiceServer(s.grpcServer, v1.NewSettingService(s.store, &s.profile, s.licenseService))
v1pb.RegisterAnomalyServiceServer(s.grpcServer, v1.NewAnomalyService(s.store))
v1pb.RegisterSQLServiceServer(s.grpcServer, v1.NewSQLService())
v1pb.RegisterExternalVersionControlServiceServer(s.grpcServer, v1.NewExternalVersionControlService(s.store))
- v1pb.RegisterRiskServiceServer(s.grpcServer, v1.NewRiskService(s.store))
+ v1pb.RegisterRiskServiceServer(s.grpcServer, v1.NewRiskService(s.store, s.licenseService))
v1pb.RegisterReviewServiceServer(s.grpcServer, v1.NewReviewService(s.store, s.ActivityManager))
reflection.Register(s.grpcServer)