-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
236 lines (196 loc) · 6.47 KB
/
client.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
/*
Copyright 2022-2024 EscherCloud.
Copyright 2024 the Unikorn Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package openstack
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/utils/openstack/clientconfig"
)
// authenticatedClient returns a provider client used to initialize service clients.
func authenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) {
// TODO: the JWT token issuer will cap the expiry at that of the
// keystone token, so we shouldn't get an unauthorized error. Just
// as well as we cannot disambiguate from what gophercloud returns.
client, err := openstack.AuthenticatedClient(options)
if err != nil {
return nil, err
}
return client, nil
}
// CredentialProvider abstracts authentication methods.
type CredentialProvider interface {
// Client returns a new provider client.
Client() (*gophercloud.ProviderClient, error)
}
// ApplicationCredentialProvider allows use of an application credential.
type ApplicationCredentialProvider struct {
endpoint string
id string
secret string
}
// Ensure the interface is implemented.
var _ CredentialProvider = &ApplicationCredentialProvider{}
// NewApplicationCredentialProvider creates a client that comsumes application
// credentials for authentication.
func NewApplicationCredentialProvider(endpoint, id, secret string) *ApplicationCredentialProvider {
return &ApplicationCredentialProvider{
endpoint: endpoint,
id: id,
secret: secret,
}
}
// Client implements the Provider interface.
func (p *ApplicationCredentialProvider) Client() (*gophercloud.ProviderClient, error) {
options := gophercloud.AuthOptions{
IdentityEndpoint: p.endpoint,
ApplicationCredentialID: p.id,
ApplicationCredentialSecret: p.secret,
AllowReauth: true,
}
return authenticatedClient(options)
}
// PasswordProvider allows use of an application credential.
type PasswordProvider struct {
endpoint string
userID string
password string
projectID string
}
// Ensure the interface is implemented.
var _ CredentialProvider = &PasswordProvider{}
// NewPasswordProvider creates a client that comsumes passwords
// for authentication.
func NewPasswordProvider(endpoint, userID, password, projectID string) *PasswordProvider {
return &PasswordProvider{
endpoint: endpoint,
userID: userID,
password: password,
projectID: projectID,
}
}
// Client implements the Provider interface.
func (p *PasswordProvider) Client() (*gophercloud.ProviderClient, error) {
options := gophercloud.AuthOptions{
IdentityEndpoint: p.endpoint,
UserID: p.userID,
Password: p.password,
TenantID: p.projectID,
AllowReauth: true,
}
return authenticatedClient(options)
}
// DomainScopedPasswordProvider allows use of an application credential.
type DomainScopedPasswordProvider struct {
endpoint string
userID string
password string
domainID string
}
// Ensure the interface is implemented.
var _ CredentialProvider = &DomainScopedPasswordProvider{}
// NewDomainScopedPasswordProvider creates a client that comsumes passwords
// for authentication.
func NewDomainScopedPasswordProvider(endpoint, userID, password, domainID string) *DomainScopedPasswordProvider {
return &DomainScopedPasswordProvider{
endpoint: endpoint,
userID: userID,
password: password,
domainID: domainID,
}
}
// Client implements the Provider interface.
func (p *DomainScopedPasswordProvider) Client() (*gophercloud.ProviderClient, error) {
options := gophercloud.AuthOptions{
IdentityEndpoint: p.endpoint,
UserID: p.userID,
Password: p.password,
Scope: &gophercloud.AuthScope{
DomainID: p.domainID,
},
AllowReauth: true,
}
return authenticatedClient(options)
}
// TokenProvider creates a client from an endpoint and token.
type TokenProvider struct {
// endpoint is the Keystone endpoint to hit to get access to tokens
// and the service catalog.
endpoint string
// token is an Openstack authorization token.
token string
}
// Ensure the interface is implemented.
var _ CredentialProvider = &TokenProvider{}
// NewTokenProvider returns a new initialized provider.
func NewTokenProvider(endpoint, token string) *TokenProvider {
return &TokenProvider{
endpoint: endpoint,
token: token,
}
}
// Client implements the Provider interface.
func (p *TokenProvider) Client() (*gophercloud.ProviderClient, error) {
options := gophercloud.AuthOptions{
IdentityEndpoint: p.endpoint,
TokenID: p.token,
AllowReauth: true,
}
return authenticatedClient(options)
}
// CloudsProvider cretes a client from clouds.yaml.
type CloudsProvider struct {
// cloud is the key to lookup in clouds.yaml.
cloud string
}
// Ensure the interface is implemented.
var _ CredentialProvider = &CloudsProvider{}
// NewTokenProvider returns a new initialized provider.
func NewCloudsProvider(cloud string) *CloudsProvider {
return &CloudsProvider{
cloud: cloud,
}
}
// Client implements the Provider interface.
func (p *CloudsProvider) Client() (*gophercloud.ProviderClient, error) {
clientOpts := &clientconfig.ClientOpts{
Cloud: p.cloud,
}
options, err := clientconfig.AuthOptions(clientOpts)
if err != nil {
return nil, err
}
return authenticatedClient(*options)
}
// UnauthenticatedProvider is used for token issue.
type UnauthenticatedProvider struct {
// endpoint is the Keystone endpoint to hit to get access to tokens
// and the service catalog.
endpoint string
}
// Ensure the interface is implemented.
var _ CredentialProvider = &UnauthenticatedProvider{}
// NewTokenProvider returns a new initialized provider.
func NewUnauthenticatedProvider(endpoint string) *UnauthenticatedProvider {
return &UnauthenticatedProvider{
endpoint: endpoint,
}
}
// Client implements the Provider interface.
func (p *UnauthenticatedProvider) Client() (*gophercloud.ProviderClient, error) {
client, err := openstack.NewClient(p.endpoint)
if err != nil {
return nil, err
}
return client, nil
}