Skip to content

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

feat: use `ResourceIdField` in SSO (#4865)

boojack85aea43f820b
diff --git a/frontend/src/components/EnvironmentForm.vue b/frontend/src/components/EnvironmentForm.vue
index 6e73364e0c..f8c70e0ac9 100644
--- a/frontend/src/components/EnvironmentForm.vue
+++ b/frontend/src/components/EnvironmentForm.vue
@@ -22,7 +22,7 @@
ref="resourceIdField"
resource="environment"
:readonly="!create"
- :default-value="state.environment.resourceId"
+ :value="state.environment.resourceId"
:resource-title="state.environment.name"
:validator="validateResourceId"
/>
@@ -487,7 +487,7 @@ const validateResourceId = async (resourceId: ResourceId) => {
);
if (env) {
return t("resource-id.validation.duplicated", {
- resource: t(`common.environment`),
+ resource: t("resource.environment"),
});
}
} catch (error) {
diff --git a/frontend/src/components/IdentityProviderCreateForm.vue b/frontend/src/components/IdentityProviderCreateForm.vue
index b0d2188188..745dae3471 100644
--- a/frontend/src/components/IdentityProviderCreateForm.vue
+++ b/frontend/src/components/IdentityProviderCreateForm.vue
@@ -38,33 +38,34 @@
</div>
</div>
- <!-- OAuth2 form group -->
+ <!-- OAuth2 templates group -->
<div
- v-if="state.type === IdentityProviderType.OAUTH2"
+ v-if="isCreating && state.type === IdentityProviderType.OAUTH2"
class="w-full flex flex-col justify-start items-start space-y-3"
>
- <template v-if="isCreating">
- <p class="textlabel mt-4">
- {{ $t("settings.sso.form.use-template") }}
- </p>
- <div class="w-full flex flex-row justify-start items-start space-x-2">
- <label
- v-for="template in templateList"
- :key="template.title"
- class="w-24 h-24 border rounded-md flex flex-col justify-center items-center cursor-pointer hover:bg-gray-100"
- :for="`radio-${template.title}`"
- @click="handleTemplateSelect(template)"
- >
- <span>{{ template.title }}</span>
- <input
- :id="`radio-${template.title}`"
- type="radio"
- class="btn mt-4"
- :checked="selectedTemplate?.title === template.title"
- />
- </label>
- </div>
- </template>
+ <p class="textlabel mt-4">
+ {{ $t("settings.sso.form.use-template") }}
+ </p>
+ <div class="w-full flex flex-row justify-start items-start space-x-2">
+ <label
+ v-for="template in templateList"
+ :key="template.title"
+ class="w-24 h-24 border rounded-md flex flex-col justify-center items-center cursor-pointer hover:bg-gray-100"
+ :for="`radio-${template.title}`"
+ @click="handleTemplateSelect(template)"
+ >
+ <span>{{ template.title }}</span>
+ <input
+ :id="`radio-${template.title}`"
+ type="radio"
+ class="btn mt-4"
+ :checked="selectedTemplate?.title === template.title"
+ />
+ </label>
+ </div>
+ </div>
+
+ <div class="w-full flex flex-col justify-start items-start space-y-3">
<p class="text-lg font-medium !mt-4">
{{ $t("settings.sso.form.basic-information") }}
</p>
@@ -80,20 +81,13 @@
class="textfield mt-1 w-full"
:placeholder="$t('settings.sso.form.name-description')"
/>
- </div>
- <div
- v-if="isCreating"
- class="w-full flex flex-col justify-start items-start"
- >
- <p class="textlabel">
- {{ $t("settings.sso.form.resource-id") }}
- <span class="text-red-600">*</span>
- </p>
- <input
- v-model="identityProvider.name"
- type="text"
- class="textfield mt-1 w-full"
- :placeholder="$t('settings.sso.form.resource-id-description')"
+ <ResourceIdField
+ ref="resourceIdField"
+ resource="idp"
+ :readonly="!isCreating"
+ :value="resourceId"
+ :resource-title="identityProvider.title"
+ :validator="validateResourceId"
/>
</div>
<div class="w-full flex flex-col justify-start items-start">
@@ -108,7 +102,13 @@
:placeholder="$t('settings.sso.form.domain-description')"
/>
</div>
+ </div>
+ <!-- OAuth2 form group -->
+ <div
+ v-if="state.type === IdentityProviderType.OAUTH2"
+ class="w-full flex flex-col justify-start items-start space-y-3"
+ >
<div class="w-full flex flex-col justify-start items-start">
<p class="text-lg font-medium mt-2">
{{ $t("settings.sso.form.identity-provider-information") }}
@@ -316,50 +316,6 @@
v-else-if="state.type === IdentityProviderType.OIDC"
class="w-full flex flex-col justify-start items-start space-y-3"
>
- <p class="text-lg font-medium !mt-4">
- {{ $t("settings.sso.form.basic-information") }}
- </p>
- <div class="w-full flex flex-col justify-start items-start">
- <p class="textlabel">
- {{ $t("settings.sso.form.name") }}
- <span class="text-red-600">*</span>
- </p>
- <input
- v-model="identityProvider.title"
- :disabled="!allowEdit"
- type="text"
- class="textfield mt-1 w-full"
- :placeholder="$t('settings.sso.form.name-description')"
- />
- </div>
- <div
- v-if="isCreating"
- class="w-full flex flex-col justify-start items-start"
- >
- <p class="textlabel">
- {{ $t("settings.sso.form.resource-id") }}
- <span class="text-red-600">*</span>
- </p>
- <input
- v-model="identityProvider.name"
- type="text"
- class="textfield mt-1 w-full"
- :placeholder="$t('settings.sso.form.resource-id-description')"
- />
- </div>
- <div class="w-full flex flex-col justify-start items-start">
- <p class="textlabel">
- {{ $t("settings.sso.form.domain") }}
- </p>
- <input
- v-model="identityProvider.domain"
- :disabled="!allowEdit"
- type="text"
- class="textfield mt-1 w-full"
- :placeholder="$t('settings.sso.form.domain-description')"
- />
- </div>
-
<div class="w-full flex flex-col justify-start items-start">
<p class="text-lg font-medium mt-2">
{{ $t("settings.sso.form.identity-provider-information") }}
@@ -589,7 +545,7 @@
<script lang="ts" setup>
import { cloneDeep, head, isEqual } from "lodash-es";
-import { ClientError } from "nice-grpc-common";
+import { ClientError, Status } from "nice-grpc-common";
import { toClipboard } from "@soerenmartius/vue3-clipboard";
import { useI18n } from "vue-i18n";
import { computed, reactive, defineProps, ref, onMounted, watch } from "vue";
@@ -609,10 +565,16 @@ import {
identityProviderTypeToString,
openWindowForSSO,
} from "@/utils";
-import { OAuthWindowEventPayload } from "@/types";
+import { OAuthWindowEventPayload, ResourceId } from "@/types";
import { identityProviderClient } from "@/grpcweb";
import { State } from "@/types/proto/v1/common";
import { useRouter } from "vue-router";
+import {
+ getIdentityProviderResourceId,
+ idpNamePrefix,
+} from "@/store/modules/v1/common";
+import ResourceIdField from "./ResourceIdField.vue";
+import { getErrorCode } from "@/utils/grpcweb";
interface LocalState {
type: IdentityProviderType;
@@ -642,6 +604,7 @@ const configForOIDC = ref<OIDCIdentityProviderConfig>(
fieldMapping: FieldMapping.fromPartial({}),
})
);
+const resourceIdField = ref<InstanceType<typeof ResourceIdField>>();
const selectedTemplate = ref<IdentityProviderTemplate>();
const currentIdentityProvider = computed(() => {
@@ -676,6 +639,12 @@ const isDeleted = computed(() => {
return currentIdentityProvider.value?.state === State.DELETED;
});
+const resourceId = computed(() => {
+ return isCreating.value
+ ? ""
+ : getIdentityProviderResourceId(identityProvider.value.name);
+});
+
const userDocLink = computed(() => {
if (state.type === IdentityProviderType.OAUTH2) {
return "https://www.bytebase.com/docs/administration/sso/oauth2?source=console";
@@ -701,7 +670,7 @@ const originIdentityProvider = computed(() => {
});
const isFormCompleted = computed(() => {
- if (!identityProvider.value.name || !identityProvider.value.title) {
+ if (!identityProvider.value.title) {
return false;
}
@@ -738,7 +707,11 @@ const allowEdit = computed(() => {
});
const allowCreate = computed(() => {
- if (!isFormCompleted.value) {
+ if (
+ !isFormCompleted.value ||
+ !resourceIdField.value?.resourceId ||
+ !resourceIdField.value?.isValidated
+ ) {
return false;
}
return true;
@@ -847,6 +820,27 @@ const copyRedirectUrl = () => {
});
};
+const validateResourceId = async (resourceId: ResourceId) => {
+ if (!resourceId) {
+ return;
+ }
+
+ try {
+ const idp = await identityProviderStore.getOrFetchIdentityProviderByName(
+ idpNamePrefix + resourceId
+ );
+ if (idp) {
+ return t("resource-id.validation.duplicated", {
+ resource: t("resource.idp"),
+ });
+ }
+ } catch (error) {
+ if (getErrorCode(error) !== Status.NOT_FOUND) {
+ throw error;
+ }
+ }
+};
+
const testConnection = () => {
if (
state.type === IdentityProviderType.OAUTH2 ||
@@ -863,7 +857,6 @@ const handleTemplateSelect = (template: IdentityProviderTemplate) => {
selectedTemplate.value = template;
identityProvider.value.title = template.title;
- identityProvider.value.name = template.name;
identityProvider.value.domain = template.domain;
if (template.type === IdentityProviderType.OAUTH2) {
configForOAuth2.value = {
@@ -938,6 +931,7 @@ const handleDiscardChangesButtonClick = async () => {
const handleCreateButtonClick = async () => {
const identityProviderCreate: IdentityProvider = {
...identityProvider.value,
+ name: resourceIdField.value?.resourceId as string,
type: state.type,
config: {},
};
diff --git a/frontend/src/components/ResourceIdField.vue b/frontend/src/components/ResourceIdField.vue
index e3e40a7ecc..a3649b5f7d 100644
--- a/frontend/src/components/ResourceIdField.vue
+++ b/frontend/src/components/ResourceIdField.vue
@@ -2,7 +2,7 @@
<div
v-bind="$attrs"
class="w-full max-w-full"
- :class="[shouldShowResourceIdField ? 'mt-6' : '']"
+ :class="[shouldShowResourceIdField ? 'mt-4' : '']"
>
<template v-if="!shouldShowResourceIdField">
<p v-if="state.resourceId" class="w-full text-gray-500 text-sm pt-2">
@@ -64,7 +64,7 @@ const characters = "abcdefghijklmnopqrstuvwxyz1234567890";
const resourceIdPattern = /^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$/;
-interface ValidateMessage {
+interface ValidatedMessage {
type: "warning" | "error";
message: string;
}
@@ -72,21 +72,21 @@ interface ValidateMessage {
interface LocalState {
resourceId: string;
isResourceIdChanged: boolean;
- validatedMessages: ValidateMessage[];
+ validatedMessages: ValidatedMessage[];
}
-type ResourceType = "environment";
+type ResourceType = "environment" | "idp";
const props = withDefaults(
defineProps<{
resource: ResourceType;
- defaultValue: string;
+ value: string;
resourceTitle: string;
readonly: boolean;
validator: (resourceId: ResourceId) => Promise<string | undefined>;
}>(),
{
- defaultValue: "",
+ value: "",
resourceTitle: "",
readonly: false,
}
@@ -94,7 +94,7 @@ const props = withDefaults(
const { t } = useI18n();
const state = reactive<LocalState>({
- resourceId: props.defaultValue,
+ resourceId: props.value,
isResourceIdChanged: false,
validatedMessages: [],
});
@@ -103,6 +103,8 @@ const getPrefix = (resource: string) => {
switch (resource) {
case "environment":
return "env";
+ case "idp":
+ return "idp";
default:
return "";
}
@@ -110,7 +112,7 @@ const getPrefix = (resource: string) => {
const randomSuffix = randomString(4).toLowerCase();
const resourceName = computed(() => {
- return t(`common.${props.resource}`);
+ return t(`resource.${props.resource}`);
});
const shouldShowResourceIdField = computed(() => {
@@ -118,14 +120,21 @@ const shouldShowResourceIdField = computed(() => {
});
watch(
- () => props.resourceTitle,
+ () => props.value,
(newValue) => {
+ state.resourceId = newValue;
+ }
+);
+
+watch(
+ () => props.resourceTitle,
+ (resourceTitle) => {
if (props.readonly) {
return;
}
- if (!state.isResourceIdChanged) {
- const formatedTitle = newValue
+ if (!state.isResourceIdChanged && resourceTitle) {
+ const formatedTitle = resourceTitle
.toLowerCase()
.split("")
.map((char) => {
@@ -144,6 +153,9 @@ watch(
formatedTitle || randomString(4).toLowerCase()
}-${randomSuffix}`;
}
+ },
+ {
+ immediate: true,
}
);
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 6d3da14cce..c38fd2fc9c 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1888,6 +1888,10 @@
"sensitive-data": {
"self": "Sensitive Data"
},
+ "resource": {
+ "environment": "Environment",
+ "idp": "Identity Provider"
+ },
"resource-id": {
"self": "{resource} ID",
"description": "The {resource} ID is a unique identifier. You cannot change it after created.",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index cfae287041..ff69a307d1 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1887,6 +1887,10 @@
"sensitive-data": {
"self": "敏感数据"
},
+ "resource": {
+ "environment": "环境",
+ "idp": "身份提供商"
+ },
"resource-id": {
"self": "{resource} ID",
"description": "{resource} ID 是一个唯一的标识符。创建之后无法修改。",
diff --git a/frontend/src/store/modules/v1/common.ts b/frontend/src/store/modules/v1/common.ts
index 4a6585baad..8d7bcb671e 100644
--- a/frontend/src/store/modules/v1/common.ts
+++ b/frontend/src/store/modules/v1/common.ts
@@ -1,7 +1,8 @@
-import { UNKNOWN_ID } from "@/types";
+import { ResourceId, UNKNOWN_ID } from "@/types";
export const userNamePrefix = "users/";
export const environmentNamePrefix = "environments/";
+export const idpNamePrefix = "idps/";
export const getNameParentTokens = (
name: string,
@@ -36,3 +37,8 @@ export const getEnvironmentId = (name: string): number => {
const environmentId = Number(tokens[0] || UNKNOWN_ID);
return environmentId;
};
+
+export const getIdentityProviderResourceId = (name: string): ResourceId => {
+ const tokens = getNameParentTokens(name, [idpNamePrefix]);
+ return tokens[0];
+};