Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
- `automation`:
- [v0.1.1](services/automation/CHANGELOG.md#v011)
- **Dependencies:** Bump `org.openapitools:jackson-databind-nullable` from `0.2.8` to `0.2.12`
- `serverbackup`:
- [v0.2.1](services/serverbackup/CHANGELOG.md#v021)
- **Dependencies:** Bump `org.openapitools:jackson-databind-nullable` from `0.2.8` to `0.2.12`
- `serverupdate`:
- [v0.2.2](services/serverupdate/CHANGELOG.md#v022)
- **Dependencies:** Bump `org.openapitools:jackson-databind-nullable` from `0.2.8` to `0.2.12`
- `kms`:
- [v0.1.0](services/kms/CHANGELOG.md#v010)
- Initial onboarding of STACKIT Java SDK for STACKIT Key Management Service (KMS)
- `objectstorage`:
- [v0.5.0](services/objectstorage/CHANGELOG.md#v050)
- **Dependencies:** Bump `org.openapitools:jackson-databind-nullable` from `0.2.8` to `0.2.12`
Expand All @@ -23,6 +20,12 @@
- [v0.7.0](services/resourcemanager/CHANGELOG.md#v070)
- **Dependencies:** Bump `org.openapitools:jackson-databind-nullable` from `0.2.8` to `0.2.12`
- **Feature:** The `members` attribute in the `CreateProjectPayload` model class is now optional (previously required) and therefore changed from `@javax.annotation.Nonnull` to `@javax.annotation.Nullable`
- `serverbackup`:
- [v0.2.1](services/serverbackup/CHANGELOG.md#v021)
- **Dependencies:** Bump `org.openapitools:jackson-databind-nullable` from `0.2.8` to `0.2.12`
- `serverupdate`:
- [v0.2.2](services/serverupdate/CHANGELOG.md#v022)
- **Dependencies:** Bump `org.openapitools:jackson-databind-nullable` from `0.2.8` to `0.2.12`
- `sfs`:
- [v0.6.0](services/sfs/CHANGELOG.md#v060)
- **Dependencies:** Bump `org.openapitools:jackson-databind-nullable` from `0.2.8` to `0.2.12`
Expand Down
6 changes: 6 additions & 0 deletions examples/kms/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies {
implementation project (':services:kms')
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
}

ext.mainClassName = 'cloud.stackit.sdk.kms.examples.KmsExample'
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package cloud.stackit.sdk.kms.examples;

import cloud.stackit.sdk.core.KeyFlowAuthenticator;
import cloud.stackit.sdk.core.config.CoreConfiguration;
import cloud.stackit.sdk.core.exception.ApiException;
import cloud.stackit.sdk.kms.v1api.api.KmsApi;
import cloud.stackit.sdk.kms.v1api.model.Algorithm;
import cloud.stackit.sdk.kms.v1api.model.CreateKeyPayload;
import cloud.stackit.sdk.kms.v1api.model.CreateKeyRingPayload;
import cloud.stackit.sdk.kms.v1api.model.Key;
import cloud.stackit.sdk.kms.v1api.model.KeyList;
import cloud.stackit.sdk.kms.v1api.model.KeyRing;
import cloud.stackit.sdk.kms.v1api.model.KeyRingList;
import cloud.stackit.sdk.kms.v1api.model.Purpose;
import cloud.stackit.sdk.kms.v1api.model.Version;
import cloud.stackit.sdk.kms.v1api.model.VersionList;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;

/**
* Creates a key ring and symmetric key, lists metadata, then cleans up only the created resources.
* Key deletion is scheduled by KMS; you'll have to manually delete the key and the keyring
* afterwards.
*/
@SuppressWarnings("PMD.SystemPrintln")
public final class KmsExample {
private static final String REGION = "eu01";
private static final long WAIT_TIMEOUT_NANOS = TimeUnit.MINUTES.toNanos(10);
private static final long POLL_INTERVAL_SECONDS = 5;

private KmsExample() {}

public static void main(String[] args) throws IOException, ApiException, InterruptedException {
// The id of your STACKIT project, read from an environment variable for this example.
String projectIdValue = System.getenv("STACKIT_PROJECT_ID");
if (projectIdValue == null || projectIdValue.isEmpty()) {
System.err.println("Environment variable 'STACKIT_PROJECT_ID' not found.");
return;
}
UUID projectId = UUID.fromString(projectIdValue);

// Credentials are read from ~/.stackit/credentials.json or the environment variables
// STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY.
CoreConfiguration configuration = new CoreConfiguration();
OkHttpClient httpClient = new OkHttpClient();
KeyFlowAuthenticator authenticator = new KeyFlowAuthenticator(httpClient, configuration);
httpClient =
httpClient
.newBuilder()
.authenticator(authenticator)
.callTimeout(60, TimeUnit.SECONDS)
.build();
KmsApi kmsApi = new KmsApi(httpClient);

runExample(kmsApi, projectId);
}

private static void runExample(KmsApi kmsApi, UUID projectId)
throws ApiException, InterruptedException {
UUID keyRingID = null;
UUID keyID = null;
System.out.println("creating key ring");
try {
KeyRing keyRing =
kmsApi.createKeyRing(
projectId,
REGION,
new CreateKeyRingPayload()
.displayName("java-sdk-example-keyring")
.description("A Java SDK example key ring"));
keyRingID = Objects.requireNonNull(keyRing.getId());
waitForKeyRing(kmsApi, projectId, keyRingID);
System.out.println("key ring created: " + keyRingID);
System.out.println("creating key");
Key key =
kmsApi.createKey(
projectId,
REGION,
keyRingID,
new CreateKeyPayload()
.algorithm(Algorithm.AES_256_GCM)
.protection("software")
.purpose(Purpose.SYMMETRIC_ENCRYPT_DECRYPT)
.displayName("java-sdk-example-key")
.description("A Java SDK example key"));
keyID = Objects.requireNonNull(key.getId());
waitForKey(kmsApi, projectId, keyRingID, keyID);
System.out.println("key created: " + keyID);
listKeyRings(kmsApi, projectId);
} finally {
if (keyID != null) {
// schedule key for deletion
System.out.println("scheduling key deletion");
kmsApi.deleteKey(projectId, REGION, keyRingID, keyID);
System.out.println(
"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.");
}
// key ring can only be deleted if no key exists.
if (keyRingID != null && keyID == null) {
System.out.println("deleting key ring");
kmsApi.deleteKeyRing(projectId, REGION, keyRingID);
System.out.println("key ring deleted");
}
}
}

private static void waitForKeyRing(KmsApi kmsApi, UUID projectId, UUID keyRingId)
throws ApiException, InterruptedException {
waitUntil(
"key ring " + keyRingId + " to become active",
() -> {
KeyRing.StateEnum state =
kmsApi.getKeyRing(projectId, REGION, keyRingId).getState();
if (state == KeyRing.StateEnum.ACTIVE) {
return true;
}
if (state != KeyRing.StateEnum.CREATING) {
throw new IllegalStateException("Unexpected key ring state: " + state);
}
return false;
});
}

private static void waitForKey(KmsApi kmsApi, UUID projectId, UUID keyRingId, UUID keyId)
throws ApiException, InterruptedException {
waitUntil(
"key " + keyId + " to become active",
() -> {
Key.StateEnum state =
kmsApi.getKey(projectId, REGION, keyRingId, keyId).getState();
if (state == Key.StateEnum.ACTIVE) {
return true;
}
if (state != Key.StateEnum.CREATING) {
throw new IllegalStateException("Unexpected key state: " + state);
}
return false;
});
}

private static void waitUntil(String description, ApiCondition condition)
throws ApiException, InterruptedException {
long start = System.nanoTime();
while (System.nanoTime() - start < WAIT_TIMEOUT_NANOS) {
if (condition.isSatisfied()) {
return;
}
TimeUnit.SECONDS.sleep(POLL_INTERVAL_SECONDS);
}
throw new IllegalStateException("Timed out waiting for " + description);
}

@FunctionalInterface
private interface ApiCondition {
boolean isSatisfied() throws ApiException;
}

private static void listKeyRings(KmsApi kmsApi, UUID projectId) throws ApiException {
System.out.println("List key rings:");
KeyRingList keyRings = kmsApi.listKeyRings(projectId, REGION);
for (KeyRing keyRing : Objects.requireNonNull(keyRings.getKeyRings())) {
UUID keyRingId = Objects.requireNonNull(keyRing.getId());
System.out.println("* Key Ring ID: " + keyRingId);
System.out.println("* Key Ring Name: " + keyRing.getDisplayName());
System.out.println("* Key Ring State: " + keyRing.getState());
listKeys(kmsApi, projectId, keyRingId);
}
}

private static void listKeys(KmsApi kmsApi, UUID projectId, UUID keyRingId)
throws ApiException {
KeyList keys = kmsApi.listKeys(projectId, REGION, keyRingId);
for (Key key : Objects.requireNonNull(keys.getKeys())) {
UUID keyId = Objects.requireNonNull(key.getId());
System.out.println(" * Key ID: " + keyId);
System.out.println(" * Key Name: " + key.getDisplayName());
System.out.println(" * Algorithm: " + key.getAlgorithm());
System.out.println(" * Purpose: " + key.getPurpose());
System.out.println(" * Key State: " + key.getState());
if (key.getState() == Key.StateEnum.DELETED) {
continue;
}

VersionList versions = kmsApi.listVersions(projectId, REGION, keyRingId, keyId);
for (Version version : Objects.requireNonNull(versions.getVersions())) {
System.out.println(" * Version Number: " + version.getNumber());
System.out.println(" * Version State: " + version.getState());
System.out.println(" * Disabled: " + version.getDisabled());
}
}
}
}
2 changes: 2 additions & 0 deletions services/kms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## v0.1.0
- Initial onboarding of STACKIT Java SDK for STACKIT Key Management Service (KMS)
80 changes: 80 additions & 0 deletions services/kms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# STACKIT Java SDK for STACKIT Key Management Service API

This package is part of the STACKIT Java SDK. For additional information, please visit the [GitHub repository](https://github.com/stackitcloud/stackit-sdk-java) of the SDK.

## Installation from Maven Central (recommended)

The release artifacts for this SDK submodule are available on [Maven Central](https://central.sonatype.com/artifact/cloud.stackit.sdk/kms).

### Maven users

Add this dependency to your project's POM:

```xml
<dependency>
<groupId>cloud.stackit.sdk</groupId>
<artifactId>kms</artifactId>
<version><SDK_VERSION></version>
<scope>compile</scope>
</dependency>
```

### Gradle users

Add this dependency to your project's build file:

```groovy
repositories {
mavenCentral()
}

dependencies {
implementation "cloud.stackit.sdk:kms:<SDK_VERSION>"
}
```

## Installation from local build

Building the API client library requires:
1. Java SDK (version 11 to 21 should be supported) installed on your system

To install the API client library to your local Maven repository, simply execute:

```shell
./gradlew services:kms:publishToMavenLocal
```

### Maven users

Add this dependency to your project's POM:

```xml
<dependency>
<groupId>cloud.stackit.sdk</groupId>
<artifactId>kms</artifactId>
<version><SDK_VERSION></version>
<scope>compile</scope>
</dependency>
```

### Gradle users

Add this dependency to your project's build file:

```groovy
repositories {
mavenLocal()
}

dependencies {
implementation "cloud.stackit.sdk:kms:<SDK_VERSION>"
}
```

## Getting Started

See the [kms examples](https://github.com/stackitcloud/stackit-sdk-java/tree/main/examples/kms/src/main/java/cloud/stackit/sdk/kms/examples).

## Recommendation

It's recommended to create an instance of `ApiClient` per thread in a multithreaded environment to avoid any potential issues.
1 change: 1 addition & 0 deletions services/kms/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
19 changes: 19 additions & 0 deletions services/kms/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

ext {
jakarta_annotation_version = "1.3.5"
}

dependencies {
implementation "com.google.code.findbugs:jsr305:3.0.2"
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'io.gsonfire:gson-fire:1.9.0'
implementation 'jakarta.ws.rs:jakarta.ws.rs-api:2.1.6'
implementation 'org.openapitools:jackson-databind-nullable:0.2.12'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.18.0'
implementation "jakarta.annotation:jakarta.annotation-api:$jakarta_annotation_version"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.3'
testImplementation 'org.mockito:mockito-core:3.12.4'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.3'
}
1 change: 1 addition & 0 deletions services/kms/oas_commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
08b95d7f2e3b9c288a1b5d1f1f6d13b0632b7ad0
Loading
Loading