|
| 1 | +// Copyright (c) 2015-2025 MinIO, Inc. |
| 2 | +// |
| 3 | +// This file is part of MinIO Object Storage stack |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +package cmd |
| 19 | + |
| 20 | +import ( |
| 21 | + "github.com/minio/cli" |
| 22 | + json "github.com/minio/colorjson" |
| 23 | + "github.com/minio/madmin-go/v3" |
| 24 | + "github.com/minio/mc/pkg/probe" |
| 25 | +) |
| 26 | + |
| 27 | +var adminAccesskeySTSRevokeFlags = []cli.Flag{ |
| 28 | + cli.BoolFlag{ |
| 29 | + Name: "all", |
| 30 | + Usage: "revoke all STS accounts for the specified user", |
| 31 | + }, |
| 32 | + cli.BoolFlag{ |
| 33 | + Name: "self", |
| 34 | + Usage: "revoke all STS accounts for the authenticated user", |
| 35 | + }, |
| 36 | + cli.StringFlag{ |
| 37 | + Name: "token-type", |
| 38 | + Usage: "specify the token type to revoke", |
| 39 | + }, |
| 40 | +} |
| 41 | + |
| 42 | +var adminAccesskeySTSRevokeCmd = cli.Command{ |
| 43 | + Name: "sts-revoke", |
| 44 | + Usage: "revokes all STS accounts or specified types for the specified user", |
| 45 | + Action: mainAdminAccesskeySTSRevoke, |
| 46 | + OnUsageError: onUsageError, |
| 47 | + Before: setGlobalsFromContext, |
| 48 | + Flags: append(adminAccesskeySTSRevokeFlags, globalFlags...), |
| 49 | + CustomHelpTemplate: `NAME: |
| 50 | + {{.HelpName}} - {{.Usage}} |
| 51 | +
|
| 52 | +USAGE: |
| 53 | + {{.HelpName}} ALIAS USER [--all | --token-type TOKEN_TYPE] |
| 54 | +
|
| 55 | + Exactly one of --all or --token-type must be specified. |
| 56 | +
|
| 57 | +FLAGS: |
| 58 | + {{range .VisibleFlags}}{{.}} |
| 59 | + {{end}} |
| 60 | +EXAMPLES: |
| 61 | + 1. Revoke all STS accounts for user "user1" |
| 62 | + {{.Prompt}} {{.HelpName}} myminio user1 --all |
| 63 | +
|
| 64 | + 2. Revoke STS accounts of a token type "app-1" for user "user1" |
| 65 | + {{.Prompt}} {{.HelpName}} myminio user1 --token-type app-1 |
| 66 | +
|
| 67 | + 3. Revoke all STS accounts for the authenticated user |
| 68 | + {{.Prompt}} {{.HelpName}} myminio --self |
| 69 | +
|
| 70 | + 4. Revoke STS accounts of a token type "app-1" for the authenticated user |
| 71 | + {{.Prompt}} {{.HelpName}} myminio --self --token-type app-1 |
| 72 | +`, |
| 73 | +} |
| 74 | + |
| 75 | +type stsRevokeMessage struct { |
| 76 | + Status string `json:"status"` |
| 77 | + User string `json:"user"` |
| 78 | + TokenRevokeType string `json:"tokenRevokeType,omitempty"` |
| 79 | +} |
| 80 | + |
| 81 | +func (m stsRevokeMessage) String() string { |
| 82 | + userString := "user " + m.User |
| 83 | + if m.User == "" { |
| 84 | + userString = "authenticated user" |
| 85 | + } |
| 86 | + if m.TokenRevokeType == "" { |
| 87 | + return "Successfully revoked all STS accounts for " + userString |
| 88 | + } |
| 89 | + return "Successfully revoked all STS accounts of type " + m.TokenRevokeType + " for " + userString |
| 90 | +} |
| 91 | + |
| 92 | +func (m stsRevokeMessage) JSON() string { |
| 93 | + if m.Status == "" { |
| 94 | + m.Status = "success" |
| 95 | + } |
| 96 | + jsonMessageBytes, e := json.MarshalIndent(m, "", " ") |
| 97 | + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") |
| 98 | + |
| 99 | + return string(jsonMessageBytes) |
| 100 | +} |
| 101 | + |
| 102 | +// checkSTSRevokeSyntax - validate all the passed arguments |
| 103 | +func checkSTSRevokeSyntax(ctx *cli.Context) { |
| 104 | + if len(ctx.Args()) > 2 || len(ctx.Args()) == 0 { |
| 105 | + showCommandHelpAndExit(ctx, 1) |
| 106 | + } |
| 107 | + |
| 108 | + if !ctx.Bool("self") && ctx.Args().Get(1) == "" { |
| 109 | + fatalIf(errInvalidArgument().Trace(), "Must specify user or use --self flag.") |
| 110 | + } |
| 111 | + |
| 112 | + if ctx.Bool("self") && ctx.Args().Get(1) != "" { |
| 113 | + fatalIf(errInvalidArgument().Trace(), "Cannot specify user with --self flag.") |
| 114 | + } |
| 115 | + |
| 116 | + if (!ctx.Bool("all") && ctx.String("token-type") == "") || (ctx.Bool("all") && ctx.String("token-type") != "") { |
| 117 | + fatalIf(errDummy().Trace(), "Exactly one of --all or --token-type must be specified.") |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// mainAdminAccesskeySTSRevoke is the handle for "mc admin accesskey sts-revoke" command. |
| 122 | +func mainAdminAccesskeySTSRevoke(ctx *cli.Context) error { |
| 123 | + checkSTSRevokeSyntax(ctx) |
| 124 | + |
| 125 | + // Get the alias parameter from cli |
| 126 | + args := ctx.Args() |
| 127 | + aliasedURL := args.Get(0) |
| 128 | + user := args.Get(1) // will be empty if --self flag is set |
| 129 | + tokenRevokeType := ctx.String("token-type") |
| 130 | + fullRevoke := ctx.Bool("all") |
| 131 | + |
| 132 | + // Create a new MinIO Admin Client |
| 133 | + client, err := newAdminClient(aliasedURL) |
| 134 | + fatalIf(err, "Unable to initialize admin connection.") |
| 135 | + |
| 136 | + e := client.RevokeTokens(globalContext, madmin.RevokeTokensReq{ |
| 137 | + User: user, |
| 138 | + TokenRevokeType: tokenRevokeType, |
| 139 | + FullRevoke: fullRevoke, |
| 140 | + }) |
| 141 | + fatalIf(probe.NewError(e).Trace(args...), "Unable to revoke tokens for %s", user) |
| 142 | + |
| 143 | + printMsg(stsRevokeMessage{ |
| 144 | + User: user, |
| 145 | + TokenRevokeType: tokenRevokeType, |
| 146 | + }) |
| 147 | + |
| 148 | + return nil |
| 149 | +} |
0 commit comments