Add LDAP policy attach/detach commands (#4390)

Co-authored-by: Harshavardhana <harsha@minio.io>
This commit is contained in:
Aditya Manthramurthy
2022-12-12 16:23:28 -08:00
committed by GitHub
co-authored by Harshavardhana
parent b8974e5aed
commit a13c1a1f15
6 changed files with 283 additions and 6 deletions
+157
View File
@@ -0,0 +1,157 @@
// 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 (
"fmt"
"github.com/charmbracelet/lipgloss"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/madmin-go/v2"
"github.com/minio/mc/pkg/probe"
)
var adminIDPLdapPolicyAttachFlags = []cli.Flag{
cli.StringFlag{
Name: "user, u",
Usage: "attach policy to user by DN or by login name",
},
cli.StringFlag{
Name: "group, g",
Usage: "attach policy to LDAP Group DN",
},
}
var adminIDPLdapPolicyAttachCmd = cli.Command{
Name: "attach",
Usage: "attach a policy to an entity",
Action: mainAdminIDPLdapPolicyAttach,
Before: setGlobalsFromContext,
Flags: append(adminIDPLdapPolicyAttachFlags, globalFlags...),
OnUsageError: onUsageError,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [FLAGS] TARGET POLICY [POLICY...] [ --user=USER | --group=GROUP ]
Exactly one "--user" or "--group" flag is required.
POLICY:
Name of a policy on the MinIO server.
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Attach policy "mypolicy" to a user
{{.Prompt}} {{.HelpName}} play/ mypolicy --user='uid=bobfisher,ou=people,ou=hwengg,dc=min,dc=io'
2. Attach policies "policy1" and "policy2" to a group
{{.Prompt}} {{.HelpName}} play/ policy1 policy2 --group='cn=projectb,ou=groups,ou=swengg,dc=min,dc=io'
`,
}
// Quote from AWS policy naming requirement (ref:
// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-quotas.html):
//
// Names of users, groups, roles, policies, instance profiles, and server
// certificates must be alphanumeric, including the following common characters:
// plus (+), equal (=), comma (,), period (.), at (@), underscore (_), and
// hyphen (-).
func mainAdminIDPLdapPolicyAttach(ctx *cli.Context) error {
// We need exactly one alias, and at least one policy.
if len(ctx.Args()) < 2 {
showCommandHelpAndExit(ctx, 1)
}
user := ctx.String("user")
group := ctx.String("group")
args := ctx.Args()
aliasedURL := args.Get(0)
policies := args[1:]
req := madmin.PolicyAssociationReq{
Policies: policies,
User: user,
Group: group,
}
if err := req.IsValid(); err != nil {
fatalIf(probe.NewError(err), "Invalid policy attach arguments.")
}
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")
res, e := client.AttachPolicyLDAP(globalContext, req)
fatalIf(probe.NewError(e), "Unable to make LDAP policy association")
m := policyAssociationMessage{
attach: true,
Status: "success",
PoliciesAttached: res.PoliciesAttached,
User: user,
Group: group,
}
printMsg(m)
return nil
}
type policyAssociationMessage struct {
attach bool
Status string `json:"status"`
PoliciesAttached []string `json:"policiesAttached,omitempty"`
PoliciesDetached []string `json:"policiesDetached,omitempty"`
User string `json:"user,omitempty"`
Group string `json:"group,omitempty"`
}
func (m policyAssociationMessage) String() string {
style := lipgloss.NewStyle().Foreground(lipgloss.Color("#04B575")) // green
policiesS := style.Render("Attached Policies:")
entityS := style.Render("To User:")
policies := m.PoliciesAttached
entity := m.User
switch {
case m.User != "" && m.attach:
case m.User != "" && !m.attach:
policiesS = style.Render("Detached Policies:")
policies = m.PoliciesDetached
entityS = style.Render("From User:")
case m.Group != "" && m.attach:
entityS = style.Render("To Group:")
entity = m.Group
case m.Group != "" && !m.attach:
policiesS = style.Render("Detached Policies:")
policies = m.PoliciesDetached
entityS = style.Render("From Group:")
entity = m.Group
}
return fmt.Sprintf("%s %v\n%s %s\n", policiesS, policies, entityS, entity)
}
func (m policyAssociationMessage) JSON() string {
jsonMessageBytes, e := json.MarshalIndent(m, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonMessageBytes)
}
+116
View File
@@ -0,0 +1,116 @@
// 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 (
"errors"
"github.com/minio/cli"
"github.com/minio/madmin-go/v2"
"github.com/minio/mc/pkg/probe"
)
var adminIDPLdapPolicyDetachFlags = []cli.Flag{
cli.StringFlag{
Name: "user, u",
Usage: "attach policy to user by DN or by login name",
},
cli.StringFlag{
Name: "group, g",
Usage: "attach policy to LDAP Group DN",
},
}
var adminIDPLdapPolicyDetachCmd = cli.Command{
Name: "detach",
Usage: "detach a policy from an entity",
Action: mainAdminIDPLdapPolicyDetach,
Before: setGlobalsFromContext,
Flags: append(adminIDPLdapPolicyDetachFlags, globalFlags...),
OnUsageError: onUsageError,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [FLAGS] TARGET POLICY [POLICY...] [ --user=USER | --group=GROUP ]
Exactly one of "--user" or "--group" is required.
POLICY:
Name of a policy on the MinIO server.
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Detach policy "mypolicy" from a user
{{.Prompt}} {{.HelpName}} play/ mypolicy --user='uid=bobfisher,ou=people,ou=hwengg,dc=min,dc=io'
2. Detach policies "policy1" and "policy2" from a group
{{.Prompt}} {{.HelpName}} play/ policy1 policy2 --group='cn=projectb,ou=groups,ou=swengg,dc=min,dc=io'
`,
}
// Quote from AWS policy naming requirement (ref:
// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-quotas.html):
//
// Names of users, groups, roles, policies, instance profiles, and server
// certificates must be alphanumeric, including the following common characters:
// plus (+), equal (=), comma (,), period (.), at (@), underscore (_), and
// hyphen (-).
func mainAdminIDPLdapPolicyDetach(ctx *cli.Context) error {
// We need exactly one alias, and at least one policy.
if len(ctx.Args()) < 2 {
showCommandHelpAndExit(ctx, 1)
}
user := ctx.String("user")
group := ctx.String("group")
if user == "" && group == "" {
err := errors.New("at least one of --user or --group is required.")
fatalIf(probe.NewError(err), "Missing flag in command")
}
args := ctx.Args()
aliasedURL := args.Get(0)
policies := args[1:]
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")
res, e := client.DetachPolicyLDAP(globalContext,
madmin.PolicyAssociationReq{
Policies: policies,
User: user,
Group: group,
})
fatalIf(probe.NewError(e), "Unable to make LDAP policy association")
m := policyAssociationMessage{
attach: false,
Status: "success",
PoliciesDetached: res.PoliciesDetached,
User: user,
Group: group,
}
printMsg(m)
return nil
}
+2
View File
@@ -20,6 +20,8 @@ package cmd
import "github.com/minio/cli"
var adminIDPLdapPolicySubcommands = []cli.Command{
adminIDPLdapPolicyAttachCmd,
adminIDPLdapPolicyDetachCmd,
adminIDPLdapPolicyEntitiesCmd,
}
+2
View File
@@ -375,6 +375,8 @@ var completeCmds = map[string]complete.Predictor{
"/admin/idp/ldap/disable": aliasCompleter,
"/admin/idp/ldap/policy/entities": aliasCompleter,
"/admin/idp/ldap/policy/attach": aliasCompleter,
"/admin/idp/ldap/policy/detach": aliasCompleter,
"/admin/policy/info": aliasCompleter,
"/admin/policy/set": aliasCompleter,
+2 -2
View File
@@ -19,10 +19,10 @@ require (
github.com/minio/cli v1.24.2
github.com/minio/colorjson v1.0.4
github.com/minio/filepath v1.0.0
github.com/minio/madmin-go/v2 v2.0.0
github.com/minio/madmin-go/v2 v2.0.1
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/minio-go/v7 v7.0.44
github.com/minio/pkg v1.5.5
github.com/minio/pkg v1.5.6
github.com/minio/selfupdate v0.5.0
github.com/minio/sha256-simd v1.0.0
github.com/mitchellh/go-homedir v1.1.0
+4 -4
View File
@@ -506,16 +506,16 @@ github.com/minio/colorjson v1.0.4/go.mod h1:ZgE8vYon4xC4yfBPclP/2gqMRYw+p+xRsBbL
github.com/minio/filepath v1.0.0 h1:fvkJu1+6X+ECRA6G3+JJETj4QeAYO9sV43I79H8ubDY=
github.com/minio/filepath v1.0.0/go.mod h1:/nRZA2ldl5z6jT9/KQuvZcQlxZIMQoFFQPvEXx9T/Bw=
github.com/minio/madmin-go v1.6.6/go.mod h1:ATvkBOLiP3av4D++2v1UEHC/QzsGtgXD5kYvvRYzdKs=
github.com/minio/madmin-go/v2 v2.0.0 h1:VR+zCIeoHPveppvMUhZ/vmok1UJp4pH38M4wkqpyT88=
github.com/minio/madmin-go/v2 v2.0.0/go.mod h1:5aFi/VLWBHC2DEFfGIlUmAeJhaF4ZAjuYpEWZFU14Zw=
github.com/minio/madmin-go/v2 v2.0.1 h1:WFfe12P18k9WSEFUZzUaBOQ78vjMBafM1YjgtXkkJoM=
github.com/minio/madmin-go/v2 v2.0.1/go.mod h1:5aFi/VLWBHC2DEFfGIlUmAeJhaF4ZAjuYpEWZFU14Zw=
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
github.com/minio/minio-go/v7 v7.0.41/go.mod h1:nCrRzjoSUQh8hgKKtu3Y708OLvRLtuASMg2/nvmbarw=
github.com/minio/minio-go/v7 v7.0.44 h1:9zUJ7iU7ax2P1jOvTp6nVrgzlZq3AZlFm0XfRFDKstM=
github.com/minio/minio-go/v7 v7.0.44/go.mod h1:nCrRzjoSUQh8hgKKtu3Y708OLvRLtuASMg2/nvmbarw=
github.com/minio/pkg v1.5.4/go.mod h1:2MOaRFdmFKULD+uOLc3qHLGTQTuxCNPKNPfLBTxC8CA=
github.com/minio/pkg v1.5.5 h1:z53jAVkXpRD+Y4LBt9cN+EaPUbWD6iOWhZuC90JvGqk=
github.com/minio/pkg v1.5.5/go.mod h1:EiGlHS2xaooa2VMxhJsxxAZHDObHVUB3HwtuoEXOCVE=
github.com/minio/pkg v1.5.6 h1:4OUvRU1gDWilu/dohkJMVapylXN8q94kU5MgkOJ/x0I=
github.com/minio/pkg v1.5.6/go.mod h1:EiGlHS2xaooa2VMxhJsxxAZHDObHVUB3HwtuoEXOCVE=
github.com/minio/selfupdate v0.5.0 h1:0UH1HlL49+2XByhovKl5FpYTjKfvrQ2sgL1zEXK6mfI=
github.com/minio/selfupdate v0.5.0/go.mod h1:mcDkzMgq8PRcpCRJo/NlPY7U45O5dfYl2Y0Rg7IustY=
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=