Skip to content

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

fix: get resource type for policy (#3849)

rebelicebe3f1e240d19
diff --git a/store/policy.go b/store/policy.go
index cfb24394b9..dc4c24b58c 100644
--- a/store/policy.go
+++ b/store/policy.go
@@ -409,6 +409,7 @@ func findPolicyImpl(ctx context.Context, tx *Tx, find *api.PolicyFind) ([]*polic
updater_id,
updated_ts,
row_status,
+ resource_type,
resource_id,
inherit_from_parent,
type,
@@ -433,6 +434,7 @@ func findPolicyImpl(ctx context.Context, tx *Tx, find *api.PolicyFind) ([]*polic
&policyRaw.UpdaterID,
&policyRaw.UpdatedTs,
&policyRaw.RowStatus,
+ &policyRaw.ResourceType,
&policyRaw.ResourceID,
&policyRaw.InheritFromParent,
&policyRaw.Type,
@@ -510,7 +512,7 @@ func upsertPolicyImpl(ctx context.Context, tx *Tx, upsert *api.PolicyUpsert) (*p
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, inherit_from_parent, type, payload
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, row_status, resource_type, resource_id, inherit_from_parent, type, payload
`, strings.Join(set, ","))
var policyRaw policyRaw
if err := tx.QueryRowContext(ctx, query,
@@ -529,6 +531,7 @@ func upsertPolicyImpl(ctx context.Context, tx *Tx, upsert *api.PolicyUpsert) (*p
&policyRaw.UpdaterID,
&policyRaw.UpdatedTs,
&policyRaw.RowStatus,
+ &policyRaw.ResourceType,
&policyRaw.ResourceID,
&policyRaw.InheritFromParent,
&policyRaw.Type,
diff --git a/tests/backup_restore_test.go b/tests/backup_restore_test.go
index 74926ebf43..a49767b5da 100644
--- a/tests/backup_restore_test.go
+++ b/tests/backup_restore_test.go
@@ -426,7 +426,7 @@ func setUpForPITRTest(ctx context.Context, t *testing.T, ctl *controller) (*api.
buf, err := json.Marshal(policy)
a.NoError(err)
str := string(buf)
- err = ctl.upsertPolicy(api.PolicyUpsert{
+ _, err = ctl.upsertPolicy(api.PolicyUpsert{
ResourceType: api.PolicyResourceTypeEnvironment,
ResourceID: prodEnvironment.ID,
Type: api.PolicyTypeBackupPlan,
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 2b4764feb9..927af8719c 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -1622,7 +1622,7 @@ func TestVCS_SQL_Review(t *testing.T) {
policyPayload, err := prodTemplateSQLReviewPolicy()
a.NoError(err)
- err = ctl.upsertPolicy(api.PolicyUpsert{
+ _, err = ctl.upsertPolicy(api.PolicyUpsert{
ResourceType: api.PolicyResourceTypeEnvironment,
ResourceID: prodEnvironment.ID,
Type: api.PolicyTypeSQLReview,
diff --git a/tests/sensitive_data_test.go b/tests/sensitive_data_test.go
index 5cdd04dd62..1a964b949a 100644
--- a/tests/sensitive_data_test.go
+++ b/tests/sensitive_data_test.go
@@ -163,7 +163,7 @@ func TestSensitiveData(t *testing.T) {
a.NoError(err)
payloadString := string(policyPayload)
- err = ctl.upsertPolicy(api.PolicyUpsert{
+ _, err = ctl.upsertPolicy(api.PolicyUpsert{
ResourceType: api.PolicyResourceTypeDatabase,
ResourceID: database.ID,
Type: api.PolicyTypeSensitiveData,
diff --git a/tests/sql_review_test.go b/tests/sql_review_test.go
index 96adba18c3..e9909ee615 100644
--- a/tests/sql_review_test.go
+++ b/tests/sql_review_test.go
@@ -299,7 +299,7 @@ func TestSQLReviewForPostgreSQL(t *testing.T) {
policyPayload, err := prodTemplateSQLReviewPolicy()
a.NoError(err)
- err = ctl.upsertPolicy(api.PolicyUpsert{
+ _, err = ctl.upsertPolicy(api.PolicyUpsert{
ResourceType: api.PolicyResourceTypeEnvironment,
ResourceID: prodEnvironment.ID,
Type: api.PolicyTypeSQLReview,
@@ -310,13 +310,14 @@ func TestSQLReviewForPostgreSQL(t *testing.T) {
err = ctl.setLicense()
a.NoError(err)
- err = ctl.upsertPolicy(api.PolicyUpsert{
+ policy, err := ctl.upsertPolicy(api.PolicyUpsert{
ResourceType: api.PolicyResourceTypeEnvironment,
ResourceID: prodEnvironment.ID,
Type: api.PolicyTypeSQLReview,
Payload: &policyPayload,
})
a.NoError(err)
+ a.NotNil(policy.Environment)
instance, err := ctl.addInstance(api.InstanceCreate{
EnvironmentID: prodEnvironment.ID,
@@ -354,7 +355,7 @@ func TestSQLReviewForPostgreSQL(t *testing.T) {
// disable the SQL review policy
disable := string(api.Archived)
- err = ctl.upsertPolicy(api.PolicyUpsert{
+ _, err = ctl.upsertPolicy(api.PolicyUpsert{
ResourceType: api.PolicyResourceTypeEnvironment,
ResourceID: prodEnvironment.ID,
Type: api.PolicyTypeSQLReview,
@@ -1032,7 +1033,7 @@ func TestSQLReviewForMySQL(t *testing.T) {
policyPayload, err := prodTemplateSQLReviewPolicy()
a.NoError(err)
- err = ctl.upsertPolicy(api.PolicyUpsert{
+ _, err = ctl.upsertPolicy(api.PolicyUpsert{
ResourceType: api.PolicyResourceTypeEnvironment,
ResourceID: prodEnvironment.ID,
Type: api.PolicyTypeSQLReview,
@@ -1043,13 +1044,14 @@ func TestSQLReviewForMySQL(t *testing.T) {
err = ctl.setLicense()
a.NoError(err)
- err = ctl.upsertPolicy(api.PolicyUpsert{
+ policy, err := ctl.upsertPolicy(api.PolicyUpsert{
ResourceType: api.PolicyResourceTypeEnvironment,
ResourceID: prodEnvironment.ID,
Type: api.PolicyTypeSQLReview,
Payload: &policyPayload,
})
a.NoError(err)
+ a.NotNil(policy.Environment)
instance, err := ctl.addInstance(api.InstanceCreate{
EnvironmentID: prodEnvironment.ID,
@@ -1120,7 +1122,7 @@ func TestSQLReviewForMySQL(t *testing.T) {
// disable the SQL review policy
disable := string(api.Archived)
- err = ctl.upsertPolicy(api.PolicyUpsert{
+ _, err = ctl.upsertPolicy(api.PolicyUpsert{
ResourceType: api.PolicyResourceTypeEnvironment,
ResourceID: prodEnvironment.ID,
Type: api.PolicyTypeSQLReview,
diff --git a/tests/tests.go b/tests/tests.go
index 7a4e0e015d..798d7a86c5 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -1845,22 +1845,22 @@ func (ctl *controller) patchSetting(settingPatch api.SettingPatch) error {
}
// upsertPolicy upserts the policy.
-func (ctl *controller) upsertPolicy(policyUpsert api.PolicyUpsert) error {
+func (ctl *controller) upsertPolicy(policyUpsert api.PolicyUpsert) (*api.Policy, error) {
buf := new(bytes.Buffer)
if err := jsonapi.MarshalPayload(buf, &policyUpsert); err != nil {
- return errors.Wrap(err, "failed to marshal policyUpsert")
+ return nil, errors.Wrap(err, "failed to marshal policyUpsert")
}
body, err := ctl.patch(fmt.Sprintf("/policy/%s/%d?type=%s", strings.ToLower(string(policyUpsert.ResourceType)), policyUpsert.ResourceID, policyUpsert.Type), buf)
if err != nil {
- return err
+ return nil, err
}
policy := new(api.Policy)
if err = jsonapi.UnmarshalPayload(body, policy); err != nil {
- return errors.Wrap(err, "fail to unmarshal policy response")
+ return nil, errors.Wrap(err, "fail to unmarshal policy response")
}
- return nil
+ return policy, nil
}
// deletePolicy deletes the archived policy.