Skip to content

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

fix: ensure consistent default rollout policy across backend and frontend (#17964)

boojack700d365fdf33
diff --git a/backend/api/v1/org_policy_service.go b/backend/api/v1/org_policy_service.go
index 692ace8779..80b224da98 100644
--- a/backend/api/v1/org_policy_service.go
+++ b/backend/api/v1/org_policy_service.go
@@ -48,8 +48,20 @@ func NewOrgPolicyService(store *store.Store, licenseService *enterprise.LicenseS
// GetPolicy gets a policy in a specific resource.
func (s *OrgPolicyService) GetPolicy(ctx context.Context, req *connect.Request[v1pb.GetPolicyRequest]) (*connect.Response[v1pb.Policy], error) {
- policy, _, err := s.findPolicyMessage(ctx, req.Msg.Name)
+ policy, parent, err := s.findPolicyMessage(ctx, req.Msg.Name)
if err != nil {
+ connectErr := connect.CodeOf(err)
+ // For ROLLOUT_POLICY, return default policy if not found
+ if connectErr == connect.CodeNotFound {
+ policyType, extractErr := extractPolicyTypeFromName(req.Msg.Name)
+ if extractErr == nil && policyType == storepb.Policy_ROLLOUT {
+ defaultPolicy, defaultErr := s.getDefaultRolloutPolicy(ctx, parent, req.Msg.Name)
+ if defaultErr != nil {
+ return nil, defaultErr
+ }
+ return connect.NewResponse(defaultPolicy), nil
+ }
+ }
return nil, err
}
@@ -230,6 +242,56 @@ func (s *OrgPolicyService) DeletePolicy(ctx context.Context, req *connect.Reques
return connect.NewResponse(&emptypb.Empty{}), nil
}
+// extractPolicyTypeFromName extracts the policy type from a policy name.
+func extractPolicyTypeFromName(policyName string) (storepb.Policy_Type, error) {
+ tokens := strings.Split(policyName, common.PolicyNamePrefix)
+ if len(tokens) != 2 {
+ return storepb.Policy_TYPE_UNSPECIFIED, errors.Errorf("invalid policy name %s", policyName)
+ }
+
+ v1PolicyType, ok := v1pb.PolicyType_value[strings.ToUpper(tokens[1])]
+ if !ok {
+ return storepb.Policy_TYPE_UNSPECIFIED, errors.Errorf("invalid policy type %v", tokens[1])
+ }
+
+ return convertV1PBToStorePBPolicyType(v1pb.PolicyType(v1PolicyType))
+}
+
+// getDefaultRolloutPolicy returns the default rollout policy when no custom policy exists.
+// Uses the shared store.GetDefaultRolloutPolicy to ensure consistency across API and store layers.
+func (*OrgPolicyService) getDefaultRolloutPolicy(_ context.Context, parent string, policyName string) (*v1pb.Policy, error) {
+ resourceType, _, err := getPolicyResourceTypeAndResource(parent)
+ if err != nil {
+ return nil, err
+ }
+
+ v1ResourceType := v1pb.PolicyResourceType_RESOURCE_TYPE_UNSPECIFIED
+ switch resourceType {
+ case storepb.Policy_WORKSPACE:
+ v1ResourceType = v1pb.PolicyResourceType_WORKSPACE
+ case storepb.Policy_ENVIRONMENT:
+ v1ResourceType = v1pb.PolicyResourceType_ENVIRONMENT
+ case storepb.Policy_PROJECT:
+ v1ResourceType = v1pb.PolicyResourceType_PROJECT
+ default:
+ // Keep the default RESOURCE_TYPE_UNSPECIFIED
+ }
+
+ // Get the default rollout policy from the shared store function
+ defaultStorePBPolicy := store.GetDefaultRolloutPolicy()
+
+ return &v1pb.Policy{
+ Name: policyName,
+ ResourceType: v1ResourceType,
+ Type: v1pb.PolicyType_ROLLOUT_POLICY,
+ InheritFromParent: false,
+ Enforce: true,
+ Policy: &v1pb.Policy_RolloutPolicy{
+ RolloutPolicy: convertToV1PBRolloutPolicy(defaultStorePBPolicy),
+ },
+ }, nil
+}
+
// findPolicyMessage finds the policy and the parent name by the policy name.
func (s *OrgPolicyService) findPolicyMessage(ctx context.Context, policyName string) (*store.PolicyMessage, string, error) {
tokens := strings.Split(policyName, common.PolicyNamePrefix)
diff --git a/backend/api/v1/org_policy_service_converter.go b/backend/api/v1/org_policy_service_converter.go
index afa66c648e..c45d6ceded 100644
--- a/backend/api/v1/org_policy_service_converter.go
+++ b/backend/api/v1/org_policy_service_converter.go
@@ -102,6 +102,29 @@ func convertToStorePBRolloutPolicy(policy *v1pb.RolloutPolicy) *storepb.RolloutP
}
}
+func convertToV1PBRolloutPolicy(policy *storepb.RolloutPolicy) *v1pb.RolloutPolicy {
+ return &v1pb.RolloutPolicy{
+ Automatic: policy.Automatic,
+ Roles: policy.Roles,
+ Checkers: convertToV1PBCheckers(policy.Checkers),
+ }
+}
+
+func convertToV1PBCheckers(checkers *storepb.RolloutPolicy_Checkers) *v1pb.RolloutPolicy_Checkers {
+ if checkers == nil {
+ return nil
+ }
+ result := &v1pb.RolloutPolicy_Checkers{
+ RequiredIssueApproval: checkers.RequiredIssueApproval,
+ }
+ if checkers.RequiredStatusChecks != nil {
+ result.RequiredStatusChecks = &v1pb.RolloutPolicy_Checkers_RequiredStatusChecks{
+ PlanCheckEnforcement: v1pb.RolloutPolicy_Checkers_PlanCheckEnforcement(checkers.RequiredStatusChecks.PlanCheckEnforcement),
+ }
+ }
+ return result
+}
+
func convertToStorePBCheckers(checkers *v1pb.RolloutPolicy_Checkers) *storepb.RolloutPolicy_Checkers {
if checkers == nil {
return nil
diff --git a/backend/store/policy.go b/backend/store/policy.go
index 11a1c937af..39b4a66580 100644
--- a/backend/store/policy.go
+++ b/backend/store/policy.go
@@ -128,6 +128,26 @@ func (s *Store) getIamPolicy(ctx context.Context, find *FindPolicyMessage) (*Iam
}, nil
}
+// GetDefaultRolloutPolicy returns the default rollout policy when no custom policy exists.
+// This is used as a fallback for both API and store layers to ensure consistent defaults.
+// Default values:
+// - automatic: false (manual rollout required)
+// - roles: [] (no role restrictions)
+// - requiredIssueApproval: true (issue must be approved before rollout)
+// - planCheckEnforcement: ERROR_ONLY (block rollout only on errors, not warnings)
+func GetDefaultRolloutPolicy() *storepb.RolloutPolicy {
+ return &storepb.RolloutPolicy{
+ Automatic: false,
+ Roles: []string{},
+ Checkers: &storepb.RolloutPolicy_Checkers{
+ RequiredIssueApproval: true,
+ RequiredStatusChecks: &storepb.RolloutPolicy_Checkers_RequiredStatusChecks{
+ PlanCheckEnforcement: storepb.RolloutPolicy_Checkers_ERROR_ONLY,
+ },
+ },
+ }
+}
+
func (s *Store) GetRolloutPolicy(ctx context.Context, environment string) (*storepb.RolloutPolicy, error) {
resource := common.FormatEnvironment(environment)
resourceType := storepb.Policy_ENVIRONMENT
@@ -141,15 +161,7 @@ func (s *Store) GetRolloutPolicy(ctx context.Context, environment string) (*stor
return nil, errors.Wrapf(err, "failed to get policy")
}
if policy == nil {
- // Return default rollout policy with checkers.
- return &storepb.RolloutPolicy{
- Checkers: &storepb.RolloutPolicy_Checkers{
- RequiredIssueApproval: true,
- RequiredStatusChecks: &storepb.RolloutPolicy_Checkers_RequiredStatusChecks{
- PlanCheckEnforcement: storepb.RolloutPolicy_Checkers_ERROR_ONLY,
- },
- },
- }, nil
+ return GetDefaultRolloutPolicy(), nil
}
p := &storepb.RolloutPolicy{}
diff --git a/frontend/src/store/modules/v1/policy.ts b/frontend/src/store/modules/v1/policy.ts
index a58e0cba5b..af91135989 100644
--- a/frontend/src/store/modules/v1/policy.ts
+++ b/frontend/src/store/modules/v1/policy.ts
@@ -309,8 +309,25 @@ export const usePolicyByParentAndType = (
};
};
-// Default RolloutPolicy payload is somehow strict to prevent auto rollout
-
+// getEmptyRolloutPolicy returns a default rollout policy for UI display purposes.
+//
+// IMPORTANT: These defaults MUST match the backend defaults defined in:
+// - backend/store/policy.go: GetDefaultRolloutPolicy()
+// - backend/api/v1/org_policy_service.go: getDefaultRolloutPolicy()
+//
+// This function is used for:
+// 1. Showing default policy in creation forms (so users see what they'll get)
+// 2. Allowing users to customize before creation
+// 3. Detecting if user customized from defaults (only persist if customized)
+//
+// The backend automatically returns these defaults via GetPolicy API when no
+// custom policy exists in the database, so we only persist customizations.
+//
+// Default values:
+// - automatic: false (manual rollout required)
+// - roles: [] (no role restrictions)
+// - requiredIssueApproval: true (issue must be approved before rollout)
+// - planCheckEnforcement: ERROR_ONLY (block rollout only on errors, not warnings)
export const getEmptyRolloutPolicy = (
parentPath: string,
resourceType: PolicyResourceType
diff --git a/frontend/src/views/EnvironmentDashboard.vue b/frontend/src/views/EnvironmentDashboard.vue
index 0e83741d37..2f993db32f 100644
--- a/frontend/src/views/EnvironmentDashboard.vue
+++ b/frontend/src/views/EnvironmentDashboard.vue
@@ -113,6 +113,7 @@
<script lang="ts" setup>
import { create } from "@bufbuild/protobuf";
+import { isEqual } from "lodash-es";
import { PlusIcon, ListOrderedIcon, GripVerticalIcon } from "lucide-vue-next";
import { NTabs, NTabPane, NButton } from "naive-ui";
import { onMounted, computed, reactive, watch, h, ref } from "vue";
@@ -264,13 +265,16 @@ const doCreate = async (params: {
});
await environmentV1Store.fetchEnvironments();
- const requests = [
- policyV1Store.upsertPolicy({
+ // Only persist rollout policy if user customized it from the defaults
+ // Otherwise, let the backend return defaults dynamically via GetPolicy API
+ const isCustomized = !isEqual(rolloutPolicy, DEFAULT_NEW_ROLLOUT_POLICY);
+ if (isCustomized) {
+ await policyV1Store.upsertPolicy({
parentPath: `${environmentNamePrefix}${createdEnvironment.id}`,
policy: rolloutPolicy,
- }),
- ];
- await Promise.all(requests);
+ });
+ }
+
state.showCreateModal = false;
selectEnvironment(createdEnvironment.order);
};
diff --git a/frontend/src/views/EnvironmentDetail.vue b/frontend/src/views/EnvironmentDetail.vue
index 8a8b2902b0..90d7ed5dba 100644
--- a/frontend/src/views/EnvironmentDetail.vue
+++ b/frontend/src/views/EnvironmentDetail.vue
@@ -26,16 +26,10 @@ import {
} from "@/components/EnvironmentForm";
import { useEnvironmentV1Store } from "@/store";
import { environmentNamePrefix } from "@/store/modules/v1/common";
-import {
- usePolicyV1Store,
- getEmptyRolloutPolicy,
-} from "@/store/modules/v1/policy";
+import { usePolicyV1Store } from "@/store/modules/v1/policy";
import { formatEnvironmentName } from "@/types";
import type { Policy } from "@/types/proto-es/v1/org_policy_service_pb";
-import {
- PolicyType,
- PolicyResourceType,
-} from "@/types/proto-es/v1/org_policy_service_pb";
+import { PolicyType } from "@/types/proto-es/v1/org_policy_service_pb";
import type { Environment } from "@/types/v1/environment";
import { type VueClass } from "@/utils";
@@ -78,23 +72,14 @@ watch(() => props.environmentName, prepareEnvironment, {
immediate: true,
});
-const preparePolicy = () => {
- policyV1Store
- .fetchPolicies({
- parent: stateEnvironmentName.value,
- resourceType: PolicyResourceType.ENVIRONMENT,
- })
- .then((policies) => {
- const rolloutPolicy = policies.find(
- (policy) => policy.type === PolicyType.ROLLOUT_POLICY
- );
- state.rolloutPolicy =
- rolloutPolicy ||
- getEmptyRolloutPolicy(
- stateEnvironmentName.value,
- PolicyResourceType.ENVIRONMENT
- );
- });
+// Fetch rollout policy directly using getOrFetchPolicyByParentAndType
+// The backend now returns default policy if none exists, so no manual fallback needed
+const preparePolicy = async () => {
+ const policy = await policyV1Store.getOrFetchPolicyByParentAndType({
+ parentPath: stateEnvironmentName.value,
+ policyType: PolicyType.ROLLOUT_POLICY,
+ });
+ state.rolloutPolicy = policy;
};
watchEffect(preparePolicy);