forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudcredential.go
204 lines (170 loc) · 5.51 KB
/
cloudcredential.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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/schema"
)
// CloudCredential represents the current cloud credential for the model.
type CloudCredential interface {
Owner() string
Cloud() string
Name() string
AuthType() string
Attributes() map[string]string
}
// CloudCredentialArgs is an argument struct used to create a new internal
// cloudCredential type that supports the CloudCredential interface.
type CloudCredentialArgs struct {
Owner names.UserTag
Cloud names.CloudTag
Name string
AuthType string
Attributes map[string]string
}
func newCloudCredential(args CloudCredentialArgs) *cloudCredential {
return &cloudCredential{
Version: 2,
Owner_: args.Owner.Id(),
Cloud_: args.Cloud.Id(),
Name_: args.Name,
AuthType_: args.AuthType,
Attributes_: args.Attributes,
}
}
// cloudCredential represents an IP CloudCredential of some form.
type cloudCredential struct {
Version int `yaml:"version"`
Owner_ string `yaml:"owner"`
Cloud_ string `yaml:"cloud"`
Name_ string `yaml:"name"`
AuthType_ string `yaml:"auth-type"`
Attributes_ map[string]string `yaml:"attributes,omitempty"`
}
// Owner implements CloudCredential.
func (c *cloudCredential) Owner() string {
return c.Owner_
}
// Cloud implements CloudCredential.
func (c *cloudCredential) Cloud() string {
return c.Cloud_
}
// Name implements CloudCredential.
func (c *cloudCredential) Name() string {
return c.Name_
}
// AuthType implements CloudCredential.
func (c *cloudCredential) AuthType() string {
return c.AuthType_
}
// Attributes implements CloudCredential.
func (c *cloudCredential) Attributes() map[string]string {
return c.Attributes_
}
// importCloudCredential constructs a new CloudCredential from a map
// representing a serialised CloudCredential instance.
func importCloudCredential(source map[string]interface{}) (*cloudCredential, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "cloudCredential version schema check failed")
}
importFunc, ok := cloudCredentialDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
return importFunc(source)
}
type cloudCredentialDeserializationFunc func(map[string]interface{}) (*cloudCredential, error)
var cloudCredentialDeserializationFuncs = map[int]cloudCredentialDeserializationFunc{
1: importCloudCredentialV1,
2: importCloudCredentialV2,
}
func cloudCredentialV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"owner": schema.String(),
"cloud": schema.String(),
"name": schema.String(),
"auth-type": schema.String(),
"attributes": schema.StringMap(schema.String()),
}
defaults := schema.Defaults{
"attributes": schema.Omit,
}
return fields, defaults
}
func cloudCredentialV2Fields() (schema.Fields, schema.Defaults) {
return cloudCredentialV1Fields()
}
func importCloudCredentialV1(source map[string]interface{}) (*cloudCredential, error) {
fields, defaults := cloudCredentialV2Fields()
return importCloudCredentialHandler(fields, defaults, 1, source)
}
func importCloudCredentialV2(source map[string]interface{}) (*cloudCredential, error) {
fields, defaults := cloudCredentialV2Fields()
return importCloudCredentialHandler(fields, defaults, 2, source)
}
func importCloudCredentialHandler(
fields schema.Fields,
defaults schema.Defaults,
importVersion int,
source map[string]interface{},
) (*cloudCredential, error) {
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "cloudCredential v%d schema check failed", importVersion)
}
valid := coerced.(map[string]interface{})
creds := &cloudCredential{
Version: 2,
Owner_: valid["owner"].(string),
Cloud_: valid["cloud"].(string),
Name_: valid["name"].(string),
AuthType_: valid["auth-type"].(string),
}
if attributes, found := valid["attributes"]; found {
creds.Attributes_ = convertToStringMap(attributes)
}
if importVersion >= 2 {
return creds, nil
}
switch creds.AuthType_ {
case "oauth2withcert":
creds, err = migrateOAuth2WithCertAuthType(creds)
case "certificate":
creds, err = migrateCertificateAuthType(creds)
}
return creds, err
}
func migrateOAuth2WithCertAuthType(cred *cloudCredential) (*cloudCredential, error) {
clientCert, clientCertExists := cred.Attributes_["ClientCertificateData"]
clientCertKey, clientCertKeyExists := cred.Attributes_["ClientKeyData"]
if clientCertExists && clientCertKeyExists {
cred.AuthType_ = "clientcertificate"
cred.Attributes_ = map[string]string{
"ClientCertificateData": clientCert,
"ClientKeyData": clientCertKey,
}
} else if token, tokenExists := cred.Attributes_["Token"]; tokenExists {
cred.AuthType_ = "oauth2"
cred.Attributes_ = map[string]string{
"Token": token,
}
} else {
return nil, errors.NotValidf("migrating oauth2cert must have either ClientCertificateData & ClientKeyData or Token attribute")
}
return cred, nil
}
func migrateCertificateAuthType(cred *cloudCredential) (*cloudCredential, error) {
token, tokenExists := cred.Attributes_["Token"]
if !tokenExists {
// This isn't a problem Kubernetes certificate type we need to migrate
return cred, nil
}
cred.AuthType_ = "oauth2"
cred.Attributes_ = map[string]string{
"Token": token,
}
return cred, nil
}