@@ -5,10 +5,7 @@ import (
55 "os"
66
77 jks "github.com/pavlo-v-chernykh/keystore-go/v4"
8- "github.com/wttech/aemc/pkg/common/pathx"
9- "github.com/wttech/aemc/pkg/keystore"
108 "github.com/wttech/aemc/pkg/user"
11- "golang.org/x/exp/slices"
129)
1310
1411type UserManager struct {
@@ -23,154 +20,12 @@ const (
2320 UsersPath = "/home/users"
2421)
2522
26- func (um * UserManager ) KeystoreStatus (scope , id string ) (* keystore.Status , error ) {
27- userKeystorePath := assembleUserPath (scope , id ) + ".ks.json"
28-
29- response , err := um .instance .http .Request ().Get (userKeystorePath )
30-
31- if err != nil {
32- return nil , fmt .Errorf ("%s > cannot read user Keystore: %w" , um .instance .IDColor (), err )
33- }
34-
35- if response .IsError () {
36- return nil , fmt .Errorf ("%s > cannot read user keystore: %s" , um .instance .IDColor (), response .Status ())
37- }
38-
39- result , err := keystore .UnmarshalStatus (response .RawBody ())
40- if err != nil {
41- return nil , fmt .Errorf ("%s > cannot parse user Keystore status response: %w" , um .instance .IDColor (), err )
42- }
43-
44- return result , nil
45- }
46-
47- func (um * UserManager ) KeystoreCreate (scope , id , keystorePassword string ) (bool , error ) {
48- statusResponse , statusError := um .KeystoreStatus (scope , id )
49- if statusError != nil {
50- return false , statusError
51- }
52-
53- if statusResponse .Created {
54- return false , nil
55- }
56-
57- pathParams := map [string ]string {
58- "newPassword" : keystorePassword ,
59- "rePassword" : keystorePassword ,
60- ":operation" : "createStore" ,
61- }
62-
63- userKeystoreCreatePath := assembleUserPath (scope , id ) + ".ks.html"
64- postResponse , postError := um .instance .http .Request ().SetQueryParams (pathParams ).Post (userKeystoreCreatePath )
65-
66- if postError != nil {
67- return false , fmt .Errorf ("%s > cannot create user keystore: %w" , um .instance .IDColor (), postError )
68- }
69-
70- if postResponse .IsError () {
71- return false , fmt .Errorf ("%s > cannot create user keystore: %s" , um .instance .IDColor (), postResponse .Status ())
72- }
73-
74- return true , nil
75- }
76-
77- func (um * UserManager ) AddKeystoreKey (scope , id , keystoreFilePath , keystoreFilePassword , privateKeyAlias , privateKeyPassword , privateKeyNewAlias string ) (bool , error ) {
78- if ! pathx .Exists (keystoreFilePath ) {
79- return false , fmt .Errorf ("%s > keystore file does not exist: %s" , um .instance .IDColor (), keystoreFilePath )
80- }
81- if privateKeyNewAlias == "" {
82- privateKeyNewAlias = privateKeyAlias
83- }
84- if privateKeyPassword == "" {
85- privateKeyPassword = keystoreFilePassword
86- }
87-
88- readKeystore , err := readKeyStore (keystoreFilePath , []byte (keystoreFilePassword ))
89- if err != nil {
90- return false , fmt .Errorf ("%s > cannot read keystore file %s: %w" , um .instance .IDColor (), keystoreFilePath , err )
91- }
92-
93- aliases := readKeystore .Aliases ()
94- if aliases == nil {
95- return false , fmt .Errorf ("%s > keystore does not contain any aliases" , um .instance .IDColor ())
96- }
97- if ! slices .Contains (aliases , privateKeyAlias ) {
98- return false , fmt .Errorf ("%s > keystore does not contain alias: %s" , um .instance .IDColor (), privateKeyAlias )
99- }
100-
101- status , err := um .KeystoreStatus (scope , id )
102- if err != nil {
103- return false , err
104- }
105-
106- if status == nil || ! status .Created {
107- return false , fmt .Errorf ("%s > cannot add keystore key: keystore does not exist" , um .instance .IDColor ())
108- }
109- if status .HasAlias (privateKeyAlias ) {
110- return false , nil
111- }
112-
113- requestFiles := map [string ]string {
114- "keyStore" : keystoreFilePath ,
115- }
116-
117- keystorePath := assembleUserPath (scope , id ) + ".ks.html"
118- formData := map [string ]string {
119- "keyStorePass" : keystoreFilePassword ,
120- "alias" : privateKeyAlias ,
121- "keyPassword" : privateKeyPassword ,
122- "newAlias" : privateKeyNewAlias ,
123- "keyStoreType" : "jks" ,
124- }
125-
126- response , err := um .instance .http .Request ().
127- SetFiles (requestFiles ).
128- SetFormData (formData ).
129- Post (keystorePath )
130-
131- if err != nil {
132- return false , fmt .Errorf ("%s > cannot add keystore key: %w" , um .instance .IDColor (), err )
133- }
134- if response .IsError () {
135- return false , fmt .Errorf ("%s > cannot add keystore key: %s" , um .instance .IDColor (), response .Status ())
136- }
137- return true , nil
138- }
139-
140- func (um * UserManager ) DeleteKeystoreKey (scope , id , privateKeyAlias string ) (bool , error ) {
141- status , err := um .KeystoreStatus (scope , id )
142- if err != nil {
143- return false , err
144- }
145-
146- if status == nil || ! status .Created {
147- return false , fmt .Errorf ("%s > cannot delete keystore key: keystore does not exist" , um .instance .IDColor ())
148- }
149- if ! status .HasAlias (privateKeyAlias ) {
150- return false , nil
151- }
152-
153- formData := map [string ]string {
154- "removeAlias" : privateKeyAlias ,
155- }
156-
157- userKeystorePath := assembleUserPath (scope , id ) + ".ks.html"
158- response , err := um .instance .http .Request ().
159- SetFormData (formData ).
160- Post (userKeystorePath )
161-
162- if err != nil {
163- return false , fmt .Errorf ("%s > cannot delete keystore key: %w" , um .instance .IDColor (), err )
164- }
165- if response .IsError () {
166- return false , fmt .Errorf ("%s > cannot delete keystore key: %s" , um .instance .IDColor (), response .Status ())
167- }
168-
169- return true , nil
23+ func (um * UserManager ) Keystore () * KeystoreManager {
24+ return & KeystoreManager {instance : um .instance }
17025}
17126
17227func (um * UserManager ) ReadState (scope string , id string ) (* user.Status , error ) {
173- userPath := assembleUserPath (scope , id )
28+ userPath := composeUserPath (scope , id )
17429
17530 response , err := um .instance .http .Request ().Get (userPath + ".json" )
17631
@@ -195,7 +50,7 @@ func (um *UserManager) SetPassword(scope string, id string, password string) (bo
19550 return false , err
19651 }
19752
198- userPath := assembleUserPath (scope , id )
53+ userPath := composeUserPath (scope , id )
19954 passwordCheckResponse , err := um .instance .http .Request ().
20055 SetBasicAuth (userStatus .AuthorizableID , password ).
20156 Get (userPath + ".json" )
@@ -222,7 +77,7 @@ func (um *UserManager) SetPassword(scope string, id string, password string) (bo
22277 return true , nil
22378}
22479
225- func assembleUserPath (scope string , id string ) string {
80+ func composeUserPath (scope string , id string ) string {
22681 if scope == "" {
22782 return UsersPath + "/" + id
22883 }
0 commit comments