Skip to content

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

refactor: preparing for access control policy (#3488)

Danny Xu409f51f4394b
diff --git a/api/policy.go b/api/policy.go
index 9c1da9e06b..16a370b9fd 100644
--- a/api/policy.go
+++ b/api/policy.go
@@ -40,6 +40,8 @@ const (
PolicyTypeEnvironmentTier PolicyType = "bb.policy.environment-tier"
// PolicyTypeSensitiveData is the sensitive data policy type.
PolicyTypeSensitiveData PolicyType = "bb.policy.sensitive-data"
+ // PolicyTypeAccessControl is the access control policy type.
+ PolicyTypeAccessControl PolicyType = "bb.policy.access-control"
// PipelineApprovalValueManualNever means the pipeline will automatically be approved without user intervention.
PipelineApprovalValueManualNever PipelineApprovalValue = "MANUAL_APPROVAL_NEVER"
@@ -78,13 +80,14 @@ const (
)
var (
- // policyTypes is a set of all policy types.
- policyTypes = map[PolicyType]bool{
- PolicyTypePipelineApproval: true,
- PolicyTypeBackupPlan: true,
- PolicyTypeSQLReview: true,
- PolicyTypeEnvironmentTier: true,
- PolicyTypeSensitiveData: true,
+ // allowedResourceTypes includes allowed resource types for each policy type.
+ allowedResourceTypes = map[PolicyType][]PolicyResourceType{
+ PolicyTypePipelineApproval: {PolicyResourceTypeEnvironment},
+ PolicyTypeBackupPlan: {PolicyResourceTypeEnvironment},
+ PolicyTypeSQLReview: {PolicyResourceTypeEnvironment},
+ PolicyTypeEnvironmentTier: {PolicyResourceTypeEnvironment},
+ PolicyTypeSensitiveData: {PolicyResourceTypeDatabase},
+ PolicyTypeAccessControl: {PolicyResourceTypeEnvironment, PolicyResourceTypeDatabase},
}
)
@@ -107,8 +110,9 @@ type Policy struct {
Environment *Environment `jsonapi:"relation,environment"`
// Domain specific fields
- Type PolicyType `jsonapi:"attr,type"`
- Payload string `jsonapi:"attr,payload"`
+ InheritFromParent bool `jsonapi:"attr,inheritFromParent"`
+ Type PolicyType `jsonapi:"attr,type"`
+ Payload string `jsonapi:"attr,payload"`
}
// PolicyFind is the message to get a policy.
@@ -137,8 +141,9 @@ type PolicyUpsert struct {
ResourceID int
// Domain specific fields
- Type PolicyType
- Payload *string `jsonapi:"attr,payload"`
+ InheritFromParent *bool
+ Type PolicyType
+ Payload *string `jsonapi:"attr,payload"`
}
// PolicyDelete is the message to delete a policy.
@@ -282,6 +287,29 @@ func (p *SensitiveDataPolicy) String() (string, error) {
return string(s), nil
}
+// AccessControlPolicy is the policy configuration for database access control.
+// It is only applicable to database and environment resource type.
+type AccessControlPolicy struct {
+ // TODO(d): define access control policy.
+}
+
+// UnmarshalAccessControlPolicy will unmarshal payload to access control policy.
+func UnmarshalAccessControlPolicy(payload string) (*AccessControlPolicy, error) {
+ var p AccessControlPolicy
+ if err := json.Unmarshal([]byte(payload), &p); err != nil {
+ return nil, errors.Wrapf(err, "failed to unmarshal access control policy %q", payload)
+ }
+ return &p, nil
+}
+
+func (p *AccessControlPolicy) String() (string, error) {
+ s, err := json.Marshal(p)
+ if err != nil {
+ return "", err
+ }
+ return string(s), nil
+}
+
// UnmarshalEnvironmentTierPolicy will unmarshal payload to environment tier policy.
func UnmarshalEnvironmentTierPolicy(payload string) (*EnvironmentTierPolicy, error) {
var p EnvironmentTierPolicy
@@ -293,7 +321,7 @@ func UnmarshalEnvironmentTierPolicy(payload string) (*EnvironmentTierPolicy, err
// ValidatePolicyType will validate the policy type.
func ValidatePolicyType(pType PolicyType) error {
- if !policyTypes[pType] {
+ if _, ok := allowedResourceTypes[pType]; !ok {
return errors.Errorf("invalid policy type: %s", pType)
}
return nil
@@ -304,76 +332,87 @@ func ValidatePolicy(resourceType PolicyResourceType, pType PolicyType, payload *
if payload == nil {
return errors.Errorf("empty payload")
}
- switch resourceType {
- case PolicyResourceTypeEnvironment:
- switch pType {
- case PolicyTypePipelineApproval:
- pa, err := UnmarshalPipelineApprovalPolicy(*payload)
- if err != nil {
- return err
- }
- if pa.Value != PipelineApprovalValueManualNever && pa.Value != PipelineApprovalValueManualAlways {
- return errors.Errorf("invalid approval policy value: %q", *payload)
- }
- issueTypeSeen := make(map[IssueType]bool)
- for _, group := range pa.AssigneeGroupList {
- if group.IssueType != IssueDatabaseSchemaUpdate &&
- group.IssueType != IssueDatabaseSchemaUpdateGhost &&
- group.IssueType != IssueDatabaseDataUpdate {
- return errors.Errorf("invalid assignee group issue type %q", group.IssueType)
- }
- if issueTypeSeen[group.IssueType] {
- return errors.Errorf("duplicate assignee group issue type %q", group.IssueType)
- }
- issueTypeSeen[group.IssueType] = true
- }
- return nil
- case PolicyTypeBackupPlan:
- bp, err := UnmarshalBackupPlanPolicy(*payload)
- if err != nil {
- return err
- }
- if bp.Schedule != BackupPlanPolicyScheduleUnset && bp.Schedule != BackupPlanPolicyScheduleDaily && bp.Schedule != BackupPlanPolicyScheduleWeekly {
- return errors.Errorf("invalid backup plan policy schedule: %q", bp.Schedule)
- }
- return nil
- case PolicyTypeSQLReview:
- sr, err := UnmarshalSQLReviewPolicy(*payload)
- if err != nil {
- return err
- }
- if err := sr.Validate(); err != nil {
- return errors.Wrap(err, "invalid SQL review policy")
- }
- return nil
- case PolicyTypeEnvironmentTier:
- p, err := UnmarshalEnvironmentTierPolicy(*payload)
- if err != nil {
- return err
+ hasResourceType := false
+ for _, rt := range allowedResourceTypes[pType] {
+ if rt == resourceType {
+ hasResourceType = true
+ }
+ }
+ if !hasResourceType {
+ return errors.Errorf("invalid resource type %s and policy type %s pair", resourceType, pType)
+ }
+
+ switch pType {
+ case PolicyTypePipelineApproval:
+ pa, err := UnmarshalPipelineApprovalPolicy(*payload)
+ if err != nil {
+ return err
+ }
+ if pa.Value != PipelineApprovalValueManualNever && pa.Value != PipelineApprovalValueManualAlways {
+ return errors.Errorf("invalid approval policy value: %q", *payload)
+ }
+ issueTypeSeen := make(map[IssueType]bool)
+ for _, group := range pa.AssigneeGroupList {
+ if group.IssueType != IssueDatabaseSchemaUpdate &&
+ group.IssueType != IssueDatabaseSchemaUpdateGhost &&
+ group.IssueType != IssueDatabaseDataUpdate {
+ return errors.Errorf("invalid assignee group issue type %q", group.IssueType)
}
- if p.EnvironmentTier != EnvironmentTierValueProtected && p.EnvironmentTier != EnvironmentTierValueUnprotected {
- return errors.Errorf("invalid environment tier value %q", p.EnvironmentTier)
+ if issueTypeSeen[group.IssueType] {
+ return errors.Errorf("duplicate assignee group issue type %q", group.IssueType)
}
- return nil
+ issueTypeSeen[group.IssueType] = true
+ }
+ return nil
+ case PolicyTypeBackupPlan:
+ bp, err := UnmarshalBackupPlanPolicy(*payload)
+ if err != nil {
+ return err
+ }
+ if bp.Schedule != BackupPlanPolicyScheduleUnset && bp.Schedule != BackupPlanPolicyScheduleDaily && bp.Schedule != BackupPlanPolicyScheduleWeekly {
+ return errors.Errorf("invalid backup plan policy schedule: %q", bp.Schedule)
}
- case PolicyResourceTypeDatabase:
- if pType == PolicyTypeSensitiveData {
- p, err := UnmarshalSensitiveDataPolicy(*payload)
- if err != nil {
- return err
+ return nil
+ case PolicyTypeSQLReview:
+ sr, err := UnmarshalSQLReviewPolicy(*payload)
+ if err != nil {
+ return err
+ }
+ if err := sr.Validate(); err != nil {
+ return errors.Wrap(err, "invalid SQL review policy")
+ }
+ return nil
+ case PolicyTypeEnvironmentTier:
+ p, err := UnmarshalEnvironmentTierPolicy(*payload)
+ if err != nil {
+ return err
+ }
+ if p.EnvironmentTier != EnvironmentTierValueProtected && p.EnvironmentTier != EnvironmentTierValueUnprotected {
+ return errors.Errorf("invalid environment tier value %q", p.EnvironmentTier)
+ }
+ return nil
+ case PolicyTypeSensitiveData:
+ p, err := UnmarshalSensitiveDataPolicy(*payload)
+ if err != nil {
+ return err
+ }
+ for _, v := range p.SensitiveDataList {
+ if v.Table == "" || v.Column == "" {
+ return errors.Errorf("sensitive data policy rule cannot have empty table or column name")
}
- for _, v := range p.SensitiveDataList {
- if v.Table == "" || v.Column == "" {
- return errors.Errorf("sensitive data policy rule cannot have empty table or column name")
- }
- if v.Type != SensitiveDataMaskTypeDefault {
- return errors.Errorf("sensitive data policy rule must have mask type %q", SensitiveDataMaskTypeDefault)
- }
+ if v.Type != SensitiveDataMaskTypeDefault {
+ return errors.Errorf("sensitive data policy rule must have mask type %q", SensitiveDataMaskTypeDefault)
}
- return nil
}
+ return nil
+ case PolicyTypeAccessControl:
+ _, err := UnmarshalAccessControlPolicy(*payload)
+ if err != nil {
+ return err
+ }
+ return nil
}
- return errors.Errorf("invalid resource type %s and policy type %s pair", resourceType, pType)
+ return nil
}
// GetDefaultPolicy will return the default value for the given policy type.
diff --git a/store/policy.go b/store/policy.go
index cdb8f732c0..4c533de0f5 100644
--- a/store/policy.go
+++ b/store/policy.go
@@ -26,12 +26,13 @@ type policyRaw struct {
UpdatedTs int64
// Related fields
- ResourceType api.PolicyResourceType
- EnvironmentID int
+ ResourceType api.PolicyResourceType
+ ResourceID int
// Domain specific fields
- Type api.PolicyType
- Payload string
+ InheritFromParent bool
+ Type api.PolicyType
+ Payload string
}
// toPolicy creates an instance of Policy based on the PolicyRaw.
@@ -49,11 +50,12 @@ func (raw *policyRaw) toPolicy() *api.Policy {
// Related fields
ResourceType: raw.ResourceType,
- ResourceID: raw.EnvironmentID,
+ ResourceID: raw.ResourceID,
// Domain specific fields
- Type: raw.Type,
- Payload: raw.Payload,
+ InheritFromParent: raw.InheritFromParent,
+ Type: raw.Type,
+ Payload: raw.Payload,
}
}
@@ -201,10 +203,10 @@ func (s *Store) GetNormalSQLReviewPolicy(ctx context.Context, find *api.PolicyFi
return nil, err
}
if policy.RowStatus == api.Archived {
- return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("SQL review policy ID: %d for environment %d is archived", policy.ID, policy.EnvironmentID)}
+ return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("SQL review policy ID: %d for environment %d is archived", policy.ID, policy.ResourceID)}
}
if policy.ID == api.DefaultPolicyID {
- return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("SQL review policy ID: %d for environment %d not found", policy.ID, policy.EnvironmentID)}
+ return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("SQL review policy ID: %d for environment %d not found", policy.ID, policy.ResourceID)}
}
return api.UnmarshalSQLReviewPolicy(policy.Payload)
}
@@ -316,7 +318,7 @@ func (s *Store) getPolicyRaw(ctx context.Context, find *api.PolicyFind) (*policy
ret.ResourceType = *find.ResourceType
}
if find.ResourceID != nil {
- ret.EnvironmentID = *find.ResourceID
+ ret.ResourceID = *find.ResourceID
}
} else if len(policyRawList) > 1 {
return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d policy with filter %+v, expect 1. ", len(policyRawList), find)}
@@ -361,6 +363,7 @@ func findPolicyImpl(ctx context.Context, tx *Tx, find *api.PolicyFind, mode comm
updated_ts,
row_status,
resource_id,
+ inherit_from_parent,
type,
payload
FROM policy
@@ -383,7 +386,8 @@ func findPolicyImpl(ctx context.Context, tx *Tx, find *api.PolicyFind, mode comm
&policyRaw.UpdaterID,
&policyRaw.UpdatedTs,
&policyRaw.RowStatus,
- &policyRaw.EnvironmentID,
+ &policyRaw.ResourceID,
+ &policyRaw.InheritFromParent,
&policyRaw.Type,
&policyRaw.Payload,
); err != nil {
@@ -440,7 +444,7 @@ func findPolicyImpl(ctx context.Context, tx *Tx, find *api.PolicyFind, mode comm
&policyRaw.UpdaterID,
&policyRaw.UpdatedTs,
&policyRaw.RowStatus,
- &policyRaw.EnvironmentID,
+ &policyRaw.ResourceID,
&policyRaw.Type,
&policyRaw.Payload,
); err != nil {
@@ -499,22 +503,26 @@ func upsertPolicyImpl(ctx context.Context, tx *Tx, upsert *api.PolicyUpsert, mod
rowStatus := string(api.Normal)
upsert.RowStatus = &rowStatus
}
+ inheritFromParent := true
+ if upsert.InheritFromParent != nil {
+ inheritFromParent = *upsert.InheritFromParent
+ }
- // TODO(d): handle resource type.
query := fmt.Sprintf(`
INSERT INTO policy (
creator_id,
updater_id,
resource_type,
resource_id,
+ inherit_from_parent,
type,
payload,
row_status
)
- VALUES ($1, $2, $3, $4, $5, $6, $7)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT(resource_type, resource_id, type) DO UPDATE SET
%s
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, row_status, resource_id, type, payload
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, row_status, resource_id, inherit_from_parent, type, payload
`, strings.Join(set, ","))
var policyRaw policyRaw
if err := tx.QueryRowContext(ctx, query,
@@ -522,6 +530,7 @@ func upsertPolicyImpl(ctx context.Context, tx *Tx, upsert *api.PolicyUpsert, mod
upsert.UpdaterID,
upsert.ResourceType,
upsert.ResourceID,
+ inheritFromParent,
upsert.Type,
upsert.Payload,
upsert.RowStatus,
@@ -532,7 +541,8 @@ func upsertPolicyImpl(ctx context.Context, tx *Tx, upsert *api.PolicyUpsert, mod
&policyRaw.UpdaterID,
&policyRaw.UpdatedTs,
&policyRaw.RowStatus,
- &policyRaw.EnvironmentID,
+ &policyRaw.ResourceID,
+ &policyRaw.InheritFromParent,
&policyRaw.Type,
&policyRaw.Payload,
); err != nil {
@@ -594,7 +604,7 @@ func upsertPolicyImpl(ctx context.Context, tx *Tx, upsert *api.PolicyUpsert, mod
&policyRaw.UpdaterID,
&policyRaw.UpdatedTs,
&policyRaw.RowStatus,
- &policyRaw.EnvironmentID,
+ &policyRaw.ResourceID,
&policyRaw.Type,
&policyRaw.Payload,
); err != nil {