|
| 1 | +/* |
| 2 | + * MinIO Client (C) 2020 MinIO, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "io/ioutil" |
| 21 | + |
| 22 | + "github.com/minio/cli" |
| 23 | + json "github.com/minio/mc/pkg/colorjson" |
| 24 | + "github.com/minio/mc/pkg/probe" |
| 25 | + "github.com/minio/minio/pkg/console" |
| 26 | +) |
| 27 | + |
| 28 | +var adminUserSAGenerateCmd = cli.Command{ |
| 29 | + Name: "generate", |
| 30 | + Usage: "generate a new service account", |
| 31 | + Action: mainAdminUserSAGenerate, |
| 32 | + Before: setGlobalsFromContext, |
| 33 | + Flags: globalFlags, |
| 34 | + CustomHelpTemplate: `NAME: |
| 35 | + {{.HelpName}} - {{.Usage}} |
| 36 | +
|
| 37 | +USAGE: |
| 38 | + {{.HelpName}} TARGET PARENT-USER [POLICY_FILE] |
| 39 | +
|
| 40 | +PARENT-USER: |
| 41 | + The parent user. |
| 42 | +
|
| 43 | +POLICY_FILE: |
| 44 | + The path of the policy to apply for the new service account. |
| 45 | + When unspecified, the policy of the parent user will be evaluated |
| 46 | + instead for all type of service accounts requests. |
| 47 | +
|
| 48 | +FLAGS: |
| 49 | + {{range .VisibleFlags}}{{.}} |
| 50 | + {{end}} |
| 51 | +
|
| 52 | +EXAMPLES: |
| 53 | + 1. Add a new service account under the name of 'foobar' to MinIO server. |
| 54 | + {{.Prompt}} {{.HelpName}} myminio foobar /tmp/policy.json |
| 55 | +`, |
| 56 | +} |
| 57 | + |
| 58 | +func checkAdminUserSAGenerateSyntax(ctx *cli.Context) { |
| 59 | + if len(ctx.Args()) < 2 || len(ctx.Args()) > 3 { |
| 60 | + cli.ShowCommandHelpAndExit(ctx, "generate", 1) // last argument is exit code |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// saMessage container for content message structure |
| 65 | +type saMessage struct { |
| 66 | + AccessKey string `json:"accessKey,omitempty"` |
| 67 | + SecretKey string `json:"secretKey,omitempty"` |
| 68 | + SessionToken string `json:"sessionToken,omitempty"` |
| 69 | +} |
| 70 | + |
| 71 | +func (u saMessage) String() string { |
| 72 | + dot := console.Colorize("SA", "‣ ") |
| 73 | + msg := dot + console.Colorize("SA", "Access Key: ") + console.Colorize("AccessKey", u.AccessKey) + "\n" |
| 74 | + msg += dot + console.Colorize("SA", "Secret Key: ") + console.Colorize("SecretKey", u.SecretKey) + "\n" |
| 75 | + msg += dot + console.Colorize("SA", "Session Token: ") + console.Colorize("SessionToken", u.SessionToken) |
| 76 | + |
| 77 | + return msg |
| 78 | +} |
| 79 | + |
| 80 | +func (u saMessage) JSON() string { |
| 81 | + jsonMessageBytes, e := json.MarshalIndent(u, "", " ") |
| 82 | + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") |
| 83 | + |
| 84 | + return string(jsonMessageBytes) |
| 85 | +} |
| 86 | + |
| 87 | +func mainAdminUserSAGenerate(ctx *cli.Context) error { |
| 88 | + setSACommandColors() |
| 89 | + checkAdminUserSAGenerateSyntax(ctx) |
| 90 | + |
| 91 | + // Get the alias parameter from cli |
| 92 | + args := ctx.Args() |
| 93 | + aliasedURL := args.Get(0) |
| 94 | + |
| 95 | + // Create a new MinIO Admin Client |
| 96 | + client, err := newAdminClient(aliasedURL) |
| 97 | + fatalIf(err, "Unable to initialize admin connection.") |
| 98 | + |
| 99 | + parentUser := args.Get(1) |
| 100 | + policyDocPath := args.Get(2) |
| 101 | + |
| 102 | + var policyDoc []byte |
| 103 | + var e error |
| 104 | + |
| 105 | + if len(policyDocPath) > 0 { |
| 106 | + policyDoc, e = ioutil.ReadFile(policyDocPath) |
| 107 | + fatalIf(probe.NewError(e).Trace(args...), "Cannot load the policy document") |
| 108 | + } |
| 109 | + |
| 110 | + creds, e := client.AddServiceAccount(globalContext, parentUser, string(policyDoc)) |
| 111 | + fatalIf(probe.NewError(e).Trace(args...), "Cannot add new service account") |
| 112 | + |
| 113 | + printMsg(saMessage{ |
| 114 | + AccessKey: creds.AccessKey, |
| 115 | + SecretKey: creds.SecretKey, |
| 116 | + SessionToken: creds.SessionToken, |
| 117 | + }) |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
0 commit comments