Skip to content

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

feat(frontend): domain restriction setting (#12419)

boojack9f3dbb8b1755
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index 2d41942571..585db7ec7c 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -295,7 +295,7 @@ func (s *AuthService) UpdateUser(ctx context.Context, request *v1pb.UpdateUserRe
allowedDomains = setting.Domains
}
if err := validateEmail(request.User.Email, allowedDomains); err != nil {
- return nil, status.Errorf(codes.InvalidArgument, "invalid email %q format: %v", request.User.Email, err)
+ return nil, status.Errorf(codes.InvalidArgument, "invalid email %q, error: %v", request.User.Email, err)
}
user, err := s.store.GetUserByEmail(ctx, request.User.Email)
if err != nil {
@@ -849,7 +849,7 @@ func (s *AuthService) getOrCreateUserWithIDP(ctx context.Context, request *v1pb.
// If the email is still invalid, we will return an error.
if err := validateEmail(email, allowedDomains); err != nil {
- return nil, status.Errorf(codes.NotFound, "unable to identify the user by provider user info")
+ return nil, status.Errorf(codes.InvalidArgument, "invalid email %q, error: %v", email, err)
}
user, err := s.store.GetUserByEmail(ctx, email)
if err != nil {
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.go
index fb158ab928..e036bc7ea0 100644
--- a/backend/api/v1/setting_service.go
+++ b/backend/api/v1/setting_service.go
@@ -10,6 +10,7 @@ import (
"strings"
"time"
+ "github.com/asaskevich/govalidator"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
@@ -208,6 +209,18 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *v1pb.Update
payload.MaximumRoleExpiration = nil
}
}
+ if payload.Domains != nil && len(payload.Domains) > 0 {
+ for _, domain := range payload.Domains {
+ if !govalidator.IsDNSName(domain) {
+ return nil, status.Errorf(codes.InvalidArgument, "invalid domain: %s", domain)
+ }
+ }
+ }
+ if payload.EnforceIdentityDomain {
+ if payload.Domains == nil || len(payload.Domains) == 0 {
+ return nil, status.Errorf(codes.InvalidArgument, "enforce identity domain is enabled but no domain is set")
+ }
+ }
bytes, err := protojson.Marshal(payload)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to marshal setting for %s with error: %v", apiSettingName, err)
diff --git a/frontend/src/components/GeneralSetting/DomainRestrictionSetting.vue b/frontend/src/components/GeneralSetting/DomainRestrictionSetting.vue
new file mode 100644
index 0000000000..dbd2e0e67a
--- /dev/null
+++ b/frontend/src/components/GeneralSetting/DomainRestrictionSetting.vue
@@ -0,0 +1,112 @@
+<template>
+ <div class="mb-7 mt-4 lg:mt-0 border-t pt-4">
+ <p class="font-medium flex flex-row justify-start items-center">
+ <span class="mr-2">{{
+ $t("settings.general.workspace.domain-restriction.self")
+ }}</span>
+ <FeatureBadge feature="bb.feature.secure-token" />
+ </p>
+ <p class="text-sm text-gray-400 mt-1">
+ {{ $t("settings.general.workspace.domain-restriction.description") }}
+ </p>
+ <div class="w-full flex flex-col gap-2 mt-2">
+ <NInput
+ v-model:value="state.domain"
+ :readonly="!allowEdit"
+ :placeholder="
+ $t(
+ 'settings.general.workspace.domain-restriction.domain-input-placeholder'
+ )
+ "
+ type="text"
+ />
+ <div class="w-full flex flex-row justify-between items-center">
+ <NCheckbox
+ v-model:checked="state.enableRestriction"
+ :disabled="!state.domain"
+ :readonly="!allowEdit"
+ >
+ <p class="font-medium">
+ {{
+ $t(
+ "settings.general.workspace.domain-restriction.members-restriction.self"
+ )
+ }}
+ </p>
+ <p class="text-sm text-gray-400 leading-tight">
+ {{
+ $t(
+ "settings.general.workspace.domain-restriction.members-restriction.description"
+ )
+ }}
+ </p>
+ </NCheckbox>
+ <NButton
+ type="primary"
+ :disabled="!allowSaveUpdates"
+ @click="saveDomainRestrictionSettings"
+ >
+ {{ $t("common.update") }}
+ </NButton>
+ </div>
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { head, isEqual } from "lodash-es";
+import { NButton, NCheckbox, NInput } from "naive-ui";
+import { computed, reactive } from "vue";
+import { useI18n } from "vue-i18n";
+import { pushNotification } from "@/store";
+import { useSettingV1Store } from "@/store/modules/v1/setting";
+
+const getInitialState = (): LocalState => {
+ const defaultState: LocalState = {
+ domain: "",
+ enableRestriction: false,
+ };
+ if (
+ Array.isArray(settingV1Store.workspaceProfileSetting?.domains) &&
+ settingV1Store.workspaceProfileSetting?.domains.length > 0
+ ) {
+ defaultState.domain =
+ head(settingV1Store.workspaceProfileSetting?.domains) || "";
+ defaultState.enableRestriction =
+ settingV1Store.workspaceProfileSetting?.enforceIdentityDomain || false;
+ }
+ return defaultState;
+};
+
+interface LocalState {
+ domain: string;
+ enableRestriction: boolean;
+}
+
+defineProps<{
+ allowEdit: boolean;
+}>();
+
+const { t } = useI18n();
+const settingV1Store = useSettingV1Store();
+const state = reactive<LocalState>(getInitialState());
+
+const allowSaveUpdates = computed(() => {
+ return !isEqual(state, getInitialState());
+});
+
+const saveDomainRestrictionSettings = async () => {
+ if (state.domain.length === 0) {
+ state.enableRestriction = false;
+ }
+ await settingV1Store.updateWorkspaceProfile({
+ domains: [state.domain],
+ enforceIdentityDomain: state.enableRestriction,
+ });
+ pushNotification({
+ module: "bytebase",
+ style: "SUCCESS",
+ title: t("settings.general.workspace.config-updated"),
+ });
+};
+</script>
diff --git a/frontend/src/components/GeneralSetting/SecuritySetting.vue b/frontend/src/components/GeneralSetting/SecuritySetting.vue
index 01754157b4..435d6ab770 100644
--- a/frontend/src/components/GeneralSetting/SecuritySetting.vue
+++ b/frontend/src/components/GeneralSetting/SecuritySetting.vue
@@ -114,6 +114,7 @@
</div>
<SignInFrequencySetting :allow-edit="allowEdit" />
<MaximumRoleExpirationSetting :allow-edit="allowEdit" />
+ <DomainRestrictionSetting :allow-edit="allowEdit" />
</div>
</div>
@@ -141,6 +142,8 @@ import {
PolicyResourceType,
PolicyType,
} from "@/types/proto/v1/org_policy_service";
+import DomainRestrictionSetting from "./DomainRestrictionSetting.vue";
+import MaximumRoleExpirationSetting from "./MaximumRoleExpirationSetting.vue";
import SignInFrequencySetting from "./SignInFrequencySetting.vue";
interface LocalState {
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 2473b09cca..91b77c5401 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -653,6 +653,15 @@
"days": "Day(s)",
"never-expires": "Never expires"
},
+ "domain-restriction": {
+ "self": "Domain restriction",
+ "description": "Specify the domain that could be used as the email domain for workspace members. Leave it empty to allow any domain.",
+ "domain-input-placeholder": "Please input domain, e.g. example.com",
+ "members-restriction": {
+ "self": "Members restriction",
+ "description": "Restrict sign-in to users with email addresses from the specified domains."
+ }
+ },
"config-updated": "Configuration is updated.",
"plugin": {
"openai": {
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 44a2492811..29ee5efe9b 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -653,6 +653,15 @@
"days": "Días)",
"never-expires": "Nunca expira"
},
+ "domain-restriction": {
+ "self": "Restricción de dominio",
+ "description": "Especifique el dominio que podría usarse como dominio de correo electrónico para los miembros del espacio de trabajo. Déjelo vacío para permitir cualquier dominio.",
+ "domain-input-placeholder": "Por favor ingrese el dominio, p.e. ejemplo.com",
+ "members-restriction": {
+ "self": "Restricción de miembros",
+ "description": "Restrinja el inicio de sesión a usuarios con direcciones de correo electrónico de los dominios especificados."
+ }
+ },
"config-updated": "La configuración se ha actualizado.",
"plugin": {
"openai": {
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 9c79228e78..7cd838e859 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -653,6 +653,15 @@
"days": "日々)",
"never-expires": "期限切れなし"
},
+ "domain-restriction": {
+ "self": "ドメイン制限",
+ "description": "ワークスペース メンバーの電子メール ドメインとして使用できるドメインを指定します。任意のドメインを許可する場合は空のままにします。",
+ "domain-input-placeholder": "ドメインを入力してください。例: example.com",
+ "members-restriction": {
+ "self": "メンバー制限",
+ "description": "指定されたドメインの電子メール アドレスを持つユーザーのみにサインインを制限します。"
+ }
+ },
"config-updated": "構成が更新されました。",
"plugin": {
"openai": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index afd58cb978..06b80b7882 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -653,6 +653,15 @@
"days": "天数",
"never-expires": "永不过期"
},
+ "domain-restriction": {
+ "self": "域名限制",
+ "description": "可用于指定成员电子邮箱的域名。留空以允许任何邮箱。",
+ "domain-input-placeholder": "请输入域名,例如 example.com",
+ "members-restriction": {
+ "self": "成员邮箱限制",
+ "description": "仅允许具有指定域名的电子邮箱的用户进行登录或注册。"
+ }
+ },
"config-updated": "配置已更新。",
"plugin": {
"openai": {
diff --git a/go.mod b/go.mod
index d31ba92965..2cd55e9fe2 100644
--- a/go.mod
+++ b/go.mod
@@ -11,6 +11,7 @@ require (
github.com/ClickHouse/clickhouse-go/v2 v2.25.0
github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0
github.com/antlr4-go/antlr/v4 v4.13.1
+ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/aws/aws-sdk-go-v2 v1.27.0
github.com/aws/aws-sdk-go-v2/config v1.27.16
github.com/aws/aws-sdk-go-v2/credentials v1.17.16
diff --git a/go.sum b/go.sum
index 287b594ef7..d6bdb37e7a 100644
--- a/go.sum
+++ b/go.sum
@@ -702,6 +702,8 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A=
+github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
+github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=