/
credhub_getter.go
96 lines (76 loc) · 2.12 KB
/
credhub_getter.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package bosh
import (
"fmt"
"path/filepath"
"github.com/cloudfoundry/bosh-bootloader/fileio"
yaml "gopkg.in/yaml.v2"
)
type CredhubGetter struct {
stateStore stateStore
reader fileio.FileReader
}
func NewCredhubGetter(stateStore stateStore, fileIO fileio.FileReader) CredhubGetter {
return CredhubGetter{
stateStore: stateStore,
reader: fileIO,
}
}
func (c CredhubGetter) GetServer() (string, error) {
var p struct {
InternalIp string `yaml:"internal_ip"`
}
varsDir, err := c.stateStore.GetVarsDir()
if err != nil {
return "", fmt.Errorf("Get vars directory: %s", err)
}
varsFile, err := c.reader.ReadFile(filepath.Join(varsDir, "director-vars-file.yml"))
if err != nil {
return "", fmt.Errorf("Read director-vars-file.yml file: %s", err)
}
err = yaml.Unmarshal(varsFile, &p)
if err != nil {
return "", err
}
return fmt.Sprintf("https://%s:8844", p.InternalIp), nil
}
func (c CredhubGetter) GetCerts() (string, error) {
var certs struct {
CredhubCA struct {
Certificate string `yaml:"certificate"`
} `yaml:"credhub_ca"`
UAASSL struct {
Certificate string `yaml:"certificate"`
} `yaml:"uaa_ssl"`
}
varsDir, err := c.stateStore.GetVarsDir()
if err != nil {
return "", fmt.Errorf("Get vars directory: %s", err)
}
varsStore, err := c.reader.ReadFile(filepath.Join(varsDir, "director-vars-store.yml"))
if err != nil {
return "", fmt.Errorf("Read director-vars-store.yml file: %s", err)
}
err = yaml.Unmarshal(varsStore, &certs)
if err != nil {
return "", err
}
return fmt.Sprintf("%s%s", certs.CredhubCA.Certificate, certs.UAASSL.Certificate), nil
}
func (c CredhubGetter) GetPassword() (string, error) {
var certs struct {
Password string `yaml:"credhub_admin_client_secret"`
}
varsDir, err := c.stateStore.GetVarsDir()
if err != nil {
return "", fmt.Errorf("Get vars directory: %s", err)
}
varsStore, err := c.reader.ReadFile(filepath.Join(varsDir, "director-vars-store.yml"))
if err != nil {
return "", fmt.Errorf("Read director-vars-store.yml file: %s", err)
}
err = yaml.Unmarshal(varsStore, &certs)
if err != nil {
return "", err
}
return certs.Password, nil
}