Skip to content

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

fix: login restriction and missing feature check (#19721)

ecmadao5ae3fbf92851
diff --git a/backend/api/auth/header.go b/backend/api/auth/header.go
index 0ec9cb975c..172f5e0c57 100644
--- a/backend/api/auth/header.go
+++ b/backend/api/auth/header.go
@@ -76,6 +76,10 @@ func GetRefreshTokenDuration(ctx context.Context, store *store.Store, licenseSer
if workspaceProfile.GetRefreshTokenDuration().GetSeconds() > 0 {
refreshTokenDuration = workspaceProfile.GetRefreshTokenDuration().AsDuration()
}
+
+ if err := licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_PASSWORD_RESTRICTIONS); err != nil {
+ return refreshTokenDuration
+ }
// 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
diff --git a/backend/api/v1/actuator_service.go b/backend/api/v1/actuator_service.go
index 689a2b9a6a..9e1d4ec1e7 100644
--- a/backend/api/v1/actuator_service.go
+++ b/backend/api/v1/actuator_service.go
@@ -153,15 +153,18 @@ func (s *ActuatorService) SetupSample(
return connect.NewResponse(&emptypb.Empty{}), nil
}
-var defaultAccountRestriction = &v1pb.Restriction{
- DisallowSignup: true,
- DisallowPasswordSignin: true,
- PasswordRestriction: &v1pb.WorkspaceProfileSetting_PasswordRestriction{
- MinLength: 8,
- },
-}
-
func (s *ActuatorService) getServerInfo(ctx context.Context, workspaceID string) (*v1pb.ActuatorInfo, error) {
+ restriction, err := getAccountRestriction(
+ ctx,
+ s.store,
+ s.licenseService,
+ s.profile.SaaS,
+ workspaceID,
+ )
+ if err != nil {
+ return nil, err
+ }
+
serverInfo := v1pb.ActuatorInfo{
Version: s.profile.Version,
GitCommit: s.profile.GitCommit,
@@ -171,7 +174,7 @@ func (s *ActuatorService) getServerInfo(ctx context.Context, workspaceID string)
Docker: s.profile.IsDocker,
ExternalUrlFromFlag: s.profile.ExternalURL != "",
ReplicaCount: int32(s.licenseService.CountActiveReplicas(ctx)),
- Restriction: defaultAccountRestriction,
+ Restriction: restriction,
}
if workspaceID != "" {
@@ -200,20 +203,14 @@ func (s *ActuatorService) getServerInfo(ctx context.Context, workspaceID string)
}
serverInfo.ActivatedUserCount = int32(activeEndUserCount)
- setting, err := s.store.GetWorkspaceProfileSetting(ctx, workspaceID)
- if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find workspace setting"))
- }
- serverInfo.Restriction = &v1pb.Restriction{
- PasswordRestriction: convertToV1PasswordRestriction(setting.GetPasswordRestriction()),
- DisallowSignup: setting.DisallowSignup,
- DisallowPasswordSignin: setting.DisallowPasswordSignin,
- }
-
// Check if sample instances are available
hasSampleInstances, _ := s.store.HasSampleInstances(ctx, workspaceID)
serverInfo.EnableSample = hasSampleInstances
+ setting, err := s.store.GetWorkspaceProfileSetting(ctx, workspaceID)
+ if err != nil {
+ return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find workspace setting"))
+ }
// Prefer command-line flag over database value for external URL
externalURL := setting.ExternalUrl
if s.profile.ExternalURL != "" {
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index 4667eec6d7..7ae27eebeb 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -129,19 +129,25 @@ func (s *AuthService) needResetPassword(ctx context.Context, user *store.UserMes
if user.Type != storepb.PrincipalType_END_USER {
return false
}
+
if err := s.licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_PASSWORD_RESTRICTIONS); err != nil {
return false
}
- setting, err := s.store.GetWorkspaceProfileSetting(ctx, workspaceID)
+ restriction, err := getAccountRestriction(
+ ctx,
+ s.store,
+ s.licenseService,
+ s.profile.SaaS,
+ workspaceID,
+ )
if err != nil {
- slog.Error("failed to get workspace setting", log.BBError(err))
+ slog.Error("failed to get workspace restriction", log.BBError(err), slog.String("workspace", workspaceID))
return false
}
- passwordRestriction := setting.GetPasswordRestriction()
if user.Profile.LastLoginTime == nil {
- if !passwordRestriction.GetRequireResetPasswordForFirstLogin() {
+ if !restriction.PasswordRestriction.GetRequireResetPasswordForFirstLogin() {
return false
}
count, err := s.store.CountActiveEndUsersPerWorkspace(ctx, workspaceID)
@@ -153,12 +159,12 @@ func (s *AuthService) needResetPassword(ctx context.Context, user *store.UserMes
return count > 1
}
- if passwordRestriction.GetPasswordRotation() != nil {
+ if restriction.PasswordRestriction.GetPasswordRotation() != nil {
lastChangePasswordTime := user.CreatedAt
if user.Profile.LastChangePasswordTime != nil {
lastChangePasswordTime = user.Profile.LastChangePasswordTime.AsTime()
}
- if lastChangePasswordTime.Add(passwordRestriction.GetPasswordRotation().AsDuration()).Before(time.Now()) {
+ if lastChangePasswordTime.Add(restriction.PasswordRestriction.GetPasswordRotation().AsDuration()).Before(time.Now()) {
return true
}
}
@@ -218,23 +224,20 @@ func (s *AuthService) Signup(ctx context.Context, req *connect.Request[v1pb.Sign
workspaceID = existingWsID
}
- if workspaceID != "" {
- if !s.profile.SaaS {
- if err := s.licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_DISALLOW_SELF_SERVICE_SIGNUP); err == nil {
- setting, err := s.store.GetWorkspaceProfileSetting(ctx, workspaceID)
- if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get workspace setting"))
- }
- if setting.DisallowSignup {
- return nil, connect.NewError(connect.CodePermissionDenied, errors.Errorf("sign up is disallowed for this workspace"))
- }
- }
- }
-
- if err := validatePassword(ctx, s.store, workspaceID, request.Password); err != nil {
- return nil, err
- }
- } else if err := validatePasswordWithRestriction(request.Password, convertToStorePasswordRestriction(defaultAccountRestriction.PasswordRestriction)); err != nil {
+ restriction, err := getAccountRestriction(
+ ctx,
+ s.store,
+ s.licenseService,
+ s.profile.SaaS,
+ workspaceID,
+ )
+ if err != nil {
+ return nil, err
+ }
+ if restriction.DisallowSignup {
+ return nil, connect.NewError(connect.CodePermissionDenied, errors.Errorf("sign up is disallowed for this workspace %v", workspaceID))
+ }
+ if err := validatePasswordWithRestriction(request.Password, convertToStorePasswordRestriction(restriction.PasswordRestriction)); err != nil {
return nil, err
}
@@ -921,15 +924,19 @@ func (s *AuthService) validateLoginPermissions(ctx context.Context, user *store.
return nil
}
- loginViaIDP := request.GetIdpName() != ""
-
- // Check disallow password signin
- if err := s.licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_DISALLOW_PASSWORD_SIGNIN); err == nil {
- setting, err := s.store.GetWorkspaceProfileSetting(ctx, workspaceID)
+ // Check disallow password signin when not through IDP
+ if request.GetIdpName() == "" {
+ restriction, err := getAccountRestriction(
+ ctx,
+ s.store,
+ s.licenseService,
+ s.profile.SaaS,
+ workspaceID,
+ )
if err != nil {
- return connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find workspace setting"))
+ return err
}
- if setting.DisallowPasswordSignin && !loginViaIDP {
+ if restriction.DisallowPasswordSignin {
return connect.NewError(connect.CodePermissionDenied, errors.Errorf("password signin is disallowed"))
}
}
@@ -1331,3 +1338,52 @@ func (s *AuthService) accountToUser(ctx context.Context, account *store.AccountM
MemberDeleted: account.MemberDeleted,
}, nil
}
+
+func getAccountRestriction(
+ ctx context.Context,
+ stores *store.Store,
+ licenseService *enterprise.LicenseService,
+ saas bool,
+ workspaceID string,
+) (*v1pb.Restriction, error) {
+ defaultPasswordRestriction := &v1pb.WorkspaceProfileSetting_PasswordRestriction{
+ MinLength: 8,
+ }
+ restriction := &v1pb.Restriction{
+ DisallowSignup: false,
+ DisallowPasswordSignin: false,
+ PasswordRestriction: defaultPasswordRestriction,
+ }
+
+ if workspaceID != "" {
+ setting, err := stores.GetWorkspaceProfileSetting(ctx, workspaceID)
+ if err != nil {
+ return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find profile setting for workspace %v", workspaceID))
+ }
+
+ restriction = &v1pb.Restriction{
+ PasswordRestriction: convertToV1PasswordRestriction(setting.GetPasswordRestriction()),
+ DisallowSignup: setting.DisallowSignup,
+ DisallowPasswordSignin: setting.DisallowPasswordSignin,
+ }
+
+ // Override if features are not enabled
+ if licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_DISALLOW_SELF_SERVICE_SIGNUP) != nil {
+ restriction.DisallowSignup = false
+ }
+ if licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_DISALLOW_PASSWORD_SIGNIN) != nil {
+ restriction.DisallowPasswordSignin = false
+ }
+ if licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_PASSWORD_RESTRICTIONS) != nil {
+ restriction.PasswordRestriction = defaultPasswordRestriction
+ }
+ }
+
+ // Override for SaaS
+ if saas {
+ restriction.DisallowSignup = true
+ restriction.DisallowPasswordSignin = true
+ }
+
+ return restriction, nil
+}
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.go
index ea1bf6e8e5..2fee30b3a4 100644
--- a/backend/api/v1/setting_service.go
+++ b/backend/api/v1/setting_service.go
@@ -208,7 +208,7 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *connect.Req
log.LogLevel.Set(level)
case "value.workspace_profile.disallow_signup":
if s.profile.SaaS {
- return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("feature %s is unavailable in current mode", settingName))
+ return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("the disallow_signup cannot be changed in SaaS mode"))
}
if payload.DisallowSignup {
if err := s.licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_DISALLOW_SELF_SERVICE_SIGNUP); err != nil {
@@ -218,7 +218,7 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *connect.Req
oldSetting.DisallowSignup = payload.DisallowSignup
case "value.workspace_profile.external_url":
if s.profile.SaaS {
- return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("feature %s is unavailable in current mode", settingName))
+ return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("the external_url cannot be changed in SaaS mode"))
}
// Prevent changing external URL via UI when it's set via command-line flag
if s.profile.ExternalURL != "" {
@@ -286,6 +286,9 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *connect.Req
case "value.workspace_profile.database_change_mode":
oldSetting.DatabaseChangeMode = payload.DatabaseChangeMode
case "value.workspace_profile.disallow_password_signin":
+ if s.profile.SaaS {
+ return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("the disallow_password_signin cannot be changed in SaaS mode"))
+ }
if payload.DisallowPasswordSignin {
// We should still allow users to turn it off.
if err := s.licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_DISALLOW_PASSWORD_SIGNIN); err != nil {
diff --git a/backend/api/v1/user_service.go b/backend/api/v1/user_service.go
index ef1c736ac7..420c1f8612 100644
--- a/backend/api/v1/user_service.go
+++ b/backend/api/v1/user_service.go
@@ -230,7 +230,7 @@ func (s *UserService) CreateUser(ctx context.Context, request *connect.Request[v
// Validate password.
password := request.Msg.User.Password
if password != "" {
- if err := validatePassword(ctx, s.store, workspaceID, password); err != nil {
+ if err := s.validatePassword(ctx, workspaceID, password); err != nil {
return nil, err
}
} else {
@@ -264,13 +264,18 @@ func (s *UserService) CreateUser(ctx context.Context, request *connect.Request[v
return connect.NewResponse(userResponse), nil
}
-func validatePassword(ctx context.Context, store *store.Store, workspace, password string) error {
- setting, err := store.GetWorkspaceProfileSetting(ctx, workspace)
+func (s *UserService) validatePassword(ctx context.Context, workspaceID, password string) error {
+ restriction, err := getAccountRestriction(
+ ctx,
+ s.store,
+ s.licenseService,
+ s.profile.SaaS,
+ workspaceID,
+ )
if err != nil {
- return connect.NewError(connect.CodeInternal, errors.Errorf("failed to get password restriction with error: %v", err))
+ return err
}
- passwordRestriction := setting.GetPasswordRestriction()
-
+ passwordRestriction := convertToStorePasswordRestriction(restriction.PasswordRestriction)
return validatePasswordWithRestriction(password, passwordRestriction)
}
@@ -364,7 +369,7 @@ func (s *UserService) UpdateUser(ctx context.Context, request *connect.Request[v
if user.Type != storepb.PrincipalType_END_USER {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("password can be mutated for end users only"))
}
- if err := validatePassword(ctx, s.store, common.GetWorkspaceIDFromContext(ctx), request.Msg.User.Password); err != nil {
+ if err := s.validatePassword(ctx, common.GetWorkspaceIDFromContext(ctx), request.Msg.User.Password); err != nil {
return nil, err
}
passwordPatch = &request.Msg.User.Password
diff --git a/frontend/src/components/GeneralSetting/GeneralSetting.vue b/frontend/src/components/GeneralSetting/GeneralSetting.vue
index 0ff6cb045a..ade38dbd85 100644
--- a/frontend/src/components/GeneralSetting/GeneralSetting.vue
+++ b/frontend/src/components/GeneralSetting/GeneralSetting.vue
@@ -25,7 +25,7 @@
/>
</div>
<BBAttention
- v-if="externalUrlFromFlag"
+ v-if="externalUrlFromFlag && !isSaaSMode"
:type="'info'"
class="mb-3"
:description="