Skip to content

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

fix: nil pointer and the cache for policy (#3810)

ecmadao6ce8f59b321a
diff --git a/store/environment.go b/store/environment.go
index f980b8efc6..6d3966883d 100644
--- a/store/environment.go
+++ b/store/environment.go
@@ -139,7 +139,7 @@ func (s *Store) GetEnvironmentByID(ctx context.Context, id int) (*api.Environmen
return nil, errors.Wrapf(err, "failed to get environment with ID %d", id)
}
if envRaw == nil {
- return nil, nil
+ return nil, common.Errorf(common.NotFound, "environment %d not found", id)
}
env, err := s.composeEnvironment(ctx, envRaw)
@@ -249,11 +249,8 @@ func (s *Store) upsertEnvironmentPolicy(
return nil, err
}
- // Cache environment tier policy as it is used widely.
- if policyUpsert.Type == api.PolicyTypeEnvironmentTier {
- if err := s.cache.UpsertCache(tierPolicyCacheNamespace, policyUpsert.ResourceID, policyRaw); err != nil {
- return nil, err
- }
+ if err := s.upsertPolicyCache(policyUpsert.Type, policyUpsert.ResourceID, policyRaw.Payload); err != nil {
+ return nil, err
}
return policyRaw, nil
diff --git a/store/policy.go b/store/policy.go
index 7b15e96992..0f591a7790 100644
--- a/store/policy.go
+++ b/store/policy.go
@@ -70,17 +70,10 @@ func (s *Store) UpsertPolicy(ctx context.Context, upsert *api.PolicyUpsert) (*ap
return nil, errors.Wrapf(err, "failed to compose policy with policyRaw[%+v]", policyRaw)
}
- // Cache environment tier policy as it is used widely.
- switch upsert.Type {
- case api.PolicyTypeEnvironmentTier:
- if err := s.cache.UpsertCache(tierPolicyCacheNamespace, upsert.ResourceID, &policy.Payload); err != nil {
- return nil, err
- }
- case api.PolicyTypePipelineApproval:
- if err := s.cache.UpsertCache(approvalPolicyCacheNamespace, upsert.ResourceID, &policy.Payload); err != nil {
- return nil, err
- }
+ if err := s.upsertPolicyCache(upsert.Type, upsert.ResourceID, policy.Payload); err != nil {
+ return nil, err
}
+
return policy, nil
}
@@ -329,11 +322,13 @@ func (s *Store) composePolicy(ctx context.Context, raw *policyRaw) (*api.Policy,
}
policy.Updater = updater
- env, err := s.GetEnvironmentByID(ctx, policy.ResourceID)
- if err != nil {
- return nil, err
+ if policy.ResourceType == api.PolicyResourceTypeEnvironment {
+ env, err := s.GetEnvironmentByID(ctx, policy.ResourceID)
+ if err != nil {
+ return nil, err
+ }
+ policy.Environment = env
}
- policy.Environment = env
return policy, nil
}
@@ -560,3 +555,18 @@ func (*Store) deletePolicyImpl(ctx context.Context, tx *Tx, delete *api.PolicyDe
}
return nil
}
+
+// Cache environment tier policy and pipeline approval policy as it is used widely.
+func (s *Store) upsertPolicyCache(policyType api.PolicyType, resourceID int, payload string) error {
+ switch policyType {
+ case api.PolicyTypeEnvironmentTier:
+ if err := s.cache.UpsertCache(tierPolicyCacheNamespace, resourceID, &payload); err != nil {
+ return err
+ }
+ case api.PolicyTypePipelineApproval:
+ if err := s.cache.UpsertCache(approvalPolicyCacheNamespace, resourceID, &payload); err != nil {
+ return err
+ }
+ }
+ return nil
+}