Skip to content

Commit 4dc8a7a

Browse files
committed
rework AuthenticationExample option 2
1 parent b1f0763 commit 4dc8a7a

File tree

4 files changed

+85
-50
lines changed

4 files changed

+85
-50
lines changed

README.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ This repository contains the STACKIT SDKs for Java.
1616

1717
Requires Java 8 or higher.
1818

19-
The release artifacts of the STACKIT Java SDK are available on [Maven Central](https://central.sonatype.com/namespace/cloud.stackit.sdk).
19+
The release artifacts of the STACKIT Java SDK are available on [Maven Central](https://central.sonatype.com/namespace/cloud.stackit.sdk).
2020
See below how to use them in your Java project.
2121

2222
### Maven
2323

24-
Add the dependencies for the services you want to interact with to your project's POM, e.g. `iaas` and `resourcemanager` (replace `<SDK_VERSION>` with the latest version of each SDK submdoule):
24+
Add the dependencies for the services you want to interact with to your project's POM, e.g. `iaas` and `resourcemanager` (replace `<SDK_VERSION>` with the latest version of each SDK submdoule):
2525

2626
```xml
2727
<dependency>
@@ -97,31 +97,33 @@ This will make it much easier to configure the key flow authentication in the SD
9797
To configure the key flow, follow this steps:
9898

9999
1. Create a service account key:
100-
- Use the STACKIT Portal: go to the `Service Accounts` tab, choose a `Service Account` and go to `Service Account Keys` to create a key. For more details, see [Create a service account key](https://docs.stackit.cloud/stackit/en/create-a-service-account-key-175112456.html).
100+
- Use the STACKIT Portal: go to the `Service Accounts` tab, choose a `Service Account` and go to `Service Account Keys` to create a key. For more details, see [Create a service account key](https://docs.stackit.cloud/stackit/en/create-a-service-account-key-175112456.html).
101101
2. Save the content of the service account key by copying it and saving it in a JSON file. The expected format of the service account key is **JSON** with the following structure:
102102

103-
```json
104-
{
105-
"id": "uuid",
106-
"publicKey": "public key",
107-
"createdAt": "2023-08-24T14:15:22Z",
108-
"validUntil": "2023-08-24T14:15:22Z",
109-
"keyType": "USER_MANAGED",
110-
"keyOrigin": "USER_PROVIDED",
111-
"keyAlgorithm": "RSA_2048",
112-
"active": true,
113-
"credentials": {
114-
"kid": "string",
115-
116-
"sub": "uuid",
117-
"aud": "string",
118-
"privateKey": "(OPTIONAL) private key when generated by the SA service"
119-
}
120-
}
121-
```
103+
```json
104+
{
105+
"id": "uuid",
106+
"publicKey": "public key",
107+
"createdAt": "2023-08-24T14:15:22Z",
108+
"validUntil": "2023-08-24T14:15:22Z",
109+
"keyType": "USER_MANAGED",
110+
"keyOrigin": "USER_PROVIDED",
111+
"keyAlgorithm": "RSA_2048",
112+
"active": true,
113+
"credentials": {
114+
"kid": "string",
115+
116+
"sub": "uuid",
117+
"aud": "string",
118+
"privateKey": "(OPTIONAL) private key when generated by the SA service"
119+
}
120+
}
121+
```
122122

123123
3. Configure the service account key for authentication in the SDK by following one of the alternatives below:
124+
124125
- using the configuration options:
126+
125127
```java
126128
CoreConfiguration config =
127129
new CoreConfiguration()
@@ -130,6 +132,7 @@ To configure the key flow, follow this steps:
130132

131133
ResourceManagerApi api = new ResourceManagerApi(config);
132134
```
135+
133136
- setting the environment variable: `STACKIT_SERVICE_ACCOUNT_KEY_PATH`
134137
- setting `STACKIT_SERVICE_ACCOUNT_KEY_PATH` in the credentials file (see above)
135138

@@ -145,11 +148,11 @@ To configure the key flow, follow this steps:
145148
> - setting the environment variable: `STACKIT_PRIVATE_KEY_PATH`
146149
> - setting `STACKIT_PRIVATE_KEY_PATH` in the credentials file (see above)
147150

148-
> **Alternatively, if you can't store the credentials in a file, e.g. when using it in a pipeline**, you can store the credentials in environment variables:
151+
> **Alternatively, if you can't store the credentials in a file, e.g. when using it in a pipeline**, you can store the credentials in environment variables:
152+
>
149153
> - setting the environment variable `STACKIT_SERVICE_ACCOUNT_KEY` with the content of the service account key
150154
> - (OPTIONAL) setting the environment variable `STACKIT_PRIVATE_KEY` with the content of the private key
151155
152-
153156
4. The SDK will search for the keys and, if valid, will use them to get access and refresh tokens which will be used to authenticate all the requests.
154157
155158
Check the [authentication example](examples/authentication/src/main/java/cloud/stackit/sdk/authentication/examples/AuthenticationExample.java) for more details.
@@ -166,7 +169,7 @@ import cloud.stackit.sdk.resourcemanager.model.ListOrganizationsResponse;
166169
import java.io.IOException;
167170
168171
class CustomEndpointExample {
169-
public static void main(String[] args) {
172+
public static void main(String[] args) {
170173
CoreConfiguration config =
171174
new CoreConfiguration()
172175
.serviceAccountKey("/path/to/sa_key.json")

examples/authentication/src/main/java/cloud/stackit/sdk/authentication/examples/AuthenticationExample.java

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import cloud.stackit.sdk.core.config.CoreConfiguration;
44
import cloud.stackit.sdk.resourcemanager.api.ResourceManagerApi;
55
import cloud.stackit.sdk.resourcemanager.model.ListOrganizationsResponse;
6+
import java.io.File;
7+
import java.io.FileNotFoundException;
8+
import java.io.IOException;
9+
import java.util.Scanner;
610

711
class AuthenticationExample {
8-
public static void main(String[] args) {
12+
public static void main(String[] args) throws IOException {
913
///////////////////////////////////////////////////////
1014
// Option 1: setting the paths to service account key (and private key) as configuration
1115
///////////////////////////////////////////////////////
@@ -33,37 +37,43 @@ public static void main(String[] args) {
3337
///////////////////////////////////////////////////////
3438
// Option 2: setting the service account key (and private key) as configuration
3539
///////////////////////////////////////////////////////
36-
String SERVICE_ACCOUNT_KEY =
37-
"{\n"
38-
+ " \"id\": \"uuid\",\n"
39-
+ " \"publicKey\": \"public key\",\n"
40-
+ " \"createdAt\": \"2023-08-24T14:15:22Z\",\n"
41-
+ " \"validUntil\": \"2023-08-24T14:15:22Z\",\n"
42-
+ " \"keyType\": \"USER_MANAGED\",\n"
43-
+ " \"keyOrigin\": \"USER_PROVIDED\",\n"
44-
+ " \"keyAlgorithm\": \"RSA_2048\",\n"
45-
+ " \"active\": true,\n"
46-
+ " \"credentials\": {\n"
47-
+ " \"kid\": \"string\",\n"
48-
+ " \"iss\": \"[email protected]\",\n"
49-
+ " \"sub\": \"uuid\",\n"
50-
+ " \"aud\": \"string\",\n"
51-
+ " \"privateKey\": \"(OPTIONAL) private key when generated by the SA service\"\n"
52-
+ " }\n"
53-
+ " }";
54-
String PRIVATE_KEY =
55-
"-----BEGIN PRIVATE KEY-----\n"
56-
+ "MIIJQw...ZL+U\n"
57-
+ "lm+dqO...xQ8=\n"
58-
+ "-----END PRIVATE KEY-----";
40+
41+
// read key content from a file, in production you can also read it e.g. from STACKIT
42+
// secrets manager, so it's only kept in-memory
43+
String serviceAccountKeyPath =
44+
"examples/authentication/src/main/java/cloud/stackit/sdk/authentication/examples/dummy_credentials/dummy-service-account-key.json";
45+
File serviceAccountKeyFile = new File(serviceAccountKeyPath);
46+
StringBuilder serviceAccountKeyContent = new StringBuilder();
47+
try (Scanner myReader = new Scanner(serviceAccountKeyFile)) {
48+
while (myReader.hasNextLine()) {
49+
serviceAccountKeyContent.append(myReader.nextLine());
50+
}
51+
} catch (FileNotFoundException e) {
52+
throw new RuntimeException(e);
53+
}
54+
55+
String privateKeyPath =
56+
"examples/authentication/src/main/java/cloud/stackit/sdk/authentication/examples/dummy_credentials/dummy-private-key.pem";
57+
File privateKeyFile = new File(privateKeyPath);
58+
StringBuilder privateKeyContent = new StringBuilder();
59+
try (Scanner myReader = new Scanner(privateKeyFile)) {
60+
while (myReader.hasNextLine()) {
61+
privateKeyContent.append(myReader.nextLine());
62+
}
63+
} catch (FileNotFoundException e) {
64+
throw new RuntimeException(e);
65+
}
66+
67+
String SERVICE_ACCOUNT_KEY = serviceAccountKeyContent.toString();
68+
String PRIVATE_KEY = privateKeyContent.toString();
5969

6070
try {
6171
ResourceManagerApi api =
6272
new ResourceManagerApi(
6373
new CoreConfiguration()
6474
.serviceAccountKey(SERVICE_ACCOUNT_KEY)
6575
// Optional: if private key not included in service account key
66-
.privateKeyPath(PRIVATE_KEY));
76+
.privateKey(PRIVATE_KEY));
6777

6878
/* list all organizations */
6979
ListOrganizationsResponse response =
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIJQw...ZL+U
3+
...
4+
lm+dqO...xQ8=
5+
-----END PRIVATE KEY-----
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "uuid",
3+
"publicKey": "public key",
4+
"createdAt": "2023-08-24T14:15:22Z",
5+
"validUntil": "2023-08-24T14:15:22Z",
6+
"keyType": "USER_MANAGED",
7+
"keyOrigin": "USER_PROVIDED",
8+
"keyAlgorithm": "RSA_2048",
9+
"active": true,
10+
"credentials": {
11+
"kid": "string",
12+
13+
"sub": "uuid",
14+
"aud": "string",
15+
"privateKey": "(OPTIONAL) private key when generated by the SA service"
16+
}
17+
}

0 commit comments

Comments
 (0)