Add policy set command (#2852)
This is a BREAKING change. - This command sets policy for user and group - `mc admin user set-policy` is now removed. - `mc admin uesr add` no longer accepts a policy to set on the user. Add user info command to show groups of a user
This commit is contained in:
committed by
kannappanr
parent
e541715e42
commit
3b94d4d3e0
+14
-4
@@ -17,6 +17,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/fatih/color"
|
||||
@@ -62,10 +63,12 @@ func checkAdminPolicyAddSyntax(ctx *cli.Context) {
|
||||
|
||||
// userPolicyMessage container for content message structure
|
||||
type userPolicyMessage struct {
|
||||
op string
|
||||
Status string `json:"status"`
|
||||
Policy string `json:"policy,omitempty"`
|
||||
PolicyJSON []byte `json:"policyJSON,omitempty"`
|
||||
op string
|
||||
Status string `json:"status"`
|
||||
Policy string `json:"policy,omitempty"`
|
||||
PolicyJSON []byte `json:"policyJSON,omitempty"`
|
||||
UserOrGroup string `json:"userOrGroup,omitempty"`
|
||||
IsGroup bool `json:"isGroup"`
|
||||
}
|
||||
|
||||
func (u userPolicyMessage) String() string {
|
||||
@@ -82,6 +85,13 @@ func (u userPolicyMessage) String() string {
|
||||
return console.Colorize("PolicyMessage", "Removed policy `"+u.Policy+"` successfully.")
|
||||
case "add":
|
||||
return console.Colorize("PolicyMessage", "Added policy `"+u.Policy+"` successfully.")
|
||||
case "set":
|
||||
fragment := "user"
|
||||
if u.IsGroup {
|
||||
fragment = "group"
|
||||
}
|
||||
return console.Colorize("PolicyMessage",
|
||||
fmt.Sprintf("Policy %s is set on %s `%s`", u.Policy, fragment, u.UserOrGroup))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* MinIO Client (C) 2019 MinIO, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
)
|
||||
|
||||
var adminPolicySetCmd = cli.Command{
|
||||
Name: "set",
|
||||
Usage: "set IAM policy on a user or group",
|
||||
Action: mainAdminPolicySet,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} TARGET POLICYNAME [ user=username1 | group=groupname1 ]
|
||||
|
||||
POLICYNAME:
|
||||
Name of the policy on the MinIO server.
|
||||
|
||||
FLAGS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}
|
||||
EXAMPLES:
|
||||
1. Set the "readwrite" policy for user "james".
|
||||
$ {{.HelpName}} myminio readwrite user=james
|
||||
2. Set the "readonly" policy for group "auditors".
|
||||
$ {{.HelpName}} myminio readonly group=auditors
|
||||
`,
|
||||
}
|
||||
|
||||
var (
|
||||
errBadUserGroupArg = errors.New("Last argument must be of the form user=xx or group=xx")
|
||||
)
|
||||
|
||||
func checkAdminPolicySetSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) != 3 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "set", 1) // last argument is exit code
|
||||
}
|
||||
}
|
||||
|
||||
func parseEntityArg(arg string) (userOrGroup string, isGroup bool, err error) {
|
||||
parts := strings.Split(arg, "=")
|
||||
switch {
|
||||
case len(parts) != 2:
|
||||
fallthrough
|
||||
case parts[1] == "":
|
||||
err = errBadUserGroupArg
|
||||
case strings.ToLower(parts[0]) == "user":
|
||||
userOrGroup = parts[1]
|
||||
isGroup = false
|
||||
case strings.ToLower(parts[0]) == "group":
|
||||
userOrGroup = parts[1]
|
||||
isGroup = true
|
||||
default:
|
||||
err = errBadUserGroupArg
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// mainAdminPolicySet is the handler for "mc admin policy set" command.
|
||||
func mainAdminPolicySet(ctx *cli.Context) error {
|
||||
checkAdminPolicySetSyntax(ctx)
|
||||
|
||||
console.SetColor("PolicyMessage", color.New(color.FgGreen))
|
||||
console.SetColor("Policy", color.New(color.FgBlue))
|
||||
|
||||
// Get the alias parameter from cli
|
||||
args := ctx.Args()
|
||||
aliasedURL := args.Get(0)
|
||||
policyName := args.Get(1)
|
||||
entityArg := args.Get(2)
|
||||
|
||||
userOrGroup, isGroup, e1 := parseEntityArg(entityArg)
|
||||
fatalIf(probe.NewError(e1).Trace(args...), "Bad last argument")
|
||||
|
||||
// Create a new MinIO Admin Client
|
||||
client, err := newAdminClient(aliasedURL)
|
||||
fatalIf(err, "Cannot get a configured admin connection.")
|
||||
|
||||
e := client.SetPolicy(policyName, userOrGroup, isGroup)
|
||||
|
||||
if e == nil {
|
||||
printMsg(userPolicyMessage{
|
||||
op: "set",
|
||||
Policy: policyName,
|
||||
UserOrGroup: userOrGroup,
|
||||
IsGroup: isGroup,
|
||||
})
|
||||
} else {
|
||||
fatalIf(probe.NewError(e), "Cannot set the policy")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -29,6 +29,7 @@ var adminPolicyCmd = cli.Command{
|
||||
adminPolicyRemoveCmd,
|
||||
adminPolicyListCmd,
|
||||
adminPolicyInfoCmd,
|
||||
adminPolicySetCmd,
|
||||
},
|
||||
HideHelpCommand: true,
|
||||
}
|
||||
|
||||
+21
-16
@@ -17,6 +17,9 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
json "github.com/minio/mc/pkg/colorjson"
|
||||
@@ -34,25 +37,22 @@ var adminUserAddCmd = cli.Command{
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} TARGET ACCESSKEY SECRETKEY POLICYNAME
|
||||
|
||||
POLICYNAME:
|
||||
Name of the policy available on MinIO server.
|
||||
{{.HelpName}} TARGET ACCESSKEY SECRETKEY
|
||||
|
||||
FLAGS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}
|
||||
EXAMPLES:
|
||||
1. Add a new user 'foobar' to MinIO server with policy 'writeonly'.
|
||||
1. Add a new user 'foobar' to MinIO server.
|
||||
$ set +o history
|
||||
$ {{.HelpName}} myminio foobar foo12345 writeonly
|
||||
$ {{.HelpName}} myminio foobar foo12345
|
||||
$ set -o history
|
||||
`,
|
||||
}
|
||||
|
||||
// checkAdminUserAddSyntax - validate all the passed arguments
|
||||
func checkAdminUserAddSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) != 4 {
|
||||
if len(ctx.Args()) != 3 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "add", 1) // last argument is exit code
|
||||
}
|
||||
}
|
||||
@@ -60,11 +60,12 @@ func checkAdminUserAddSyntax(ctx *cli.Context) {
|
||||
// userMessage container for content message structure
|
||||
type userMessage struct {
|
||||
op string
|
||||
Status string `json:"status"`
|
||||
AccessKey string `json:"accessKey,omitempty"`
|
||||
SecretKey string `json:"secretKey,omitempty"`
|
||||
PolicyName string `json:"policyName,omitempty"`
|
||||
UserStatus string `json:"userStatus,omitempty"`
|
||||
Status string `json:"status"` // TODO: remove this?
|
||||
AccessKey string `json:"accessKey,omitempty"`
|
||||
SecretKey string `json:"secretKey,omitempty"`
|
||||
PolicyName string `json:"policyName,omitempty"`
|
||||
UserStatus string `json:"userStatus,omitempty"`
|
||||
MemberOf []string `json:"memberOf,omitempty"`
|
||||
}
|
||||
|
||||
func (u userMessage) String() string {
|
||||
@@ -80,8 +81,14 @@ func (u userMessage) String() string {
|
||||
Field{"AccessKey", accessFieldMaxLen},
|
||||
Field{"PolicyName", policyFieldMaxLen},
|
||||
).buildRow(u.UserStatus, u.AccessKey, u.PolicyName)
|
||||
case "policy":
|
||||
return console.Colorize("UserMessage", "Set a policy `"+u.PolicyName+"` for user `"+u.AccessKey+"` successfully.")
|
||||
case "info":
|
||||
return console.Colorize("UserMessage", strings.Join(
|
||||
[]string{
|
||||
fmt.Sprintf("AccessKey: %s", u.AccessKey),
|
||||
fmt.Sprintf("Status: %s", u.UserStatus),
|
||||
fmt.Sprintf("PolicyName: %s", u.PolicyName),
|
||||
fmt.Sprintf("MemberOf: %s", strings.Join(u.MemberOf, ",")),
|
||||
}, "\n"))
|
||||
case "remove":
|
||||
return console.Colorize("UserMessage", "Removed user `"+u.AccessKey+"` successfully.")
|
||||
case "disable":
|
||||
@@ -118,8 +125,6 @@ func mainAdminUserAdd(ctx *cli.Context) error {
|
||||
|
||||
fatalIf(probe.NewError(client.AddUser(args.Get(1), args.Get(2))).Trace(args...), "Cannot add new user")
|
||||
|
||||
fatalIf(probe.NewError(client.SetUserPolicy(args.Get(1), args.Get(3))).Trace(args...), "Cannot set user policy for new user")
|
||||
|
||||
printMsg(userMessage{
|
||||
op: "add",
|
||||
AccessKey: args.Get(1),
|
||||
|
||||
@@ -23,40 +23,37 @@ import (
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
)
|
||||
|
||||
var adminUserPolicyCmd = cli.Command{
|
||||
Name: "set-policy",
|
||||
Usage: "set policy for user",
|
||||
Action: mainAdminUserPolicy,
|
||||
var adminUserInfoCmd = cli.Command{
|
||||
Name: "info",
|
||||
Usage: "display info of a user",
|
||||
Action: mainAdminUserInfo,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} TARGET USERNAME POLICYNAME
|
||||
|
||||
POLICYNAME:
|
||||
Name of the canned policy created on MinIO server.
|
||||
{{.HelpName}} TARGET ACCESSKEY
|
||||
|
||||
FLAGS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}
|
||||
EXAMPLES:
|
||||
1. Set a policy 'writeonly' to 'foobar' on MinIO server.
|
||||
$ {{.HelpName}} myminio foobar writeonly
|
||||
1. Display the info of a user "foobar".
|
||||
$ {{.HelpName}} myminio foobar foo12345
|
||||
`,
|
||||
}
|
||||
|
||||
// checkAdminUserPolicySyntax - validate all the passed arguments
|
||||
func checkAdminUserPolicySyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) != 3 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "policy", 1) // last argument is exit code
|
||||
// checkAdminUserAddSyntax - validate all the passed arguments
|
||||
func checkAdminUserInfoSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) != 2 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "info", 1) // last argument is exit code
|
||||
}
|
||||
}
|
||||
|
||||
// mainAdminUserPolicy is the handle for "mc admin user policy" command.
|
||||
func mainAdminUserPolicy(ctx *cli.Context) error {
|
||||
checkAdminUserPolicySyntax(ctx)
|
||||
// mainAdminUserInfo is the handler for "mc admin user info" command.
|
||||
func mainAdminUserInfo(ctx *cli.Context) error {
|
||||
checkAdminUserInfoSyntax(ctx)
|
||||
|
||||
console.SetColor("UserMessage", color.New(color.FgGreen))
|
||||
|
||||
@@ -68,13 +65,15 @@ func mainAdminUserPolicy(ctx *cli.Context) error {
|
||||
client, err := newAdminClient(aliasedURL)
|
||||
fatalIf(err, "Cannot get a configured admin connection.")
|
||||
|
||||
fatalIf(probe.NewError(client.SetUserPolicy(args.Get(1), args.Get(2))).Trace(args...), "Cannot set user policy for user")
|
||||
user, e := client.GetUserInfo(args.Get(1))
|
||||
fatalIf(probe.NewError(e).Trace(args...), "Cannot get user info")
|
||||
|
||||
printMsg(userMessage{
|
||||
op: "policy",
|
||||
op: "info",
|
||||
AccessKey: args.Get(1),
|
||||
PolicyName: args.Get(2),
|
||||
UserStatus: "enabled",
|
||||
PolicyName: user.PolicyName,
|
||||
UserStatus: string(user.Status),
|
||||
MemberOf: user.MemberOf,
|
||||
})
|
||||
|
||||
return nil
|
||||
+1
-1
@@ -26,11 +26,11 @@ var adminUserCmd = cli.Command{
|
||||
Flags: globalFlags,
|
||||
Subcommands: []cli.Command{
|
||||
adminUserAddCmd,
|
||||
adminUserPolicyCmd,
|
||||
adminUserDisableCmd,
|
||||
adminUserEnableCmd,
|
||||
adminUserRemoveCmd,
|
||||
adminUserListCmd,
|
||||
adminUserInfoCmd,
|
||||
},
|
||||
HideHelpCommand: true,
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ require (
|
||||
github.com/mattn/go-isatty v0.0.7
|
||||
github.com/mattn/go-runewidth v0.0.4 // indirect
|
||||
github.com/minio/cli v1.21.0
|
||||
github.com/minio/minio v0.0.0-20190802212500-414a7eca839d
|
||||
github.com/minio/minio v0.0.0-20190813204106-bf9b619d8656
|
||||
github.com/minio/minio-go/v6 v6.0.32
|
||||
github.com/minio/sha256-simd v0.1.0
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
|
||||
@@ -216,8 +216,8 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z
|
||||
github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
|
||||
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||
github.com/gorilla/rpc v0.0.0-20160517062331-bd3317b8f670/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ=
|
||||
github.com/gorilla/rpc v1.2.0+incompatible h1:V3Dz9mWwCvHKm0N+mVM2A/hShV+hLUMUdzoyHQjr1NA=
|
||||
github.com/gorilla/rpc v1.2.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ=
|
||||
github.com/gorilla/rpc v1.2.0+incompatible h1:V3Dz9mWwCvHKm0N+mVM2A/hShV+hLUMUdzoyHQjr1NA=
|
||||
github.com/gorilla/rpc v1.2.0+incompatible/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ=
|
||||
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
@@ -722,6 +722,8 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3 h1:4y9KwBHBgBNwDbtu44R5o1fdOCQUEXhbk/P4A9WmJq0=
|
||||
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa h1:KIDDMLT1O0Nr7TSxp8xM5tJcdn8tgyAONntO829og1M=
|
||||
golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||
|
||||
Reference in New Issue
Block a user