Skip to content

Commit

Permalink
Add admin users
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana committed Oct 15, 2018
1 parent 7bbc81f commit 593a72b
Show file tree
Hide file tree
Showing 27 changed files with 1,495 additions and 115 deletions.
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Expected behaviour
## Expected behavior

## Actual behaviour
## Actual behavior

## Steps to reproduce the behaviour
## Steps to reproduce the behavior

## mc version
- (paste output of `mc version`)
Expand Down
2 changes: 1 addition & 1 deletion buildscripts/checkdeps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _init() {

## FIXME:
## In OSX, 'readlink -f' option does not exist, hence
## we have our own readlink -f behaviour here.
## we have our own readlink -f behavior here.
## Once OSX has the option, below function is good enough.
##
## readlink() {
Expand Down
10 changes: 5 additions & 5 deletions cmd/admin-credentials.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Minio Client (C) 2016, 2017 Minio, Inc.
* Minio Client (C) 2016, 2017, 2018 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@ const credsCmdName = "credentials"

var adminCredsCmd = cli.Command{
Name: credsCmdName,
Usage: "Change server access and secret keys",
Usage: "Change Admin server access and secret keys",
Action: mainAdminCreds,
Flags: append(adminCredsFlags, globalFlags...),
CustomHelpTemplate: `NAME:
Expand All @@ -42,8 +42,8 @@ FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Set new credentials of a Minio server represented by its alias 'play'.
$ {{.HelpName}} play/ minio minio123
1. Set new **admin** credentials of a Minio server represented by its alias 'alias'.
$ {{.HelpName}} alias/ minio minio123
`,
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func mainAdminCreds(ctx *cli.Context) error {
fatalIf(err, "Cannot get a configured admin connection.")

// Change the credentials of the specified Minio server
e := client.SetCredentials(accessKey, secretKey)
e := client.SetAdminCredentials(accessKey, secretKey)
fatalIf(probe.NewError(e), "Unable to set new credentials to '"+aliasedURL+"'.")

return nil
Expand Down
2 changes: 2 additions & 0 deletions cmd/admin-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var adminCmd = cli.Command{
Subcommands: []cli.Command{
adminServiceCmd,
adminInfoCmd,
adminUsersCmd,
adminPoliciesCmd,
adminCredsCmd,
adminConfigCmd,
adminHealCmd,
Expand Down
123 changes: 123 additions & 0 deletions cmd/admin-policies-add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Minio Client (C) 2018 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 (
"encoding/json"
"io/ioutil"

"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/mc/pkg/probe"
)

var adminPoliciesAddCmd = cli.Command{
Name: "add",
Usage: "Add new policies",
Action: mainAdminPoliciesAdd,
Before: setGlobalsFromContext,
Flags: globalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET POLICYNAME POLICYFILE
POLICYNAME:
Name of the canned policy on Minio server.
POLICYFILE:
Name of the policy file associated with the policy name.
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Add a new canned policy 'writeonly'.
$ {{.HelpName}} myminio writeonly /tmp/writeonly.json
`,
}

// checkAdminPoliciesAddSyntax - validate all the passed arguments
func checkAdminPoliciesAddSyntax(ctx *cli.Context) {
if len(ctx.Args()) != 3 {
cli.ShowCommandHelpAndExit(ctx, "add", 1) // last argument is exit code
}
}

// 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"`
}

func (u userPolicyMessage) String() string {
switch u.op {
case "list":
if len(u.PolicyJSON) > 0 {
return string(u.PolicyJSON)
}
policyFieldMaxLen := 20
// Create a new pretty table with cols configuration
return newPrettyTable(" ",
Field{"Policy", policyFieldMaxLen},
).buildRow(u.Policy)
case "remove":
return console.Colorize("PolicyMessage", "Removed policy `"+u.Policy+"` successfully.")
case "add":
return console.Colorize("PolicyMessage", "Added policy `"+u.Policy+"` successfully.")
}
return ""
}

func (u userPolicyMessage) JSON() string {
u.Status = "success"
jsonMessageBytes, e := json.Marshal(u)
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")

return string(jsonMessageBytes)
}

// mainAdminPoliciesAdd is the handle for "mc admin users add" command.
func mainAdminPoliciesAdd(ctx *cli.Context) error {
checkAdminPoliciesAddSyntax(ctx)

console.SetColor("PolicyMessage", color.New(color.FgGreen))

// Get the alias parameter from cli
args := ctx.Args()
aliasedURL := args.Get(0)

policy, e := ioutil.ReadFile(args.Get(2))
fatalIf(probe.NewError(e).Trace(args...), "Unable to get policy")

// Create a new Minio Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Cannot get a configured admin connection.")

fatalIf(probe.NewError(client.AddCannedPolicy(args.Get(1), string(policy))).Trace(args...), "Cannot add new policy")

printMsg(userPolicyMessage{
op: "add",
Policy: args.Get(1),
})

return nil
}
93 changes: 93 additions & 0 deletions cmd/admin-policies-list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Minio Client (C) 2018 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 (
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/mc/pkg/probe"
)

var adminPoliciesListCmd = cli.Command{
Name: "list",
Usage: "List policies",
Action: mainAdminPoliciesList,
Before: setGlobalsFromContext,
Flags: globalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET POLICYNAME
POLICYNAME:
Name of the canned policy on Minio server.
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. List all policies.
$ {{.HelpName}} myminio
2. List only one policy.
$ {{.HelpName}} myminio writeonly
`,
}

// checkAdminPoliciesListSyntax - validate all the passed arguments
func checkAdminPoliciesListSyntax(ctx *cli.Context) {
if len(ctx.Args()) < 1 || len(ctx.Args()) > 2 {
cli.ShowCommandHelpAndExit(ctx, "list", 1) // last argument is exit code
}
}

// mainAdminPoliciesList is the handle for "mc admin users add" command.
func mainAdminPoliciesList(ctx *cli.Context) error {
checkAdminPoliciesListSyntax(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)

// Create a new Minio Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Cannot get a configured admin connection.")

policies, e := client.ListCannedPolicies()
fatalIf(probe.NewError(e).Trace(args...), "Cannot list policies")

if policyName := args.Get(1); policyName != "" {
printMsg(userPolicyMessage{
op: "list",
Policy: policyName,
PolicyJSON: policies[policyName],
})
} else {
for k := range policies {
printMsg(userPolicyMessage{
op: "list",
Policy: k,
})
}
}
return nil
}
79 changes: 79 additions & 0 deletions cmd/admin-policies-remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Minio Client (C) 2018 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 (
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/mc/pkg/probe"
)

var adminPoliciesRemoveCmd = cli.Command{
Name: "remove",
Usage: "Remove policies",
Action: mainAdminPoliciesRemove,
Before: setGlobalsFromContext,
Flags: globalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET POLICYNAME
POLICYNAME:
Name of the canned policy on Minio server.
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Remove a canned policy 'writeonly'.
$ {{.HelpName}} myminio writeonly
`,
}

// checkAdminPoliciesRemoveSyntax - validate all the passed arguments
func checkAdminPoliciesRemoveSyntax(ctx *cli.Context) {
if len(ctx.Args()) != 2 {
cli.ShowCommandHelpAndExit(ctx, "remove", 1) // last argument is exit code
}
}

// mainAdminPoliciesRemove is the handle for "mc admin users add" command.
func mainAdminPoliciesRemove(ctx *cli.Context) error {
checkAdminPoliciesRemoveSyntax(ctx)

console.SetColor("PolicyMessage", color.New(color.FgGreen))

// Get the alias parameter from cli
args := ctx.Args()
aliasedURL := args.Get(0)

// Create a new Minio Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Cannot get a configured admin connection.")

fatalIf(probe.NewError(client.RemoveCannedPolicy(args.Get(1))).Trace(args...), "Cannot remove policy")

printMsg(userPolicyMessage{
op: "remove",
Policy: args.Get(1),
})

return nil
}
Loading

0 comments on commit 593a72b

Please sign in to comment.