Skip to content

Commit cf128de

Browse files
Do not fail mc-admin-policy-attach if policy already attached/detached (#5058)
Currently, attempts to attach a policy to a user who already has the policy attached to them results in a 400. This change handles cases where policy attach/detach operations are automated using scripts/jobs. A re-run of the attach/detach step should not fail the entire job.
1 parent 07eeb10 commit cf128de

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

cmd/admin-policy-attach.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import (
2323
"github.com/minio/mc/pkg/probe"
2424
)
2525

26+
const (
27+
errCodeChangeAlreadyApplied = "XMinioAdminPolicyChangeAlreadyApplied"
28+
)
29+
2630
var adminAttachPolicyFlags = []cli.Flag{
2731
cli.StringFlag{
2832
Name: "user, u",
@@ -97,7 +101,10 @@ func userAttachOrDetachPolicy(ctx *cli.Context, attach bool) error {
97101
} else {
98102
res, e = client.DetachPolicy(globalContext, req)
99103
}
100-
fatalIf(probe.NewError(e), "Unable to make user/group policy association")
104+
105+
if e != nil && madmin.ToErrorResponse(e).Code != errCodeChangeAlreadyApplied {
106+
fatalIf(probe.NewError(e), "Unable to make user/group policy association")
107+
}
101108

102109
var emptyResp madmin.PolicyAssociationResp
103110
if res.UpdatedAt == emptyResp.UpdatedAt {

functional-tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,9 @@ function test_admin_users() {
967967
# check that the user can write objects with readwrite policy
968968
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin policy attach "$SERVER_ALIAS" readwrite --user="${username}"
969969

970+
# verify that re-attaching an already attached policy to a user does not result in a failure.
971+
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin policy attach "$SERVER_ALIAS" readwrite --user="${username}"
972+
970973
# Validate that the correct policy has been added to the user
971974
"${MC_CMD[@]}" --json admin user list "${SERVER_ALIAS}" | jq -r '.policyName' | grep --quiet "^readwrite$"
972975
rv=$?

0 commit comments

Comments
 (0)