|
| 1 | +package cloud.stackit.sdk.kms.examples; |
| 2 | + |
| 3 | +import cloud.stackit.sdk.core.KeyFlowAuthenticator; |
| 4 | +import cloud.stackit.sdk.core.config.CoreConfiguration; |
| 5 | +import cloud.stackit.sdk.core.exception.ApiException; |
| 6 | +import cloud.stackit.sdk.kms.v1api.api.KmsApi; |
| 7 | +import cloud.stackit.sdk.kms.v1api.model.Algorithm; |
| 8 | +import cloud.stackit.sdk.kms.v1api.model.CreateKeyPayload; |
| 9 | +import cloud.stackit.sdk.kms.v1api.model.CreateKeyRingPayload; |
| 10 | +import cloud.stackit.sdk.kms.v1api.model.Key; |
| 11 | +import cloud.stackit.sdk.kms.v1api.model.KeyList; |
| 12 | +import cloud.stackit.sdk.kms.v1api.model.KeyRing; |
| 13 | +import cloud.stackit.sdk.kms.v1api.model.KeyRingList; |
| 14 | +import cloud.stackit.sdk.kms.v1api.model.Purpose; |
| 15 | +import cloud.stackit.sdk.kms.v1api.model.Version; |
| 16 | +import cloud.stackit.sdk.kms.v1api.model.VersionList; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Objects; |
| 19 | +import java.util.UUID; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import okhttp3.OkHttpClient; |
| 22 | + |
| 23 | +/** |
| 24 | + * Creates a key ring and symmetric key, lists metadata, then cleans up only the created resources. |
| 25 | + * Key deletion is scheduled by KMS; you'll have to manually delete the key and the keyring |
| 26 | + * afterwards. |
| 27 | + */ |
| 28 | +@SuppressWarnings("PMD.SystemPrintln") |
| 29 | +public final class KmsExample { |
| 30 | + private static final String REGION = "eu01"; |
| 31 | + private static final long WAIT_TIMEOUT_NANOS = TimeUnit.MINUTES.toNanos(10); |
| 32 | + private static final long POLL_INTERVAL_SECONDS = 5; |
| 33 | + |
| 34 | + private KmsExample() {} |
| 35 | + |
| 36 | + public static void main(String[] args) throws IOException, ApiException, InterruptedException { |
| 37 | + // The id of your STACKIT project, read from an environment variable for this example. |
| 38 | + String projectIdValue = System.getenv("STACKIT_PROJECT_ID"); |
| 39 | + if (projectIdValue == null || projectIdValue.isEmpty()) { |
| 40 | + System.err.println("Environment variable 'STACKIT_PROJECT_ID' not found."); |
| 41 | + return; |
| 42 | + } |
| 43 | + UUID projectId = UUID.fromString(projectIdValue); |
| 44 | + |
| 45 | + // Credentials are read from ~/.stackit/credentials.json or the environment variables |
| 46 | + // STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY. |
| 47 | + CoreConfiguration configuration = new CoreConfiguration(); |
| 48 | + OkHttpClient httpClient = new OkHttpClient(); |
| 49 | + KeyFlowAuthenticator authenticator = new KeyFlowAuthenticator(httpClient, configuration); |
| 50 | + httpClient = |
| 51 | + httpClient |
| 52 | + .newBuilder() |
| 53 | + .authenticator(authenticator) |
| 54 | + .callTimeout(60, TimeUnit.SECONDS) |
| 55 | + .build(); |
| 56 | + KmsApi kmsApi = new KmsApi(httpClient); |
| 57 | + |
| 58 | + runExample(kmsApi, projectId); |
| 59 | + } |
| 60 | + |
| 61 | + private static void runExample(KmsApi kmsApi, UUID projectId) |
| 62 | + throws ApiException, InterruptedException { |
| 63 | + UUID keyRingID = null; |
| 64 | + UUID keyID = null; |
| 65 | + System.out.println("creating key ring"); |
| 66 | + try { |
| 67 | + KeyRing keyRing = |
| 68 | + kmsApi.createKeyRing( |
| 69 | + projectId, |
| 70 | + REGION, |
| 71 | + new CreateKeyRingPayload() |
| 72 | + .displayName("java-sdk-example-keyring") |
| 73 | + .description("A Java SDK example key ring")); |
| 74 | + keyRingID = Objects.requireNonNull(keyRing.getId()); |
| 75 | + waitForKeyRing(kmsApi, projectId, keyRingID); |
| 76 | + System.out.println("key ring created: " + keyRingID); |
| 77 | + System.out.println("creating key"); |
| 78 | + Key key = |
| 79 | + kmsApi.createKey( |
| 80 | + projectId, |
| 81 | + REGION, |
| 82 | + keyRingID, |
| 83 | + new CreateKeyPayload() |
| 84 | + .algorithm(Algorithm.AES_256_GCM) |
| 85 | + .protection("software") |
| 86 | + .purpose(Purpose.SYMMETRIC_ENCRYPT_DECRYPT) |
| 87 | + .displayName("java-sdk-example-key") |
| 88 | + .description("A Java SDK example key")); |
| 89 | + keyID = Objects.requireNonNull(key.getId()); |
| 90 | + waitForKey(kmsApi, projectId, keyRingID, keyID); |
| 91 | + System.out.println("key created: " + keyID); |
| 92 | + listKeyRings(kmsApi, projectId); |
| 93 | + } finally { |
| 94 | + if (keyID != null) { |
| 95 | + // schedule key for deletion |
| 96 | + System.out.println("scheduling key deletion"); |
| 97 | + kmsApi.deleteKey(projectId, REGION, keyRingID, keyID); |
| 98 | + System.out.println( |
| 99 | + "Key scheduled for deletion. ⚠️ The key ring can't be deleted until the key deletion is done. This may take some days. Delete the key ring manually after this period."); |
| 100 | + } |
| 101 | + // key ring can only be deleted if no key exists. |
| 102 | + if (keyRingID != null && keyID == null) { |
| 103 | + System.out.println("deleting key ring"); |
| 104 | + kmsApi.deleteKeyRing(projectId, REGION, keyRingID); |
| 105 | + System.out.println("key ring deleted"); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static void waitForKeyRing(KmsApi kmsApi, UUID projectId, UUID keyRingId) |
| 111 | + throws ApiException, InterruptedException { |
| 112 | + waitUntil( |
| 113 | + "key ring " + keyRingId + " to become active", |
| 114 | + () -> { |
| 115 | + KeyRing.StateEnum state = |
| 116 | + kmsApi.getKeyRing(projectId, REGION, keyRingId).getState(); |
| 117 | + if (state == KeyRing.StateEnum.ACTIVE) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + if (state != KeyRing.StateEnum.CREATING) { |
| 121 | + throw new IllegalStateException("Unexpected key ring state: " + state); |
| 122 | + } |
| 123 | + return false; |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + private static void waitForKey(KmsApi kmsApi, UUID projectId, UUID keyRingId, UUID keyId) |
| 128 | + throws ApiException, InterruptedException { |
| 129 | + waitUntil( |
| 130 | + "key " + keyId + " to become active", |
| 131 | + () -> { |
| 132 | + Key.StateEnum state = |
| 133 | + kmsApi.getKey(projectId, REGION, keyRingId, keyId).getState(); |
| 134 | + if (state == Key.StateEnum.ACTIVE) { |
| 135 | + return true; |
| 136 | + } |
| 137 | + if (state != Key.StateEnum.CREATING) { |
| 138 | + throw new IllegalStateException("Unexpected key state: " + state); |
| 139 | + } |
| 140 | + return false; |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + private static void waitUntil(String description, ApiCondition condition) |
| 145 | + throws ApiException, InterruptedException { |
| 146 | + long start = System.nanoTime(); |
| 147 | + while (System.nanoTime() - start < WAIT_TIMEOUT_NANOS) { |
| 148 | + if (condition.isSatisfied()) { |
| 149 | + return; |
| 150 | + } |
| 151 | + TimeUnit.SECONDS.sleep(POLL_INTERVAL_SECONDS); |
| 152 | + } |
| 153 | + throw new IllegalStateException("Timed out waiting for " + description); |
| 154 | + } |
| 155 | + |
| 156 | + @FunctionalInterface |
| 157 | + private interface ApiCondition { |
| 158 | + boolean isSatisfied() throws ApiException; |
| 159 | + } |
| 160 | + |
| 161 | + private static void listKeyRings(KmsApi kmsApi, UUID projectId) throws ApiException { |
| 162 | + System.out.println("List key rings:"); |
| 163 | + KeyRingList keyRings = kmsApi.listKeyRings(projectId, REGION); |
| 164 | + for (KeyRing keyRing : Objects.requireNonNull(keyRings.getKeyRings())) { |
| 165 | + UUID keyRingId = Objects.requireNonNull(keyRing.getId()); |
| 166 | + System.out.println("* Key Ring ID: " + keyRingId); |
| 167 | + System.out.println("* Key Ring Name: " + keyRing.getDisplayName()); |
| 168 | + System.out.println("* Key Ring State: " + keyRing.getState()); |
| 169 | + listKeys(kmsApi, projectId, keyRingId); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private static void listKeys(KmsApi kmsApi, UUID projectId, UUID keyRingId) |
| 174 | + throws ApiException { |
| 175 | + KeyList keys = kmsApi.listKeys(projectId, REGION, keyRingId); |
| 176 | + for (Key key : Objects.requireNonNull(keys.getKeys())) { |
| 177 | + UUID keyId = Objects.requireNonNull(key.getId()); |
| 178 | + System.out.println(" * Key ID: " + keyId); |
| 179 | + System.out.println(" * Key Name: " + key.getDisplayName()); |
| 180 | + System.out.println(" * Algorithm: " + key.getAlgorithm()); |
| 181 | + System.out.println(" * Purpose: " + key.getPurpose()); |
| 182 | + System.out.println(" * Key State: " + key.getState()); |
| 183 | + if (key.getState() == Key.StateEnum.DELETED) { |
| 184 | + continue; |
| 185 | + } |
| 186 | + |
| 187 | + VersionList versions = kmsApi.listVersions(projectId, REGION, keyRingId, keyId); |
| 188 | + for (Version version : Objects.requireNonNull(versions.getVersions())) { |
| 189 | + System.out.println(" * Version Number: " + version.getNumber()); |
| 190 | + System.out.println(" * Version State: " + version.getState()); |
| 191 | + System.out.println(" * Disabled: " + version.getDisabled()); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments