diff --git a/cmd/admin-idp-ldap-policy-attach.go b/cmd/admin-idp-ldap-policy-attach.go new file mode 100644 index 00000000..6850b915 --- /dev/null +++ b/cmd/admin-idp-ldap-policy-attach.go @@ -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 . + +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) +} diff --git a/cmd/admin-idp-ldap-policy-detach.go b/cmd/admin-idp-ldap-policy-detach.go new file mode 100644 index 00000000..4d995c4d --- /dev/null +++ b/cmd/admin-idp-ldap-policy-detach.go @@ -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 . + +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 +} diff --git a/cmd/admin-idp-ldap-policy.go b/cmd/admin-idp-ldap-policy.go index a210aee6..41d755a0 100644 --- a/cmd/admin-idp-ldap-policy.go +++ b/cmd/admin-idp-ldap-policy.go @@ -20,6 +20,8 @@ package cmd import "github.com/minio/cli" var adminIDPLdapPolicySubcommands = []cli.Command{ + adminIDPLdapPolicyAttachCmd, + adminIDPLdapPolicyDetachCmd, adminIDPLdapPolicyEntitiesCmd, } diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index d278f341..4f61935c 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -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, diff --git a/go.mod b/go.mod index b50bdc5b..b45d5b72 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b4f85bc8..cc5c1e81 100644 --- a/go.sum +++ b/go.sum @@ -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=