forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_identity_mapping.go
123 lines (101 loc) · 3.79 KB
/
user_identity_mapping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package create
import (
"fmt"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
userv1 "github.com/openshift/api/user/v1"
userv1client "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
)
const UserIdentityMappingRecommendedName = "useridentitymapping"
var (
userIdentityMappingLong = templates.LongDesc(`
Typically, identities are automatically mapped to users during login. If automatic
mapping is disabled (by using the "lookup" mapping method), or a mapping needs to
be manually established between an identity and a user, this command can be used
to create a useridentitymapping object.`)
userIdentityMappingExample = templates.Examples(`
# Map the identity "acme_ldap:adamjones" to the user "ajones"
%[1]s acme_ldap:adamjones ajones`)
)
type CreateUserIdentityMappingOptions struct {
CreateSubcommandOptions *CreateSubcommandOptions
User string
Identity string
UserIdentityMappingClient userv1client.UserIdentityMappingsGetter
}
func NewCreateUserIdentityMappingOptions(streams genericclioptions.IOStreams) *CreateUserIdentityMappingOptions {
return &CreateUserIdentityMappingOptions{
CreateSubcommandOptions: NewCreateSubcommandOptions(streams),
}
}
// NewCmdCreateUserIdentityMapping is a macro command to create a new identity
func NewCmdCreateUserIdentityMapping(name, fullName string, f genericclioptions.RESTClientGetter, streams genericclioptions.IOStreams) *cobra.Command {
o := NewCreateUserIdentityMappingOptions(streams)
cmd := &cobra.Command{
Use: name + " <IDENTITY_NAME> <USER_NAME>",
Short: "Manually map an identity to a user.",
Long: userIdentityMappingLong,
Example: fmt.Sprintf(userIdentityMappingExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(cmd, f, args))
cmdutil.CheckErr(o.Run())
},
}
o.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)
cmdutil.AddDryRunFlag(cmd)
return cmd
}
func (o *CreateUserIdentityMappingOptions) Complete(cmd *cobra.Command, f genericclioptions.RESTClientGetter, args []string) error {
switch len(args) {
case 0:
return fmt.Errorf("identity is required")
case 1:
return fmt.Errorf("user name is required")
case 2:
o.Identity = args[0]
o.User = args[1]
default:
return fmt.Errorf("exactly two arguments (identity and user name) are supported, not: %v", args)
}
clientConfig, err := f.ToRESTConfig()
if err != nil {
return err
}
o.UserIdentityMappingClient, err = userv1client.NewForConfig(clientConfig)
if err != nil {
return err
}
o.CreateSubcommandOptions.Namespace, o.CreateSubcommandOptions.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
o.CreateSubcommandOptions.DryRun = cmdutil.GetDryRunFlag(cmd)
if o.CreateSubcommandOptions.DryRun {
o.CreateSubcommandOptions.PrintFlags.Complete("%s (dry run)")
}
o.CreateSubcommandOptions.Printer, err = o.CreateSubcommandOptions.PrintFlags.ToPrinter()
if err != nil {
return err
}
return nil
}
func (o *CreateUserIdentityMappingOptions) Run() error {
mapping := &userv1.UserIdentityMapping{
// this is ok because we know exactly how we want to be serialized
TypeMeta: metav1.TypeMeta{APIVersion: userv1.SchemeGroupVersion.String(), Kind: "UserIdentityMapping"},
Identity: corev1.ObjectReference{Name: o.Identity},
User: corev1.ObjectReference{Name: o.User},
}
var err error
if !o.CreateSubcommandOptions.DryRun {
mapping, err = o.UserIdentityMappingClient.UserIdentityMappings().Create(mapping)
if err != nil {
return err
}
}
return o.CreateSubcommandOptions.Printer.PrintObj(mapping, o.CreateSubcommandOptions.Out)
}