Skip to content

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

feat: feature guard for policy (#532)

ecmadaoea18fa9ad41e
diff --git a/frontend/src/components/EnvironmentForm.vue b/frontend/src/components/EnvironmentForm.vue
index d4094f43e8..fc4fa0261d 100644
--- a/frontend/src/components/EnvironmentForm.vue
+++ b/frontend/src/components/EnvironmentForm.vue
@@ -3,7 +3,7 @@
<div class="grid grid-cols-1 gap-y-6 gap-x-4">
<div class="col-span-1">
<label for="name" class="textlabel">
- {{ $t('common.environments') }} <span class="text-red-600">*</span>
+ {{ $t("common.environments") }} <span class="text-red-600">*</span>
</label>
<BBTextField
class="mt-2 w-full"
@@ -14,9 +14,9 @@
/>
</div>
<div class="col-span-1">
- <label class="textlabel"> {{ $t('policy.approval.name') }} </label>
+ <label class="textlabel"> {{ $t("policy.approval.name") }} </label>
<div class="mt-1 textinfolabel">
- {{ $t('policy.approval.info') }}
+ {{ $t("policy.approval.info") }}
</div>
<div class="mt-4 flex flex-col space-y-4">
<div class="flex space-x-4">
@@ -25,18 +25,14 @@
name="manual-approval-always"
tabindex="-1"
type="radio"
- class="
- text-accent
- disabled:text-accent-disabled
- focus:ring-accent
- "
+ class="text-accent disabled:text-accent-disabled focus:ring-accent"
value="MANUAL_APPROVAL_ALWAYS"
:disabled="!allowEdit"
/>
<div class="-mt-0.5">
- <div class="textlabel">{{ $t('policy.approval.manual') }}</div>
+ <div class="textlabel">{{ $t("policy.approval.manual") }}</div>
<div class="mt-1 textinfolabel">
- {{ $t('policy.approval.manual-info') }}
+ {{ $t("policy.approval.manual-info") }}
</div>
</div>
</div>
@@ -46,43 +42,37 @@
name="manual-approval-never"
tabindex="-1"
type="radio"
- class="
- text-accent
- disabled:text-accent-disabled
- focus:ring-accent
- "
+ class="text-accent disabled:text-accent-disabled focus:ring-accent"
value="MANUAL_APPROVAL_NEVER"
:disabled="!allowEdit"
/>
<div class="-mt-0.5">
- <div class="textlabel">{{ $t('policy.approval.auto') }}</div>
+ <div class="textlabel">{{ $t("policy.approval.auto") }}</div>
<div class="mt-1 textinfolabel">
- {{ $t('policy.approval.auto-info') }}
+ {{ $t("policy.approval.auto-info") }}
</div>
</div>
</div>
</div>
</div>
<div class="col-span-1">
- <label class="textlabel"> {{ $t('policy.backup.name') }} </label>
+ <label class="textlabel"> {{ $t("policy.backup.name") }} </label>
<div class="mt-4 flex flex-col space-y-4">
<div class="flex space-x-4">
<input
v-model="state.backupPolicy.payload.schedule"
tabindex="-1"
type="radio"
- class="
- text-accent
- disabled:text-accent-disabled
- focus:ring-accent
- "
+ class="text-accent disabled:text-accent-disabled focus:ring-accent"
value="UNSET"
:disabled="!allowEdit"
/>
<div class="-mt-0.5">
- <div class="textlabel">{{ $t('policy.backup.not-enforced') }}</div>
+ <div class="textlabel">
+ {{ $t("policy.backup.not-enforced") }}
+ </div>
<div class="mt-1 textinfolabel">
- {{ $t('policy.backup.not-enforced-info') }}
+ {{ $t("policy.backup.not-enforced-info") }}
</div>
</div>
</div>
@@ -91,18 +81,14 @@
v-model="state.backupPolicy.payload.schedule"
tabindex="-1"
type="radio"
- class="
- text-accent
- disabled:text-accent-disabled
- focus:ring-accent
- "
+ class="text-accent disabled:text-accent-disabled focus:ring-accent"
value="DAILY"
:disabled="!allowEdit"
/>
<div class="-mt-0.5">
- <div class="textlabel">{{ $t('policy.backup.daily') }}</div>
+ <div class="textlabel">{{ $t("policy.backup.daily") }}</div>
<div class="mt-1 textinfolabel">
- {{ $t('policy.backup.daily-info') }}
+ {{ $t("policy.backup.daily-info") }}
</div>
</div>
</div>
@@ -111,18 +97,14 @@
v-model="state.backupPolicy.payload.schedule"
tabindex="-1"
type="radio"
- class="
- text-accent
- disabled:text-accent-disabled
- focus:ring-accent
- "
+ class="text-accent disabled:text-accent-disabled focus:ring-accent"
value="WEEKLY"
:disabled="!allowEdit"
/>
<div class="-mt-0.5">
- <div class="textlabel">{{ $t('policy.backup.weekly') }}</div>
+ <div class="textlabel">{{ $t("policy.backup.weekly") }}</div>
<div class="mt-1 textinfolabel">
- {{ $t('policy.backup.weekly-info') }}
+ {{ $t("policy.backup.weekly-info") }}
</div>
</div>
</div>
@@ -136,7 +118,7 @@
class="btn-normal py-2 px-4"
@click.prevent="$emit('cancel')"
>
- {{ $t('common.cancel') }}
+ {{ $t("common.cancel") }}
</button>
<button
type="submit"
@@ -144,7 +126,7 @@
:disabled="!allowCreate"
@click.prevent="createEnvironment"
>
- {{ $t('common.create') }}
+ {{ $t("common.create") }}
</button>
</div>
<!-- Update button group -->
@@ -155,7 +137,9 @@
:style="'ARCHIVE'"
:button-text="$t('environment.archive')"
:ok-text="$t('common.archive')"
- :confirm-title="$t('environment.archive') + ` '${state.environment.name}'?`"
+ :confirm-title="
+ $t('environment.archive') + ` '${state.environment.name}'?`
+ "
:confirm-description="$t('environment.archive-info')"
:require-confirm="true"
@confirm="archiveEnvironment"
@@ -167,7 +151,9 @@
:style="'RESTORE'"
:button-text="$t('environment.restore')"
:ok-text="$t('common.restore')"
- :confirm-title="$t('environment.restore') + ` '${state.environment.name}'?`"
+ :confirm-title="
+ $t('environment.restore') + ` '${state.environment.name}'?`
+ "
:confirm-description="''"
:require-confirm="true"
@confirm="restoreEnvironment"
@@ -181,7 +167,7 @@
:disabled="!valueChanged"
@click.prevent="revertEnvironment"
>
- {{ $t('common.revert') }}
+ {{ $t("common.revert") }}
</button>
<button
type="submit"
@@ -189,7 +175,7 @@
:disabled="!valueChanged"
@click.prevent="updateEnvironment"
>
- {{ $t('common.update') }}
+ {{ $t("common.update") }}
</button>
</div>
</div>
@@ -321,7 +307,7 @@ export default {
emit("update", patchedEnvironment);
}
- if (state.approvalPolicy != props.approvalPolicy) {
+ if (!isEqual(props.approvalPolicy, state.approvalPolicy)) {
emit(
"update-policy",
(state.environment as Environment).id,
@@ -330,7 +316,7 @@ export default {
);
}
- if (state.backupPolicy != props.backupPolicy) {
+ if (!isEqual(props.backupPolicy, state.backupPolicy)) {
emit(
"update-policy",
(state.environment as Environment).id,
diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.yml
index 782afd9f91..b3990b6e8a 100644
--- a/frontend/src/locales/en.yml
+++ b/frontend/src/locales/en.yml
@@ -1152,6 +1152,12 @@ subscription:
bb-feature-multi-tenancy:
title: Multi-tenancy
desc: Intelligent database management for tenants using identical schemas.
+ bb-feature-approval-policy:
+ title: Approval policy
+ desc: Approval policy controls whether the schama change task requires manual approval.
+ bb-feature-backup-policy:
+ title: Backup policy
+ desc: Backup policy will auto-backup your database based on the schedule.
bb-feature-backward-compatibility:
title: Backward compatibility
remind: MySQL and TiDB support schema change with backward compatibility. {upgrade} to open this feature.
diff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.yml
index 82ca5e1941..8bdacae67e 100644
--- a/frontend/src/locales/zh-CN.yml
+++ b/frontend/src/locales/zh-CN.yml
@@ -1021,6 +1021,12 @@ subscription:
bb-feature-multi-tenancy:
title: 多租户
desc: 「多租户功能」可以同时管理多个属于不同租户的同构数据库,帮您更统一和方便地对多个数据库进行管理和表结构变更。
+ bb-feature-approval-policy:
+ title: 审批策略
+ desc: 「审批策略」可以控制数据库 schema 的变更是否需要手动审批
+ bb-feature-backup-policy:
+ title: 备份策略
+ desc: 「备份策略」可以根据指定的周期自动备份数据库
bb-feature-backward-compatibility:
title: 向后兼容
remind: MySQL 和 TiDB 支持向后兼容的 schema 迁移,可以通过{upgrade}来开启该功能。
diff --git a/frontend/src/types/common.ts b/frontend/src/types/common.ts
index 6d20725ab5..5d2beeec6b 100644
--- a/frontend/src/types/common.ts
+++ b/frontend/src/types/common.ts
@@ -18,6 +18,7 @@ import { ProjectWebhook } from "./projectWebhook";
import { Repository } from "./repository";
import { VCS } from "./vcs";
import { DeploymentConfig } from "./deployment";
+import { DefaultApporvalPolicy } from "./policy";
// System bot id
export const SYSTEM_BOT_ID = 1;
@@ -310,7 +311,7 @@ export const unknown = (
environment: UNKNOWN_ENVIRONMENT,
type: "bb.policy.pipeline-approval",
payload: {
- value: "MANUAL_APPROVAL_ALWAYS",
+ value: DefaultApporvalPolicy,
},
};
@@ -680,7 +681,7 @@ export const empty = (
environment: EMPTY_ENVIRONMENT,
type: "bb.policy.pipeline-approval",
payload: {
- value: "MANUAL_APPROVAL_ALWAYS",
+ value: DefaultApporvalPolicy,
},
};
diff --git a/frontend/src/types/policy.ts b/frontend/src/types/policy.ts
index d0cc13378e..2f4f2426d5 100644
--- a/frontend/src/types/policy.ts
+++ b/frontend/src/types/policy.ts
@@ -12,12 +12,17 @@ export type PipelineApporvalPolicyPayload = {
value: PipelineApprovalPolicyValue;
};
+export const DefaultApporvalPolicy: PipelineApprovalPolicyValue =
+ "MANUAL_APPROVAL_ALWAYS";
+
export type BackupPlanPolicySchedule = "UNSET" | "DAILY" | "WEEKLY";
export type PolicyBackupPlanPolicyPayload = {
schedule: BackupPlanPolicySchedule;
};
+export const DefaultSchedulePolicy: BackupPlanPolicySchedule = "UNSET";
+
export type PolicyPayload =
| PipelineApporvalPolicyPayload
| PolicyBackupPlanPolicyPayload;
diff --git a/frontend/src/views/EnvironmentDashboard.vue b/frontend/src/views/EnvironmentDashboard.vue
index 0f6c71fc73..3ea240b28e 100644
--- a/frontend/src/views/EnvironmentDashboard.vue
+++ b/frontend/src/views/EnvironmentDashboard.vue
@@ -18,7 +18,7 @@
class="btn-normal py-2 px-4"
@click.prevent="discardReorder"
>
- {{ $t('common.cancel') }}
+ {{ $t("common.cancel") }}
</button>
<button
type="submit"
@@ -26,7 +26,7 @@
:disabled="!orderChanged"
@click.prevent="doReorder"
>
- {{ $t('common.apply') }}
+ {{ $t("common.apply") }}
</button>
</div>
<EnvironmentDetail
@@ -67,6 +67,12 @@
@cancel="state.showGuide = false"
>
</BBAlert>
+
+ <FeatureModal
+ v-if="state.missingRequiredFeature != undefined"
+ :feature="state.missingRequiredFeature"
+ @cancel="state.missingRequiredFeature = undefined"
+ />
</template>
<script lang="ts">
@@ -76,7 +82,14 @@ import { useRouter } from "vue-router";
import { array_swap } from "../utils";
import EnvironmentDetail from "../views/EnvironmentDetail.vue";
import EnvironmentForm from "../components/EnvironmentForm.vue";
-import { Environment, EnvironmentCreate, Policy, PolicyUpsert } from "../types";
+import {
+ Environment,
+ EnvironmentCreate,
+ Policy,
+ PolicyUpsert,
+ DefaultApporvalPolicy,
+ DefaultSchedulePolicy,
+} from "../types";
import { BBTabItem } from "../bbkit/types";
const DEFAULT_NEW_ENVIRONMENT: EnvironmentCreate = {
@@ -86,14 +99,14 @@ const DEFAULT_NEW_ENVIRONMENT: EnvironmentCreate = {
// The default value should be consistent with the GetDefaultPolicy from the backend.
const DEFAULT_NEW_APPROVAL_POLICY: PolicyUpsert = {
payload: {
- value: "MANUAL_APPROVAL_ALWAYS",
+ value: DefaultApporvalPolicy,
},
};
// The default value should be consistent with the GetDefaultPolicy from the backend.
const DEFAULT_NEW_BACKUP_PLAN_POLICY: PolicyUpsert = {
payload: {
- schedule: "UNSET",
+ schedule: DefaultSchedulePolicy,
},
};
@@ -103,6 +116,9 @@ interface LocalState {
showCreateModal: boolean;
reorder: boolean;
showGuide: boolean;
+ missingRequiredFeature?:
+ | "bb.feature.approval-policy"
+ | "bb.feature.backup-policy";
}
export default {
@@ -220,6 +236,21 @@ export default {
approvalPolicy: Policy,
backupPolicy: Policy
) => {
+ if (
+ approvalPolicy.payload.value !== DefaultApporvalPolicy &&
+ !store.getters["subscription/feature"]("bb.feature.approval-policy")
+ ) {
+ state.missingRequiredFeature = "bb.feature.approval-policy";
+ return;
+ }
+ if (
+ backupPolicy.payload.schedule !== DefaultSchedulePolicy &&
+ !store.getters["subscription/feature"]("bb.feature.backup-policy")
+ ) {
+ state.missingRequiredFeature = "bb.feature.backup-policy";
+ return;
+ }
+
store
.dispatch("environment/createEnvironment", newEnvironment)
.then((environment: Environment) => {
diff --git a/frontend/src/views/EnvironmentDetail.vue b/frontend/src/views/EnvironmentDetail.vue
index 402313c20d..6648c4f315 100644
--- a/frontend/src/views/EnvironmentDetail.vue
+++ b/frontend/src/views/EnvironmentDetail.vue
@@ -12,6 +12,11 @@
@restore="doRestore"
@update-policy="updatePolicy"
/>
+ <FeatureModal
+ v-if="state.missingRequiredFeature != undefined"
+ :feature="state.missingRequiredFeature"
+ @cancel="state.missingRequiredFeature = undefined"
+ />
</template>
<script lang="ts">
@@ -25,6 +30,9 @@ import {
EnvironmentPatch,
Policy,
PolicyType,
+ FeatureType,
+ DefaultApporvalPolicy,
+ DefaultSchedulePolicy,
} from "../types";
import { idFromSlug } from "../utils";
@@ -33,6 +41,9 @@ interface LocalState {
showArchiveModal: boolean;
approvalPolicy?: Policy;
backupPolicy?: Policy;
+ missingRequiredFeature?:
+ | "bb.feature.approval-policy"
+ | "bb.feature.backup-policy";
}
export default {
@@ -127,6 +138,22 @@ export default {
type: PolicyType,
policy: Policy
) => {
+ if (
+ type === "bb.policy.pipeline-approval" &&
+ policy.payload.value !== DefaultApporvalPolicy &&
+ !store.getters["subscription/feature"]("bb.feature.approval-policy")
+ ) {
+ state.missingRequiredFeature = "bb.feature.approval-policy";
+ return;
+ }
+ if (
+ type === "bb.policy.backup-plan" &&
+ policy.payload.schedule !== DefaultSchedulePolicy &&
+ !store.getters["subscription/feature"]("bb.feature.backup-policy")
+ ) {
+ state.missingRequiredFeature = "bb.feature.backup-policy";
+ return;
+ }
store
.dispatch("policy/upsertPolicyByEnvironmentAndType", {
environmentId,
diff --git a/server/policy.go b/server/policy.go
index ff39ce3f91..ec1c00aff5 100644
--- a/server/policy.go
+++ b/server/policy.go
@@ -11,6 +11,26 @@ import (
"github.com/labstack/echo/v4"
)
+// hasAccessToUpdatePolicy checks if user can access to policy control feature.
+// return nil if user has access.
+func (s *Server) hasAccessToUpsertPolicy(policyUpsert *api.PolicyUpsert) error {
+ defaultPolicy, err := api.GetDefaultPolicy(policyUpsert.Type)
+ if err != nil {
+ return err
+ }
+ switch policyUpsert.Type {
+ case api.PolicyTypePipelineApproval:
+ if policyUpsert.Payload != defaultPolicy && !s.feature(api.FeatureApprovalPolicy) {
+ return fmt.Errorf(api.FeatureApprovalPolicy.AccessErrorMessage())
+ }
+ case api.PolicyTypeBackupPlan:
+ if policyUpsert.Payload != defaultPolicy && !s.feature(api.FeatureBackupPolicy) {
+ return fmt.Errorf(api.FeatureBackupPolicy.AccessErrorMessage())
+ }
+ }
+ return nil
+}
+
func (s *Server) registerPolicyRoutes(g *echo.Group) {
g.PATCH("/policy/environment/:environmentID", func(c echo.Context) error {
ctx := context.Background()
@@ -27,10 +47,15 @@ func (s *Server) registerPolicyRoutes(g *echo.Group) {
if err := api.ValidatePolicy(pType, ""); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid policy type: %q", pType)).SetInternal(err)
}
+
policyUpsert.EnvironmentID = environmentID
policyUpsert.Type = pType
policyUpsert.UpdaterID = c.Get(getPrincipalIDContextKey()).(int)
+ if err := s.hasAccessToUpsertPolicy(policyUpsert); err != nil {
+ return echo.NewHTTPError(http.StatusForbidden, err.Error()).SetInternal(err)
+ }
+
policy, err := s.PolicyService.UpsertPolicy(ctx, policyUpsert)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to set policy for type %q", pType)).SetInternal(err)