@@ -111,12 +111,12 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, _ ...Opt
111
111
return nil , nil , errors .Wrap (ctx , err , op )
112
112
}
113
113
114
+ databaseWrapper , err := r .kms .GetWrapper (ctx , c .ProjectId , kms .KeyPurposeDatabase )
115
+ if err != nil {
116
+ return nil , nil , errors .Wrap (ctx , err , op , errors .WithMsg ("unable to get database wrapper" ))
117
+ }
114
118
// If secrets were passed in, HMAC 'em
115
119
if c .Secrets != nil && len (c .Secrets .GetFields ()) > 0 {
116
- databaseWrapper , err := r .kms .GetWrapper (ctx , c .ProjectId , kms .KeyPurposeDatabase )
117
- if err != nil {
118
- return nil , nil , errors .Wrap (ctx , err , op , errors .WithMsg ("unable to get database wrapper" ))
119
- }
120
120
if err := c .hmacSecrets (ctx , databaseWrapper ); err != nil {
121
121
return nil , nil , errors .Wrap (ctx , err , op , errors .WithMsg ("error hmac'ing passed-in secrets" ))
122
122
}
@@ -188,11 +188,7 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, _ ...Opt
188
188
if err != nil {
189
189
return errors .Wrap (ctx , err , op )
190
190
}
191
- dbWrapper , err := r .kms .GetWrapper (ctx , c .ProjectId , kms .KeyPurposeDatabase )
192
- if err != nil {
193
- return errors .Wrap (ctx , err , op , errors .WithMsg ("unable to get db wrapper" ))
194
- }
195
- if err := hcSecret .encrypt (ctx , dbWrapper ); err != nil {
191
+ if err := hcSecret .encrypt (ctx , databaseWrapper ); err != nil {
196
192
return errors .Wrap (ctx , err , op )
197
193
}
198
194
if hcSecret != nil {
@@ -280,6 +276,11 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
280
276
return nil , nil , db .NoRowsAffected , errors .New (ctx , errors .VersionMismatch , op , fmt .Sprintf ("catalog version mismatch, want=%d, got=%d" , currentCatalog .GetVersion (), version ))
281
277
}
282
278
279
+ databaseWrapper , err := r .kms .GetWrapper (ctx , c .ProjectId , kms .KeyPurposeDatabase )
280
+ if err != nil {
281
+ return nil , nil , db .NoRowsAffected , errors .Wrap (ctx , err , op , errors .WithMsg ("unable to get database wrapper" ))
282
+ }
283
+
283
284
// Clone the catalog so that we can set fields.
284
285
newCatalog := currentCatalog .clone ()
285
286
var updateAttributes bool
@@ -318,10 +319,6 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
318
319
nullFields = append (nullFields , "SecretsHmac" )
319
320
default :
320
321
// If secrets were passed in, HMAC 'em
321
- databaseWrapper , err := r .kms .GetWrapper (ctx , c .ProjectId , kms .KeyPurposeDatabase )
322
- if err != nil {
323
- return nil , nil , db .NoRowsAffected , errors .Wrap (ctx , err , op , errors .WithMsg ("unable to get database wrapper" ))
324
- }
325
322
if err := newCatalog .hmacSecrets (ctx , databaseWrapper ); err != nil {
326
323
return nil , nil , db .NoRowsAffected , errors .Wrap (ctx , err , op , errors .WithMsg ("error hmac'ing passed-in secrets" ))
327
324
}
@@ -386,10 +383,6 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
386
383
}
387
384
}
388
385
389
- dbWrapper , err := r .kms .GetWrapper (ctx , newCatalog .ProjectId , kms .KeyPurposeDatabase )
390
- if err != nil {
391
- return nil , nil , db .NoRowsAffected , errors .Wrap (ctx , err , op , errors .WithMsg ("unable to get db wrapper" ))
392
- }
393
386
// Get the oplog.
394
387
oplogWrapper , err := r .kms .GetWrapper (ctx , newCatalog .ProjectId , kms .KeyPurposeOplog )
395
388
if err != nil {
@@ -479,7 +472,7 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
479
472
if err != nil {
480
473
return errors .Wrap (ctx , err , op )
481
474
}
482
- if err := hcSecret .encrypt (ctx , dbWrapper ); err != nil {
475
+ if err := hcSecret .encrypt (ctx , databaseWrapper ); err != nil {
483
476
return errors .Wrap (ctx , err , op )
484
477
}
485
478
0 commit comments