forked from pivotal-cf/on-demand-service-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credhubstore.go
202 lines (164 loc) · 5.4 KB
/
credhubstore.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (C) 2015-Present Pivotal Software, Inc. All rights reserved.
// This program and the accompanying materials are made available under
// the terms of the under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package credhub
import (
"errors"
"fmt"
"log"
"strings"
"encoding/json"
"code.cloudfoundry.org/credhub-cli/credhub"
"code.cloudfoundry.org/credhub-cli/credhub/credentials"
"code.cloudfoundry.org/credhub-cli/credhub/credentials/values"
"code.cloudfoundry.org/credhub-cli/credhub/permissions"
"github.com/pivotal-cf/on-demand-service-broker/boshdirector"
"github.com/pivotal-cf/on-demand-service-broker/broker"
)
type Store struct {
credhubClient CredhubClient
}
//go:generate counterfeiter -o fakes/fake_credhub_client.go . CredhubClient
type CredhubClient interface {
GetById(id string) (credentials.Credential, error)
GetLatestVersion(name string) (credentials.Credential, error)
FindByPartialName(partialName string) (credentials.FindResults, error)
SetJSON(name string, value values.JSON) (credentials.JSON, error)
SetValue(name string, value values.Value) (credentials.Value, error)
AddPermission(credName string, actor string, ops []string) (*permissions.Permission, error)
Delete(name string) error
}
func Build(APIURL string, options ...credhub.Option) (*Store, error) {
credhubClient, err := credhub.New(APIURL, options...)
if err != nil {
return &Store{}, err
}
credhubStore := New(credhubClient)
return credhubStore, nil
}
func New(credhubClient CredhubClient) *Store {
return &Store{credhubClient: credhubClient}
}
func (c *Store) Set(key string, value interface{}) error {
var err error
switch credValue := value.(type) {
case map[string]interface{}:
_, err = c.credhubClient.SetJSON(key, values.JSON(credValue))
case string:
_, err = c.credhubClient.SetValue(key, values.Value(credValue))
default:
return errors.New("Unknown credential type")
}
return err
}
func (c *Store) AddPermission(credName string, actor string, ops []string) (*permissions.Permission, error) {
return c.credhubClient.AddPermission(credName, actor, ops)
}
func (c *Store) BulkDelete(paths []string, logger *log.Logger) error {
for _, path := range paths {
if err := c.Delete(path); err != nil {
logger.Printf("could not delete secret '%s': %s", path, err.Error())
return err
}
}
return nil
}
func (c *Store) FindNameLike(name string, logger *log.Logger) ([]string, error) {
results, err := c.credhubClient.FindByPartialName(name)
if err != nil {
return nil, err
}
var paths []string
for _, result := range results.Credentials {
paths = append(paths, result.Name)
}
return paths, nil
}
func (c *Store) Delete(key string) error {
return c.credhubClient.Delete(key)
}
func (c *Store) BulkGet(secretsToFetch map[string]boshdirector.Variable, logger *log.Logger) (map[string]string, error) {
ret := map[string]string{}
for name, deploymentVar := range secretsToFetch {
var cred credentials.Credential
var err error
if deploymentVar.ID != "" {
cred, err = c.credhubClient.GetById(deploymentVar.ID)
} else {
cred, err = c.credhubClient.GetLatestVersion(deploymentVar.Path)
}
if err != nil {
logger.Printf("Could not resolve %s: %s", name, err)
continue
}
var keyValue string
namePieces := strings.Split(strings.Trim(name, "()"), ".")
if len(namePieces) == 2 {
keyValue, err = getSubKey(cred.Value, namePieces[1])
} else {
keyValue, err = getKey(cred.Value)
}
if err != nil {
logger.Println(err.Error())
continue
}
ret[name] = keyValue
}
return ret, nil
}
func getKey(requestedValue interface{}) (string, error) {
switch credValue := requestedValue.(type) {
case string:
return credValue, nil
case map[string]interface{}:
// this will catch structured types: certificate, user, rsa, ssh
credValueJSON, err := json.Marshal(credValue)
if err != nil {
return "", errors.New("failed to marshal secret: " + err.Error())
}
return string(credValueJSON), nil
default:
return "", fmt.Errorf("unexpected datatype received from credhub %T", credValue)
}
}
func getSubKey(credValue interface{}, subkey string) (string, error) {
var requestedValue string
switch credValue := credValue.(type) {
case map[string]interface{}:
var jsonPart interface{}
var ok bool
if jsonPart, ok = credValue[subkey]; !ok {
return "", fmt.Errorf("credential does not contain key '%s'", subkey)
}
if jsonStr, ok := jsonPart.(string); ok {
requestedValue = jsonStr
break
}
partBytes, err := json.Marshal(jsonPart)
if err != nil {
return "", err
}
requestedValue = string(partBytes)
case string:
return "", fmt.Errorf("string type credential cannot have key '%s'", subkey)
default:
return "", fmt.Errorf("unknown credential type")
}
return requestedValue, nil
}
func (c *Store) BulkSet(secretsToSet []broker.ManifestSecret) error {
for _, secret := range secretsToSet {
if err := c.Set(secret.Path, secret.Value); err != nil {
return err
}
}
return nil
}