Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not fail mc-admin-policy-attach if policy already attached/detached #5058

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion cmd/admin-policy-attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"github.com/minio/mc/pkg/probe"
)

const (
errCodeChangeAlreadyApplied = "XMinioAdminPolicyChangeAlreadyApplied"
)

var adminAttachPolicyFlags = []cli.Flag{
cli.StringFlag{
Name: "user, u",
Expand Down Expand Up @@ -68,6 +72,16 @@ func mainAdminPolicyAttach(ctx *cli.Context) error {
return userAttachOrDetachPolicy(ctx, true)
}

func isCodeChangeAlreadyAppliedError(e error) bool {
dhananjaykrutika marked this conversation as resolved.
Show resolved Hide resolved
switch v := e.(type) {
case madmin.ErrorResponse:
if v.Code == errCodeChangeAlreadyApplied {
return true
}
}
return false
}

func userAttachOrDetachPolicy(ctx *cli.Context, attach bool) error {
if len(ctx.Args()) < 2 {
showCommandHelpAndExit(ctx, 1) // last argument is exit code
Expand Down Expand Up @@ -97,7 +111,10 @@ func userAttachOrDetachPolicy(ctx *cli.Context, attach bool) error {
} else {
res, e = client.DetachPolicy(globalContext, req)
}
fatalIf(probe.NewError(e), "Unable to make user/group policy association")

if e != nil && !isCodeChangeAlreadyAppliedError(e) {
dhananjaykrutika marked this conversation as resolved.
Show resolved Hide resolved
fatalIf(probe.NewError(e), "Unable to make user/group policy association")
}

var emptyResp madmin.PolicyAssociationResp
if res.UpdatedAt == emptyResp.UpdatedAt {
Expand Down
3 changes: 3 additions & 0 deletions functional-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,9 @@ function test_admin_users() {
# check that the user can write objects with readwrite policy
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin policy attach "$SERVER_ALIAS" readwrite --user="${username}"

# verify that re-attaching an already attached policy to a user does not result in a failure.
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin policy attach "$SERVER_ALIAS" readwrite --user="${username}"

# Validate that the correct policy has been added to the user
"${MC_CMD[@]}" --json admin user list "${SERVER_ALIAS}" | jq -r '.policyName' | grep --quiet "^readwrite$"
rv=$?
Expand Down
Loading