/
admin-user-svcacct-add.go
371 lines (324 loc) · 10.9 KB
/
admin-user-svcacct-add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"bytes"
"crypto/rand"
"encoding/base64"
"fmt"
"os"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/madmin-go/v3"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/v2/console"
"github.com/minio/pkg/v2/policy"
)
var adminUserSvcAcctAddFlags = []cli.Flag{
cli.StringFlag{
Name: "access-key",
Usage: "set an access key for the service account",
},
cli.StringFlag{
Name: "secret-key",
Usage: "set a secret key for the service account",
},
cli.StringFlag{
Name: "policy",
Usage: "path to a JSON policy file",
},
cli.StringFlag{
Name: "name",
Usage: "friendly name for the service account",
},
cli.StringFlag{
Name: "description",
Usage: "description for the service account",
},
cli.StringFlag{
Name: "comment",
Hidden: true,
Usage: "description for the service account (DEPRECATED: use --description instead)",
},
cli.StringFlag{
Name: "expiry",
Usage: "time of expiration for the service account",
},
}
var adminUserSvcAcctAddCmd = cli.Command{
Name: "add",
Usage: "add a new service account",
Action: mainAdminUserSvcAcctAdd,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: append(adminUserSvcAcctAddFlags, globalFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} ALIAS ACCOUNT [FLAGS]
ACCOUNT:
An account could be a regular MinIO user, STS or LDAP user.
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Add a new service account for user 'foobar' to MinIO server with a name and description.
{{.Prompt}} {{.HelpName}} myminio foobar --name uploaderKey --description "foobar uploader scripts"
2. Add a new service account to MinIO server with specified access key and secret key for user 'foobar'.
{{.Prompt}} {{.HelpName}} myminio foobar --access-key "myaccesskey" --secret-key "mysecretkey"
3. Add a new service account to MinIO server with specified access key and random secret key for user 'foobar'.
{{.Prompt}} {{.HelpName}} myminio foobar --access-key "myaccesskey"
4. Add a new service account to MinIO server with specified secret key and random access key for user 'foobar'.
{{.Prompt}} {{.HelpName}} myminio foobar --secret-key "mysecretkey"
5. Add a new service account to MinIO server with specified expiry date in the future for user 'foobar'.
{{.Prompt}} {{.HelpName}} myminio foobar --expiry 2023-06-24T10:00:00-07:00
`,
}
// checkAdminUserSvcAcctAddSyntax - validate all the passed arguments
func checkAdminUserSvcAcctAddSyntax(ctx *cli.Context) {
if len(ctx.Args()) != 2 {
showCommandHelpAndExit(ctx, 1)
}
}
// acctMessage container for content message structure
type acctMessage struct {
op acctOp
Status string `json:"status"`
AccessKey string `json:"accessKey,omitempty"`
SecretKey string `json:"secretKey,omitempty"`
ParentUser string `json:"parentUser,omitempty"`
ImpliedPolicy bool `json:"impliedPolicy,omitempty"`
Policy json.RawMessage `json:"policy,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
AccountStatus string `json:"accountStatus,omitempty"`
MemberOf []string `json:"memberOf,omitempty"`
Expiration *time.Time `json:"expiration,omitempty"`
}
const (
accessFieldMaxLen = 20
)
type acctOp int
const (
svcAccOpAdd = acctOp(iota)
svcAccOpList
svcAccOpInfo
svcAccOpRemove
svcAccOpDisable
svcAccOpEnable
svcAccOpSet
stsAccOpInfo
// Maximum length for MinIO access key.
// There is no max length enforcement for access keys
accessKeyMaxLen = 20
// Maximum length for Expiration timestamp
expirationMaxLen = 29
// Alpha numeric table used for generating access keys.
alphaNumericTable = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Total length of the alpha numeric table.
alphaNumericTableLen = byte(len(alphaNumericTable))
// Maximum secret key length for MinIO, this
// is used when autogenerating new credentials.
// There is no max length enforcement for secret keys
secretKeyMaxLen = 40
)
var supportedTimeFormats = []string{
"2006-01-02",
"2006-01-02T15:04",
"2006-01-02T15:04:05",
time.RFC3339,
}
func (u acctMessage) String() string {
switch u.op {
case svcAccOpList:
// Create a new pretty table with cols configuration
return newPrettyTable(" | ",
Field{"AccessKey", accessFieldMaxLen},
Field{"Expiration", expirationMaxLen},
).buildRow(u.AccessKey, func() string {
if u.Expiration != nil && !u.Expiration.IsZero() {
return (*u.Expiration).String()
}
return "no-expiry"
}())
case stsAccOpInfo, svcAccOpInfo:
policyField := ""
if u.ImpliedPolicy {
policyField = "implied"
} else {
policyField = "embedded"
}
return console.Colorize("AccMessage", strings.Join(
[]string{
fmt.Sprintf("AccessKey: %s", u.AccessKey),
fmt.Sprintf("ParentUser: %s", u.ParentUser),
fmt.Sprintf("Status: %s", u.AccountStatus),
fmt.Sprintf("Name: %s", u.Name),
fmt.Sprintf("Description: %s", u.Description),
fmt.Sprintf("Policy: %s", policyField),
func() string {
if u.Expiration != nil {
return fmt.Sprintf("Expiration: %s", humanize.Time(*u.Expiration))
}
return "Expiration: no-expiry"
}(),
}, "\n"))
case svcAccOpRemove:
return console.Colorize("AccMessage", "Removed service account `"+u.AccessKey+"` successfully.")
case svcAccOpDisable:
return console.Colorize("AccMessage", "Disabled service account `"+u.AccessKey+"` successfully.")
case svcAccOpEnable:
return console.Colorize("AccMessage", "Enabled service account `"+u.AccessKey+"` successfully.")
case svcAccOpAdd:
if u.Expiration != nil && !u.Expiration.IsZero() && !u.Expiration.Equal(timeSentinel) {
return console.Colorize("AccMessage",
fmt.Sprintf("Access Key: %s\nSecret Key: %s\nExpiration: %s", u.AccessKey, u.SecretKey, *u.Expiration))
}
return console.Colorize("AccMessage",
fmt.Sprintf("Access Key: %s\nSecret Key: %s\nExpiration: no-expiry", u.AccessKey, u.SecretKey))
case svcAccOpSet:
return console.Colorize("AccMessage", "Edited service account `"+u.AccessKey+"` successfully.")
}
return ""
}
// generateCredentials - creates randomly generated credentials of maximum
// allowed length.
func generateCredentials() (accessKey, secretKey string, err error) {
readBytes := func(size int) (data []byte, err error) {
data = make([]byte, size)
var n int
if n, err = rand.Read(data); err != nil {
return nil, err
} else if n != size {
return nil, fmt.Errorf("Not enough data. Expected to read: %v bytes, got: %v bytes", size, n)
}
return data, nil
}
// Generate access key.
keyBytes, err := readBytes(accessKeyMaxLen)
if err != nil {
return "", "", err
}
for i := 0; i < accessKeyMaxLen; i++ {
keyBytes[i] = alphaNumericTable[keyBytes[i]%alphaNumericTableLen]
}
accessKey = string(keyBytes)
// Generate secret key.
keyBytes, err = readBytes(secretKeyMaxLen)
if err != nil {
return "", "", err
}
secretKey = strings.ReplaceAll(string([]byte(base64.StdEncoding.EncodeToString(keyBytes))[:secretKeyMaxLen]),
"/", "+")
return accessKey, secretKey, nil
}
func (u acctMessage) JSON() string {
u.Status = "success"
jsonMessageBytes, e := json.MarshalIndent(u, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonMessageBytes)
}
// mainAdminUserSvcAcctAdd is the handle for "mc admin user svcacct add" command.
func mainAdminUserSvcAcctAdd(ctx *cli.Context) error {
checkAdminUserSvcAcctAddSyntax(ctx)
console.SetColor("AccMessage", color.New(color.FgGreen))
// Get the alias parameter from cli
args := ctx.Args()
aliasedURL := args.Get(0)
user := args.Get(1)
accessKey := ctx.String("access-key")
secretKey := ctx.String("secret-key")
policyPath := ctx.String("policy")
name := ctx.String("name")
description := ctx.String("description")
if description == "" {
description = ctx.String("comment")
}
expiry := ctx.String("expiry")
// generate access key and secret key
if len(accessKey) <= 0 || len(secretKey) <= 0 {
randomAccessKey, randomSecretKey, err := generateCredentials()
if err != nil {
fatalIf(probe.NewError(err), "Unable to add a new service account")
}
if len(accessKey) <= 0 {
accessKey = randomAccessKey
}
if len(secretKey) <= 0 {
secretKey = randomSecretKey
}
}
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")
var policyBytes []byte
if policyPath != "" {
// Validate the policy document and ensure it has at least when statement
var e error
policyBytes, e = os.ReadFile(policyPath)
fatalIf(probe.NewError(e), "Unable to open the policy document.")
p, e := policy.ParseConfig(bytes.NewReader(policyBytes))
fatalIf(probe.NewError(e), "Unable to parse the policy document.")
if p.IsEmpty() {
fatalIf(errInvalidArgument(), "Empty policy documents are not allowed.")
}
}
var expiryTime time.Time
var expiryPointer *time.Time
if expiry != "" {
location, e := time.LoadLocation("Local")
if e != nil {
fatalIf(probe.NewError(e), "Unable to parse the expiry argument.")
}
patternMatched := false
for _, format := range supportedTimeFormats {
t, e := time.ParseInLocation(format, expiry, location)
if e == nil {
patternMatched = true
expiryTime = t
expiryPointer = &expiryTime
break
}
}
if !patternMatched {
fatalIf(probe.NewError(fmt.Errorf("expiry argument is not matching any of the supported patterns")), "unable to parse the expiry argument.")
}
}
opts := madmin.AddServiceAccountReq{
Policy: policyBytes,
AccessKey: accessKey,
SecretKey: secretKey,
Name: name,
Description: description,
TargetUser: user,
Expiration: expiryPointer,
}
creds, e := client.AddServiceAccount(globalContext, opts)
fatalIf(probe.NewError(e).Trace(args...), "Unable to add a new service account.")
printMsg(acctMessage{
op: svcAccOpAdd,
AccessKey: creds.AccessKey,
SecretKey: creds.SecretKey,
Expiration: &creds.Expiration,
AccountStatus: "enabled",
})
return nil
}