/
read.go
64 lines (53 loc) · 1.38 KB
/
read.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
package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
vault "github.com/hashicorp/vault/api"
gentlemen "gopkg.in/h2non/gentleman.v2"
)
// read from Vault on the path, given the access token
func (c *client) Read(path string, token string) (*vault.Secret, error) {
var req *gentlemen.Request
req = c.httpclient.Get()
req.Path("/v1/" + path)
req.SetHeader("X-Vault-Token", token)
resp, err := req.Do()
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
// check if we have an error
if (resp.StatusCode >= 200 && resp.StatusCode < 400) || resp.StatusCode == 429 {
// we have no error
var body vault.Secret
errJSON := resp.JSON(&body)
if errJSON != nil {
return nil, errJSON
}
return &body, nil
}
if resp.RawResponse.Body != nil && resp.StatusCode == 404 {
return nil, nil
}
// we have an error : store it in the buffer and try to decode it
var bodyBuf bytes.Buffer
if _, err := io.Copy(&bodyBuf, resp.RawResponse.Body); err != nil {
return nil, err
}
var errorMsgs []string
errJSON := json.Unmarshal(bodyBuf.Bytes(), &errorMsgs)
if errJSON != nil {
return nil, errors.New(bodyBuf.String())
}
// we could not decode : write the errors in a raw format
var errBody bytes.Buffer
for _, errMsg := range errorMsgs {
errBody.WriteString(fmt.Sprintf("* %s", errMsg))
}
return nil, fmt.Errorf(errBody.String())
}