fix: skip policy usage validation for cache update (#19008)

When updating the policy cache, we do not need to validate policy usage
as the policy has already been deleted by the node sending the
notification.
This commit is contained in:
Aditya Manthramurthy 2024-02-07 20:39:53 -08:00 committed by GitHub
parent 7e082f232e
commit e104b183d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 35 deletions

View File

@ -1166,8 +1166,11 @@ func (store *IAMStoreSys) PolicyNotificationHandler(ctx context.Context, policy
return err return err
} }
// DeletePolicy - deletes policy from storage and cache. // DeletePolicy - deletes policy from storage and cache. When this called in
func (store *IAMStoreSys) DeletePolicy(ctx context.Context, policy string) error { // response to a notification (i.e. isFromNotification = true), it skips the
// validation of policy usage and the attempt to delete in the backend as well
// (as this is already done by the notifying node).
func (store *IAMStoreSys) DeletePolicy(ctx context.Context, policy string, isFromNotification bool) error {
if policy == "" { if policy == "" {
return errInvalidArgument return errInvalidArgument
} }
@ -1175,6 +1178,7 @@ func (store *IAMStoreSys) DeletePolicy(ctx context.Context, policy string) error
cache := store.lock() cache := store.lock()
defer store.unlock() defer store.unlock()
if !isFromNotification {
// Check if policy is mapped to any existing user or group. If so, we do not // Check if policy is mapped to any existing user or group. If so, we do not
// allow deletion of the policy. If the policy is mapped to an STS account, // allow deletion of the policy. If the policy is mapped to an STS account,
// we do allow deletion. // we do allow deletion.
@ -1212,6 +1216,7 @@ func (store *IAMStoreSys) DeletePolicy(ctx context.Context, policy string) error
if err != nil { if err != nil {
return err return err
} }
}
delete(cache.iamPolicyDocsMap, policy) delete(cache.iamPolicyDocsMap, policy)
cache.updatedAt = time.Now() cache.updatedAt = time.Now()

View File

@ -526,7 +526,9 @@ func (sys *IAMSys) GetRolePolicy(arnStr string) (arn.ARN, string, error) {
return roleArn, rolePolicy, nil return roleArn, rolePolicy, nil
} }
// DeletePolicy - deletes a canned policy from backend or etcd. // DeletePolicy - deletes a canned policy from backend. `notifyPeers` is true
// whenever this is called via the API. It is false when called via a
// notification from another peer. This is to avoid infinite loops.
func (sys *IAMSys) DeletePolicy(ctx context.Context, policyName string, notifyPeers bool) error { func (sys *IAMSys) DeletePolicy(ctx context.Context, policyName string, notifyPeers bool) error {
if !sys.Initialized() { if !sys.Initialized() {
return errServerNotInitialized return errServerNotInitialized
@ -540,7 +542,7 @@ func (sys *IAMSys) DeletePolicy(ctx context.Context, policyName string, notifyPe
} }
} }
err := sys.store.DeletePolicy(ctx, policyName) err := sys.store.DeletePolicy(ctx, policyName, !notifyPeers)
if err != nil { if err != nil {
return err return err
} }