forked from juju/juju
/
cloudcredentials.go
252 lines (233 loc) · 7.69 KB
/
cloudcredentials.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"github.com/juju/errors"
jujutxn "github.com/juju/txn"
"github.com/juju/utils/set"
"gopkg.in/juju/names.v2"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
"github.com/juju/juju/cloud"
)
// cloudCredentialDoc records information about a user's cloud credentials.
type cloudCredentialDoc struct {
DocID string `bson:"_id"`
Owner string `bson:"owner"`
Cloud string `bson:"cloud"`
Name string `bson:"name"`
Revoked bool `bson:"revoked"`
AuthType string `bson:"auth-type"`
Attributes map[string]string `bson:"attributes,omitempty"`
}
// CloudCredential returns the cloud credential for the given tag.
func (st *State) CloudCredential(tag names.CloudCredentialTag) (cloud.Credential, error) {
coll, cleanup := st.getCollection(cloudCredentialsC)
defer cleanup()
var doc cloudCredentialDoc
err := coll.FindId(cloudCredentialDocID(tag)).One(&doc)
if err == mgo.ErrNotFound {
return cloud.Credential{}, errors.NotFoundf(
"cloud credential %q", tag.Id(),
)
} else if err != nil {
return cloud.Credential{}, errors.Annotatef(
err, "getting cloud credential %q", tag.Id(),
)
}
return doc.toCredential(), nil
}
// CloudCredentials returns the user's cloud credentials for a given cloud,
// keyed by credential name.
func (st *State) CloudCredentials(user names.UserTag, cloudName string) (map[string]cloud.Credential, error) {
coll, cleanup := st.getCollection(cloudCredentialsC)
defer cleanup()
var doc cloudCredentialDoc
credentials := make(map[string]cloud.Credential)
iter := coll.Find(bson.D{
{"owner", user.Id()},
{"cloud", cloudName},
}).Iter()
for iter.Next(&doc) {
tag, err := doc.cloudCredentialTag()
if err != nil {
return nil, errors.Trace(err)
}
credentials[tag.Id()] = doc.toCredential()
}
if err := iter.Err(); err != nil {
return nil, errors.Annotatef(
err, "cannot get cloud credentials for user %q, cloud %q",
user.Id(), cloudName,
)
}
return credentials, nil
}
// UpdateCloudCredential adds or updates a cloud credential with the given tag.
func (st *State) UpdateCloudCredential(tag names.CloudCredentialTag, credential cloud.Credential) error {
credentials := map[names.CloudCredentialTag]cloud.Credential{tag: credential}
buildTxn := func(attempt int) ([]txn.Op, error) {
cloudName := tag.Cloud().Id()
cloud, err := st.Cloud(cloudName)
if err != nil {
return nil, errors.Trace(err)
}
ops, err := validateCloudCredentials(cloud, cloudName, credentials)
if err != nil {
return nil, errors.Annotate(err, "validating cloud credentials")
}
_, err = st.CloudCredential(tag)
if err != nil && !errors.IsNotFound(err) {
return nil, errors.Maskf(err, "fetching cloud credentials")
}
if err == nil {
ops = append(ops, updateCloudCredentialOp(tag, credential))
} else {
ops = append(ops, createCloudCredentialOp(tag, credential))
}
return ops, nil
}
if err := st.run(buildTxn); err != nil {
return errors.Annotate(err, "updating cloud credentials")
}
return nil
}
// RemoveCloudCredential removes a cloud credential with the given tag.
func (st *State) RemoveCloudCredential(tag names.CloudCredentialTag) error {
buildTxn := func(attempt int) ([]txn.Op, error) {
_, err := st.CloudCredential(tag)
if errors.IsNotFound(err) {
return nil, jujutxn.ErrNoOperations
}
if err != nil {
return nil, errors.Trace(err)
}
return removeCloudCredentialOps(tag), nil
}
if err := st.run(buildTxn); err != nil {
return errors.Annotate(err, "removing cloud credential")
}
return nil
}
// createCloudCredentialOp returns a txn.Op that will create
// a cloud credential.
func createCloudCredentialOp(tag names.CloudCredentialTag, cred cloud.Credential) txn.Op {
return txn.Op{
C: cloudCredentialsC,
Id: cloudCredentialDocID(tag),
Assert: txn.DocMissing,
Insert: &cloudCredentialDoc{
Owner: tag.Owner().Id(),
Cloud: tag.Cloud().Id(),
Name: tag.Name(),
AuthType: string(cred.AuthType()),
Attributes: cred.Attributes(),
Revoked: cred.Revoked,
},
}
}
// updateCloudCredentialOp returns a txn.Op that will update
// a cloud credential.
func updateCloudCredentialOp(tag names.CloudCredentialTag, cred cloud.Credential) txn.Op {
return txn.Op{
C: cloudCredentialsC,
Id: cloudCredentialDocID(tag),
Assert: txn.DocExists,
Update: bson.D{{"$set", bson.D{
{"auth-type", string(cred.AuthType())},
{"attributes", cred.Attributes()},
{"revoked", cred.Revoked},
}}},
}
}
// removeCloudCredentialOp returns a txn.Op that will remove
// a cloud credential.
func removeCloudCredentialOps(tag names.CloudCredentialTag) []txn.Op {
return []txn.Op{{
C: cloudCredentialsC,
Id: cloudCredentialDocID(tag),
Assert: txn.DocExists,
Remove: true,
}}
}
func cloudCredentialDocID(tag names.CloudCredentialTag) string {
return fmt.Sprintf("%s#%s#%s", tag.Cloud().Id(), tag.Owner().Id(), tag.Name())
}
func (c cloudCredentialDoc) cloudCredentialTag() (names.CloudCredentialTag, error) {
ownerTag := names.NewUserTag(c.Owner)
id := fmt.Sprintf("%s/%s/%s", c.Cloud, ownerTag.Id(), c.Name)
if !names.IsValidCloudCredential(id) {
return names.CloudCredentialTag{}, errors.NotValidf("cloud credential ID")
}
return names.NewCloudCredentialTag(id), nil
}
func (c cloudCredentialDoc) toCredential() cloud.Credential {
out := cloud.NewCredential(cloud.AuthType(c.AuthType), c.Attributes)
out.Revoked = c.Revoked
out.Label = c.Name
return out
}
// validateCloudCredentials checks that the supplied cloud credentials are
// valid for use with the controller's cloud, and returns a set of txn.Ops
// to assert the same in a transaction. The map keys are the cloud credential
// IDs.
//
// TODO(rogpeppe) We're going to a lot of effort here to assert that a
// cloud's auth types haven't changed since we looked at them a moment
// ago, but we don't support changing a cloud's definition currently and
// it's not clear that doing so would be a good idea, as changing a
// cloud's auth type would invalidate all existing credentials and would
// usually involve a new provider version and juju binary too, so
// perhaps all this code is unnecessary.
func validateCloudCredentials(
cloud cloud.Cloud,
cloudName string,
credentials map[names.CloudCredentialTag]cloud.Credential,
) ([]txn.Op, error) {
requiredAuthTypes := make(set.Strings)
for tag, credential := range credentials {
if tag.Cloud().Id() != cloudName {
return nil, errors.NewNotValid(nil, fmt.Sprintf(
"credential %q for non-matching cloud is not valid (expected %q)",
tag.Id(), cloudName,
))
}
var found bool
for _, authType := range cloud.AuthTypes {
if credential.AuthType() == authType {
found = true
break
}
}
if !found {
return nil, errors.NewNotValid(nil, fmt.Sprintf(
"credential %q with auth-type %q is not supported (expected one of %q)",
tag.Id(), credential.AuthType(), cloud.AuthTypes,
))
}
requiredAuthTypes.Add(string(credential.AuthType()))
}
ops := make([]txn.Op, len(requiredAuthTypes))
for i, authType := range requiredAuthTypes.SortedValues() {
ops[i] = txn.Op{
C: cloudsC,
Id: cloudName,
Assert: bson.D{{"auth-types", authType}},
}
}
return ops, nil
}
// WatchCredential returns a new NotifyWatcher watching for
// changes to the specified credential.
func (st *State) WatchCredential(cred names.CloudCredentialTag) NotifyWatcher {
filter := func(rawId interface{}) bool {
id, ok := rawId.(string)
if !ok {
return false
}
return id == cloudCredentialDocID(cred)
}
return newNotifyCollWatcher(st, cloudCredentialsC, filter)
}