forked from ciao-project/ciao
/
identity.go
293 lines (244 loc) · 7.13 KB
/
identity.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// Copyright (c) 2016 Intel Corporation
//
// 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 main
import (
"crypto/tls"
"fmt"
"net/http"
"github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/identity/v3/tokens"
)
// Project represents a tenant UUID and friendly name.
type Project struct {
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
}
type getResult struct {
tokens.GetResult
}
// Domain is a collection of users, groups and projects.
// Here we only need to fetch a domain UUID and friendly name.
type Domain struct {
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
}
// User represents an Openstack identity (e.g. Keystone) user.
// We fetch the user UUID, friendly name and the domain it belongs
// to.
type User struct {
ValidDomain Domain `mapstructure:"domain"`
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
}
// UserProjects represents the list of projects a user has access to.
type UserProjects struct {
Projects []struct {
Description string `json:"description"`
DomainID string `json:"domain_id"`
Enabled bool `json:"enabled"`
ID string `json:"id"`
ParentID string `json:"parent_id"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Name string `json:"name"`
} `json:"projects"`
Links struct {
Self string `json:"self"`
Previous interface{} `json:"previous"`
Next interface{} `json:"next"`
} `json:"links"`
}
func (r getResult) extractUserID() (string, error) {
if r.Err != nil {
return "", r.Err
}
var response struct {
Token struct {
ValidUser User `mapstructure:"user"`
} `mapstructure:"token"`
}
err := mapstructure.Decode(r.Body, &response)
if err != nil {
return "", err
}
return response.Token.ValidUser.ID, nil
}
func (r getResult) extractProject() (string, error) {
if r.Err != nil {
return "", r.Err
}
var response struct {
Token struct {
ValidProject Project `mapstructure:"project"`
} `mapstructure:"token"`
}
err := mapstructure.Decode(r.Body, &response)
if err != nil {
return "", err
}
return response.Token.ValidProject.ID, nil
}
func getScopedToken(username string, password string, projectScope string) (string, string, string, error) {
var scope *tokens.Scope
opt := gophercloud.AuthOptions{
IdentityEndpoint: *identityURL + "/v3/",
Username: username,
Password: password,
DomainID: "default",
AllowReauth: true,
}
provider, err := openstack.NewClient(opt.IdentityEndpoint)
if err != nil {
errorf("Could not get ProviderClient %s\n", err)
return "", "", "", nil
}
if caCertPool != nil {
transport := &http.Transport{
TLSClientConfig: &tls.Config{RootCAs: caCertPool},
}
provider.HTTPClient.Transport = transport
}
err = openstack.Authenticate(provider, opt)
if err != nil {
errorf("Could not get AuthenticatedClient %s\n", err)
return "", "", "", nil
}
client := openstack.NewIdentityV3(provider)
if client == nil {
errorf("something went wrong")
return "", "", "", nil
}
scope = nil
if projectScope != "" {
scope = &tokens.Scope{
ProjectName: projectScope,
DomainName: "default",
}
}
token, err := tokens.Create(client, opt, scope).Extract()
if err != nil {
errorf("Could not extract token %s\n", err)
return "", "", "", nil
}
r := tokens.Get(client, token.ID)
result := getResult{r}
tenantID, err := result.extractProject()
if err != nil {
errorf("Could not extract tenant ID %s\n", err)
return "", "", "", nil
}
userID, err := result.extractUserID()
if err != nil {
errorf("Could not extract user ID %s\n", err)
return "", "", "", nil
}
infof("Got token %s for tenant %s, user %s (%s, %s, %s)\n", token.ID, tenantID, userID, username, password, projectScope)
return token.ID, tenantID, userID, nil
}
func getUnscopedToken(username string, password string) (string, string, string, error) {
return getScopedToken(username, password, "")
}
func getUserProjects(username string, password string) ([]Project, error) {
var projects UserProjects
var userProjects []Project
token, _, user, err := getUnscopedToken(username, password)
if err != nil {
return nil, err
}
identity := fmt.Sprintf("%s/v3/users/%s/projects", *identityURL, user)
resp, err := sendHTTPRequestToken("GET", identity, nil, token, nil)
if err != nil {
return nil, err
}
err = unmarshalHTTPResponse(resp, &projects)
if err != nil {
return nil, err
}
for _, project := range projects.Projects {
newProject := Project{
ID: project.ID,
Name: project.Name,
}
userProjects = append(userProjects, newProject)
}
return userProjects, nil
}
// IdentityProjects represents the list of all existing projects.
type IdentityProjects struct {
Links struct {
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
Self string `json:"self"`
} `json:"links"`
Projects []struct {
Description interface{} `json:"description"`
DomainID string `json:"domain_id"`
Enabled bool `json:"enabled"`
ID string `json:"id"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Name string `json:"name"`
ParentID interface{} `json:"parent_id"`
} `json:"projects"`
}
func getAllProjects(username string, password string) (*IdentityProjects, error) {
var projects IdentityProjects
token, _, _, err := getUnscopedToken(username, password)
if err != nil {
return nil, err
}
identity := fmt.Sprintf("%s/v3/projects", *identityURL)
resp, err := sendHTTPRequestToken("GET", identity, nil, token, nil)
if err != nil {
return nil, err
}
err = unmarshalHTTPResponse(resp, &projects)
if err != nil {
return nil, err
}
return &projects, nil
}
func getTenant(username string, password string, tenantID string) (string, string, error) {
projects, err := getUserProjects(username, password)
if err != nil {
return "", "", err
}
if len(projects) <= 0 {
return "", tenantID, fmt.Errorf("No tenant name for %s", username)
}
if tenantID == "" {
if len(projects) == 1 {
tenantName := projects[0].Name
tenantID = projects[0].ID
return tenantName, tenantID, nil
}
fmt.Printf("Available projects for %s:\n", *identityUser)
for i, p := range projects {
fmt.Printf("\t Project[%d]: %s (%s)\n", i+1, p.Name, p.ID)
}
return "", "", fmt.Errorf("Please specify a project to use with -tenant-name or -tenant-id")
}
for _, p := range projects {
if p.ID == tenantID {
return p.Name, tenantID, nil
}
}
return "", tenantID, fmt.Errorf("No tenant name for %s", tenantID)
}