/
set.go
133 lines (103 loc) · 3.91 KB
/
set.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
package credhub
import (
"encoding/json"
"io"
"net/http"
"github.com/hashicorp/go-version"
"code.cloudfoundry.org/credhub-cli/credhub/credentials"
"code.cloudfoundry.org/credhub-cli/credhub/credentials/values"
)
// Option can be provided to New() to specify additional parameters for
// connecting to the CredHub server
type SetOption func(*SetOptions) error
// SetValue sets a value credential with a user-provided value.
func (ch *CredHub) SetValue(name string, value values.Value, options ...SetOption) (credentials.Value, error) {
var cred credentials.Value
err := ch.setCredential(name, "value", value, &cred, options...)
return cred, err
}
// SetJSON sets a JSON credential with a user-provided value.
func (ch *CredHub) SetJSON(name string, value values.JSON, options ...SetOption) (credentials.JSON, error) {
var cred credentials.JSON
err := ch.setCredential(name, "json", value, &cred, options...)
return cred, err
}
// SetPassword sets a password credential with a user-provided value.
func (ch *CredHub) SetPassword(name string, value values.Password, options ...SetOption) (credentials.Password, error) {
var cred credentials.Password
err := ch.setCredential(name, "password", value, &cred, options...)
return cred, err
}
// SetUser sets a user credential with a user-provided value.
func (ch *CredHub) SetUser(name string, value values.User, options ...SetOption) (credentials.User, error) {
var cred credentials.User
err := ch.setCredential(name, "user", value, &cred, options...)
return cred, err
}
// SetCertificate sets a certificate credential with a user-provided value.
func (ch *CredHub) SetCertificate(name string, value values.Certificate, options ...SetOption) (credentials.Certificate, error) {
var cred credentials.Certificate
err := ch.setCredential(name, "certificate", value, &cred, options...)
return cred, err
}
// SetRSA sets an RSA credential with a user-provided value.
func (ch *CredHub) SetRSA(name string, value values.RSA, options ...SetOption) (credentials.RSA, error) {
var cred credentials.RSA
err := ch.setCredential(name, "rsa", value, &cred, options...)
return cred, err
}
// SetSSH sets an SSH credential with a user-provided value.
func (ch *CredHub) SetSSH(name string, value values.SSH, options ...SetOption) (credentials.SSH, error) {
var cred credentials.SSH
err := ch.setCredential(name, "ssh", value, &cred, options...)
return cred, err
}
// SetCredential sets a credential of any type with a user-provided value.
func (ch *CredHub) SetCredential(name, credType string, value interface{}, options ...SetOption) (credentials.Credential, error) {
var cred credentials.Credential
err := ch.setCredential(name, credType, value, &cred, options...)
return cred, err
}
type setRequest struct {
Name string `json:"name"`
Type string `json:"type"`
Value interface{} `json:"value"`
Mode string `json:"mode,omitempty"`
SetOptions
}
type SetOptions struct {
Metadata credentials.Metadata `json:"metadata,omitempty"`
}
func (ch *CredHub) setCredential(name, credType string, value, cred interface{}, options ...SetOption) error {
request := &setRequest{
Name: name,
Type: credType,
Value: value,
}
serverVersion, err := ch.ServerVersion()
if err != nil {
return err
}
if serverVersion.Segments()[0] < 2 {
request.Mode = "overwrite"
}
for _, option := range options {
if err := option(&request.SetOptions); err != nil {
return err
}
}
if request.Metadata != nil && !supportsMetadata(serverVersion) {
return ServerDoesNotSupportMetadataError
}
resp, err := ch.Request(http.MethodPut, "/api/v1/data", nil, request, true)
if err != nil {
return err
}
defer resp.Body.Close()
defer io.Copy(io.Discard, resp.Body)
return json.NewDecoder(resp.Body).Decode(cred)
}
func supportsMetadata(v *version.Version) bool {
supportedVersion := version.Must(version.NewVersion("2.6.0"))
return v.GreaterThan(supportedVersion) || v.Equal(supportedVersion)
}