Skip to content

Commit cee1347

Browse files
committed
feat(kms): onboard kms service
STACKITSDK-610
1 parent 4798168 commit cee1347

50 files changed

Lines changed: 18136 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Release (2026-MM-DD)
22

3+
- `kms`:
4+
- [v0.1.0](services/kms/CHANGELOG.md#v010)
5+
- Initial onboarding of STACKIT Java SDK for STACKIT Key Management Service (KMS)
36
- `automation`:
47
- [v0.1.1](services/automation/CHANGELOG.md#v011)
58
- **Dependencies:** Bump `org.openapitools:jackson-databind-nullable` from `0.2.8` to `0.2.12`

‎examples/kms/build.gradle‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dependencies {
2+
implementation project (':services:kms')
3+
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
4+
}
5+
6+
ext.mainClassName = 'cloud.stackit.sdk.kms.examples.KmsExample'
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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("key scheduled for deletion, delete keyring manually");
99+
}
100+
// key ring can only be deleted if no key exists.
101+
if (keyRingID != null && keyID == null) {
102+
System.out.println("deleting key ring");
103+
kmsApi.deleteKeyRing(projectId, REGION, keyRingID);
104+
System.out.println("key ring deleted");
105+
}
106+
}
107+
}
108+
109+
private static void waitForKeyring(KmsApi kmsApi, UUID projectId, UUID keyRingId)
110+
throws ApiException, InterruptedException {
111+
waitUntil(
112+
"key ring " + keyRingId + " to become active",
113+
() -> {
114+
KeyRing.StateEnum state =
115+
kmsApi.getKeyRing(projectId, REGION, keyRingId).getState();
116+
if (state == KeyRing.StateEnum.ACTIVE) {
117+
return true;
118+
}
119+
if (state != KeyRing.StateEnum.CREATING) {
120+
throw new IllegalStateException("Unexpected key ring state: " + state);
121+
}
122+
return false;
123+
});
124+
}
125+
126+
private static void waitForKey(KmsApi kmsApi, UUID projectId, UUID keyRingId, UUID keyId)
127+
throws ApiException, InterruptedException {
128+
waitUntil(
129+
"key " + keyId + " to become active",
130+
() -> {
131+
Key.StateEnum state =
132+
kmsApi.getKey(projectId, REGION, keyRingId, keyId).getState();
133+
if (state == Key.StateEnum.ACTIVE) {
134+
return true;
135+
}
136+
if (state != Key.StateEnum.CREATING) {
137+
throw new IllegalStateException("Unexpected key state: " + state);
138+
}
139+
return false;
140+
});
141+
}
142+
143+
private static void waitUntil(String description, ApiCondition condition)
144+
throws ApiException, InterruptedException {
145+
long start = System.nanoTime();
146+
while (System.nanoTime() - start < WAIT_TIMEOUT_NANOS) {
147+
if (condition.isSatisfied()) {
148+
return;
149+
}
150+
TimeUnit.SECONDS.sleep(POLL_INTERVAL_SECONDS);
151+
}
152+
throw new IllegalStateException("Timed out waiting for " + description);
153+
}
154+
155+
@FunctionalInterface
156+
private interface ApiCondition {
157+
boolean isSatisfied() throws ApiException;
158+
}
159+
160+
private static void listKeyRings(KmsApi kmsApi, UUID projectId) throws ApiException {
161+
System.out.println("List key rings:");
162+
KeyRingList keyRings = kmsApi.listKeyRings(projectId, REGION);
163+
for (KeyRing keyRing : Objects.requireNonNull(keyRings.getKeyRings())) {
164+
UUID keyRingId = Objects.requireNonNull(keyRing.getId());
165+
System.out.println("* Key Ring ID: " + keyRingId);
166+
System.out.println("* Key Ring Name: " + keyRing.getDisplayName());
167+
System.out.println("* Key Ring State: " + keyRing.getState());
168+
listKeys(kmsApi, projectId, keyRingId);
169+
}
170+
}
171+
172+
private static void listKeys(KmsApi kmsApi, UUID projectId, UUID keyRingId)
173+
throws ApiException {
174+
KeyList keys = kmsApi.listKeys(projectId, REGION, keyRingId);
175+
for (Key key : Objects.requireNonNull(keys.getKeys())) {
176+
UUID keyId = Objects.requireNonNull(key.getId());
177+
System.out.println(" * Key ID: " + keyId);
178+
System.out.println(" * Key Name: " + key.getDisplayName());
179+
System.out.println(" * Algorithm: " + key.getAlgorithm());
180+
System.out.println(" * Purpose: " + key.getPurpose());
181+
System.out.println(" * Key State: " + key.getState());
182+
if (key.getState() == Key.StateEnum.DELETED) {
183+
continue;
184+
}
185+
186+
VersionList versions = kmsApi.listVersions(projectId, REGION, keyRingId, keyId);
187+
for (Version version : Objects.requireNonNull(versions.getVersions())) {
188+
System.out.println(" * Version Number: " + version.getNumber());
189+
System.out.println(" * Version State: " + version.getState());
190+
System.out.println(" * Disabled: " + version.getDisabled());
191+
}
192+
}
193+
}
194+
}

‎services/kms/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## v0.1.0
2+
- Initial onboarding of STACKIT Java SDK for STACKIT Key Management Service (KMS)

‎services/kms/README.md‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# STACKIT Java SDK for STACKIT Key Management Service API
2+
3+
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.
4+
5+
## Installation from Maven Central (recommended)
6+
7+
The release artifacts for this SDK submodule are available on [Maven Central](https://central.sonatype.com/artifact/cloud.stackit.sdk/kms).
8+
9+
### Maven users
10+
11+
Add this dependency to your project's POM:
12+
13+
```xml
14+
<dependency>
15+
<groupId>cloud.stackit.sdk</groupId>
16+
<artifactId>kms</artifactId>
17+
<version><SDK_VERSION></version>
18+
<scope>compile</scope>
19+
</dependency>
20+
```
21+
22+
### Gradle users
23+
24+
Add this dependency to your project's build file:
25+
26+
```groovy
27+
repositories {
28+
mavenCentral()
29+
}
30+
31+
dependencies {
32+
implementation "cloud.stackit.sdk:kms:<SDK_VERSION>"
33+
}
34+
```
35+
36+
## Installation from local build
37+
38+
Building the API client library requires:
39+
1. Java SDK (version 11 to 21 should be supported) installed on your system
40+
41+
To install the API client library to your local Maven repository, simply execute:
42+
43+
```shell
44+
./gradlew services:kms:publishToMavenLocal
45+
```
46+
47+
### Maven users
48+
49+
Add this dependency to your project's POM:
50+
51+
```xml
52+
<dependency>
53+
<groupId>cloud.stackit.sdk</groupId>
54+
<artifactId>kms</artifactId>
55+
<version><SDK_VERSION></version>
56+
<scope>compile</scope>
57+
</dependency>
58+
```
59+
60+
### Gradle users
61+
62+
Add this dependency to your project's build file:
63+
64+
```groovy
65+
repositories {
66+
mavenLocal()
67+
}
68+
69+
dependencies {
70+
implementation "cloud.stackit.sdk:kms:<SDK_VERSION>"
71+
}
72+
```
73+
74+
## Getting Started
75+
76+
See the [kms examples](https://github.com/stackitcloud/stackit-sdk-java/tree/main/examples/kms/src/main/java/cloud/stackit/sdk/kms/examples).
77+
78+
## Recommendation
79+
80+
It's recommended to create an instance of `ApiClient` per thread in a multithreaded environment to avoid any potential issues.

‎services/kms/VERSION‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.0

‎services/kms/build.gradle‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
ext {
3+
jakarta_annotation_version = "1.3.5"
4+
}
5+
6+
dependencies {
7+
implementation "com.google.code.findbugs:jsr305:3.0.2"
8+
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
9+
implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'
10+
implementation 'com.google.code.gson:gson:2.9.1'
11+
implementation 'io.gsonfire:gson-fire:1.9.0'
12+
implementation 'jakarta.ws.rs:jakarta.ws.rs-api:2.1.6'
13+
implementation 'org.openapitools:jackson-databind-nullable:0.2.12'
14+
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.18.0'
15+
implementation "jakarta.annotation:jakarta.annotation-api:$jakarta_annotation_version"
16+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.3'
17+
testImplementation 'org.mockito:mockito-core:3.12.4'
18+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.3'
19+
}

‎services/kms/oas_commit‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
08b95d7f2e3b9c288a1b5d1f1f6d13b0632b7ad0
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* STACKIT Key Management Service API
3+
* This API provides endpoints for managing keys and key rings.
4+
*
5+
* The version of the OpenAPI document: 1.0.0
6+
*
7+
*
8+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9+
* https://openapi-generator.tech
10+
* Do not edit the class manually.
11+
*/
12+
13+
package cloud.stackit.sdk.kms.v1api;
14+
15+
import cloud.stackit.sdk.core.exception.ApiException;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
/**
20+
* Callback for asynchronous API call.
21+
*
22+
* @param <T> The return type
23+
*/
24+
public interface ApiCallback<T> {
25+
/**
26+
* This is called when the API call fails.
27+
*
28+
* @param e The exception causing the failure
29+
* @param statusCode Status code of the response if available, otherwise it would be 0
30+
* @param responseHeaders Headers of the response if available, otherwise it would be null
31+
*/
32+
void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders);
33+
34+
/**
35+
* This is called when the API call succeeded.
36+
*
37+
* @param result The result deserialized from response
38+
* @param statusCode Status code of the response
39+
* @param responseHeaders Headers of the response
40+
*/
41+
void onSuccess(T result, int statusCode, Map<String, List<String>> responseHeaders);
42+
43+
/**
44+
* This is called when the API upload processing.
45+
*
46+
* @param bytesWritten bytes Written
47+
* @param contentLength content length of request body
48+
* @param done write end
49+
*/
50+
void onUploadProgress(long bytesWritten, long contentLength, boolean done);
51+
52+
/**
53+
* This is called when the API download processing.
54+
*
55+
* @param bytesRead bytes Read
56+
* @param contentLength content length of the response
57+
* @param done Read end
58+
*/
59+
void onDownloadProgress(long bytesRead, long contentLength, boolean done);
60+
}

0 commit comments

Comments
 (0)