-
Notifications
You must be signed in to change notification settings - Fork 2
/
existingvolumebroker.go
430 lines (351 loc) · 11.9 KB
/
existingvolumebroker.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package existingvolumebroker
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/pivotal-cf/brokerapi/domain"
"github.com/pivotal-cf/brokerapi/domain/apiresponses"
"net/http"
"path"
"regexp"
"sync"
"crypto/md5"
"code.cloudfoundry.org/clock"
"code.cloudfoundry.org/goshims/osshim"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/service-broker-store/brokerstore"
vmo "code.cloudfoundry.org/volume-mount-options"
vmou "code.cloudfoundry.org/volume-mount-options/utils"
)
const (
DEFAULT_CONTAINER_PATH = "/var/vcap/data"
SHARE_KEY = "share"
SOURCE_KEY = "source"
VERSION_KEY = "version"
)
type lock interface {
Lock()
Unlock()
}
type BrokerType int
const (
BrokerTypeNFS BrokerType = iota
BrokerTypeSMB
)
type Broker struct {
brokerType BrokerType
logger lager.Logger
os osshim.Os
mutex lock
clock clock.Clock
store brokerstore.Store
services Services
configMask vmo.MountOptsMask
DisallowedBindOverrides []string
}
//go:generate counterfeiter -o fakes/fake_services.go . Services
type Services interface {
List() []domain.Service
}
func New(
brokerType BrokerType,
logger lager.Logger,
services Services,
os osshim.Os,
clock clock.Clock,
store brokerstore.Store,
configMask vmo.MountOptsMask,
) *Broker {
theBroker := Broker{
brokerType: brokerType,
logger: logger,
os: os,
mutex: &sync.Mutex{},
clock: clock,
store: store,
services: services,
configMask: configMask,
DisallowedBindOverrides: []string{SHARE_KEY, SOURCE_KEY},
}
return &theBroker
}
func (b *Broker) isNFSBroker() bool {
return b.brokerType == BrokerTypeNFS
}
func (b *Broker) isSMBBroker() bool {
return b.brokerType == BrokerTypeSMB
}
func (b *Broker) Services(_ context.Context) ([]domain.Service, error) {
logger := b.logger.Session("services")
logger.Info("start")
defer logger.Info("end")
return b.services.List(), nil
}
func (b *Broker) Provision(context context.Context, instanceID string, details domain.ProvisionDetails, _ bool) (_ domain.ProvisionedServiceSpec, e error) {
logger := b.logger.Session("provision").WithData(lager.Data{"instanceID": instanceID, "details": details})
logger.Info("start")
defer logger.Info("end")
var configuration map[string]interface{}
var decoder = json.NewDecoder(bytes.NewBuffer(details.RawParameters))
err := decoder.Decode(&configuration)
if err != nil {
return domain.ProvisionedServiceSpec{}, apiresponses.ErrRawParamsInvalid
}
share := stringifyShare(configuration[SHARE_KEY])
if share == "" {
return domain.ProvisionedServiceSpec{}, errors.New("config requires a \"share\" key")
}
if _, ok := configuration[SOURCE_KEY]; ok {
return domain.ProvisionedServiceSpec{}, errors.New("create configuration contains the following invalid option: ['" + SOURCE_KEY + "']")
}
if b.isNFSBroker() {
re := regexp.MustCompile("^[^/]+:/")
match := re.MatchString(share)
if match {
return domain.ProvisionedServiceSpec{}, errors.New("syntax error for share: no colon allowed after server")
}
}
b.mutex.Lock()
defer b.mutex.Unlock()
defer func() {
out := b.store.Save(logger)
if e == nil {
e = out
}
}()
instanceDetails := brokerstore.ServiceInstance{
ServiceID: details.ServiceID,
PlanID: details.PlanID,
OrganizationGUID: details.OrganizationGUID,
SpaceGUID: details.SpaceGUID,
ServiceFingerPrint: configuration,
}
if b.instanceConflicts(instanceDetails, instanceID) {
return domain.ProvisionedServiceSpec{}, apiresponses.ErrInstanceAlreadyExists
}
err = b.store.CreateInstanceDetails(instanceID, instanceDetails)
if err != nil {
return domain.ProvisionedServiceSpec{}, fmt.Errorf("failed to store instance details: %s", err.Error())
}
logger.Info("service-instance-created", lager.Data{"instanceDetails": instanceDetails})
return domain.ProvisionedServiceSpec{IsAsync: false}, nil
}
func (b *Broker) Deprovision(context context.Context, instanceID string, details domain.DeprovisionDetails, _ bool) (_ domain.DeprovisionServiceSpec, e error) {
logger := b.logger.Session("deprovision")
logger.Info("start")
defer logger.Info("end")
b.mutex.Lock()
defer b.mutex.Unlock()
defer func() {
out := b.store.Save(logger)
if e == nil {
e = out
}
}()
_, err := b.store.RetrieveInstanceDetails(instanceID)
if err != nil {
return domain.DeprovisionServiceSpec{}, apiresponses.ErrInstanceDoesNotExist
}
err = b.store.DeleteInstanceDetails(instanceID)
if err != nil {
return domain.DeprovisionServiceSpec{}, err
}
return domain.DeprovisionServiceSpec{IsAsync: false, OperationData: "deprovision"}, nil
}
func (b *Broker) Bind(context context.Context, instanceID string, bindingID string, bindDetails domain.BindDetails, _ bool) (_ domain.Binding, e error) {
logger := b.logger.Session("bind")
logger.Info("start", lager.Data{"bindingID": bindingID, "details": bindDetails})
defer logger.Info("end")
b.mutex.Lock()
defer b.mutex.Unlock()
defer func() {
out := b.store.Save(logger)
if e == nil {
e = out
}
}()
logger.Info("starting-broker-bind")
instanceDetails, err := b.store.RetrieveInstanceDetails(instanceID)
if err != nil {
return domain.Binding{}, apiresponses.ErrInstanceDoesNotExist
}
if bindDetails.AppGUID == "" {
return domain.Binding{}, apiresponses.ErrAppGuidNotProvided
}
opts, err := getFingerprint(instanceDetails.ServiceFingerPrint)
if err != nil {
return domain.Binding{}, err
}
var bindOpts map[string]interface{}
if len(bindDetails.RawParameters) > 0 {
if err = json.Unmarshal(bindDetails.RawParameters, &bindOpts); err != nil {
return domain.Binding{}, err
}
}
for k, v := range bindOpts {
for _, disallowed := range b.DisallowedBindOverrides {
if k == disallowed {
err := errors.New(fmt.Sprintf("bind configuration contains the following invalid option: ['%s']", k))
logger.Error("err-override-not-allowed-in-bind", err, lager.Data{"key": k})
return domain.Binding{}, apiresponses.NewFailureResponse(
err, http.StatusBadRequest, "invalid-raw-params",
)
}
}
opts[k] = v
}
mode, err := evaluateMode(opts)
if err != nil {
logger.Error("error-evaluating-mode", err)
return domain.Binding{}, err
}
mountOpts, err := vmo.NewMountOpts(opts, b.configMask)
if err != nil {
logger.Error("error-generating-mount-options", err)
return domain.Binding{}, apiresponses.NewFailureResponse(err, http.StatusBadRequest, "invalid-params")
}
if b.bindingConflicts(bindingID, bindDetails) {
return domain.Binding{}, apiresponses.ErrBindingAlreadyExists
}
logger.Info("retrieved-instance-details", lager.Data{"instanceDetails": instanceDetails})
err = b.store.CreateBindingDetails(bindingID, bindDetails)
if err != nil {
return domain.Binding{}, err
}
driverName := "smbdriver"
if b.isNFSBroker() {
driverName = "nfsv3driver"
// for backwards compatibility the nfs flavor has to issue source strings
// with nfs:// prefix (otherwise the mapfs-mounter wont construct the correct
// mount string for the kernel mount
//
// see (https://github.com/cloudfoundry/nfsv3driver/blob/ac1e1d26fec9a8551cacfabafa6e035f233c83e0/mapfs_mounter.go#L121)
mountOpts[SOURCE_KEY] = fmt.Sprintf("nfs://%s", mountOpts[SOURCE_KEY])
}
logger.Debug("volume-service-binding", lager.Data{"driver": driverName, "mountOpts": mountOpts})
s, err := b.hash(mountOpts)
if err != nil {
logger.Error("error-calculating-volume-id", err, lager.Data{"config": mountOpts, "bindingID": bindingID, "instanceID": instanceID})
return domain.Binding{}, err
}
volumeId := fmt.Sprintf("%s-%s", instanceID, s)
mountConfig := map[string]interface{}{}
for k, v := range mountOpts {
mountConfig[k] = v
}
ret := domain.Binding{
Credentials: struct{}{}, // if nil, cloud controller chokes on response
VolumeMounts: []domain.VolumeMount{{
ContainerDir: evaluateContainerPath(opts, instanceID),
Mode: mode,
Driver: driverName,
DeviceType: "shared",
Device: domain.SharedDevice{
VolumeId: volumeId,
MountConfig: mountConfig,
},
}},
}
return ret, nil
}
func (b *Broker) hash(mountOpts map[string]interface{}) (string, error) {
var (
bytes []byte
err error
)
if bytes, err = json.Marshal(mountOpts); err != nil {
return "", err
}
return fmt.Sprintf("%x", md5.Sum(bytes)), nil
}
func (b *Broker) Unbind(context context.Context, instanceID string, bindingID string, details domain.UnbindDetails, _ bool) (_ domain.UnbindSpec, e error) {
logger := b.logger.Session("unbind")
logger.Info("start")
defer logger.Info("end")
b.mutex.Lock()
defer b.mutex.Unlock()
defer func() {
out := b.store.Save(logger)
if e == nil {
e = out
}
}()
if _, err := b.store.RetrieveInstanceDetails(instanceID); err != nil {
return domain.UnbindSpec{}, apiresponses.ErrInstanceDoesNotExist
}
if _, err := b.store.RetrieveBindingDetails(bindingID); err != nil {
return domain.UnbindSpec{}, apiresponses.ErrBindingDoesNotExist
}
if err := b.store.DeleteBindingDetails(bindingID); err != nil {
return domain.UnbindSpec{}, err
}
return domain.UnbindSpec{}, nil
}
func (b *Broker) Update(context context.Context, instanceID string, details domain.UpdateDetails, _ bool) (domain.UpdateServiceSpec, error) {
return domain.UpdateServiceSpec{},
apiresponses.NewFailureResponse(
errors.New("This service does not support instance updates. Please delete your service instance and create a new one with updated configuration."),
422,
"",
)
}
func (b *Broker) LastOperation(_ context.Context, instanceID string, _ domain.PollDetails) (domain.LastOperation, error) {
logger := b.logger.Session("last-operation").WithData(lager.Data{"instanceID": instanceID})
logger.Info("start")
defer logger.Info("end")
b.mutex.Lock()
defer b.mutex.Unlock()
return domain.LastOperation{}, errors.New("unrecognized operationData")
}
func (b *Broker) GetInstance(ctx context.Context, instanceID string) (domain.GetInstanceDetailsSpec, error) {
panic("implement me")
}
func (b *Broker) LastBindingOperation(ctx context.Context, instanceID, bindingID string, details domain.PollDetails) (domain.LastOperation, error) {
panic("implement me")
}
func (b *Broker) GetBinding(ctx context.Context, instanceID, bindingID string) (domain.GetBindingSpec, error) {
panic("implement me")
}
func (b *Broker) instanceConflicts(details brokerstore.ServiceInstance, instanceID string) bool {
return b.store.IsInstanceConflict(instanceID, brokerstore.ServiceInstance(details))
}
func (b *Broker) bindingConflicts(bindingID string, details domain.BindDetails) bool {
return b.store.IsBindingConflict(bindingID, details)
}
func evaluateContainerPath(parameters map[string]interface{}, volId string) string {
if containerPath, ok := parameters["mount"]; ok && containerPath != "" {
return containerPath.(string)
}
return path.Join(DEFAULT_CONTAINER_PATH, volId)
}
func evaluateMode(parameters map[string]interface{}) (string, error) {
if ro, ok := parameters["readonly"]; ok {
roc := vmou.InterfaceToString(ro)
if roc == "true" {
return "r", nil
}
return "", apiresponses.NewFailureResponse(fmt.Errorf("Invalid ro parameter value: %q", roc), http.StatusBadRequest, "invalid-ro-param")
}
return "rw", nil
}
func getFingerprint(rawObject interface{}) (map[string]interface{}, error) {
fingerprint, ok := rawObject.(map[string]interface{})
if ok {
return fingerprint, nil
} else {
// legacy service instances only store the "share" key in the service fingerprint.
share, ok := rawObject.(string)
if ok {
return map[string]interface{}{SHARE_KEY: share}, nil
}
return nil, errors.New("unable to deserialize service fingerprint")
}
}
func stringifyShare(data interface{}) string {
if val, ok := data.(string); ok {
return val
}
return ""
}