@@ -150,7 +150,7 @@ type nodeConfig struct {
150
150
}
151
151
152
152
// Create
153
- func Create (name string , content []byte , serviceAccountName string , serviceAccountProject string , encryptionKey string , grantPermission bool ) (respBody []byte , err error ) {
153
+ func Create (name string , content []byte , serviceAccountName string , serviceAccountProject string , encryptionKey string , grantPermission bool , createSecret bool ) (respBody []byte , err error ) {
154
154
155
155
var secretVersion string
156
156
@@ -278,34 +278,54 @@ func Create(name string, content []byte, serviceAccountName string, serviceAccou
278
278
c .ConnectorDetails = nil
279
279
280
280
//handle secrets for username
281
- if c .AuthConfig != nil && c .AuthConfig .UserPassword .PasswordDetails != nil {
282
- payload , err := readSecretFile (c .AuthConfig .UserPassword .PasswordDetails .Reference )
283
- if err != nil {
284
- return nil , err
285
- }
286
-
287
- //check if a Cloud KMS key was passsed, assume the file is encrypted
288
- if encryptionKey != "" {
289
- encryptionKey := path .Join ("projects" , apiclient .GetProjectID (), encryptionKey )
290
- payload , err = cloudkms .DecryptSymmetric (encryptionKey , payload )
291
- if err != nil {
292
- return nil , err
281
+ if c .AuthConfig != nil {
282
+ switch c .AuthConfig .AuthType {
283
+ case "USER_PASSWORD" :
284
+ if c .AuthConfig .UserPassword .PasswordDetails != nil {
285
+ if createSecret {
286
+ payload , err := readSecretFile (c .AuthConfig .UserPassword .PasswordDetails .Reference )
287
+ if err != nil {
288
+ return nil , err
289
+ }
290
+
291
+ //check if a Cloud KMS key was passsed, assume the file is encrypted
292
+ if encryptionKey != "" {
293
+ encryptionKey := path .Join ("projects" , apiclient .GetProjectID (), encryptionKey )
294
+ payload , err = cloudkms .DecryptSymmetric (encryptionKey , payload )
295
+ if err != nil {
296
+ return nil , err
297
+ }
298
+ }
299
+
300
+ if secretVersion , err = secmgr .Create (apiclient .GetProjectID (), c .AuthConfig .UserPassword .PasswordDetails .SecretName , payload ); err != nil {
301
+ return nil , err
302
+ }
303
+
304
+ secretName := c .AuthConfig .UserPassword .PasswordDetails .SecretName
305
+ c .AuthConfig .UserPassword .Password = new (secret )
306
+ c .AuthConfig .UserPassword .Password .SecretVersion = secretVersion
307
+ c .AuthConfig .UserPassword .PasswordDetails = nil //clean the input
308
+ if grantPermission && c .ServiceAccount != nil {
309
+ //grant connector service account access to secretVersion
310
+ if err = apiclient .SetSecretManagerIAMPermission (apiclient .GetProjectID (), secretName , * c .ServiceAccount ); err != nil {
311
+ return nil , err
312
+ }
313
+ }
314
+ } else {
315
+ c .AuthConfig .UserPassword .Password = new (secret )
316
+ c .AuthConfig .UserPassword .Password .SecretVersion = fmt .Sprintf ("projects/%s/secrets/%s/versions/1" , apiclient .GetProjectID (), c .AuthConfig .UserPassword .PasswordDetails .SecretName )
317
+ c .AuthConfig .UserPassword .PasswordDetails = nil //clean the input
318
+ }
293
319
}
294
- }
295
-
296
- if secretVersion , err = secmgr .Create (apiclient .GetProjectID (), c .AuthConfig .UserPassword .PasswordDetails .SecretName , payload ); err != nil {
297
- return nil , err
298
- }
299
- secretName := c .AuthConfig .UserPassword .PasswordDetails .SecretName
300
- c .AuthConfig .UserPassword .Password = new (secret )
301
- c .AuthConfig .UserPassword .Password .SecretVersion = secretVersion
302
- c .AuthConfig .UserPassword .PasswordDetails = nil //clean the input
303
-
304
- if grantPermission && c .ServiceAccount != nil {
305
- //grant connector service account access to secretVersion
306
- if err = apiclient .SetSecretManagerIAMPermission (apiclient .GetProjectID (), secretName , * c .ServiceAccount ); err != nil {
307
- return nil , err
320
+ case "OAUTH2_JWT_BEARER" :
321
+ if createSecret {
322
+ clilog .Warning .Println ("Creating secrets for OAUTH2_JET_BEARER is not implemented" )
323
+ } else {
324
+ c .AuthConfig .Oauth2JwtBearer .ClientKey .SecretVersion = fmt .Sprintf ("projects/%s/secrets/%s/versions/1" , apiclient .GetProjectID (), c .AuthConfig .Oauth2JwtBearer .ClientKeyDetails .SecretName )
308
325
}
326
+ case "OAUTH2_CLIENT_CREDENTIALS" :
327
+ default :
328
+ clilog .Warning .Printf ("Creating secrets for %s is not implemented\n " , c .AuthConfig .AuthType )
309
329
}
310
330
}
311
331
@@ -331,7 +351,7 @@ func Delete(name string) (respBody []byte, err error) {
331
351
}
332
352
333
353
// Get
334
- func Get (name string , view string , minimal bool ) (respBody []byte , err error ) {
354
+ func Get (name string , view string , minimal bool , overrides bool ) (respBody []byte , err error ) {
335
355
u , _ := url .Parse (apiclient .GetBaseConnectorURL ())
336
356
q := u .Query ()
337
357
if view != "" {
@@ -358,6 +378,27 @@ func Get(name string, view string, minimal bool) (respBody []byte, err error) {
358
378
c .ConnectorDetails .Version = getConnectorVersion (* c .ConnectorVersion )
359
379
c .ConnectorVersion = nil
360
380
c .Name = nil
381
+ if overrides {
382
+ switch c .AuthConfig .AuthType {
383
+ case "USER_PASSWORD" :
384
+ p := c .AuthConfig .UserPassword .Password .SecretVersion
385
+ c .AuthConfig .UserPassword .PasswordDetails = new (secretDetails )
386
+ c .AuthConfig .UserPassword .PasswordDetails .SecretName = strings .Split (p , "/" )[3 ]
387
+ c .AuthConfig .UserPassword .Password = nil
388
+ case "OAUTH2_JWT_BEARER" :
389
+ p := c .AuthConfig .Oauth2JwtBearer .ClientKey .SecretVersion
390
+ c .AuthConfig .Oauth2JwtBearer .ClientKeyDetails = new (secretDetails )
391
+ c .AuthConfig .Oauth2JwtBearer .ClientKeyDetails .SecretName = strings .Split (p , "/" )[3 ]
392
+ c .AuthConfig .Oauth2JwtBearer .ClientKey = nil
393
+ }
394
+ if isGoogleConnection (c .ConnectorDetails .Name ) {
395
+ for _ , configVar := range c .ConfigVariables {
396
+ if configVar .Key == "project_id" {
397
+ * configVar .StringValue = "$PROJECT_ID$"
398
+ }
399
+ }
400
+ }
401
+ }
361
402
connectionPayload , err := json .Marshal (c )
362
403
if err != nil {
363
404
return nil , err
@@ -426,7 +467,7 @@ func readSecretFile(name string) (payload []byte, err error) {
426
467
}
427
468
428
469
// Import
429
- func Import (folder string ) (err error ) {
470
+ func Import (folder string , createSecret bool ) (err error ) {
430
471
431
472
apiclient .SetPrintOutput (false )
432
473
errs := []string {}
@@ -448,8 +489,8 @@ func Import(folder string) (err error) {
448
489
return err
449
490
}
450
491
451
- if _ , err := Get (name , "" , false ); err != nil { //create only if connection doesn't exist
452
- _ , err = Create (name , content , "" , "" , "" , false )
492
+ if _ , err := Get (name , "" , false , false ); err != nil { //create only if connection doesn't exist
493
+ _ , err = Create (name , content , "" , "" , "" , false , createSecret )
453
494
if err != nil {
454
495
errs = append (errs , err .Error ())
455
496
}
@@ -525,3 +566,11 @@ func getConnectorVersion(version string) int {
525
566
func getConnectionName (name string ) string {
526
567
return name [strings .LastIndex (name , "/" )+ 1 :]
527
568
}
569
+
570
+ func isGoogleConnection (connectionName string ) bool {
571
+ if connectionName == "pubsub" || connectionName == "gcs" || connectionName == "biqguery" ||
572
+ connectionName == "cloudsql-mysql" || connectionName == "cloudsql-postgresql" || connectionName == "cloudsql-sqlserver" {
573
+ return true
574
+ }
575
+ return false
576
+ }
0 commit comments