28
28
import com .google .api .services .cloudkms .v1beta1 .model .KeyRing ;
29
29
import com .google .api .services .cloudkms .v1beta1 .model .ListCryptoKeyVersionsResponse ;
30
30
import com .google .api .services .cloudkms .v1beta1 .model .ListCryptoKeysResponse ;
31
+ import com .google .api .services .cloudkms .v1beta1 .model .ListKeyRingsResponse ;
31
32
import com .google .api .services .cloudkms .v1beta1 .model .Policy ;
32
33
import com .google .api .services .cloudkms .v1beta1 .model .SetIamPolicyRequest ;
33
34
@@ -114,6 +115,30 @@ public static CryptoKey createCryptoKey(String projectId, String ringId, String
114
115
return createdKey ;
115
116
}
116
117
118
+ /**
119
+ * Creates a new crypto key version for the given id.
120
+ */
121
+ public static void createCryptoKeyVersion (
122
+ String projectId , String ringId , String keyId ) throws IOException {
123
+ String location = "global" ;
124
+ // Create the Cloud KMS client.
125
+ CloudKMS kms = createAuthorizedClient ();
126
+
127
+ // The resource name of the cryptoKey
128
+ String cryptoKeys = String .format (
129
+ "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s" ,
130
+ projectId , location , ringId , keyId );
131
+
132
+ CryptoKeyVersion version = new CryptoKeyVersion ();
133
+
134
+ CryptoKeyVersion newVersion = kms .projects ().locations ().keyRings ().cryptoKeys ()
135
+ .cryptoKeyVersions ()
136
+ .create (cryptoKeys , version )
137
+ .execute ();
138
+
139
+ System .out .println (newVersion );
140
+ }
141
+
117
142
/**
118
143
* Disables the given version of the crypto key.
119
144
*/
@@ -263,11 +288,12 @@ public static Policy addMemberToCryptoKeyPolicy(
263
288
iamPolicy .setBindings (bindings );
264
289
265
290
// Set the new IAM Policy.
266
- Policy newIamPolicy = kms .projects ().locations ().keyRings ().cryptoKeys ()
291
+ Policy newIamPolicy = kms .projects ().locations ().keyRings ()
292
+ .cryptoKeys ()
267
293
.setIamPolicy (cryptoKey , new SetIamPolicyRequest ().setPolicy (iamPolicy ))
268
294
.execute ();
269
295
270
- System .out .println (newIamPolicy );
296
+ System .out .println ("Response: " + newIamPolicy );
271
297
return newIamPolicy ;
272
298
}
273
299
@@ -320,11 +346,12 @@ public static Policy addMemberToKeyRingPolicy(
320
346
iamPolicy .setBindings (bindings );
321
347
322
348
// Set the new IAM Policy.
323
- Policy newIamPolicy = kms .projects ().locations ().keyRings ()
349
+ Policy newIamPolicy = kms .projects ().locations ()
350
+ .keyRings ()
324
351
.setIamPolicy (keyring , new SetIamPolicyRequest ().setPolicy (iamPolicy ))
325
352
.execute ();
326
353
327
- System .out .println (newIamPolicy );
354
+ System .out .println ("Response: " + newIamPolicy );
328
355
return newIamPolicy ;
329
356
}
330
357
@@ -346,21 +373,26 @@ public static Policy removeMemberFromCryptoKeyPolicy(
346
373
// Get the current IAM policy and add the new account to it.
347
374
Policy iamPolicy = getCryptoKeyPolicy (projectId , ringId , keyId );
348
375
349
- List <Binding > bindings = iamPolicy .getBindings ();
376
+ if (null == iamPolicy .getBindings ()) {
377
+ // Nothing to remove
378
+ return null ;
379
+ }
380
+
350
381
// Filter out the given member
351
- for (Binding b : bindings ) {
382
+ for (Binding b : iamPolicy . getBindings () ) {
352
383
if (role .equals (b .getRole ()) && b .getMembers ().contains (member )) {
353
- b .getMembers ().remove ( member );
384
+ b .getMembers ().removeAll ( Collections . singletonList ( member ) );
354
385
break ;
355
386
}
356
387
}
357
388
358
389
// Set the new IAM Policy.
359
- Policy newIamPolicy = kms .projects ().locations ().keyRings ().cryptoKeys ()
390
+ Policy newIamPolicy = kms .projects ().locations ().keyRings ()
391
+ .cryptoKeys ()
360
392
.setIamPolicy (cryptoKey , new SetIamPolicyRequest ().setPolicy (iamPolicy ))
361
393
.execute ();
362
394
363
- System .out .println (newIamPolicy );
395
+ System .out .println ("Response: " + newIamPolicy );
364
396
return newIamPolicy ;
365
397
}
366
398
@@ -382,24 +414,54 @@ public static Policy removeMemberFromKeyRingPolicy(
382
414
// Get the current IAM policy and add the new account to it.
383
415
Policy iamPolicy = getKeyRingPolicy (projectId , ringId );
384
416
385
- List <Binding > bindings = iamPolicy .getBindings ();
386
417
// Filter out the given member
387
- for (Binding b : bindings ) {
418
+ for (Binding b : iamPolicy . getBindings () ) {
388
419
if (role .equals (b .getRole ()) && b .getMembers ().contains (member )) {
389
420
b .getMembers ().remove (member );
390
421
break ;
391
422
}
392
423
}
393
424
394
425
// Set the new IAM Policy.
395
- Policy newIamPolicy = kms .projects ().locations ().keyRings ().cryptoKeys ()
426
+ Policy newIamPolicy = kms .projects ().locations ()
427
+ .keyRings ()
396
428
.setIamPolicy (cryptoKey , new SetIamPolicyRequest ().setPolicy (iamPolicy ))
397
429
.execute ();
398
430
399
- System .out .println (newIamPolicy );
431
+ System .out .println ("Response: " + newIamPolicy );
400
432
return newIamPolicy ;
401
433
}
402
434
435
+ /**
436
+ * Prints all the keyrings in the given project.
437
+ */
438
+ public static void listKeyRings (String projectId ) throws IOException {
439
+ String location = "global" ;
440
+ // Create the Cloud KMS client.
441
+ CloudKMS kms = createAuthorizedClient ();
442
+
443
+ // The resource name of the cryptoKey
444
+ String keyRingPath = String .format (
445
+ "projects/%s/locations/%s" ,
446
+ projectId , location );
447
+
448
+ // Make the RPC call
449
+ ListKeyRingsResponse response = kms .projects ().locations ()
450
+ .keyRings ()
451
+ .list (keyRingPath )
452
+ .execute ();
453
+
454
+ // Print the returned key rings
455
+ if (null != response .getKeyRings ()) {
456
+ System .out .println ("Key Rings: " );
457
+ for (KeyRing keyRing : response .getKeyRings ()) {
458
+ System .out .println (keyRing .getName ());
459
+ }
460
+ } else {
461
+ System .out .println ("No keyrings defined." );
462
+ }
463
+ }
464
+
403
465
/**
404
466
* Prints all the keys in the given key ring.
405
467
*/
0 commit comments