forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.go
245 lines (198 loc) · 7.79 KB
/
route.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
package server
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
restful "github.com/emicklei/go-restful"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/authentication/user"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
"github.com/openshift/origin/pkg/templateservicebroker/openservicebroker/api"
)
// minimum supported client version
const minAPIVersionMajor, minAPIVersionMinor = 2, 7
// Route adds the necessary routes to a restful.Container for a given Broker
// implementing the OSB spec.
func Route(container *restful.Container, path string, b api.Broker) {
shim := func(f func(api.Broker, *restful.Request) *api.Response) func(*restful.Request, *restful.Response) {
return func(req *restful.Request, resp *restful.Response) {
response := f(b, req)
if response.Err != nil {
glog.V(2).Infof("Service broker: call to %s returned %v", path, response.Err)
resp.WriteHeaderAndJson(response.Code, &api.ErrorResponse{Description: response.Err.Error()}, restful.MIME_JSON)
} else {
resp.WriteHeaderAndJson(response.Code, response.Body, restful.MIME_JSON)
}
}
}
ws := restful.WebService{}
ws.Path(path + "/v2")
ws.Filter(apiVersion)
ws.Filter(contentType)
ws.Route(ws.GET("/catalog").To(shim(catalog)))
ws.Route(ws.PUT("/service_instances/{instance_id}").To(shim(provision)))
ws.Route(ws.DELETE("/service_instances/{instance_id}").To(shim(deprovision)))
ws.Route(ws.GET("/service_instances/{instance_id}/last_operation").To(shim(lastOperation)))
ws.Route(ws.PUT("/service_instances/{instance_id}/service_bindings/{binding_id}").To(shim(bind)))
ws.Route(ws.DELETE("/service_instances/{instance_id}/service_bindings/{binding_id}").To(shim(unbind)))
container.Add(&ws)
}
func atoi(s string) int {
rv, err := strconv.Atoi(s)
if err != nil {
rv = 0
}
return rv
}
func apiVersion(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
resp.AddHeader(api.XBrokerAPIVersion, api.APIVersion)
versions := strings.SplitN(req.HeaderParameter(api.XBrokerAPIVersion), ".", 3)
if len(versions) != 2 || atoi(versions[0]) != minAPIVersionMajor || atoi(versions[1]) < minAPIVersionMinor {
resp.WriteHeaderAndJson(http.StatusPreconditionFailed, &api.ErrorResponse{Description: fmt.Sprintf("%s header must >= %d.%d", api.XBrokerAPIVersion, minAPIVersionMajor, minAPIVersionMinor)}, restful.MIME_JSON)
return
}
chain.ProcessFilter(req, resp)
}
func contentType(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
resp.AddHeader(restful.HEADER_ContentType, restful.MIME_JSON)
if req.Request.Method == http.MethodPut && req.HeaderParameter(restful.HEADER_ContentType) != restful.MIME_JSON {
resp.WriteHeaderAndJson(http.StatusUnsupportedMediaType, &api.ErrorResponse{Description: fmt.Sprintf("%s header must == %s", restful.HEADER_ContentType, restful.MIME_JSON)}, restful.MIME_JSON)
return
}
chain.ProcessFilter(req, resp)
}
/*
The following properties MUST appear within the JSON encoded `value`:
| Property | Type | Description |
| --- | --- | --- |
| username | string | The `username` property from the Kubenernetes `user.info` object. |
| uid | string | The `uid` property from the Kubenernetes `user.info` object. |
| groups | string | The `groups` property from the Kubenernetes `user.info` object. |
Platforms MAY include additional properties.
For example, a `value` of:
```
{
"username": "duke",
"uid": "c2dde242-5ce4-11e7-988c-000c2946f14f",
"groups": { "admin", "dev" }
}
```
would appear in the HTTP Header as:
```
X-Broker-API-Originating-Identity: kubernetes eyANCiAgInVzZXJuYW1lIjogImR1a2UiLA0KICAidWlkIjogImMyZGRlMjQyLTVjZTQtMTFlNy05ODhjLTAwMGMyOTQ2ZjE0ZiIsDQogICJncm91cHMiOiB7ICJhZG1pbiIsICJkZXYiIH0NCn0=
```
*/
func getUser(req *restful.Request) (user.Info, error) {
identity := req.Request.Header.Get(api.XBrokerAPIOriginatingIdentity)
parts := strings.SplitN(identity, " ", 2)
if !strings.EqualFold(parts[0], api.OriginatingIdentitySchemeKubernetes) || len(parts) != 2 {
return nil, fmt.Errorf("couldn't parse %s header", api.XBrokerAPIOriginatingIdentity)
}
templatereq := templateapi.TemplateInstanceRequester{}
decodestrbytes, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return nil, fmt.Errorf("couldn't parse %s header: %v", api.XBrokerAPIOriginatingIdentity, err)
}
err = json.Unmarshal(decodestrbytes, &templatereq)
if err != nil {
return nil, fmt.Errorf("couldn't parse %s header with value %s: %v", api.XBrokerAPIOriginatingIdentity, string(decodestrbytes), err)
}
u := api.ConvertTemplateInstanceRequesterToUser(&templatereq)
return u, nil
}
func catalog(b api.Broker, req *restful.Request) *api.Response {
return b.Catalog()
}
func provision(b api.Broker, req *restful.Request) *api.Response {
instanceID := req.PathParameter("instance_id")
if errors := api.ValidateUUID(field.NewPath("instance_id"), instanceID); errors != nil {
return api.BadRequest(errors.ToAggregate())
}
var preq api.ProvisionRequest
err := req.ReadEntity(&preq)
if err != nil {
return api.BadRequest(err)
}
if errors := api.ValidateProvisionRequest(&preq); errors != nil {
return api.BadRequest(errors.ToAggregate())
}
if req.QueryParameter("accepts_incomplete") != "true" {
return api.NewResponse(http.StatusUnprocessableEntity, &api.AsyncRequired, nil)
}
u, err := getUser(req)
if err != nil {
return api.BadRequest(err)
}
return b.Provision(u, instanceID, &preq)
}
func deprovision(b api.Broker, req *restful.Request) *api.Response {
instanceID := req.PathParameter("instance_id")
if errors := api.ValidateUUID(field.NewPath("instance_id"), instanceID); errors != nil {
return api.BadRequest(errors.ToAggregate())
}
if req.QueryParameter("accepts_incomplete") != "true" {
return api.NewResponse(http.StatusUnprocessableEntity, &api.AsyncRequired, nil)
}
u, err := getUser(req)
if err != nil {
return api.BadRequest(err)
}
return b.Deprovision(u, instanceID)
}
func lastOperation(b api.Broker, req *restful.Request) *api.Response {
instanceID := req.PathParameter("instance_id")
if errors := api.ValidateUUID(field.NewPath("instance_id"), instanceID); errors != nil {
return api.BadRequest(errors.ToAggregate())
}
operation := api.Operation(req.QueryParameter("operation"))
if operation != api.OperationProvisioning &&
operation != api.OperationUpdating &&
operation != api.OperationDeprovisioning {
return api.BadRequest(fmt.Errorf("invalid operation"))
}
u, err := getUser(req)
if err != nil {
return api.BadRequest(err)
}
return b.LastOperation(u, instanceID, operation)
}
func bind(b api.Broker, req *restful.Request) *api.Response {
instanceID := req.PathParameter("instance_id")
errors := api.ValidateUUID(field.NewPath("instance_id"), instanceID)
bindingID := req.PathParameter("binding_id")
errors = append(errors, api.ValidateUUID(field.NewPath("binding_id"), bindingID)...)
if len(errors) > 0 {
return api.BadRequest(errors.ToAggregate())
}
var breq api.BindRequest
err := req.ReadEntity(&breq)
if err != nil {
return api.BadRequest(err)
}
if errors = api.ValidateBindRequest(&breq); errors != nil {
return api.BadRequest(errors.ToAggregate())
}
u, err := getUser(req)
if err != nil {
return api.BadRequest(err)
}
return b.Bind(u, instanceID, bindingID, &breq)
}
func unbind(b api.Broker, req *restful.Request) *api.Response {
instanceID := req.PathParameter("instance_id")
errors := api.ValidateUUID(field.NewPath("instance_id"), instanceID)
bindingID := req.PathParameter("binding_id")
errors = append(errors, api.ValidateUUID(field.NewPath("binding_id"), bindingID)...)
if len(errors) > 0 {
return api.BadRequest(errors.ToAggregate())
}
u, err := getUser(req)
if err != nil {
return api.BadRequest(err)
}
return b.Unbind(u, instanceID, bindingID)
}