-
Notifications
You must be signed in to change notification settings - Fork 39
/
v3userpass.go
258 lines (235 loc) · 6.68 KB
/
v3userpass.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
package identityservice
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/go-goose/goose/v3/testservices/hook"
)
// V3UserPassRequest Implement the v3 User Pass form of identity (Keystone)
type V3UserPassRequest struct {
Auth struct {
Identity struct {
Methods []string `json:"methods"`
Password struct {
User struct {
Name string `json:"name"`
Password string `json:"password"`
Domain struct {
Name string `json:"name,omitempty"`
} `json:"domain"`
} `json:"user"`
} `json:"password"`
} `json:"identity"`
Scope struct {
Project struct {
Name string `json:"name"`
ID string `json:"id"`
Domain struct {
Name string `json:"name,omitempty"`
} `json:"domain,omitempty"`
} `json:"project"`
Domain struct {
Name string `json:"name,omitempty"`
} `json:"domain"`
} `json:"scope"`
} `json:"auth"`
}
// V3Endpoint represents endpoints to a Service
type V3Endpoint struct {
Interface string `json:"interface"`
RegionID string `json:"region_id"`
URL string `json:"url"`
}
// NewV3Endpoints returns an array of V3Endpoint for the given Region and the
// passed admin, internal and public URLs.
func NewV3Endpoints(adminURL, internalURL, publicURL, regionID string) []V3Endpoint {
var eps []V3Endpoint
if adminURL != "" {
eps = append(eps, V3Endpoint{
RegionID: regionID,
Interface: "admin",
URL: adminURL,
})
}
if internalURL != "" {
eps = append(eps, V3Endpoint{
RegionID: regionID,
Interface: "internal",
URL: internalURL,
})
}
if publicURL != "" {
eps = append(eps, V3Endpoint{
RegionID: regionID,
Interface: "public",
URL: publicURL,
})
}
return eps
}
// V3Service represents an OpenStack web service that you can access through a URL.
type V3Service struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Endpoints []V3Endpoint `json:"endpoints"`
}
// V3TokenResponse repesent a Token returned as a response to authentication to
// keystone v3.
type V3TokenResponse struct {
Expires time.Time `json:"expires_at"`
Issued time.Time `json:"issued_at"`
Methods []string `json:"methods"`
Catalog []V3Service `json:"catalog,omitempty"`
Project *V3Project `json:"project,omitempty"`
Domain *V3Domain `json:"domain,omitempty"`
User struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"user"`
}
// V3Project represent an openstack project, A project is the base unit of ownership.
// Resources are owned by a specific project. A project is owned by a specific domain.
type V3Project struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
// V3Domain represents an authentication domain.
type V3Domain struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
// V3UserPass represents an authenticated user to a service.
type V3UserPass struct {
hook.TestService
Users
services []V3Service
}
// NewV3UserPass returns a new V3UserPass
func NewV3UserPass() *V3UserPass {
userpass := &V3UserPass{
services: make([]V3Service, 0),
}
userpass.users = make(map[string]UserInfo)
userpass.tenants = make(map[string]string)
return userpass
}
// RegisterServiceProvider registers V3UserPass as a service provider.
func (u *V3UserPass) RegisterServiceProvider(name, serviceType string, serviceProvider ServiceProvider) {
service := V3Service{
ID: name,
Name: name,
Type: serviceType,
Endpoints: serviceProvider.V3Endpoints(),
}
u.AddService(Service{V3: service})
}
// AddService adds a service to the current V3UserPass.
func (u *V3UserPass) AddService(service Service) {
u.services = append(u.services, service.V3)
}
// ReturnFailure wraps and returns an error through the http connection.
func (u *V3UserPass) ReturnFailure(w http.ResponseWriter, status int, message string) {
e := ErrorWrapper{
Error: ErrorResponse{
Message: message,
Code: status,
Title: http.StatusText(status),
},
}
if content, err := json.Marshal(e); err != nil {
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(internalError)))
w.WriteHeader(http.StatusInternalServerError)
w.Write(internalError)
} else {
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(content)))
w.WriteHeader(status)
w.Write(content)
}
}
// ServeHTTP serves V3UserPass for testing purposes.
func (u *V3UserPass) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var req V3UserPassRequest
// Testing against Canonistack, all responses are application/json, even failures
w.Header().Set("Content-Type", "application/json")
if r.Header.Get("Content-Type") != "application/json" {
u.ReturnFailure(w, http.StatusBadRequest, notJSON)
return
}
if content, err := ioutil.ReadAll(r.Body); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
} else {
if err := json.Unmarshal(content, &req); err != nil {
u.ReturnFailure(w, http.StatusBadRequest, notJSON)
return
}
}
domain := req.Auth.Scope.Project.Domain.Name
if domain == "" {
domain = req.Auth.Scope.Domain.Name
}
if domain == "" {
domain = "default"
}
// validate the Auth structure
if err := u.ProcessControlHook("preauthentication", u, req); err != nil {
u.ReturnFailure(w, http.StatusInternalServerError, err.Error())
}
userInfo, errmsg := u.authenticate(
req.Auth.Identity.Password.User.Name,
req.Auth.Identity.Password.User.Password,
domain,
)
if errmsg != "" {
u.ReturnFailure(w, http.StatusUnauthorized, errmsg)
return
}
res, err := u.generateV3TokenResponse(userInfo)
if err != nil {
u.ReturnFailure(w, http.StatusInternalServerError, err.Error())
return
}
if req.Auth.Scope.Project.Name != "" {
id, name := u.addTenant(req.Auth.Scope.Project.Name)
res.Project = &V3Project{
ID: id,
Name: name,
}
}
if req.Auth.Scope.Domain.Name != "" {
res.Domain = &V3Domain{
Name: req.Auth.Scope.Domain.Name,
}
}
content, err := json.Marshal(struct {
Token *V3TokenResponse `json:"token"`
}{
Token: res,
})
if err != nil {
u.ReturnFailure(w, http.StatusInternalServerError, err.Error())
return
}
w.Header().Set("X-Subject-Token", userInfo.Token)
w.WriteHeader(http.StatusCreated)
w.Write(content)
}
func (u *V3UserPass) generateV3TokenResponse(userInfo *UserInfo) (*V3TokenResponse, error) {
res := V3TokenResponse{}
res.Issued = time.Now()
res.Expires = res.Issued.Add(24 * time.Hour)
res.Methods = []string{"password"}
res.Catalog = u.services
res.User.ID = userInfo.Id
return &res, nil
}
// SetupHTTP attaches all the needed handlers to provide the HTTP API.
func (u *V3UserPass) SetupHTTP(mux *http.ServeMux) {
mux.Handle("/v3/auth/tokens", u)
}
func (u *V3UserPass) Stop() {
// noop
}