This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
broker.go
436 lines (362 loc) · 14.2 KB
/
broker.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
430
431
432
433
434
435
436
// Copyright 2018 the Service Broker Project 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 cloudsql
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/GoogleCloudPlatform/gcp-service-broker/brokerapi/brokers/broker_base"
"github.com/GoogleCloudPlatform/gcp-service-broker/brokerapi/brokers/models"
"github.com/GoogleCloudPlatform/gcp-service-broker/pkg/varcontext"
"github.com/GoogleCloudPlatform/gcp-service-broker/utils"
"github.com/pivotal-cf/brokerapi"
"context"
"code.cloudfoundry.org/lager"
multierror "github.com/hashicorp/go-multierror"
googlecloudsql "google.golang.org/api/sqladmin/v1beta4"
)
const (
secondGenPricingPlan string = "PER_USE"
postgresDefaultVersion string = "POSTGRES_9_6"
mySqlFirstGenDefaultVersion string = "MYSQL_5_6"
mySqlSecondGenDefaultVersion string = "MYSQL_5_7"
)
// CloudSQLBroker is the service-broker back-end for creating and binding CloudSQL instances.
type CloudSQLBroker struct {
broker_base.BrokerBase
}
// InstanceInformation holds the details needed to bind a service account to a CloudSQL instance after it has been provisioned.
type InstanceInformation struct {
InstanceName string `json:"instance_name"`
DatabaseName string `json:"database_name"`
Host string `json:"host"`
Region string `json:"region"`
LastMasterOperationId string `json:"last_master_operation_id"`
}
// Provision creates a new CloudSQL instance from the settings in the user-provided details and service plan.
func (b *CloudSQLBroker) Provision(ctx context.Context, provisionContext *varcontext.VarContext) (models.ServiceInstanceDetails, error) {
di, ii, err := createProvisionRequest(provisionContext)
if err != nil {
return models.ServiceInstanceDetails{}, err
}
// init sqladmin service
sqlService, err := b.createClient(ctx)
if err != nil {
return models.ServiceInstanceDetails{}, err
}
// make insert request
op, err := sqlService.Instances.Insert(b.ProjectId, di).Do()
if err != nil {
return models.ServiceInstanceDetails{}, fmt.Errorf("Error creating new CloudSQL instance: %s", err)
}
b.Logger.Debug("updating details", lager.Data{"from": "{}", "to": ii})
id := models.ServiceInstanceDetails{
Name: di.Name,
OperationType: models.ProvisionOperationType,
OperationId: op.Name,
}
if err := id.SetOtherDetails(ii); err != nil {
return models.ServiceInstanceDetails{}, err
}
return id, nil
}
func createProvisionRequest(vars *varcontext.VarContext) (*googlecloudsql.DatabaseInstance, *InstanceInformation, error) {
// set up database information
var di *googlecloudsql.DatabaseInstance
if vars.GetBool("is_first_gen") {
di = createFirstGenRequest(vars)
} else {
di = createInstanceRequest(vars)
}
instanceName := vars.GetString("instance_name")
di.Name = instanceName
di.Settings.BackupConfiguration = &googlecloudsql.BackupConfiguration{
Enabled: vars.GetBool("backups_enabled"),
StartTime: vars.GetString("backup_start_time"),
BinaryLogEnabled: vars.GetBool("binlog"),
}
di.Settings.IpConfiguration.AuthorizedNetworks = varctxGetAcls(vars)
di.Settings.UserLabels = vars.GetStringMapString("labels")
// Set up instance information
ii := InstanceInformation{
InstanceName: instanceName,
DatabaseName: vars.GetString("database_name"),
}
return di, &ii, vars.Error()
}
func varctxGetAcls(vars *varcontext.VarContext) []*googlecloudsql.AclEntry {
openAcls := []*googlecloudsql.AclEntry{}
authorizedNetworkCsv := vars.GetString("authorized_networks")
if authorizedNetworkCsv == "" {
return openAcls
}
for _, v := range strings.Split(authorizedNetworkCsv, ",") {
openAcls = append(openAcls, &googlecloudsql.AclEntry{Value: v})
}
return openAcls
}
func createFirstGenRequest(vars *varcontext.VarContext) *googlecloudsql.DatabaseInstance {
// set up instance resource
return &googlecloudsql.DatabaseInstance{
Settings: &googlecloudsql.Settings{
IpConfiguration: &googlecloudsql.IpConfiguration{
RequireSsl: true,
Ipv4Enabled: true,
},
Tier: vars.GetString("tier"),
PricingPlan: vars.GetString("pricing_plan"),
ActivationPolicy: vars.GetString("activation_policy"),
ReplicationType: vars.GetString("replication_type"),
},
DatabaseVersion: vars.GetString("version"),
Region: vars.GetString("region"),
}
}
func createInstanceRequest(vars *varcontext.VarContext) *googlecloudsql.DatabaseInstance {
autoResize := vars.GetBool("auto_resize")
// set up instance resource
return &googlecloudsql.DatabaseInstance{
Settings: &googlecloudsql.Settings{
IpConfiguration: &googlecloudsql.IpConfiguration{
RequireSsl: true,
Ipv4Enabled: true,
},
Tier: vars.GetString("tier"),
DataDiskSizeGb: int64(vars.GetInt("disk_size")),
LocationPreference: &googlecloudsql.LocationPreference{
Zone: vars.GetString("zone"),
},
DataDiskType: vars.GetString("disk_type"),
MaintenanceWindow: &googlecloudsql.MaintenanceWindow{
Day: int64(vars.GetInt("maintenance_window_day")),
Hour: int64(vars.GetInt("maintenance_window_hour")),
UpdateTrack: "stable",
ForceSendFields: []string{"Day", "Hour"},
},
PricingPlan: secondGenPricingPlan,
ActivationPolicy: vars.GetString("activation_policy"),
ReplicationType: vars.GetString("replication_type"),
StorageAutoResize: &autoResize,
},
DatabaseVersion: vars.GetString("version"),
Region: vars.GetString("region"),
FailoverReplica: &googlecloudsql.DatabaseInstanceFailoverReplica{
Name: vars.GetString("failover_replica_name"),
},
}
}
// Bind creates a new username, password, and set of ssl certs for the given instance.
// The function may be slow to return because CloudSQL operations are asynchronous.
// The default PCF service broker timeout may need to be raised to 90 or 120 seconds to accommodate the long bind time.
func (b *CloudSQLBroker) Bind(ctx context.Context, vc *varcontext.VarContext) (map[string]interface{}, error) {
// get context before trying to create anything to catch errors early
combinedCreds := varcontext.Builder()
useJdbcFormat := vc.GetBool("jdbc_uri_format")
if err := vc.Error(); err != nil {
return nil, err
}
// Create the service account
saCreds, err := b.BrokerBase.Bind(ctx, vc)
if err != nil {
return saCreds, err
}
combinedCreds.MergeMap(saCreds)
sqlCreds, err := b.createSqlCredentials(ctx, vc)
if err != nil {
return saCreds, err
}
combinedCreds.MergeMap(sqlCreds)
uriPrefix := ""
if useJdbcFormat {
uriPrefix = "jdbc:"
}
combinedCreds.MergeMap(map[string]interface{}{"UriPrefix": uriPrefix})
return combinedCreds.BuildMap()
}
func (b *CloudSQLBroker) BuildInstanceCredentials(ctx context.Context, bindRecord models.ServiceBindingCredentials, instanceRecord models.ServiceInstanceDetails) (map[string]interface{}, error) {
uriFormat := ""
switch instanceRecord.ServiceId {
case MySqlServiceId:
uriFormat = `${str.queryEscape(UriPrefix)}mysql://${str.queryEscape(Username)}:${str.queryEscape(Password)}@${str.queryEscape(host)}/${str.queryEscape(database_name)}?ssl_mode=required`
case PostgresServiceId:
uriFormat = `${str.queryEscape(UriPrefix)}postgres://${str.queryEscape(Username)}:${str.queryEscape(Password)}@${str.queryEscape(host)}/${str.queryEscape(database_name)}?sslmode=require&sslcert=${str.queryEscape(ClientCert)}&sslkey=${str.queryEscape(ClientKey)}&sslrootcert=${str.queryEscape(CaCert)}`
default:
return map[string]interface{}{}, errors.New("Unknown service")
}
combinedCreds, err := b.BrokerBase.BuildInstanceCredentials(ctx, bindRecord, instanceRecord)
if err != nil {
return nil, err
}
return varcontext.Builder().
MergeMap(combinedCreds).
MergeEvalResult("uri", uriFormat, varcontext.TypeString).
BuildMap()
}
// Unbind deletes the database user, service account and invalidates the ssl certs associated with this binding.
func (b *CloudSQLBroker) Unbind(ctx context.Context, instance models.ServiceInstanceDetails, binding models.ServiceBindingCredentials) error {
var accumulator error
if err := b.deleteSqlSslCert(ctx, binding, instance); err != nil {
accumulator = multierror.Append(accumulator, err)
}
if err := b.deleteSqlUserAccount(ctx, binding, instance); err != nil {
accumulator = multierror.Append(accumulator, err)
}
if err := b.BrokerBase.Unbind(ctx, instance, binding); err != nil {
accumulator = multierror.Append(accumulator, err)
}
return accumulator
}
// PollInstance gets the last operation for this instance and checks its status.
func (b *CloudSQLBroker) PollInstance(ctx context.Context, instance models.ServiceInstanceDetails) (bool, error) {
b.Logger.Info("PollInstance", lager.Data{
"instance": instance.Name,
"operation_type": instance.OperationType,
"operation_id": instance.OperationId,
})
if instance.OperationType == "" {
return false, errors.New("Couldn't find any pending operations for this CloudSQL instance.")
}
result, err := b.pollOperation(ctx, instance.OperationId)
if result == false || err != nil {
return result, err
}
if instance.OperationType == models.ProvisionOperationType {
// Update the instance information from the server side before
// creating the database. The modification happens _only_ to
// this instance of the details and is not persisted to the db.
if err := b.UpdateInstanceDetails(ctx, &instance); err != nil {
return true, err
}
return true, b.createDatabase(ctx, &instance)
}
return true, nil
}
// refreshServiceInstanceDetails fetches the settings for the instance from GCP
// and upates the provided instance with the refreshed info.
func (b *CloudSQLBroker) UpdateInstanceDetails(ctx context.Context, instance *models.ServiceInstanceDetails) error {
var instanceInfo InstanceInformation
if err := instance.GetOtherDetails(&instanceInfo); err != nil {
return err
}
client, err := b.createClient(ctx)
if err != nil {
return err
}
clouddb, err := googlecloudsql.NewInstancesService(client).Get(b.ProjectId, instance.Name).Do()
if err != nil {
return fmt.Errorf("Error getting instance from API: %s", err)
}
// update db information
instance.Url = clouddb.SelfLink
instance.Location = clouddb.Region
// update instance information
instanceInfo.Host = clouddb.IpAddresses[0].IpAddress
instanceInfo.Region = clouddb.Region
return instance.SetOtherDetails(instanceInfo)
}
// createDatabase creates tha database on the instance referenced by ServiceInstanceDetails.
func (b *CloudSQLBroker) createDatabase(ctx context.Context, instance *models.ServiceInstanceDetails) error {
var instanceInfo InstanceInformation
if err := json.Unmarshal([]byte(instance.OtherDetails), &instanceInfo); err != nil {
return fmt.Errorf("Error unmarshalling instance information.")
}
client, err := b.createClient(ctx)
if err != nil {
return err
}
d := googlecloudsql.Database{Name: instanceInfo.DatabaseName}
op, err := client.Databases.Insert(b.ProjectId, instance.Name, &d).Do()
if err != nil {
return fmt.Errorf("Error creating database: %s", err)
}
// poll for the database creation operation to be completed
// XXX: return this error exactly as is from the google api
return b.pollOperationUntilDone(ctx, op, b.ProjectId)
}
func (b *CloudSQLBroker) pollOperation(ctx context.Context, opterationId string) (bool, error) {
client, err := b.createClient(ctx)
if err != nil {
return false, err
}
// get the status of the operation
operation, err := googlecloudsql.NewOperationsService(client).Get(b.ProjectId, opterationId).Do()
if err != nil {
return false, err
}
if operation.Status == "DONE" {
if operation.Error == nil {
return true, nil
} else {
errs := ""
for _, err := range operation.Error.Errors {
errs += fmt.Sprintf("%s: %q; ", err.Code, err.Message)
}
return true, errors.New(errs)
}
}
return false, nil
}
// pollOperationUntilDone loops and waits until a cloudsql operation is done, returning an error if any is encountered
// XXX: note that for this function in particular, we are being explicit to return errors from the google api exactly
// as we get them, because further up the stack these errors will be evaluated differently and need to be preserved
func (b *CloudSQLBroker) pollOperationUntilDone(ctx context.Context, op *googlecloudsql.Operation, projectId string) error {
sqlService, err := b.createClient(ctx)
if err != nil {
return err
}
opsService := googlecloudsql.NewOperationsService(sqlService)
for {
status, err := opsService.Get(projectId, op.Name).Do()
if err != nil {
return err
}
if status.EndTime != "" {
return nil
}
b.Logger.Info("waiting for operation", lager.Data{"operation": op.Name, "status": status.Status})
// sleep for 1 second between polling so we don't hit our rate limit
time.Sleep(time.Second)
}
}
// Deprovision issues a delete call on the database instance.
func (b *CloudSQLBroker) Deprovision(ctx context.Context, instance models.ServiceInstanceDetails, details brokerapi.DeprovisionDetails) (*string, error) {
sqlService, err := b.createClient(ctx)
if err != nil {
return nil, err
}
// delete the instance from google
op, err := sqlService.Instances.Delete(b.ProjectId, instance.Name).Do()
if err != nil {
return nil, fmt.Errorf("Error deleting instance: %s", err)
}
return &op.Name, nil
}
// ProvisionsAsync indicates that CloudSQL uses asynchronous provisioning.
func (b *CloudSQLBroker) ProvisionsAsync() bool {
return true
}
// DeprovisionsAsync indicates that CloudSQL uses asynchronous deprovisioning.
func (b *CloudSQLBroker) DeprovisionsAsync() bool {
return true
}
func (b *CloudSQLBroker) createClient(ctx context.Context) (*googlecloudsql.Service, error) {
client, err := googlecloudsql.New(b.HttpConfig.Client(ctx))
if err != nil {
return nil, fmt.Errorf("Couldn't instantiate CloudSQL API client: %s", err)
}
client.UserAgent = utils.CustomUserAgent
return client, nil
}