Skip to content

Commit df99dea

Browse files
committed
extend AuthenticationExample
1 parent 44d23b5 commit df99dea

File tree

1 file changed

+96
-15
lines changed

1 file changed

+96
-15
lines changed

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

Lines changed: 96 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,104 @@
55
import cloud.stackit.sdk.resourcemanager.model.ListOrganizationsResponse;
66

77
class AuthenticationExample {
8-
public static void main(String[] args) {
9-
String SERVICE_ACCOUNT_KEY_PATH = "/path/to/your/sa/key.json";
10-
String SERVICE_ACCOUNT_MAIL = "name-1234@sa.stackit.cloud";
8+
public static void main(String[] args) {
9+
///////////////////////////////////////////////////////
10+
// Option 1: setting the paths to service account key (and private key) as configuration
11+
///////////////////////////////////////////////////////
12+
String SERVICE_ACCOUNT_KEY_PATH = "/path/to/sa_key.json";
13+
String PRIVATE_KEY_PATH = "/path/to/private_key.pem";
14+
String SERVICE_ACCOUNT_MAIL = "name-1234@sa.stackit.cloud";
1115

12-
CoreConfiguration config =
13-
new CoreConfiguration().serviceAccountKeyPath(SERVICE_ACCOUNT_KEY_PATH);
16+
try {
17+
ResourceManagerApi api = new ResourceManagerApi(
18+
new CoreConfiguration()
19+
.serviceAccountKeyPath(SERVICE_ACCOUNT_KEY_PATH)
20+
// Optional: if private key not included in service account key
21+
.privateKeyPath(PRIVATE_KEY_PATH)
22+
);
1423

15-
try {
16-
ResourceManagerApi api = new ResourceManagerApi(config);
24+
/* list all organizations */
25+
ListOrganizationsResponse response =
26+
api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null);
1727

18-
/* list all organizations */
19-
ListOrganizationsResponse response =
20-
api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null);
28+
System.out.println(response);
29+
} catch (Exception e) {
30+
throw new RuntimeException(e);
31+
}
2132

22-
System.out.println(response);
23-
} catch (Exception e) {
24-
throw new RuntimeException(e);
25-
}
26-
}
33+
///////////////////////////////////////////////////////
34+
// Option 2: setting the service account key (and private key) as configuration
35+
///////////////////////////////////////////////////////
36+
String SERVICE_ACCOUNT_KEY = "{\n" +
37+
" \"id\": \"uuid\",\n" +
38+
" \"publicKey\": \"public key\",\n" +
39+
" \"createdAt\": \"2023-08-24T14:15:22Z\",\n" +
40+
" \"validUntil\": \"2023-08-24T14:15:22Z\",\n" +
41+
" \"keyType\": \"USER_MANAGED\",\n" +
42+
" \"keyOrigin\": \"USER_PROVIDED\",\n" +
43+
" \"keyAlgorithm\": \"RSA_2048\",\n" +
44+
" \"active\": true,\n" +
45+
" \"credentials\": {\n" +
46+
" \"kid\": \"string\",\n" +
47+
" \"iss\": \"my-sa@sa.stackit.cloud\",\n" +
48+
" \"sub\": \"uuid\",\n" +
49+
" \"aud\": \"string\",\n" +
50+
" \"privateKey\": \"(OPTIONAL) private key when generated by the SA service\"\n" +
51+
" }\n" +
52+
" }";
53+
String PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n" +
54+
"MIIJQw...ZL+U\n" +
55+
"lm+dqO...xQ8=\n" +
56+
"-----END PRIVATE KEY-----";
57+
58+
try {
59+
ResourceManagerApi api = new ResourceManagerApi(
60+
new CoreConfiguration()
61+
.serviceAccountKey(SERVICE_ACCOUNT_KEY)
62+
// Optional: if private key not included in service account key
63+
.privateKeyPath(PRIVATE_KEY)
64+
);
65+
66+
/* list all organizations */
67+
ListOrganizationsResponse response =
68+
api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null);
69+
70+
System.out.println(response);
71+
} catch (Exception e) {
72+
throw new RuntimeException(e);
73+
}
74+
75+
///////////////////////////////////////////////////////
76+
// Option 3: setting the service account key (and private key) as environment variable
77+
///////////////////////////////////////////////////////
78+
// Set the service account key via environment variable:
79+
// - STACKIT_SERVICE_ACCOUNT_KEY_PATH=/path/to/sa_key.json
80+
// - STACKIT_SERVICE_ACCOUNT_KEY="<content of service account key>"
81+
//
82+
// If the private key is not included in the service account key, set also:
83+
// - STACKIT_PRIVATE_KEY_PATH=/path/to/private_key.pem
84+
// - STACKIT_PRIVATE_KEY="<content of private key>"
85+
//
86+
// If no environment variable is set, fallback to credentials file in "$HOME/.stackit/credentials.json".
87+
// Can be overridden with the environment variable `STACKIT_CREDENTIALS_PATH`
88+
// The credentials file must be a json:
89+
// {
90+
// "STACKIT_SERVICE_ACCOUNT_KEY_PATH": "path/to/sa_key.json",
91+
// "STACKIT_PRIVATE_KEY_PATH": "(OPTIONAL) when the private key isn't included in the Service Account key",
92+
// // Alternative:
93+
// "STACKIT_SERVICE_ACCOUNT_KEY": "<content of private key>",
94+
// "STACKIT_PRIVATE_KEY": "(OPTIONAL) when the private key isn't included in the Service Account key",
95+
// }
96+
try {
97+
ResourceManagerApi api = new ResourceManagerApi();
98+
99+
/* list all organizations */
100+
ListOrganizationsResponse response =
101+
api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null);
102+
103+
System.out.println(response);
104+
} catch (Exception e) {
105+
throw new RuntimeException(e);
106+
}
107+
}
27108
}

0 commit comments

Comments
 (0)