-
Notifications
You must be signed in to change notification settings - Fork 20
/
keytoolimportpassword.go
65 lines (58 loc) · 1.9 KB
/
keytoolimportpassword.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
package generator
import (
"fmt"
"io"
"github.com/ForgeRock/secret-agent/api/v1alpha1"
"github.com/pkg/errors"
)
// NewKeyToolImportPassword create a new password alias manager
func NewKeyToolImportPassword(alias *v1alpha1.KeytoolAliasConfig) *KeyToolImportPassword {
return &KeyToolImportPassword{
v1aliasConfig: alias,
refData: []byte{},
}
}
// KeyToolImportPassword alias password manager
type KeyToolImportPassword struct {
v1aliasConfig *v1alpha1.KeytoolAliasConfig
refName string
refDataKey string
refData []byte
}
// References get list of refences needed for generated a alias
func (kp *KeyToolImportPassword) References() ([]string, []string) {
kp.refName, kp.refDataKey = handleRefPath(kp.v1aliasConfig.SourcePath)
return []string{kp.refName}, []string{kp.refDataKey}
}
// LoadReferenceData loads data from references
func (kp *KeyToolImportPassword) LoadReferenceData(data map[string][]byte) error {
key := fmt.Sprintf("%s/%s", kp.refName, kp.refDataKey)
var ok bool
if kp.refData, ok = data[key]; !ok {
return errors.Wrap(errNoRefFound, fmt.Sprintf("no data for %s", key))
}
return nil
}
// Generate creates keytool password alias entry
func (kp *KeyToolImportPassword) Generate(baseDir string, baseCmd cmdRunner) error {
// echo ${IMP_PASS} | keytool -importpass -alias ${ALIAS} ${KS_PROPS} 2> /dev/null
// KS_PROPS="-keystore ${KEYSTORE} -storetype ${STORE_TYPE} -storepass ${STORE_PASS} -keypass ${KEY_PASS}"
cmd := "-importpass"
args := []string{
"-alias", kp.v1aliasConfig.Name,
}
keyToolCmd := baseCmd(cmd, args)
keyToolStdIn, err := keyToolCmd.StdinPipe()
if err != nil {
return errors.WithMessage(err, "couldn't setup keytool with stdin pipe")
}
go func() {
defer keyToolStdIn.Close()
io.WriteString(keyToolStdIn, string(kp.refData))
}()
output, err := keyToolCmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, string(output))
}
return nil
}