Skip to content

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

refactor: remove unused setting utils and use proto getters (#18350)

Danny Xu93ff60e8f310
diff --git a/backend/api/auth/header.go b/backend/api/auth/header.go
index f470de01fc..88b9a0df7f 100644
--- a/backend/api/auth/header.go
+++ b/backend/api/auth/header.go
@@ -78,21 +78,18 @@ func GetTokenDuration(ctx context.Context, store *store.Store, licenseService *e
if err != nil {
return tokenDuration
}
- passwordRestriction, err := store.GetPasswordRestriction(ctx)
- if err != nil {
- return tokenDuration
- }
- if workspaceProfile.TokenDuration != nil && workspaceProfile.TokenDuration.GetSeconds() > 0 {
- tokenDuration = workspaceProfile.TokenDuration.AsDuration()
+ if workspaceProfile.GetTokenDuration().GetSeconds() > 0 {
+ tokenDuration = workspaceProfile.GetTokenDuration().AsDuration()
}
// Currently we implement the password rotation restriction in a simple way:
// 1. Only check if users need to reset their password during login.
// 2. For the 1st time login, if `RequireResetPasswordForFirstLogin` is true, `require_reset_password` in the response will be true
// 3. Otherwise if the `PasswordRotation` exists, check the password last updated time to decide if the `require_reset_password` is true.
// So we will use the minimum value between (`workspaceProfile.TokenDuration`, `passwordRestriction.PasswordRotation`) to force to expire the token.
- if passwordRestriction.PasswordRotation != nil && passwordRestriction.PasswordRotation.GetSeconds() > 0 {
- passwordRotation := passwordRestriction.PasswordRotation.AsDuration()
+ passwordRestriction := workspaceProfile.GetPasswordRestriction()
+ if passwordRestriction.GetPasswordRotation().GetSeconds() > 0 {
+ passwordRotation := passwordRestriction.GetPasswordRotation().AsDuration()
if passwordRotation.Seconds() < tokenDuration.Seconds() {
tokenDuration = passwordRotation
}
diff --git a/backend/api/directory-sync/webhook.go b/backend/api/directory-sync/webhook.go
index da18b3e995..4d0d4c49f5 100644
--- a/backend/api/directory-sync/webhook.go
+++ b/backend/api/directory-sync/webhook.go
@@ -617,10 +617,11 @@ func (s *Service) validRequestURL(ctx context.Context, c echo.Context) error {
workspaceID := c.Param("workspaceID")
- myWorkspaceID, err := s.store.GetWorkspaceID(ctx)
+ systemSetting, err := s.store.GetSystemSetting(ctx)
if err != nil {
- return err
+ return errors.Wrap(err, "failed to get system setting")
}
+ myWorkspaceID := systemSetting.WorkspaceId
if myWorkspaceID != workspaceID {
return errors.Errorf("invalid workspace id %q, my ID %q", workspaceID, myWorkspaceID)
}
diff --git a/backend/api/v1/actuator_service.go b/backend/api/v1/actuator_service.go
index 15b281dee5..9f6cbac882 100644
--- a/backend/api/v1/actuator_service.go
+++ b/backend/api/v1/actuator_service.go
@@ -143,16 +143,13 @@ func (s *ActuatorService) getServerInfo(ctx context.Context) (*v1pb.ActuatorInfo
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find workspace setting"))
}
- passwordRestrictionSetting, err := s.store.GetPasswordRestriction(ctx)
- if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find password restriction setting"))
- }
- passwordSetting := convertToPasswordRestrictionSetting(passwordRestrictionSetting)
+ passwordSetting := convertToPasswordRestrictionSetting(setting.GetPasswordRestriction())
- workspaceID, err := s.store.GetWorkspaceID(ctx)
+ systemSetting, err := s.store.GetSystemSetting(ctx)
if err != nil {
- return nil, connect.NewError(connect.CodeInternal, err)
+ return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get system setting"))
}
+ workspaceID := systemSetting.WorkspaceId
usedFeatures, err := s.getUsedFeatures(ctx)
if err != nil {
diff --git a/backend/api/v1/audit.go b/backend/api/v1/audit.go
index 906f3e8e03..f0e227dbc3 100644
--- a/backend/api/v1/audit.go
+++ b/backend/api/v1/audit.go
@@ -169,10 +169,11 @@ func createAuditLogConnect(ctx context.Context, request, response any, method st
var parents []string
if authContext.HasWorkspaceResource() {
- workspaceID, err := storage.GetWorkspaceID(ctx)
+ systemSetting, err := storage.GetSystemSetting(ctx)
if err != nil {
- return errors.Wrapf(err, "failed to get workspace id")
+ return errors.Wrapf(err, "failed to get system setting")
}
+ workspaceID := systemSetting.WorkspaceId
parents = append(parents, common.FormatWorkspace(workspaceID))
} else {
for _, projectID := range authContext.GetProjectResources() {
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index 6da7bbb14b..c51841b96f 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -247,14 +247,15 @@ func (s *AuthService) needResetPassword(ctx context.Context, user *store.UserMes
return false
}
- passwordRestriction, err := s.store.GetPasswordRestriction(ctx)
+ setting, err := s.store.GetWorkspaceGeneralSetting(ctx)
if err != nil {
- slog.Error("failed to get password restriction", log.BBError(err))
+ slog.Error("failed to get workspace setting", log.BBError(err))
return false
}
+ passwordRestriction := setting.GetPasswordRestriction()
if user.Profile.LastLoginTime == nil {
- if !passwordRestriction.RequireResetPasswordForFirstLogin {
+ if !passwordRestriction.GetRequireResetPasswordForFirstLogin() {
return false
}
count, err := s.store.CountUsers(ctx, storepb.PrincipalType_END_USER)
@@ -266,12 +267,12 @@ func (s *AuthService) needResetPassword(ctx context.Context, user *store.UserMes
return count > 1
}
- if passwordRestriction.PasswordRotation != nil {
+ if passwordRestriction.GetPasswordRotation() != nil {
lastChangePasswordTime := user.CreatedAt
if user.Profile.LastChangePasswordTime != nil {
lastChangePasswordTime = user.Profile.LastChangePasswordTime.AsTime()
}
- if lastChangePasswordTime.Add(passwordRestriction.PasswordRotation.AsDuration()).Before(time.Now()) {
+ if lastChangePasswordTime.Add(passwordRestriction.GetPasswordRotation().AsDuration()).Before(time.Now()) {
return true
}
}
diff --git a/backend/api/v1/user_service.go b/backend/api/v1/user_service.go
index 8b93100c63..b0b8223e9c 100644
--- a/backend/api/v1/user_service.go
+++ b/backend/api/v1/user_service.go
@@ -303,23 +303,25 @@ func (s *UserService) CreateUser(ctx context.Context, request *connect.Request[v
}
func (s *UserService) validatePassword(ctx context.Context, password string) error {
- passwordRestriction, err := s.store.GetPasswordRestriction(ctx)
+ setting, err := s.store.GetWorkspaceGeneralSetting(ctx)
if err != nil {
return connect.NewError(connect.CodeInternal, errors.Errorf("failed to get password restriction with error: %v", err))
}
- if len(password) < int(passwordRestriction.MinLength) {
- return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("password length should no less than %v characters", passwordRestriction.MinLength))
+ passwordRestriction := setting.GetPasswordRestriction()
+
+ if len(password) < int(passwordRestriction.GetMinLength()) {
+ return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("password length should no less than %v characters", passwordRestriction.GetMinLength()))
}
- if passwordRestriction.RequireNumber && !regexp.MustCompile("[0-9]+").MatchString(password) {
+ if passwordRestriction.GetRequireNumber() && !regexp.MustCompile("[0-9]+").MatchString(password) {
return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("password must contains at least 1 number"))
}
- if passwordRestriction.RequireLetter && !regexp.MustCompile("[a-zA-Z]+").MatchString(password) {
+ if passwordRestriction.GetRequireLetter() && !regexp.MustCompile("[a-zA-Z]+").MatchString(password) {
return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("password must contains at least 1 lower case letter"))
}
- if passwordRestriction.RequireUppercaseLetter && !regexp.MustCompile("[A-Z]+").MatchString(password) {
+ if passwordRestriction.GetRequireUppercaseLetter() && !regexp.MustCompile("[A-Z]+").MatchString(password) {
return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("password must contains at least 1 upper case letter"))
}
- if passwordRestriction.RequireSpecialCharacter && !regexp.MustCompile(`[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+`).MatchString(password) {
+ if passwordRestriction.GetRequireSpecialCharacter() && !regexp.MustCompile(`[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+`).MatchString(password) {
return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("password must contains at least 1 special character"))
}
return nil
diff --git a/backend/enterprise/plugin/hub/hub.go b/backend/enterprise/plugin/hub/hub.go
index 6aab3acf7b..e0882013bd 100644
--- a/backend/enterprise/plugin/hub/hub.go
+++ b/backend/enterprise/plugin/hub/hub.go
@@ -112,10 +112,11 @@ func (p *Provider) parseLicense(ctx context.Context, license string) (*v1pb.Subs
func (p *Provider) findEnterpriseLicense(ctx context.Context) (*v1pb.Subscription, error) {
// Find enterprise license.
- licenseStr, err := p.store.GetEnterpriseLicense(ctx)
+ systemSetting, err := p.store.GetSystemSetting(ctx)
if err != nil {
- return nil, errors.Wrapf(err, "failed to load enterprise license from settings")
+ return nil, errors.Wrapf(err, "failed to get system setting")
}
+ licenseStr := systemSetting.License
if licenseStr != "" {
license, err := p.parseLicense(ctx, licenseStr)
if err != nil {
@@ -153,10 +154,11 @@ func (p *Provider) parseClaims(ctx context.Context, claim *claims) (*v1pb.Subscr
planType := v1pb.PlanType(v)
if claim.WorkspaceID != "" && planType == v1pb.PlanType_ENTERPRISE && !claim.Trialing {
- workspaceID, err := p.store.GetWorkspaceID(ctx)
+ systemSetting, err := p.store.GetSystemSetting(ctx)
if err != nil {
- return nil, errors.Wrapf(err, "failed to get workspace id from setting")
+ return nil, errors.Wrapf(err, "failed to get system setting")
}
+ workspaceID := systemSetting.WorkspaceId
if workspaceID != claim.WorkspaceID {
return nil, common.Errorf(common.Invalid, "the workspace id not match")
}
diff --git a/backend/plugin/schema/mssql/get_database_metadata.go b/backend/plugin/schema/mssql/get_database_metadata.go
index a957f599e3..fd2af48f7e 100644
--- a/backend/plugin/schema/mssql/get_database_metadata.go
+++ b/backend/plugin/schema/mssql/get_database_metadata.go
@@ -715,11 +715,7 @@ func (e *metadataExtractor) extractColumn(ctx parser.IColumn_definitionContext,
// Handle nullability
if nullNotNull := constraint.Null_notnull(); nullNotNull != nil {
- if nullNotNull.NOT() != nil {
- column.Nullable = false
- } else {
- column.Nullable = true
- }
+ column.Nullable = nullNotNull.NOT() == nil
}
// Handle PRIMARY KEY
diff --git a/backend/plugin/schema/mysql/get_database_metadata.go b/backend/plugin/schema/mysql/get_database_metadata.go
index ef6cdbd7ca..24ec3ed8aa 100644
--- a/backend/plugin/schema/mysql/get_database_metadata.go
+++ b/backend/plugin/schema/mysql/get_database_metadata.go
@@ -374,11 +374,7 @@ func (e *metadataExtractor) extractFieldAttributes(ctx mysql.IFieldDefinitionCon
// Check for NULL/NOT NULL
if attr.NullLiteral() != nil {
- if attr.NOT_SYMBOL() != nil {
- column.Nullable = false
- } else {
- column.Nullable = true
- }
+ column.Nullable = attr.NOT_SYMBOL() == nil
}
// Check for DEFAULT value
diff --git a/backend/runner/metricreport/reporter.go b/backend/runner/metricreport/reporter.go
index cdaf70a9a9..ff17a36221 100644
--- a/backend/runner/metricreport/reporter.go
+++ b/backend/runner/metricreport/reporter.go
@@ -191,10 +191,11 @@ func (m *Reporter) isMetricCollectionEnabled(ctx context.Context) bool {
// Identify will identify the workspace and update the subscription plan.
func (m *Reporter) identify(ctx context.Context) (string, error) {
- workspaceID, err := m.store.GetWorkspaceID(ctx)
+ systemSetting, err := m.store.GetSystemSetting(ctx)
if err != nil {
- return "", err
+ return "", errors.Wrap(err, "failed to get system setting")
}
+ workspaceID := systemSetting.WorkspaceId
subscription := m.licenseService.LoadSubscription(ctx)
plan := subscription.Plan.String()
orgID := ""
@@ -251,10 +252,13 @@ func (m *Reporter) identify(ctx context.Context) (string, error) {
// Report will report a metric.
func (m *Reporter) Report(ctx context.Context, metric *metric.Metric) {
- workspaceID, err := m.store.GetWorkspaceID(ctx)
+ systemSetting, err := m.store.GetSystemSetting(ctx)
+ var workspaceID string
if err != nil {
- slog.Error("failed to find the workspace id", log.BBError(err))
- return
+ slog.Error("failed to get system setting", log.BBError(err))
+ // On error, workspaceID remains its zero value (empty string), matching original behavior.
+ } else {
+ workspaceID = systemSetting.WorkspaceId
}
m.reportMetric(workspaceID, metric)
}
diff --git a/backend/store/setting.go b/backend/store/setting.go
index cf7d3100fa..b55d2ee8d9 100644
--- a/backend/store/setting.go
+++ b/backend/store/setting.go
@@ -29,21 +29,6 @@ type SettingMessage struct {
Value string
}
-func (s *Store) GetPasswordRestriction(ctx context.Context) (*storepb.WorkspaceProfileSetting_PasswordRestriction, error) {
- passwordRestriction := &storepb.WorkspaceProfileSetting_PasswordRestriction{
- MinLength: 8,
- }
- workspaceProfile, err := s.GetWorkspaceGeneralSetting(ctx)
- if err != nil {
- return nil, err
- }
- if workspaceProfile == nil || workspaceProfile.PasswordRestriction == nil {
- return passwordRestriction, nil
- }
-
- return workspaceProfile.PasswordRestriction, nil
-}
-
// GetWorkspaceGeneralSetting gets the workspace general setting payload.
func (s *Store) GetWorkspaceGeneralSetting(ctx context.Context) (*storepb.WorkspaceProfileSetting, error) {
setting, err := s.GetSettingV2(ctx, storepb.SettingName_WORKSPACE_PROFILE)
@@ -93,24 +78,6 @@ func (s *Store) GetSystemSetting(ctx context.Context) (*storepb.SystemSetting, e
return payload, nil
}
-// GetWorkspaceID finds the workspace id in setting bb.workspace.id.
-func (s *Store) GetWorkspaceID(ctx context.Context) (string, error) {
- systemSetting, err := s.GetSystemSetting(ctx)
- if err != nil {
- return "", errors.Wrap(err, "failed to get system setting")
- }
- return systemSetting.WorkspaceId, nil
-}
-
-// GetEnterpriseLicense retrieves the enterprise license from SYSTEM setting.
-func (s *Store) GetEnterpriseLicense(ctx context.Context) (string, error) {
- systemSetting, err := s.GetSystemSetting(ctx)
- if err != nil {
- return "", errors.Wrap(err, "failed to get system setting")
- }
- return systemSetting.License, nil
-}
-
// UpsertEnterpriseLicense updates the enterprise license in SYSTEM setting.
func (s *Store) UpsertEnterpriseLicense(ctx context.Context, license string) error {
setting, err := s.GetSettingV2(ctx, storepb.SettingName_SYSTEM)
@@ -333,61 +300,6 @@ func (s *Store) UpsertSettingV2(ctx context.Context, update *SetSettingMessage)
return &setting, nil
}
-// CreateSettingIfNotExistV2 creates a new setting only if the named setting doesn't exist.
-func (s *Store) CreateSettingIfNotExistV2(ctx context.Context, create *SettingMessage) (*SettingMessage, bool, error) {
- if v, ok := s.settingCache.Get(create.Name); ok && s.enableCache {
- return v, false, nil
- }
-
- tx, err := s.GetDB().BeginTx(ctx, nil)
- if err != nil {
- return nil, false, errors.Wrap(err, "failed to begin transaction")
- }
- defer tx.Rollback()
- settings, err := listSettingV2Impl(ctx, tx, &FindSettingMessage{Name: &create.Name})
- if err != nil {
- return nil, false, errors.Wrap(err, "failed to list settings")
- }
- if len(settings) > 1 {
- return nil, false, errors.Errorf("found settings for setting name: %v", create.Name)
- }
- if len(settings) == 1 {
- // Don't create setting if the named setting already exists.
- return settings[0], false, nil
- }
-
- q := qb.Q().Space(`
- INSERT INTO setting (name, value)
- VALUES (?, ?)
- RETURNING name, value
- `, create.Name.String(), create.Value)
-
- query, args, err := q.ToSQL()
- if err != nil {
- return nil, false, errors.Wrapf(err, "failed to build sql")
- }
-
- var setting SettingMessage
- var nameString string
- if err := tx.QueryRowContext(ctx, query, args...).Scan(
- &nameString,
- &setting.Value,
- ); err != nil {
- return nil, false, err
- }
- value, ok := storepb.SettingName_value[nameString]
- if !ok {
- return nil, false, errors.Errorf("invalid setting name string: %s", nameString)
- }
- setting.Name = storepb.SettingName(value)
-
- if err := tx.Commit(); err != nil {
- return nil, false, errors.Wrap(err, "failed to commit transaction")
- }
- s.settingCache.Add(setting.Name, &setting)
- return &setting, true, nil
-}
-
// DeleteSettingV2 deletes a setting by the name.
func (s *Store) DeleteSettingV2(ctx context.Context, name storepb.SettingName) error {
q := qb.Q().Space("DELETE FROM setting WHERE name = ?", name.String())