Skip to content

Commit e6d9c52

Browse files
committed
Add sa command to support service accounts
A new command is added to `mc admin user` command called svc and that's to create new services accounts from existing users. It is also possible to show credentials of the existing service accounts. `mc admin user sa generate PARENT-USER [POLICY_FILE]` `mc admin user sa show SERVICE_ACCOUNT_ACCESS_KEY`
1 parent 64a78e7 commit e6d9c52

File tree

6 files changed

+251
-1
lines changed

6 files changed

+251
-1
lines changed

cmd/admin-user-sa-generate.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}

cmd/admin-user-sa-show.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
"github.com/minio/cli"
21+
"github.com/minio/mc/pkg/probe"
22+
)
23+
24+
var adminUserSAShowCmd = cli.Command{
25+
Name: "show",
26+
Usage: "show the credentials of the specified service account",
27+
Action: mainAdminUserSAShow,
28+
Before: setGlobalsFromContext,
29+
Flags: globalFlags,
30+
CustomHelpTemplate: `NAME:
31+
{{.HelpName}} - {{.Usage}}
32+
33+
USAGE:
34+
{{.HelpName}} TARGET SERVICE-ACCOUNT-ACCESS-KEY
35+
36+
SERVICE-ACCOUNT-ACCESS-KEY:
37+
The access key of the service account.
38+
39+
FLAGS:
40+
{{range .VisibleFlags}}{{.}}
41+
{{end}}
42+
EXAMPLES:
43+
1. Show the credentials of the service account 'SKA762Z7UPIFS5OL1CO4'.
44+
{{.Prompt}} {{.HelpName}} myminio/ SKA762Z7UPIFS5OL1CO4
45+
`,
46+
}
47+
48+
func checkAdminUserSAShowSyntax(ctx *cli.Context) {
49+
if len(ctx.Args()) != 2 {
50+
cli.ShowCommandHelpAndExit(ctx, "show", 1) // last argument is exit code
51+
}
52+
}
53+
54+
func mainAdminUserSAShow(ctx *cli.Context) error {
55+
setSACommandColors()
56+
checkAdminUserSAShowSyntax(ctx)
57+
58+
// Get the alias parameter from cli
59+
args := ctx.Args()
60+
aliasedURL := args.Get(0)
61+
62+
// Create a new MinIO Admin Client
63+
client, err := newAdminClient(aliasedURL)
64+
fatalIf(err, "Unable to initialize admin connection.")
65+
66+
serviceAccountKey := args.Get(1)
67+
68+
creds, e := client.GetServiceAccount(globalContext, serviceAccountKey)
69+
fatalIf(probe.NewError(e).Trace(args...), "Cannot show the credentials of the specified service account")
70+
71+
printMsg(saMessage{
72+
AccessKey: creds.AccessKey,
73+
SecretKey: creds.SecretKey,
74+
SessionToken: creds.SessionToken,
75+
})
76+
77+
return nil
78+
}

cmd/admin-user-sa.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
"github.com/fatih/color"
21+
"github.com/minio/cli"
22+
"github.com/minio/minio/pkg/console"
23+
)
24+
25+
var adminUserSACmd = cli.Command{
26+
Name: "sa",
27+
Usage: "manage service accounts",
28+
Action: mainAdminUserSA,
29+
Before: setGlobalsFromContext,
30+
Flags: globalFlags,
31+
Subcommands: []cli.Command{
32+
adminUserSAGenerateCmd,
33+
adminUserSAShowCmd,
34+
},
35+
}
36+
37+
func setSACommandColors() {
38+
console.SetColor("SA", color.New(color.FgCyan, color.Bold))
39+
console.SetColor("AccessKey", color.New(color.FgYellow))
40+
console.SetColor("SecretKey", color.New(color.FgRed))
41+
console.SetColor("SessionToken", color.New(color.FgBlue))
42+
}
43+
44+
func mainAdminUserSA(ctx *cli.Context) error {
45+
cli.ShowCommandHelp(ctx, ctx.Args().First())
46+
return nil
47+
// Sub-commands like "get", "set" have their own main.
48+
}

cmd/admin-user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var adminUserCmd = cli.Command{
3131
adminUserRemoveCmd,
3232
adminUserListCmd,
3333
adminUserInfoCmd,
34+
adminUserSACmd,
3435
},
3536
HideHelpCommand: true,
3637
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/mattn/go-isatty v0.0.7
1515
github.com/mattn/go-runewidth v0.0.5 // indirect
1616
github.com/minio/cli v1.22.0
17-
github.com/minio/minio v0.0.0-20200321002836-bf545dc3203b
17+
github.com/minio/minio v0.0.0-20200321170220-27b8f18cce9b
1818
github.com/minio/minio-go/v6 v6.0.51-0.20200319192131-097caa7760c7
1919
github.com/minio/sha256-simd v0.1.1
2020
github.com/mitchellh/go-homedir v1.1.0

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ github.com/frankban/quicktest v1.7.2/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03D
8787
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
8888
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 h1:Mn26/9ZMNWSw9C9ERFA1PUxfmGpolnw2v0bKOREu5ew=
8989
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32/go.mod h1:GIjDIg/heH5DOkXY3YJ/wNhfHsQHoXGjl8G8amsYQ1I=
90+
github.com/go-ini/ini v1.52.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
9091
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
9192
github.com/go-ldap/ldap v3.0.2+incompatible/go.mod h1:qfd9rJvER9Q0/D/Sqn1DfHRoBp40uXYvFoEVrNEPqRc=
9293
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
@@ -257,6 +258,8 @@ github.com/minio/minio v0.0.0-20200312144740-ed4bd20a7cfc h1:JvRNAEmHghf0mPluxNL
257258
github.com/minio/minio v0.0.0-20200312144740-ed4bd20a7cfc/go.mod h1:QbCnTGb/blyNjrkyxB9ecKxtuWWUdcHShqED1bItwa0=
258259
github.com/minio/minio v0.0.0-20200321002836-bf545dc3203b h1:8TWH16dqfdsSddYC29tBgRpTLxITy0LN/Dg5VFZezdY=
259260
github.com/minio/minio v0.0.0-20200321002836-bf545dc3203b/go.mod h1:cUjzu4ZZy1YdtUjCSBWsxoa+z2NqCRohj6EyFIQA3gE=
261+
github.com/minio/minio v0.0.0-20200321170220-27b8f18cce9b h1:tz+JNZ3kibon/xg5lrEMR7tirgEi57bekJkqLfaIlZI=
262+
github.com/minio/minio v0.0.0-20200321170220-27b8f18cce9b/go.mod h1:cUjzu4ZZy1YdtUjCSBWsxoa+z2NqCRohj6EyFIQA3gE=
260263
github.com/minio/minio-go/v6 v6.0.45/go.mod h1:qD0lajrGW49lKZLtXKtCB4X/qkMf0a5tBvN2PaZg7Gg=
261264
github.com/minio/minio-go/v6 v6.0.50-0.20200306231101-b882ba63d570 h1:GLTZoRC6rhCTucnkJAQ63LhMU2S4CM71MRc9gfX7ohE=
262265
github.com/minio/minio-go/v6 v6.0.50-0.20200306231101-b882ba63d570/go.mod h1:qD0lajrGW49lKZLtXKtCB4X/qkMf0a5tBvN2PaZg7Gg=

0 commit comments

Comments
 (0)