-
Notifications
You must be signed in to change notification settings - Fork 481
/
password.go
219 lines (188 loc) · 6.2 KB
/
password.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*******************************************************************************
* Copyright 2019 Dell Inc.
*
* Licensed 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 secretstore
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/edgexfoundry/edgex-go/internal"
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger"
)
type passwordGenerator struct {
generatorImplementation CredentialGenerator
}
// NewPasswordGenerator wires up a pluggable password generator
// or defaults to a built-in implementation if
// the pluggable configuration is missing
func NewPasswordGenerator(lc logger.LoggingClient, passwordProvider string, passwordProviderArgs []string) CredentialGenerator {
gk := &passwordGenerator{
generatorImplementation: NewDefaultCredentialGenerator(),
}
if passwordProvider != "" {
pp := NewPasswordProvider(lc, NewDefaultExecRunner())
err := pp.SetConfiguration(passwordProvider, passwordProviderArgs)
if err != nil {
lc.Warn(fmt.Sprintf("Could not configure password generator %s: error: %s", passwordProvider, err.Error()))
return gk // fall-back to builtin
}
gk.generatorImplementation = pp
}
return gk
}
// Generate delegates password generation to underlying implementation
func (gk *passwordGenerator) Generate(ctx context.Context) (string, error) {
return gk.generatorImplementation.Generate(ctx)
}
type CredCollect struct {
Pair UserPasswordPair `json:"data"`
}
type UserPasswordPair struct {
User string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
type Cred struct {
client internal.HttpCaller
rootToken string
generator CredentialGenerator
secretServiceBaseURL string
loggingClient logger.LoggingClient
}
func NewCred(
caller internal.HttpCaller,
rootToken string,
generator CredentialGenerator,
secretServiceBaseURL string,
lc logger.LoggingClient) Cred {
return Cred{
client: caller,
rootToken: rootToken,
generator: generator,
secretServiceBaseURL: secretServiceBaseURL,
loggingClient: lc,
}
}
func (cr *Cred) AlreadyInStore(path string) (bool, error) {
pair, err := cr.getUserPasswordPair(path)
if err != nil {
if err == errNotFound {
return false, nil
}
return false, err
}
if len(pair.User) > 0 && len(pair.Password) > 0 {
return true, nil
}
return false, nil
}
func (cr *Cred) getUserPasswordPair(path string) (*UserPasswordPair, error) {
pair, err := cr.retrieve(path)
if err != nil {
return nil, err
}
return pair, nil
}
func (cr *Cred) retrieve(path string) (*UserPasswordPair, error) {
credUrl, err := cr.credPathURL(path)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, credUrl, nil)
if err != nil {
e := fmt.Errorf("error creating http request: %v", err.Error())
cr.loggingClient.Error(e.Error())
return nil, e
}
req.Header.Set(VaultToken, cr.rootToken)
resp, err := cr.client.Do(req)
if err != nil {
e := fmt.Errorf("failed to retrieve the credential pair on path %s with error %s", path, err.Error())
cr.loggingClient.Error(e.Error())
return nil, e
}
defer resp.Body.Close()
cred := CredCollect{}
if resp.StatusCode == http.StatusNotFound {
cr.loggingClient.Info(fmt.Sprintf("credential pair NOT found in secret store @/%s, status: %s", path, resp.Status))
return nil, errNotFound
} else if resp.StatusCode != http.StatusOK {
e := fmt.Errorf("failed to retrieve the credential pair on path %s with error code %d", path, resp.StatusCode)
cr.loggingClient.Error(e.Error())
return nil, e
}
if err = json.NewDecoder(resp.Body).Decode(&cred); err != nil {
e := fmt.Errorf("error decoding json response when retrieving credential pair: %s", err.Error())
cr.loggingClient.Error(e.Error())
return nil, e
}
return &cred.Pair, nil
}
func (cr *Cred) credPathURL(path string) (string, error) {
baseURL, err := url.Parse(cr.secretServiceBaseURL)
if err != nil {
e := fmt.Errorf("error parsing secret-service url: %s", err.Error())
cr.loggingClient.Error(e.Error())
return "", err
}
p, err := url.Parse(path)
if err != nil {
e := fmt.Errorf("error parsing secret-service credpath: %s", err.Error())
cr.loggingClient.Error(e.Error())
return "", err
}
fullURL := baseURL.ResolveReference(p)
return fullURL.String(), nil
}
// GeneratePassword is a pass-through to the password generator
func (cr *Cred) GeneratePassword(ctx context.Context) (string, error) {
return cr.generator.Generate(ctx)
}
func (cr *Cred) UploadToStore(pair *UserPasswordPair, path string) error {
cr.loggingClient.Debug("trying to upload the credential pair into secret store")
jsonBytes, err := json.Marshal(pair)
body := bytes.NewBuffer(jsonBytes)
credURL, err := cr.credPathURL(path)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, credURL, body)
if err != nil {
e := fmt.Errorf("error creating http request: %v", err.Error())
cr.loggingClient.Error(e.Error())
return e
}
req.Header.Set(VaultToken, cr.rootToken)
resp, err := cr.client.Do(req)
if err != nil {
e := fmt.Sprintf("failed to upload the credential pair on path %s: %s", path, err.Error())
cr.loggingClient.Error(e)
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
e := fmt.Errorf("failed to load the credential pair to the secret store: %s %s", resp.Status, string(b))
cr.loggingClient.Error(e.Error())
return e
}
cr.loggingClient.Info("successfully uploaded the credential pair into secret store")
return nil
}