-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathKmsExample.java
More file actions
816 lines (728 loc) · 38.8 KB
/
KmsExample.java
File metadata and controls
816 lines (728 loc) · 38.8 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
<
8000
div data-line-number="626" class="react-line-number react-code-text" style="padding-right:16px">626627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/**
* Copyright (c) 2016, 2026, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.Region;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.io.internal.WrappedFileInputStream;
import com.oracle.bmc.keymanagement.KmsCryptoClient;
import com.oracle.bmc.keymanagement.KmsManagementClient;
import com.oracle.bmc.keymanagement.KmsVaultClient;
import com.oracle.bmc.keymanagement.model.*;
import com.oracle.bmc.keymanagement.requests.*;
import com.oracle.bmc.keymanagement.responses.*;
import com.oracle.bmc.util.internal.StringUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* This class contains examples which cover basic KMS usage.
*
* <p>These examples assume you already have a Vault in ACTIVE state. If you need to create a new
* Vault, please refer to the createVaultTest method in this class. Please keep in mind that KMS
* does not support immediate deletion of Vaults because of the high risk; instead, you need to
* schedule the deletion of a Vault and a retention period of 7-30 days will be enforced before the
* Vault is deleted. During the retention period, you can cancel the deletion and the Vault will be
* ACTIVE again. Be careful before creating a Vault to avoid unnecessary expenses.
*/
public class KmsExample {
private static final int DEFAULT_KEY_LENGTH = 32;
// The KeyShape used for testing
private static final KeyShape TEST_KEY_SHAPE =
KeyShape.builder().algorithm(KeyShape.Algorithm.Aes).length(DEFAULT_KEY_LENGTH).build();
// Please pass in the compartmentId and the vaultId as the first and second argument
public static void main(final String[] args) throws Exception {
if (args.length != 3) {
throw new IllegalArgumentException(
"This example expects 3 arguments: compartment ID, vault ID and target compartment ID (for move example)");
}
// Read in compartmentId and vaultId and perform basic validations.
final String compartmentId = args[0];
final String vaultId = args[1];
final String targetCompartmentForMove = args[2];
if (compartmentId == null
|| compartmentId.trim().isEmpty()
|| vaultId == null
|| vaultId.trim().isEmpty()
|| targetCompartmentForMove == null
|| targetCompartmentForMove.trim().isEmpty()) {
System.out.println(
"compartmentId, vaultId and targetCompartmentForMove cannot be empty or null");
return;
}
// Configuring the AuthenticationDetailsProvider. It's assuming there is a default OCI
// config file
// "", and a profile in that config with the name "DEFAULT". Make changes to the following
// line if needed.
final String configurationFilePath = "~/.oci/config";
final String profile = "DEFAULT";
// Configuring the AuthenticationDetailsProvider. It's assuming there is a default OCI
// config file
// "~/.oci/config", and a profile in that config with the name "DEFAULT". Make changes to
// the following
// line if needed and use ConfigFileReader.parse(configurationFilePath, profile);
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
final AuthenticationDetailsProvider provider =
new ConfigFileAuthenticationDetailsProvider(configFile);
// Initialize the KMS Clients. KMS has three clients as following:
// * KmsVaultClient: The client for Vault management
// * KmsManagementClient: The client for Key management (ControlPlane)
// * KmsCryptoClient: The client for data encryption and decryption (DataPlane)
KmsVaultClient kmsVaultClient = KmsVaultClient.builder().build(provider);
// Using the us-ashburn-1 in this example, choose a different region if needed
kmsVaultClient.setRegion(Region.US_ASHBURN_1);
KmsManagementClient kmsManagementClient = KmsManagementClient.builder().build(provider);
KmsCryptoClient kmsCryptoClient = KmsCryptoClient.builder().build(provider);
// Get the details of the given Vault
Vault vault = getVaultTest(kmsVaultClient, vaultId);
// The ManagementClient and CryptoClient use Vault specific endpoints; Set them now.
kmsManagementClient.setEndpoint(vault.getManagementEndpoint());
kmsCryptoClient.setEndpoint(vault.getCryptoEndpoint());
System.out.println();
// Vault Operations
updateVaultResetTagsTest(kmsVaultClient, vault.getId());
updateVaultTest(kmsVaultClient, vault.getId());
listVaultsTest(kmsVaultClient, compartmentId);
scheduleVaultDeletionTest(kmsVaultClient, vault.getId());
// After scheduling deletion, the Vault will stay in SCHEDULING_DELETION state shortly and
// then
// transit to PENDING_DELETION state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for the vault deletion scheduling to finish");
System.out.println();
kmsVaultClient
.getWaiters()
.forVault(
GetVaultRequest.builder().vaultId(vault.getId()).build(),
Vault.LifecycleState.PendingDeletion)
.execute();
cancelVaultDeletionTest(kmsVaultClient, vault.getId());
// After cancelling deletion, the Vault will stay in CANCELLING_DELETION state shortly and
// then
// transit to ACTIVE state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for the vault deletion cancelling to finish");
System.out.println();
kmsVaultClient
.getWaiters()
.forVault(
GetVaultRequest.builder().vaultId(vault.getId()).build(),
Vault.LifecycleState.Active)
.execute();
backupVaultTest(kmsVaultClient, vault.getId());
// After backing up, the Vault will stay in BACKUP_IN_PROGRESS state shortly and then
// transit to ACTIVE state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for the vault to be backed up");
System.out.println();
kmsVaultClient
.getWaiters()
.forVault(
GetVaultRequest.builder().vaultId(vault.getId()).build(),
Vault.LifecycleState.Active)
.execute();
restoreVaultFromObjectStoreTest(kmsVaultClient, compartmentId);
// After backing up, the Vault will stay in RESTORING state shortly and then
// transit to ACTIVE state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for the vault to be restored from object store");
System.out.println();
kmsVaultClient
.getWaiters()
.forVault(
GetVaultRequest.builder().vaultId(vault.getId()).build(),
Vault.LifecycleState.Active)
.execute();
restoreVaultFromFileTest(kmsVaultClient, compartmentId);
// After restoring, the Vault will stay in RESTORING state shortly and then
// transit to ACTIVE state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for the vault to be restored from a local file");
System.out.println();
kmsVaultClient
.getWaiters()
.forVault(
GetVaultRequest.builder().vaultId(vaultId).build(),
Vault.LifecycleState.Active)
.execute();
// Management / Key Operations
String keyId = createKeyTest(kmsManagementClient, compartmentId);
// After creating a Key, the Key will stay in CREATING state shortly and then
// transit to ENABLED state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for key creation to finish");
System.out.println();
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
getKeyTest(kmsManagementClient, keyId);
updateKeyResetTagsTest(kmsManagementClient, keyId);
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
updateKeyTest(kmsManagementClient, keyId);
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
listKeysTest(kmsManagementClient, compartmentId);
disableKeyTest(kmsManagementClient, keyId);
// After disabling a Key, the Key will stay in DISABLING state shortly and then
// transit to DISABLED state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for key disabling to finish");
System.out.println();
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Disabled)
.execute();
enableKeyTest(kmsManagementClient, keyId);
// After enabling a Key, the Key will stay in ENABLING state shortly and then
// transit to ENABLED state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for key enabling to finish");
System.out.println();
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
scheduleKeyDeletionTest(kmsManagementClient, keyId);
// After scheduling deletion, the Key will stay in SCHEDULING_DELETION state shortly and
// then
// transit to PENDING_DELETION state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for the key deletion scheduling to finish");
System.out.println();
kmsManagementClient
.getWaiters()
.forKey(
GetKeyRequest.builder().keyId(keyId).build(),
Key.LifecycleState.PendingDeletion)
.execute();
cancelKeyDeletionTest(kmsManagementClient, keyId);
// After cancelling deletion, the Key will stay in CANCELLING_DELETION state shortly and
// then
// transit to Enabled state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for the key deletion cancelling to finish");
System.out.println();
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
createKeyVersionTest(kmsManagementClient, keyId);
listKeyVersionsTest(kmsManagementClient, keyId);
// Crypto Operations
String ciphertext = encryptTest(kmsCryptoClient, keyId);
decryptTest(kmsCryptoClient, keyId, ciphertext);
generateDataEncryptionKeyTest(kmsCryptoClient, keyId);
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
// Backup + Restore Operations
backupKeyTest(kmsManagementClient, keyId);
// After backing up, the Key will stay in BACKUP_IN_PROGRESS state shortly and then
// transit to ENABLED state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for the key to be backed up");
System.out.println();
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
restoreKeyFromObjectStoreTest(kmsManagementClient);
// After restoring, the Key will stay in RESTORING state shortly and then
// transit to ENABLED state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for the key to be restored from object store");
System.out.println();
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
kmsVaultClient
.getWaiters()
.forVault(
GetVaultRequest.builder().vaultId(vaultId).build(),
Vault.LifecycleState.Active)
.execute();
Thread.sleep(30000); // wait to avoid throttling
restoreKeyFromFileTest(kmsManagementClient);
// After restoring, the Key will stay in RESTORING state shortly and then
// transit to ENABLED state. Wait a bit for the transition to happen.
System.out.println("Wait a bit for the key to be restored from a local file");
System.out.println();
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
kmsVaultClient
.getWaiters()
.forVault(
GetVaultRequest.builder().vaultId(vaultId).build(),
Vault.LifecycleState.Active)
.execute();
Thread.sleep(30000); // wait to avoid throttling
// change compartment operations
changeKeyCompartmentTest(kmsManagementClient, keyId, targetCompartmentForMove);
System.out.println("Wait a bit for key move to finish");
System.out.println();
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
changeVaultCompartmentTest(kmsVaultClient, vaultId, targetCompartmentForMove);
System.out.println("Wait a bit for vault move to finish");
System.out.println();
kmsManagementClient
.getWaiters()
.forKey(GetKeyRequest.builder().keyId(keyId).build(), Key.LifecycleState.Enabled)
.execute();
}
public static Vault createVaultTest(KmsVaultClient kmsVaultClient, String compartmentId) {
System.out.println("CreateVault Test");
CreateVaultDetails createVaultDetails =
CreateVaultDetails.builder()
.compartmentId(compartmentId)
.displayName("Test-Vault-V1")
.vaultType(CreateVaultDetails.VaultType.VirtualPrivate)
.freeformTags(getSampleFreeformTagData())
.build();
CreateVaultRequest request =
CreateVaultRequest.builder().createVaultDetails(createVaultDetails).build();
CreateVaultResponse response = kmsVaultClient.createVault(request);
System.out.println("Creating a new vault: ");
System.out.println(response.getVault());
System.out.println();
return response.getVault();
}
public static Vault getVaultTest(KmsVaultClient kmsVaultClient, String vaultId) {
System.out.println("GetVault Test");
GetVaultRequest getVaultRequest = GetVaultRequest.builder().vaultId(vaultId).build();
GetVaultResponse response = kmsVaultClient.getVault(getVaultRequest);
System.out.println("Vault retrieved: ");
System.out.println(response.getVault());
System.out.println();
return response.getVault();
}
public static void listVaultsTest(KmsVaultClient kmsVaultClient, String compartmentId) {
System.out.println("ListVaults Test");
ListVaultsRequest listVaultsRequest =
ListVaultsRequest.builder().compartmentId(compartmentId).build();
ListVaultsResponse response = kmsVaultClient.listVaults(listVaultsRequest);
System.out.println("ListVaults response: ");
for (VaultSummary vault : response.getItems()) {
System.out.println(vault);
}
System.out.println();
}
public static void updateVaultResetTagsTest(KmsVaultClient kmsVaultClient, String vaultId) {
System.out.println("UpdateVaultResetTags Test");
Map<String, String> newEmptyFreeformTag = Collections.emptyMap();
UpdateVaultDetails updateVaultDetails =
UpdateVaultDetails.builder().freeformTags(newEmptyFreeformTag).build();
UpdateVaultRequest updateVaultRequest =
UpdateVaultRequest.builder()
.updateVaultDetails(updateVaultDetails)
.vaultId(vaultId)
.build();
UpdateVaultResponse response = kmsVaultClient.updateVault(updateVaultRequest);
System.out.println("Updated vault: ");
System.out.println(response.getVault());
System.out.println();
}
public static void updateVaultTest(KmsVaultClient kmsVaultClient, String vaultId) {
System.out.println("UpdateVault Test");
Map<String, String> newFreeformTag = getSampleFreeformTagData();
newFreeformTag.put("dummyfreeformkey3", "dummyfreeformvalue3");
UpdateVaultDetails updateVaultDetails =
UpdateVaultDetails.builder()
.displayName("Test-Vault-V2")
.freeformTags(newFreeformTag)
.build();
UpdateVaultRequest updateVaultRequest =
UpdateVaultRequest.builder()
.updateVaultDetails(updateVaultDetails)
.vaultId(vaultId)
.build();
UpdateVaultResponse response = kmsVaultClient.updateVault(updateVaultRequest);
System.out.println("Updated vault: ");
System.out.println(response.getVault());
System.out.println();
}
public static void scheduleVaultDeletionTest(KmsVaultClient kmsVaultClient, String vaultId) {
System.out.println("ScheduleVaultDeletion Test");
ScheduleVaultDeletionDetails scheduleVaultDeletionDetails =
ScheduleVaultDeletionDetails.builder().timeOfDeletion(null).build();
ScheduleVaultDeletionRequest scheduleVaultDeletionRequest =
ScheduleVaultDeletionRequest.builder()
.scheduleVaultDeletionDetails(scheduleVaultDeletionDetails)
.vaultId(vaultId)
.build();
ScheduleVaultDeletionResponse response =
kmsVaultClient.scheduleVaultDeletion(scheduleVaultDeletionRequest);
System.out.println("Scheduling deletion, updated vault: ");
System.out.println(response.getVault());
System.out.println();
}
public static void cancelVaultDeletionTest(KmsVaultClient kmsVaultClient, String vaultId) {
System.out.println("CancelVaultDeletion Test");
CancelVaultDeletionRequest cancelVaultDeletionRequest =
CancelVaultDeletionRequest.builder().vaultId(vaultId).build();
CancelVaultDeletionResponse response =
kmsVaultClient.cancelVaultDeletion(cancelVaultDeletionRequest);
System.out.println("Cancelling deletion, updated vault: ");
System.out.println(response.getVault());
System.out.println();
}
public static void backupVaultTest(KmsVaultClient kmsVaultClient, String vaultId) {
System.out.println("BackupVault Test");
BackupVaultRequest backupVaultRequest =
BackupVaultRequest.builder()
.backupVaultDetails(
BackupVaultDetails.builder()
.backupLocation(
BackupLocationBucket.builder()
.bucketName("JavaSdkExamples")
.objectName("BackupVaultSdkTest")
.namespace("kmstest")
.build())
.isIncludeKeys(true)
.build())
.vaultId(vaultId)
.build();
BackupVaultResponse response = kmsVaultClient.backupVault(backupVaultRequest);
System.out.println("Backing up vault, updated vault: ");
System.out.println(response.getVault());
System.out.println();
}
public static void restoreVaultFromObjectStoreTest(
KmsVaultClient kmsVaultClient, String compartmentId) {
System.out.println("RestoreVaultFromObjectStore Test");
RestoreVaultFromObjectStoreRequest restoreVaultFromObjectStoreRequest =
RestoreVaultFromObjectStoreRequest.builder()
.restoreVaultFromObjectStoreDetails(
RestoreVaultFromObjectStoreDetails.builder()
.backupLocation(
BackupLocationBucket.builder()
.bucketName("JavaSdkExamples")
.objectName("BackupVaultSdkTest")
.namespace("kmstest")
.build())
.build())
.compartmentId(compartmentId)
.build();
RestoreVaultFromObjectStoreResponse response =
kmsVaultClient.restoreVaultFromObjectStore(restoreVaultFromObjectStoreRequest);
System.out.println("Restoring vault from object store, updated vault: ");
System.out.println(response.getVault());
System.out.println();
}
public static void restoreVaultFromFileTest(
KmsVaultClient kmsVaultClient, String compartmentId) {
System.out.println("RestoreVaultFromFile Test");
// please take a second and download backup to a local file before proceeding
String backupFile = "/tmp/BackupVaultSdkTest";
try (WrappedFileInputStream fileInputStream =
new WrappedFileInputStream(new File(backupFile))) {
RestoreVaultFromFileRequest request =
RestoreVaultFromFileRequest.builder()
.compartmentId(compartmentId)
.restoreVaultFromFileDetails(fileInputStream)
.build();
RestoreVaultFromFileResponse response = kmsVaultClient.restoreVaultFromFile(request);
System.out.println("Restoring vault from local file, updated vault: ");
System.out.println(response.getVault());
System.out.println();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
public static String createKeyTest(
KmsManagementClient kmsManagementClient, String compartmentId) {
System.out.println("CreateKey Test");
CreateKeyDetails createKeyDetails =
CreateKeyDetails.builder()
.keyShape(TEST_KEY_SHAPE)
.compartmentId(compartmentId)
.displayName("Test_Key_V1")
.freeformTags(getSampleFreeformTagData())
.build();
CreateKeyRequest createKeyRequest =
CreateKeyRequest.builder().createKeyDetails(createKeyDetails).build();
CreateKeyResponse response = kmsManagementClient.createKey(createKeyRequest);
System.out.println("Creating a new key: ");
System.out.println(response.getKey());
System.out.println();
return response.getKey().getId();
}
public static void getKeyTest(KmsManagementClient kmsManagementClient, String keyId) {
System.out.println("GetKey Test");
GetKeyRequest getKeyRequest = GetKeyRequest.builder().keyId(keyId).build();
GetKeyResponse response = kmsManagementClient.getKey(getKeyRequest);
System.out.println("Key retrieved: ");
System.out.println(response.getKey());
System.out.println();
}
public static void listKeysTest(KmsManagementClient kmsManagementClient, String compartmentId) {
System.out.println("ListKeys Test");
ListKeysRequest listKeysRequest =
ListKeysRequest.builder().compartmentId(compartmentId).build();
ListKeysResponse response = kmsManagementClient.listKeys(listKeysRequest);
System.out.println("ListKeys response: ");
for (KeySummary key : response.getItems()) {
System.out.println(key);
}
System.out.println();
}
public static void updateKeyResetTagsTest(
KmsManagementClient kmsManagementClient, String keyId) {
System.out.println("UpdateKeyResetTags Test");
Map<String, String> newEmptyFreeformTag = Collections.emptyMap();
UpdateKeyDetails updateKeyDetails =
UpdateKeyDetails.builder()
.displayName("Test_Key_V2")
.freeformTags(newEmptyFreeformTag)
.build();
UpdateKeyRequest updateKeyRequest =
UpdateKeyRequest.builder().updateKeyDetails(updateKeyDetails).keyId(keyId).build();
UpdateKeyResponse response = kmsManagementClient.updateKey(updateKeyRequest);
System.out.println("Updated key: ");
System.out.println(response.getKey());
System.out.println();
}
public static void updateKeyTest(KmsManagementClient kmsManagementClient, String keyId) {
System.out.println("UpdateKey Test");
Map<String, String> newFreeformTag = getSampleFreeformTagData();
newFreeformTag.put("dummyfreeformkey3", "dummyfreeformvalue3");
UpdateKeyDetails updateKeyDetails =
UpdateKeyDetails.builder()
.displayName("Test_Key_V3")
.freeformTags(newFreeformTag)
.build();
UpdateKeyRequest updateKeyRequest =
UpdateKeyRequest.builder().updateKeyDetails(updateKeyDetails).keyId(keyId).build();
UpdateKeyResponse response = kmsManagementClient.updateKey(updateKeyRequest);
System.out.println("Updated key: ");
System.out.println(response.getKey());
System.out.println();
}
public static void disableKeyTest(KmsManagementClient kmsManagementClient, String keyId) {
System.out.println("DisableKey Test");
DisableKeyRequest disableKeyRequest = DisableKeyRequest.builder().keyId(keyId).build();
DisableKeyResponse response = kmsManagementClient.disableKey(disableKeyRequest);
System.out.println("Disabling key, updated key: ");
System.out.println(response.getKey());
System.out.println();
}
public static void enableKeyTest(KmsManagementClient kmsManagementClient, String keyId) {
System.out.println("EnableKey Test");
EnableKeyRequest enableKeyRequest = EnableKeyRequest.builder().keyId(keyId).build();
EnableKeyResponse response = kmsManagementClient.enableKey(enableKeyRequest);
System.out.println("Enabling key, updated key: ");
System.out.println(response.getKey());
System.out.println();
}
public static void cancelKeyDeletionTest(
KmsManagementClient kmsManagementClient, String keyId) {
System.out.println("CancelKeyDeletion Test");
CancelKeyDeletionRequest cancelKeyDeletionRequest =
CancelKeyDeletionRequest.builder().keyId(keyId).build();
CancelKeyDeletionResponse response =
kmsManagementClient.cancelKeyDeletion(cancelKeyDeletionRequest);
System.out.println("Cancelling key deletion, updated key: ");
System.out.println(response.getKey());
System.out.println();
}
public static void scheduleKeyDeletionTest(
KmsManagementClient kmsManagementClient, String keyId) {
System.out.println("ScheduleKeyDeletion Test");
ScheduleKeyDeletionDetails scheduleKeyDeletionDetails =
ScheduleKeyDeletionDetails.builder().timeOfDeletion(null).build();
ScheduleKeyDeletionRequest scheduleKeyDeletionRequest =
ScheduleKeyDeletionRequest.builder()
.keyId(keyId)
.scheduleKeyDeletionDetails(scheduleKeyDeletionDetails)
.build();
ScheduleKeyDeletionResponse response =
kmsManagementClient.scheduleKeyDeletion(scheduleKeyDeletionRequest);
System.out.println("Scheduling key deletion, updated key: ");
System.out.println(response.getKey());
System.out.println();
}
public static void createKeyVersionTest(KmsManagementClient kmsManagementClient, String keyId) {
System.out.println("CreateKeyVersion Test");
CreateKeyVersionRequest createKeyVersionRequest =
CreateKeyVersionRequest.builder().keyId(keyId).build();
CreateKeyVersionResponse response =
kmsManagementClient.createKeyVersion(createKeyVersionRequest);
System.out.println("Creating a new key version: ");
System.out.println(response.getKeyVersion());
System.out.println();
}
public static void listKeyVersionsTest(KmsManagementClient kmsManagementClient, String keyId) {
System.out.println("ListKeyVersions Test");
ListKeyVersionsRequest listKeyVersionsRequest =
ListKeyVersionsRequest.builder().keyId(keyId).build();
ListKeyVersionsResponse response =
kmsManagementClient.listKeyVersions(listKeyVersionsRequest);
System.out.println("ListKeyVersions response: ");
for (KeyVersionSummary keyVersion : response.getItems()) {
System.out.println(keyVersion);
}
System.out.println();
}
public static String encryptTest(KmsCryptoClient kmsCryptoClient, String keyId) {
System.out.println("Encrypt Test");
String plaintext = "I love OCI!";
EncryptDataDetails encryptDataDetails =
EncryptDataDetails.builder()
.keyId(keyId)
.plaintext(Base64.getEncoder().encodeToString(plaintext.getBytes()))
.loggingContext(getSampleLoggingContext())
.build();
EncryptRequest encryptRequest =
EncryptRequest.builder().encryptDataDetails(encryptDataDetails).build();
EncryptResponse response = kmsCryptoClient.encrypt(encryptRequest);
System.out.println("Plaintext: " + plaintext);
System.out.println("Ciphertext: " + response.getEncryptedData().getCiphertext());
System.out.println();
return response.getEncryptedData().getCiphertext();
}
public static void decryptTest(
KmsCryptoClient kmsCryptoClient, String keyId, String cipherText) {
System.out.println("Decrypt Test");
DecryptDataDetails decryptDataDetails =
DecryptDataDetails.builder()
.ciphertext(cipherText)
.keyId(keyId)
.loggingContext(getSampleLoggingContext())
.build();
DecryptRequest decryptRequest =
DecryptRequest.builder().decryptDataDetails(decryptDataDetails).build();
DecryptResponse response = kmsCryptoClient.decrypt(decryptRequest);
System.out.println("Plaintext: " + response.getDecryptedData().getPlaintext());
System.out.println();
}
public static void generateDataEncryptionKeyTest(
KmsCryptoClient kmsCryptoClient, String keyId) {
System.out.println("GenerateDataEncryptionKey Test");
GenerateKeyDetails generateKeyDetails =
GenerateKeyDetails.builder()
.keyId(keyId)
.keyShape(TEST_KEY_SHAPE)
.includePlaintextKey(true)
.loggingContext(getSampleLoggingContext())
.build();
GenerateDataEncryptionKeyRequest generateDataEncryptionKeyRequest =
GenerateDataEncryptionKeyRequest.builder()
.generateKeyDetails(generateKeyDetails)
.build();
GenerateDataEncryptionKeyResponse response =
kmsCryptoClient.generateDataEncryptionKey(generateDataEncryptionKeyRequest);
System.out.println("GenerateDataEncryptionKey response: ");
System.out.println(response.getGeneratedKey());
System.out.println();
}
public static void backupKeyTest(KmsManagementClient kmsManagementClient, String keyId) {
System.out.println("BackupKey Test");
BackupKeyRequest backupKeyRequest =
BackupKeyRequest.builder()
.backupKeyDetails(
BackupKeyDetails.builder()
.backupLocation(
BackupLocationBucket.builder()
.bucketName("JavaSdkExamples")
.objectName("BackupKeySdkTest")
.namespace("kmstest")
.build())
.build())
.keyId(keyId)
.build();
BackupKeyResponse response = kmsManagementClient.backupKey(backupKeyRequest);
System.out.println("Backing up key, updated key:");
System.out.println(response.getKey());
System.out.println();
}
public static void restoreKeyFromObjectStoreTest(KmsManagementClient kmsManagementClient) {
System.out.println("RestoreKeyFromObjectStore Test");
RestoreKeyFromObjectStoreRequest restoreKeyFromObjectStoreRequest =
RestoreKeyFromObjectStoreRequest.builder()
.restoreKeyFromObjectStoreDetails(
RestoreKeyFromObjectStoreDetails.builder()
.backupLocation(
BackupLocationBucket.builder()
.bucketName("JavaSdkExamples")
.objectName("BackupKeySdkTest")
.namespace("kmstest")
.build())
.build())
.build();
RestoreKeyFromObjectStoreResponse response =
kmsManagementClient.restoreKeyFromObjectStore(restoreKeyFromObjectStoreRequest);
System.out.println("Restoring key from object store, upd
8093
ated key: ");
System.out.println(response.getKey());
System.out.println();
}
public static void restoreKeyFromFileTest(KmsManagementClient kmsManagementClient) {
System.out.println("RestoreKeyFromFile Test");
// please take a second and download backup to a local file before proceeding
String backupFile = "/tmp/BackupKeySdkTest";
try (WrappedFileInputStream fileInputStream =
new WrappedFileInputStream(new File(backupFile))) {
RestoreKeyFromFileRequest request =
RestoreKeyFromFileRequest.builder()
.restoreKeyFromFileDetails(fileInputStream)
.build();
RestoreKeyFromFileResponse response = kmsManagementClient.restoreKeyFromFile(request);
System.out.println("Restoring key from local file, updated key: ");
System.out.println(response.getKey());
System.out.println();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
public static void changeVaultCompartmentTest(
KmsVaultClient kmsVaultClient, String vaultId, String targetCompartment) {
System.out.println("ChangeVaultCompartment Test");
ChangeVaultCompartmentDetails changeVaultCompartmentDetails =
ChangeVaultCompartmentDetails.builder().compartmentId(targetCompartment).build();
ChangeVaultCompartmentRequest request =
ChangeVaultCompartmentRequest.builder()
.vaultId(vaultId)
.changeVaultCompartmentDetails(changeVaultCompartmentDetails)
.build();
ChangeVaultCompartmentResponse response = kmsVaultClient.changeVaultCompartment(request);
System.out.println("Changing compartment of vault");
System.out.println();
}
public static void changeKeyCompartmentTest(
KmsManagementClient kmsManagementClient, String keyId, String targetCompartment) {
System.out.println("ChangeKeyCompartment Test");
ChangeKeyCompartmentDetails changeKeyCompartmentDetails =
ChangeKeyCompartmentDetails.builder().compartmentId(targetCompartment).build();
ChangeKeyCompartmentRequest request =
ChangeKeyCompartmentRequest.builder()
.keyId(keyId)
.changeKeyCompartmentDetails(changeKeyCompartmentDetails)
.build();
ChangeKeyCompartmentResponse response = kmsManagementClient.changeKeyCompartment(request);
System.out.println("Changing compartment of key");
System.out.println();
}
private static Map<String, String> getSampleFreeformTagData() {
Map<String, String> freeformTags = new HashMap<String, String>();
freeformTags.put("dummyfreeformkey1", "dummyfreeformvalue1");
freeformTags.put("dummyfreeformkey2", "dummyfreeformvalue2");
return freeformTags;
}
private static Map<String, String> getSampleLoggingContext() {
Map<String, String> loggingContext = new HashMap<String, String>();
loggingContext.put("loggingContextKey1", "loggingContextValue1");
loggingContext.put("loggingContextKey2", "loggingContextValue2");
return loggingContext;
}
}