-
Notifications
You must be signed in to change notification settings - Fork 479
/
certs.go
211 lines (179 loc) · 5.42 KB
/
certs.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
/*******************************************************************************
* 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.
*
* @author: Daniel Harms, Dell
* @author: Tingyu Zeng, Dell
*******************************************************************************/
package secretstore
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/edgexfoundry/edgex-go/internal"
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger"
)
type CertCollect struct {
Pair CertPair `json:"data"`
}
type CertPair struct {
Cert string `json:"cert,omitempty"`
Key string `json:"key,omitempty"`
}
type Certs struct {
client internal.HttpCaller
certPath string
rootToken string
secretServiceBaseURL string
loggingClient logger.LoggingClient
}
var errNotFound = errors.New("proxy cert pair not found in secret store")
func NewCerts(
caller internal.HttpCaller,
certPath string,
rootToken string,
secretServiceBaseURL string,
lc logger.LoggingClient) Certs {
return Certs{
client: caller,
certPath: certPath,
rootToken: rootToken,
secretServiceBaseURL: secretServiceBaseURL,
loggingClient: lc,
}
}
func (cs *Certs) certPathUrl() (string, error) {
baseURL, err := url.Parse(cs.secretServiceBaseURL)
if err != nil {
e := fmt.Errorf("error parsing secret-service url. check server and port properties")
cs.loggingClient.Error(e.Error())
return "", err
}
certPath, err := url.Parse(cs.certPath)
if err != nil {
e := fmt.Errorf("error parsing secret-service certpath. check certpath property")
cs.loggingClient.Error(e.Error())
return "", err
}
fullUrl := baseURL.ResolveReference(certPath)
return fullUrl.String(), nil
}
func (cs *Certs) retrieve() (*CertPair, error) {
certUrl, err := cs.certPathUrl()
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, certUrl, nil)
if err != nil {
e := fmt.Errorf("error creating http request: %v", err.Error())
cs.loggingClient.Error(e.Error())
return nil, e
}
req.Header.Set(VaultToken, cs.rootToken)
resp, err := cs.client.Do(req)
if err != nil {
e := fmt.Errorf("failed to retrieve the proxy cert on path %s with error %s", cs.certPath, err.Error())
cs.loggingClient.Error(e.Error())
return nil, e
}
defer resp.Body.Close()
cc := CertCollect{}
if resp.StatusCode == http.StatusNotFound {
cs.loggingClient.Info(fmt.Sprintf("proxy cert pair NOT found in secret store @/%s, status: %s", cs.certPath, resp.Status))
return nil, errNotFound
} else if resp.StatusCode != http.StatusOK {
e := fmt.Errorf("failed to retrieve the proxy cert pair on path %s with error code %d", cs.certPath, resp.StatusCode)
cs.loggingClient.Error(e.Error())
return nil, e
}
if err = json.NewDecoder(resp.Body).Decode(&cc); err != nil {
e := fmt.Errorf("Error decoding json response when retrieving proxy cert pair: %s", err.Error())
cs.loggingClient.Error(e.Error())
return nil, e
}
return &cc.Pair, nil
}
func (cs *Certs) AlreadyinStore() (bool, error) {
cp, err := cs.getCertPair()
if err != nil {
if err == errNotFound {
return false, nil
}
return false, err
}
if len(cp.Cert) > 0 && len(cp.Key) > 0 {
return true, nil
}
return false, nil
}
func (cs *Certs) getCertPair() (*CertPair, error) {
cp, err := cs.retrieve()
if err != nil {
return nil, err
}
return cp, nil
}
func (cs *Certs) ReadFrom(certPath string, keyPath string) (*CertPair, error) {
certPEMBlock, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, err
}
cert := string(certPEMBlock)
keyPEMBlock, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
key := string(keyPEMBlock)
cp := CertPair{
Cert: cert,
Key: key,
}
return &cp, nil
}
func (cs *Certs) UploadToStore(cp *CertPair) error {
cs.loggingClient.Info("trying to upload the proxy cert pair into secret store")
jsonBytes, err := json.Marshal(cp)
body := bytes.NewBuffer(jsonBytes)
certUrl, err := cs.certPathUrl()
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, certUrl, body)
if err != nil {
e := fmt.Errorf("error creating http request: %v", err.Error())
cs.loggingClient.Error(e.Error())
return e
}
req.Header.Set(VaultToken, cs.rootToken)
resp, err := cs.client.Do(req)
if err != nil {
e := fmt.Sprintf("failed to upload the proxy cert pair on path %s with error %s", cs.certPath, err.Error())
cs.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 proxy cert pair to the secret store: %s,%s", resp.Status, string(b))
cs.loggingClient.Error(e.Error())
return e
}
cs.loggingClient.Info("successful on uploading the proxy cert pair into secret store")
return nil
}