Skip to content

Commit 3afa3a8

Browse files
author
Piotr Andruszkiewicz
committed
Refactor UserManager and KeystoreManager: streamline keystore methods and improve structure
1 parent 2384789 commit 3afa3a8

File tree

3 files changed

+168
-154
lines changed

3 files changed

+168
-154
lines changed

cmd/aem/user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (c *CLI) KeystoreStatus() *cobra.Command {
6666
id, _ := cmd.Flags().GetString("id")
6767
scope, _ := cmd.Flags().GetString("scope")
6868

69-
result, err := instance.Auth().UserManager().KeystoreStatus(scope, id)
69+
result, err := instance.Auth().UserManager().Keystore().Status(scope, id)
7070

7171
if err != nil {
7272
c.Error(err)
@@ -98,7 +98,7 @@ func (c *CLI) KeystoreCreate() *cobra.Command {
9898
id, _ := cmd.Flags().GetString("id")
9999
scope, _ := cmd.Flags().GetString("scope")
100100
password, _ := cmd.Flags().GetString("keystore-password")
101-
changed, err := instance.Auth().UserManager().KeystoreCreate(scope, id, password)
101+
changed, err := instance.Auth().UserManager().Keystore().Create(scope, id, password)
102102

103103
if err != nil {
104104
c.Error(err)
@@ -133,7 +133,7 @@ func (c *CLI) userKeyAdd() *cobra.Command {
133133
return
134134
}
135135

136-
changed, err := instance.Auth().UserManager().AddKeystoreKey(
136+
changed, err := instance.Auth().UserManager().Keystore().AddKey(
137137
cmd.Flag("scope").Value.String(),
138138
cmd.Flag("id").Value.String(),
139139
cmd.Flag("keystore-file").Value.String(),
@@ -182,7 +182,7 @@ func (c *CLI) userKeyDelete() *cobra.Command {
182182
return
183183
}
184184

185-
changed, err := instance.Auth().UserManager().DeleteKeystoreKey(
185+
changed, err := instance.Auth().UserManager().Keystore().DeleteKey(
186186
cmd.Flag("scope").Value.String(),
187187
cmd.Flag("id").Value.String(),
188188
cmd.Flag("key-alias").Value.String(),

pkg/keystore_manager.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package pkg
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
7+
"github.com/wttech/aemc/pkg/common/pathx"
8+
"github.com/wttech/aemc/pkg/keystore"
9+
)
10+
11+
type KeystoreManager struct {
12+
instance *Instance
13+
}
14+
15+
func (km *KeystoreManager) Status(scope, id string) (*keystore.Status, error) {
16+
userKeystorePath := composeUserPath(scope, id) + ".ks.json"
17+
18+
response, err := km.instance.http.Request().Get(userKeystorePath)
19+
20+
if err != nil {
21+
return nil, fmt.Errorf("%s > cannot read user keystore: %w", km.instance.IDColor(), err)
22+
}
23+
24+
if response.IsError() {
25+
return nil, fmt.Errorf("%s > cannot read user keystore: %s", km.instance.IDColor(), response.Status())
26+
}
27+
28+
result, err := keystore.UnmarshalStatus(response.RawBody())
29+
if err != nil {
30+
return nil, fmt.Errorf("%s > cannot parse user keystore status response: %w", km.instance.IDColor(), err)
31+
}
32+
33+
return result, nil
34+
}
35+
36+
func (km *KeystoreManager) Create(scope, id, keystorePassword string) (bool, error) {
37+
statusResponse, statusError := km.Status(scope, id)
38+
if statusError != nil {
39+
return false, statusError
40+
}
41+
42+
if statusResponse.Created {
43+
return false, nil
44+
}
45+
46+
pathParams := map[string]string{
47+
"newPassword": keystorePassword,
48+
"rePassword": keystorePassword,
49+
":operation": "createStore",
50+
}
51+
52+
userKeystoreCreatePath := composeUserPath(scope, id) + ".ks.html"
53+
postResponse, postError := km.instance.http.Request().SetQueryParams(pathParams).Post(userKeystoreCreatePath)
54+
55+
if postError != nil {
56+
return false, fmt.Errorf("%s > cannot create user keystore: %w", km.instance.IDColor(), postError)
57+
}
58+
59+
if postResponse.IsError() {
60+
return false, fmt.Errorf("%s > cannot create user keystore: %s", km.instance.IDColor(), postResponse.Status())
61+
}
62+
63+
return true, nil
64+
}
65+
66+
func (km *KeystoreManager) AddKey(scope, id, keystoreFilePath, keystoreFilePassword, privateKeyAlias, privateKeyPassword, privateKeyNewAlias string) (bool, error) {
67+
if !pathx.Exists(keystoreFilePath) {
68+
return false, fmt.Errorf("%s > keystore file does not exist: %s", km.instance.IDColor(), keystoreFilePath)
69+
}
70+
if privateKeyNewAlias == "" {
71+
privateKeyNewAlias = privateKeyAlias
72+
}
73+
if privateKeyPassword == "" {
74+
privateKeyPassword = keystoreFilePassword
75+
}
76+
77+
readKeystore, err := readKeyStore(keystoreFilePath, []byte(keystoreFilePassword))
78+
if err != nil {
79+
return false, fmt.Errorf("%s > cannot read keystore file %s: %w", km.instance.IDColor(), keystoreFilePath, err)
80+
}
81+
82+
aliases := readKeystore.Aliases()
83+
if aliases == nil {
84+
return false, fmt.Errorf("%s > keystore file does not contain any aliases", km.instance.IDColor())
85+
}
86+
if !slices.Contains(aliases, privateKeyAlias) {
87+
return false, fmt.Errorf("%s > keystore file does not contain alias: %s", km.instance.IDColor(), privateKeyAlias)
88+
}
89+
90+
status, err := km.Status(scope, id)
91+
if err != nil {
92+
return false, err
93+
}
94+
95+
if status == nil || !status.Created {
96+
return false, fmt.Errorf("%s > cannot add key as keystore does not exist", km.instance.IDColor())
97+
}
98+
if status.HasAlias(privateKeyAlias) {
99+
return false, nil
100+
}
101+
102+
requestFiles := map[string]string{
103+
"keyStore": keystoreFilePath,
104+
}
105+
106+
keystorePath := composeUserPath(scope, id) + ".ks.html"
107+
formData := map[string]string{
108+
"keyStorePass": keystoreFilePassword,
109+
"alias": privateKeyAlias,
110+
"keyPassword": privateKeyPassword,
111+
"newAlias": privateKeyNewAlias,
112+
"keyStoreType": "jks",
113+
}
114+
115+
response, err := km.instance.http.Request().
116+
SetFiles(requestFiles).
117+
SetFormData(formData).
118+
Post(keystorePath)
119+
120+
if err != nil {
121+
return false, fmt.Errorf("%s > cannot add key: %w", km.instance.IDColor(), err)
122+
}
123+
if response.IsError() {
124+
return false, fmt.Errorf("%s > cannot add key: %s", km.instance.IDColor(), response.Status())
125+
}
126+
return true, nil
127+
}
128+
129+
func (km *KeystoreManager) DeleteKey(scope, id, privateKeyAlias string) (bool, error) {
130+
status, err := km.Status(scope, id)
131+
if err != nil {
132+
return false, err
133+
}
134+
135+
if status == nil || !status.Created {
136+
return false, fmt.Errorf("%s > cannot delete key: keystore does not exist", km.instance.IDColor())
137+
}
138+
if !status.HasAlias(privateKeyAlias) {
139+
return false, nil
140+
}
141+
142+
formData := map[string]string{
143+
"removeAlias": privateKeyAlias,
144+
}
145+
146+
userKeystorePath := composeUserPath(scope, id) + ".ks.html"
147+
response, err := km.instance.http.Request().
148+
SetFormData(formData).
149+
Post(userKeystorePath)
150+
151+
if err != nil {
152+
return false, fmt.Errorf("%s > cannot delete key: %w", km.instance.IDColor(), err)
153+
}
154+
if response.IsError() {
155+
return false, fmt.Errorf("%s > cannot delete key: %s", km.instance.IDColor(), response.Status())
156+
}
157+
158+
return true, nil
159+
}

pkg/user_manager.go

Lines changed: 5 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1411
type 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

17227
func (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

Comments
 (0)