/
variable.go
172 lines (143 loc) · 4.58 KB
/
variable.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
package conjurapi
import (
"encoding/base64"
"encoding/json"
"errors"
"io"
"net/http"
"github.com/cyberark/conjur-api-go/conjurapi/response"
)
// RetrieveBatchSecrets fetches values for all variables in a slice using a
// single API call
//
// The authenticated user must have execute privilege on all variables.
func (c *Client) RetrieveBatchSecrets(variableIDs []string) (map[string][]byte, error) {
jsonResponse, err := c.retrieveBatchSecrets(variableIDs, false)
if err != nil {
return nil, err
}
resolvedVariables := map[string][]byte{}
for id, value := range jsonResponse {
resolvedVariables[id] = []byte(value)
}
return resolvedVariables, nil
}
// RetrieveBatchSecretsSafe fetches values for all variables in a slice using a
// single API call. This version of the method will automatically base64-encode
// the secrets on the server side allowing the retrieval of binary values in
// batch requests. Secrets are NOT base64 encoded in the returned map.
//
// The authenticated user must have execute privilege on all variables.
func (c *Client) RetrieveBatchSecretsSafe(variableIDs []string) (map[string][]byte, error) {
jsonResponse, err := c.retrieveBatchSecrets(variableIDs, true)
if err != nil {
return nil, err
}
resolvedVariables := map[string][]byte{}
var decodedValue []byte
for id, value := range jsonResponse {
decodedValue, err = base64.StdEncoding.DecodeString(value)
if err != nil {
return nil, err
}
resolvedVariables[id] = decodedValue
}
return resolvedVariables, nil
}
// RetrieveSecret fetches a secret from a variable.
//
// The authenticated user must have execute privilege on the variable.
func (c *Client) RetrieveSecret(variableID string) ([]byte, error) {
resp, err := c.retrieveSecret(variableID)
if err != nil {
return nil, err
}
return response.DataResponse(resp)
}
// RetrieveSecretReader fetches a secret from a variable and returns it as a
// data stream.
//
// The authenticated user must have execute privilege on the variable.
func (c *Client) RetrieveSecretReader(variableID string) (io.ReadCloser, error) {
resp, err := c.retrieveSecret(variableID)
if err != nil {
return nil, err
}
return response.SecretDataResponse(resp)
}
// RetrieveSecretWithVersion fetches a specific version of a secret from a
// variable.
//
// The authenticated user must have execute privilege on the variable.
func (c *Client) RetrieveSecretWithVersion(variableID string, version int) ([]byte, error) {
resp, err := c.retrieveSecretWithVersion(variableID, version)
if err != nil {
return nil, err
}
return response.DataResponse(resp)
}
// RetrieveSecretWithVersionReader fetches a specific version of a secret from a
// variable and returns it as a data stream.
//
// The authenticated user must have execute privilege on the variable.
func (c *Client) RetrieveSecretWithVersionReader(variableID string, version int) (io.ReadCloser, error) {
resp, err := c.retrieveSecretWithVersion(variableID, version)
if err != nil {
return nil, err
}
return response.SecretDataResponse(resp)
}
func (c *Client) retrieveBatchSecrets(variableIDs []string, base64Flag bool) (map[string]string, error) {
req, err := c.RetrieveBatchSecretsRequest(variableIDs, base64Flag)
if err != nil {
return nil, err
}
resp, err := c.SubmitRequest(req)
if err != nil {
return nil, err
}
data, err := response.DataResponse(resp)
if err != nil {
return nil, err
}
if base64Flag && resp.Header.Get("Content-Encoding") != "base64" {
return nil, errors.New(
"Conjur response is not Base64-encoded. " +
"The Conjur version may not be compatible with this function - " +
"try using RetrieveBatchSecrets instead." )
}
jsonResponse := map[string]string{}
err = json.Unmarshal(data, &jsonResponse)
if err != nil {
return nil, err
}
return jsonResponse, nil
}
func (c *Client) retrieveSecret(variableID string) (*http.Response, error) {
req, err := c.RetrieveSecretRequest(variableID)
if err != nil {
return nil, err
}
return c.SubmitRequest(req)
}
func (c *Client) retrieveSecretWithVersion(variableID string, version int) (*http.Response, error) {
req, err := c.RetrieveSecretWithVersionRequest(variableID, version)
if err != nil {
return nil, err
}
return c.SubmitRequest(req)
}
// AddSecret adds a secret value to a variable.
//
// The authenticated user must have update privilege on the variable.
func (c *Client) AddSecret(variableID string, secretValue string) error {
req, err := c.AddSecretRequest(variableID, secretValue)
if err != nil {
return err
}
resp, err := c.SubmitRequest(req)
if err != nil {
return err
}
return response.EmptyResponse(resp)
}