Skip to content

Commit 9ad92cf

Browse files
author
Piotr Andruszkiewicz
committed
Refactor keystore management: streamline path composition and relocate readKeyStore function
1 parent edfd649 commit 9ad92cf

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

pkg/keystore_manager.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package pkg
22

33
import (
44
"fmt"
5+
"os"
56
"slices"
67

8+
jks "github.com/pavlo-v-chernykh/keystore-go/v4"
79
"github.com/wttech/aemc/pkg/common/pathx"
810
"github.com/wttech/aemc/pkg/keystore"
911
)
@@ -13,7 +15,7 @@ type KeystoreManager struct {
1315
}
1416

1517
func (km *KeystoreManager) Status(scope, id string) (*keystore.Status, error) {
16-
userKeystorePath := composeUserPath(scope, id) + ".ks.json"
18+
userKeystorePath := composeKeystoreStatusPath(scope, id)
1719

1820
response, err := km.instance.http.Request().Get(userKeystorePath)
1921

@@ -49,7 +51,7 @@ func (km *KeystoreManager) Create(scope, id, keystorePassword string) (bool, err
4951
":operation": "createStore",
5052
}
5153

52-
userKeystoreCreatePath := composeUserPath(scope, id) + ".ks.html"
54+
userKeystoreCreatePath := composeKeystoreOperationsPath(scope, id)
5355
postResponse, postError := km.instance.http.Request().SetQueryParams(pathParams).Post(userKeystoreCreatePath)
5456

5557
if postError != nil {
@@ -103,7 +105,7 @@ func (km *KeystoreManager) AddKey(scope, id, keystoreFilePath, keystoreFilePassw
103105
"keyStore": keystoreFilePath,
104106
}
105107

106-
keystorePath := composeUserPath(scope, id) + ".ks.html"
108+
keystorePath := composeKeystoreOperationsPath(scope, id)
107109
formData := map[string]string{
108110
"keyStorePass": keystoreFilePassword,
109111
"alias": privateKeyAlias,
@@ -143,7 +145,7 @@ func (km *KeystoreManager) DeleteKey(scope, id, privateKeyAlias string) (bool, e
143145
"removeAlias": privateKeyAlias,
144146
}
145147

146-
userKeystorePath := composeUserPath(scope, id) + ".ks.html"
148+
userKeystorePath := composeKeystoreOperationsPath(scope, id)
147149
response, err := km.instance.http.Request().
148150
SetFormData(formData).
149151
Post(userKeystorePath)
@@ -157,3 +159,27 @@ func (km *KeystoreManager) DeleteKey(scope, id, privateKeyAlias string) (bool, e
157159

158160
return true, nil
159161
}
162+
163+
func composeKeystoreStatusPath(scope, id string) string {
164+
return composeUserPath(scope, id) + ".ks.json"
165+
}
166+
167+
func composeKeystoreOperationsPath(scope, id string) string {
168+
return composeUserPath(scope, id) + ".ks.html"
169+
}
170+
171+
func readKeyStore(filename string, password []byte) (*jks.KeyStore, error) {
172+
f, err := os.Open(filename)
173+
if err != nil {
174+
return nil, err
175+
}
176+
177+
defer f.Close()
178+
179+
ks := jks.New()
180+
if err := ks.Load(f, password); err != nil {
181+
return nil, err
182+
}
183+
184+
return &ks, nil
185+
}

pkg/user_manager.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package pkg
22

33
import (
44
"fmt"
5-
"os"
65

7-
jks "github.com/pavlo-v-chernykh/keystore-go/v4"
86
"github.com/wttech/aemc/pkg/user"
97
)
108

@@ -83,19 +81,3 @@ func composeUserPath(scope string, id string) string {
8381
}
8482
return UsersPath + "/" + scope + "/" + id
8583
}
86-
87-
func readKeyStore(filename string, password []byte) (*jks.KeyStore, error) {
88-
f, err := os.Open(filename)
89-
if err != nil {
90-
return nil, err
91-
}
92-
93-
defer f.Close()
94-
95-
ks := jks.New()
96-
if err := ks.Load(f, password); err != nil {
97-
return nil, err
98-
}
99-
100-
return &ks, nil
101-
}

0 commit comments

Comments
 (0)