11package main
22
3- import "github.com/spf13/cobra"
3+ import (
4+ "fmt"
5+
6+ "github.com/spf13/cobra"
7+ )
48
59func (c * CLI ) userCmd () * cobra.Command {
610 cmd := & cobra.Command {
@@ -9,6 +13,8 @@ func (c *CLI) userCmd() *cobra.Command {
913 Aliases : []string {"usr" },
1014 }
1115 cmd .AddCommand (c .userKeyStore ())
16+ cmd .AddCommand (c .userKey ())
17+ cmd .AddCommand (c .userPassword ())
1218 return cmd
1319}
1420
@@ -24,10 +30,31 @@ func (c *CLI) userKeyStore() *cobra.Command {
2430 return cmd
2531}
2632
33+ func (c * CLI ) userKey () * cobra.Command {
34+ cmd := & cobra.Command {
35+ Use : "key" ,
36+ Short : "Private keys management" ,
37+ Aliases : []string {"keys" },
38+ }
39+ cmd .AddCommand (c .userKeyAdd ())
40+ cmd .AddCommand (c .userKeyDelete ())
41+ return cmd
42+ }
43+
44+ func (c * CLI ) userPassword () * cobra.Command {
45+ cmd := & cobra.Command {
46+ Use : "password" ,
47+ Short : "Password management" ,
48+ Aliases : []string {"pwd" },
49+ }
50+ cmd .AddCommand (c .UserPasswordSet ())
51+ return cmd
52+ }
53+
2754func (c * CLI ) KeystoreStatus () * cobra.Command {
2855 cmd := & cobra.Command {
2956 Use : "status" ,
30- Short : "Get status of keystore" ,
57+ Short : "Get status of a user keystore" ,
3158 Aliases : []string {"show" , "get" , "read" , "describe" , "ls" },
3259 Run : func (cmd * cobra.Command , args []string ) {
3360 instance , err := c .aem .InstanceManager ().One ()
@@ -39,7 +66,7 @@ func (c *CLI) KeystoreStatus() *cobra.Command {
3966 id , _ := cmd .Flags ().GetString ("id" )
4067 scope , _ := cmd .Flags ().GetString ("scope" )
4168
42- result , err := instance .Auth ().UserManager ().KeystoreStatus (scope , id )
69+ result , err := instance .Auth ().UserManager ().Keystore (). Status (scope , id )
4370
4471 if err != nil {
4572 c .Error (err )
@@ -53,14 +80,13 @@ func (c *CLI) KeystoreStatus() *cobra.Command {
5380 cmd .Flags ().String ("id" , "" , "user id" )
5481 _ = cmd .MarkFlagRequired ("id" )
5582 cmd .Flags ().String ("scope" , "" , "user scope" )
56- _ = cmd .MarkFlagRequired ("scope" )
5783 return cmd
5884}
5985
6086func (c * CLI ) KeystoreCreate () * cobra.Command {
6187 cmd := & cobra.Command {
6288 Use : "create" ,
63- Short : "Create user Keystore " ,
89+ Short : "Create user keystore " ,
6490 Aliases : []string {"make" , "new" },
6591 Run : func (cmd * cobra.Command , args []string ) {
6692 instance , err := c .aem .InstanceManager ().One ()
@@ -72,26 +98,155 @@ func (c *CLI) KeystoreCreate() *cobra.Command {
7298 id , _ := cmd .Flags ().GetString ("id" )
7399 scope , _ := cmd .Flags ().GetString ("scope" )
74100 password , _ := cmd .Flags ().GetString ("keystore-password" )
75- changed , err := instance .Auth ().UserManager ().KeystoreCreate (scope , id , password )
101+ changed , err := instance .Auth ().UserManager ().Keystore ().Create (scope , id , password )
102+
103+ if err != nil {
104+ c .Error (err )
105+ return
106+ }
107+
108+ if changed {
109+ c .Changed ("User keystore created" )
110+ } else {
111+ c .Ok ("User keystore already exists" )
112+ }
113+ },
114+ }
115+
116+ cmd .Flags ().String ("id" , "" , "user id" )
117+ _ = cmd .MarkFlagRequired ("id" )
118+ cmd .Flags ().String ("scope" , "" , "user scope" )
119+ cmd .Flags ().String ("keystore-password" , "" , "keystore password" )
120+ _ = cmd .MarkFlagRequired ("keystore-password" )
121+ return cmd
122+ }
76123
124+ func (c * CLI ) userKeyAdd () * cobra.Command {
125+ cmd := & cobra.Command {
126+ Use : "add" ,
127+ Short : "Add user private key to the keystore" ,
128+ Aliases : []string {"create" , "new" },
129+ Run : func (cmd * cobra.Command , args []string ) {
130+ instance , err := c .aem .InstanceManager ().One ()
77131 if err != nil {
78132 c .Error (err )
79133 return
80134 }
81135
136+ changed , err := instance .Auth ().UserManager ().Keystore ().AddKey (
137+ cmd .Flag ("scope" ).Value .String (),
138+ cmd .Flag ("id" ).Value .String (),
139+ cmd .Flag ("keystore-file" ).Value .String (),
140+ cmd .Flag ("keystore-password" ).Value .String (),
141+ cmd .Flag ("key-alias" ).Value .String (),
142+ cmd .Flag ("key-password" ).Value .String (),
143+ cmd .Flag ("new-alias" ).Value .String (),
144+ )
145+
146+ if err != nil {
147+ c .Error (err )
148+ return
149+ }
82150 if changed {
83- c .Changed ("User Keystore created " )
151+ c .Changed ("User key added " )
84152 } else {
85- c .Ok ("User Keystore already exists" )
153+ c .Ok ("User key already exists" )
86154 }
87155 },
88156 }
89157
90158 cmd .Flags ().String ("id" , "" , "user id" )
91159 _ = cmd .MarkFlagRequired ("id" )
92160 cmd .Flags ().String ("scope" , "" , "user scope" )
93- _ = cmd .MarkFlagRequired ("scope" )
161+ cmd .Flags ().String ("keystore-file" , "" , "path to keystore file" )
162+ _ = cmd .MarkFlagRequired ("keystore-file" )
94163 cmd .Flags ().String ("keystore-password" , "" , "keystore password" )
95164 _ = cmd .MarkFlagRequired ("keystore-password" )
165+ cmd .Flags ().String ("key-alias" , "" , "key alias" )
166+ _ = cmd .MarkFlagRequired ("key-alias" )
167+ cmd .Flags ().String ("key-password" , "" , "key password" )
168+ cmd .Flags ().String ("new-alias" , "" , "new key alias (optional)" )
169+
170+ return cmd
171+ }
172+
173+ func (c * CLI ) userKeyDelete () * cobra.Command {
174+ cmd := & cobra.Command {
175+ Use : "delete" ,
176+ Short : "Delete user private key from the keystore" ,
177+ Aliases : []string {"remove" , "rm" },
178+ Run : func (cmd * cobra.Command , args []string ) {
179+ instance , err := c .aem .InstanceManager ().One ()
180+ if err != nil {
181+ c .Error (err )
182+ return
183+ }
184+
185+ changed , err := instance .Auth ().UserManager ().Keystore ().DeleteKey (
186+ cmd .Flag ("scope" ).Value .String (),
187+ cmd .Flag ("id" ).Value .String (),
188+ cmd .Flag ("key-alias" ).Value .String (),
189+ )
190+
191+ if err != nil {
192+ c .Error (err )
193+ return
194+ }
195+ if changed {
196+ c .Changed ("User key deleted" )
197+ } else {
198+ c .Ok ("User key does not exist" )
199+ }
200+ },
201+ }
202+
203+ cmd .Flags ().String ("id" , "" , "user id" )
204+ _ = cmd .MarkFlagRequired ("id" )
205+ cmd .Flags ().String ("scope" , "" , "user scope" )
206+ cmd .Flags ().String ("key-alias" , "" , "key alias" )
207+ _ = cmd .MarkFlagRequired ("key-alias" )
208+
209+ return cmd
210+ }
211+
212+ func (c * CLI ) UserPasswordSet () * cobra.Command {
213+ cmd := & cobra.Command {
214+ Use : "set" ,
215+ Short : "Set user password. Password is read from input." ,
216+ Aliases : []string {"update" , "change" },
217+ Run : func (cmd * cobra.Command , args []string ) {
218+ instances , err := c .aem .InstanceManager ().One ()
219+ if err != nil {
220+ c .Error (err )
221+ return
222+ }
223+
224+ id , _ := cmd .Flags ().GetString ("id" )
225+ scope , _ := cmd .Flags ().GetString ("scope" )
226+
227+ var password string
228+ if err := c .ReadInput (& password ); err != nil {
229+ c .Fail (fmt .Sprintf ("error reading password from input: %s" , err ))
230+ return
231+ }
232+
233+ changed , err := instances .Auth ().UserManager ().SetPassword (scope , id , password )
234+ if err != nil {
235+ c .Error (err )
236+ return
237+ }
238+
239+ if changed {
240+ c .Changed ("User password changed" )
241+ } else {
242+ c .Ok ("User password already set" )
243+ }
244+ },
245+ }
246+
247+ cmd .Flags ().String ("id" , "" , "user id" )
248+ _ = cmd .MarkFlagRequired ("id" )
249+ cmd .Flags ().String ("scope" , "" , "user scope" )
250+
96251 return cmd
97252}
0 commit comments