/
idp-openid-subcommands.go
555 lines (461 loc) · 13.7 KB
/
idp-openid-subcommands.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Copyright (c) 2015-2023 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 (
"fmt"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/madmin-go/v3"
"github.com/minio/mc/pkg/probe"
)
var idpOpenidAddCmd = cli.Command{
Name: "add",
Usage: "Create an OpenID IDP server configuration",
Action: mainIDPOpenIDAdd,
Before: setGlobalsFromContext,
Flags: globalFlags,
OnUsageError: onUsageError,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET [CFG_NAME] [CFG_PARAMS...]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Create a default OpenID IDP configuration (CFG_NAME is omitted).
{{.Prompt}} {{.HelpName}} play/ \
client_id=minio-client-app \
client_secret=minio-client-app-secret \
config_url="http://localhost:5556/dex/.well-known/openid-configuration" \
scopes="openid,groups" \
redirect_uri="http://127.0.0.1:10000/oauth_callback" \
role_policy="consoleAdmin"
2. Create OpenID IDP configuration named "dex_test".
{{.Prompt}} {{.HelpName}} play/ dex_test \
client_id=minio-client-app \
client_secret=minio-client-app-secret \
config_url="http://localhost:5556/dex/.well-known/openid-configuration" \
scopes="openid,groups" \
redirect_uri="http://127.0.0.1:10000/oauth_callback" \
role_policy="consoleAdmin"
`,
}
func mainIDPOpenIDAdd(ctx *cli.Context) error {
return mainIDPOpenIDAddOrUpdate(ctx, false)
}
func mainIDPOpenIDAddOrUpdate(ctx *cli.Context, update bool) error {
if len(ctx.Args()) < 2 {
showCommandHelpAndExit(ctx, 1)
}
args := ctx.Args()
aliasedURL := args.Get(0)
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")
cfgName := madmin.Default
input := args[1:]
if !strings.Contains(args.Get(1), "=") {
cfgName = args.Get(1)
input = args[2:]
}
inputCfg := strings.Join(input, " ")
restart, e := client.AddOrUpdateIDPConfig(globalContext, madmin.OpenidIDPCfg, cfgName, inputCfg, update)
fatalIf(probe.NewError(e), "Unable to add OpenID IDP config to server")
// Print set config result
printMsg(configSetMessage{
targetAlias: aliasedURL,
restart: restart,
})
return nil
}
var idpOpenidUpdateCmd = cli.Command{
Name: "update",
Usage: "Update an OpenID IDP configuration",
Action: mainIDPOpenIDUpdate,
Before: setGlobalsFromContext,
OnUsageError: onUsageError,
Flags: globalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET [CFG_NAME] [CFG_PARAMS...]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Update the default OpenID IDP configuration (CFG_NAME is omitted).
{{.Prompt}} {{.HelpName}} play/
scopes="openid,groups" \
role_policy="consoleAdmin"
2. Update configuration for OpenID IDP configuration named "dex_test".
{{.Prompt}} {{.HelpName}} play/ dex_test \
scopes="openid,groups" \
role_policy="consoleAdmin"
`,
}
func mainIDPOpenIDUpdate(ctx *cli.Context) error {
return mainIDPOpenIDAddOrUpdate(ctx, true)
}
var idpOpenidRemoveCmd = cli.Command{
Name: "remove",
ShortName: "rm",
Usage: "remove OpenID IDP server configuration",
Action: mainIDPOpenIDRemove,
Before: setGlobalsFromContext,
Flags: globalFlags,
OnUsageError: onUsageError,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET [CFG_NAME]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Remove the default OpenID IDP configuration (CFG_NAME is omitted).
{{.Prompt}} {{.HelpName}} play/
2. Remove OpenID IDP configuration named "dex_test".
{{.Prompt}} {{.HelpName}} play/ dex_test
`,
}
func mainIDPOpenIDRemove(ctx *cli.Context) error {
if len(ctx.Args()) < 1 || len(ctx.Args()) > 2 {
showCommandHelpAndExit(ctx, 1)
}
args := ctx.Args()
var cfgName string
if len(args) == 2 {
cfgName = args.Get(1)
}
return idpRemove(ctx, true, cfgName)
}
func idpRemove(ctx *cli.Context, isOpenID bool, cfgName string) error {
args := ctx.Args()
aliasedURL := args.Get(0)
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")
idpType := madmin.LDAPIDPCfg
if isOpenID {
idpType = madmin.OpenidIDPCfg
}
restart, e := client.DeleteIDPConfig(globalContext, idpType, cfgName)
fatalIf(probe.NewError(e), "Unable to remove %s IDP config '%s'", idpType, cfgName)
printMsg(configSetMessage{
targetAlias: aliasedURL,
restart: restart,
})
return nil
}
var idpOpenidListCmd = cli.Command{
Name: "list",
ShortName: "ls",
Usage: "list OpenID IDP server configuration(s)",
Action: mainIDPOpenIDList,
Before: setGlobalsFromContext,
Flags: globalFlags,
OnUsageError: onUsageError,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. List configurations for OpenID IDP.
{{.Prompt}} {{.HelpName}} play/
`,
}
func mainIDPOpenIDList(ctx *cli.Context) error {
if len(ctx.Args()) != 1 {
showCommandHelpAndExit(ctx, 1)
}
return idpListCommon(ctx, true)
}
func idpListCommon(ctx *cli.Context, isOpenID bool) error {
args := ctx.Args()
aliasedURL := args.Get(0)
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")
idpType := madmin.LDAPIDPCfg
if isOpenID {
idpType = madmin.OpenidIDPCfg
}
result, e := client.ListIDPConfig(globalContext, idpType)
fatalIf(probe.NewError(e), "Unable to list IDP config for '%s'", idpType)
printMsg(idpCfgList(result))
return nil
}
type idpCfgList []madmin.IDPListItem
func (i idpCfgList) JSON() string {
bs, e := json.MarshalIndent(i, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(bs)
}
func (i idpCfgList) String() string {
maxNameWidth := len("Name")
maxRoleARNWidth := len("RoleArn")
for _, item := range i {
name := item.Name
if name == "_" {
name = "(default)" // for the un-named config, don't show `_`
}
if maxNameWidth < len(name) {
maxNameWidth = len(name)
}
if maxRoleARNWidth < len(item.RoleARN) {
maxRoleARNWidth = len(item.RoleARN)
}
}
enabledWidth := 5
// Add 2 for padding
maxNameWidth += 2
maxRoleARNWidth += 2
enabledColStyle := lipgloss.NewStyle().
Align(lipgloss.Center).
PaddingLeft(1).
PaddingRight(1).
Width(enabledWidth)
nameColStyle := lipgloss.NewStyle().
Align(lipgloss.Right).
PaddingLeft(1).
PaddingRight(1).
Width(maxNameWidth)
arnColStyle := lipgloss.NewStyle().
Align(lipgloss.Left).
PaddingLeft(1).
PaddingRight(1).
Foreground(lipgloss.Color("#04B575")). // green
Width(maxRoleARNWidth)
styles := []lipgloss.Style{enabledColStyle, nameColStyle, arnColStyle}
headers := []string{"On?", "Name", "RoleARN"}
headerRow := []string{}
// Override some style settings for the header
for ii, hdr := range headers {
headerRow = append(headerRow,
styles[ii].Copy().
Bold(true).
Foreground(lipgloss.Color("#6495ed")). // green
Align(lipgloss.Center).
Render(hdr),
)
}
lines := []string{strings.Join(headerRow, "")}
enabledOff := "🔴"
enabledOn := "🟢"
for _, item := range i {
enabled := enabledOff
if item.Enabled {
enabled = enabledOn
}
line := []string{
styles[0].Render(enabled),
styles[1].Render(item.Name),
styles[2].Render(item.RoleARN),
}
if item.Name == "_" {
// For default config, don't display `_` and make it look faint.
line[1] = styles[1].Copy().
Faint(true).
Render("(default)")
}
lines = append(lines, strings.Join(line, ""))
}
boxContent := strings.Join(lines, "\n")
boxStyle := lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder())
return boxStyle.Render(boxContent)
}
var idpOpenidInfoCmd = cli.Command{
Name: "info",
Usage: "get OpenID IDP server configuration info",
Action: mainIDPOpenIDInfo,
Before: setGlobalsFromContext,
Flags: globalFlags,
OnUsageError: onUsageError,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET [CFG_NAME]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Get configuration info on the default OpenID IDP configuration (CFG_NAME is omitted).
{{.Prompt}} {{.HelpName}} play/
2. Get configuration info on OpenID IDP configuration named "dex_test".
{{.Prompt}} {{.HelpName}} play/ dex_test
`,
}
func mainIDPOpenIDInfo(ctx *cli.Context) error {
if len(ctx.Args()) < 1 || len(ctx.Args()) > 2 {
showCommandHelpAndExit(ctx, 1)
}
args := ctx.Args()
var cfgName string
if len(args) == 2 {
cfgName = args.Get(1)
}
return idpInfo(ctx, true, cfgName)
}
func idpInfo(ctx *cli.Context, isOpenID bool, cfgName string) error {
args := ctx.Args()
aliasedURL := args.Get(0)
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")
idpType := madmin.LDAPIDPCfg
if isOpenID {
idpType = madmin.OpenidIDPCfg
}
result, e := client.GetIDPConfig(globalContext, idpType, cfgName)
fatalIf(probe.NewError(e), "Unable to get %s IDP config from server", idpType)
// Print set config result
printMsg(idpConfig(result))
return nil
}
type idpConfig madmin.IDPConfig
func (i idpConfig) JSON() string {
bs, e := json.MarshalIndent(i, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(bs)
}
func (i idpConfig) String() string {
if len(i.Info) == 0 {
return "Not configured."
}
// Determine required width for key column.
fieldColWidth := 0
for _, kv := range i.Info {
if fieldColWidth < len(kv.Key) {
fieldColWidth = len(kv.Key)
}
}
// Add 1 for the colon-suffix in each entry.
fieldColWidth++
fieldColStyle := lipgloss.NewStyle().
Width(fieldColWidth).
Foreground(lipgloss.Color("#04B575")). // green
Bold(true).
Align(lipgloss.Right)
valueColStyle := lipgloss.NewStyle().
PaddingLeft(1).
Align(lipgloss.Left)
envMarkStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("201")). // pinkish-red
PaddingLeft(1)
var lines []string
for _, kv := range i.Info {
envStr := ""
if kv.IsCfg && kv.IsEnv {
envStr = " (environment)"
}
lines = append(lines, fmt.Sprintf("%s%s%s",
fieldColStyle.Render(kv.Key+":"),
valueColStyle.Render(kv.Value),
envMarkStyle.Render(envStr),
))
}
boxContent := strings.Join(lines, "\n")
boxStyle := lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder())
return boxStyle.Render(boxContent)
}
var idpOpenidEnableCmd = cli.Command{
Name: "enable",
Usage: "enable an OpenID IDP server configuration",
Action: mainIDPOpenIDEnable,
Before: setGlobalsFromContext,
Flags: globalFlags,
OnUsageError: onUsageError,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET [CFG_NAME]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Enable the default OpenID IDP configuration (CFG_NAME is omitted).
{{.Prompt}} {{.HelpName}} play/
2. Enable OpenID IDP configuration named "dex_test".
{{.Prompt}} {{.HelpName}} play/ dex_test
`,
}
func mainIDPOpenIDEnable(ctx *cli.Context) error {
isOpenID, enable := true, true
return idpEnableDisable(ctx, isOpenID, enable)
}
func idpEnableDisable(ctx *cli.Context, isOpenID, enable bool) error {
if len(ctx.Args()) < 1 || len(ctx.Args()) > 2 {
showCommandHelpAndExit(ctx, 1)
}
args := ctx.Args()
cfgName := madmin.Default
if len(args) == 2 {
cfgName = args.Get(1)
}
aliasedURL := args.Get(0)
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")
idpType := madmin.LDAPIDPCfg
if isOpenID {
idpType = madmin.OpenidIDPCfg
}
configBody := "enable=on"
if !enable {
configBody = "enable=off"
}
restart, e := client.AddOrUpdateIDPConfig(globalContext, idpType, cfgName, configBody, true)
fatalIf(probe.NewError(e), "Unable to remove %s IDP config '%s'", idpType, cfgName)
printMsg(configSetMessage{
targetAlias: aliasedURL,
restart: restart,
})
return nil
}
var idpOpenidDisableCmd = cli.Command{
Name: "disable",
Usage: "Disable an OpenID IDP server configuration",
Action: mainIDPOpenIDDisable,
Before: setGlobalsFromContext,
Flags: globalFlags,
OnUsageError: onUsageError,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET [CFG_NAME]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Disable the default OpenID IDP configuration (CFG_NAME is omitted).
{{.Prompt}} {{.HelpName}} play/
2. Disable OpenID IDP configuration named "dex_test".
{{.Prompt}} {{.HelpName}} play/ dex_test
`,
}
func mainIDPOpenIDDisable(ctx *cli.Context) error {
isOpenID, enable := true, false
return idpEnableDisable(ctx, isOpenID, enable)
}