Skip to content

Commit 8ef5121

Browse files
committed
Adjust resource manager to use CoreConfiguration for service account
1 parent 4819888 commit 8ef5121

File tree

7 files changed

+71
-14
lines changed

7 files changed

+71
-14
lines changed

core/src/main/java/cloud/stackit/sdk/core/KeyFlowAuthenticator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cloud.stackit.sdk.core;
22

3-
import cloud.stackit.sdk.core.config.Configuration;
3+
import cloud.stackit.sdk.core.config.CoreConfiguration;
44
import cloud.stackit.sdk.core.model.ServiceAccountKey;
55
import com.auth0.jwt.JWT;
66
import com.auth0.jwt.algorithms.Algorithm;
@@ -66,7 +66,7 @@ public String getAccessToken() {
6666
* @throws InvalidKeySpecException Throws, when the private key in the service account can not be parsed
6767
* @throws IOException Throws, when on unexpected responses from the key flow
6868
*/
69-
public KeyFlowAuthenticator(Configuration cfg, ServiceAccountKey saKey) throws InvalidKeySpecException, IOException {
69+
public KeyFlowAuthenticator(CoreConfiguration cfg, ServiceAccountKey saKey) throws InvalidKeySpecException, IOException {
7070
this.saKey = saKey;
7171
this.gson = new Gson();
7272
this.httpClient = new OkHttpClient.Builder()

core/src/main/java/cloud/stackit/sdk/core/auth/SetupAuth.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cloud.stackit.sdk.core.auth;
22

33
import cloud.stackit.sdk.core.KeyFlowAuthenticator;
4-
import cloud.stackit.sdk.core.config.Configuration;
4+
import cloud.stackit.sdk.core.config.CoreConfiguration;
55
import cloud.stackit.sdk.core.config.EnvironmentVariables;
66
import cloud.stackit.sdk.core.KeyFlowInterceptor;
77
import cloud.stackit.sdk.core.model.ServiceAccountKey;
@@ -37,7 +37,7 @@ public class SetupAuth {
3737
* @throws InvalidKeySpecException when the private key can not be parsed
3838
*/
3939
public SetupAuth() throws IOException, InvalidKeySpecException, CredentialNotFoundException {
40-
this(new Configuration.Builder().build());
40+
this(new CoreConfiguration.Builder().build());
4141
}
4242

4343
/**
@@ -47,9 +47,9 @@ public SetupAuth() throws IOException, InvalidKeySpecException, CredentialNotFou
4747
* @throws CredentialNotFoundException when no configuration is set or can be found
4848
* @throws InvalidKeySpecException when the private key can not be parsed
4949
*/
50-
public SetupAuth(Configuration cfg) throws IOException, CredentialNotFoundException, InvalidKeySpecException {
50+
public SetupAuth(CoreConfiguration cfg) throws IOException, CredentialNotFoundException, InvalidKeySpecException {
5151
if (cfg == null) {
52-
cfg = new Configuration.Builder().build();
52+
cfg = new CoreConfiguration.Builder().build();
5353
}
5454

5555
ServiceAccountKey saKey = setupKeyFlow(cfg);
@@ -93,7 +93,7 @@ public Interceptor getAuthHandler() {
9393
* @throws CredentialNotFoundException throws error when no service account key or private key can be found
9494
* @throws IOException throws an error if a file can not be found
9595
*/
96-
private ServiceAccountKey setupKeyFlow(Configuration cfg) throws CredentialNotFoundException, IOException {
96+
private ServiceAccountKey setupKeyFlow(CoreConfiguration cfg) throws CredentialNotFoundException, IOException {
9797
// Explicit config in code
9898
if (cfg.getServiceAccountKey() != null && !cfg.getServiceAccountKey().trim().isEmpty()) {
9999
ServiceAccountKey saKey = ServiceAccountKey.loadFromJson(cfg.getServiceAccountKey());
@@ -135,7 +135,7 @@ private ServiceAccountKey setupKeyFlow(Configuration cfg) throws CredentialNotFo
135135
}
136136
}
137137

138-
private void loadPrivateKey(Configuration cfg, ServiceAccountKey saKey) throws CredentialNotFoundException {
138+
private void loadPrivateKey(CoreConfiguration cfg, ServiceAccountKey saKey) throws CredentialNotFoundException {
139139
if (!saKey.getCredentials().isPrivateKeySet()) {
140140
try {
141141
String privateKey = getPrivateKey(cfg);
@@ -178,7 +178,7 @@ private void loadPrivateKey(Configuration cfg, ServiceAccountKey saKey) throws C
178178
* @throws CredentialNotFoundException throws if no private key could be found
179179
* @throws IOException throws if the provided path can not be found or the file within the pathKey can not be found
180180
*/
181-
private String getPrivateKey(Configuration cfg) throws CredentialNotFoundException, IOException {
181+
private String getPrivateKey(CoreConfiguration cfg) throws CredentialNotFoundException, IOException {
182182
// Explicit code config
183183
// Set private key
184184
if (cfg.getPrivateKey() != null && !cfg.getPrivateKey().trim().isEmpty()) {

core/src/main/java/cloud/stackit/sdk/core/config/Configuration.java renamed to core/src/main/java/cloud/stackit/sdk/core/config/CoreConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.Map;
44

5-
public class Configuration {
5+
public class CoreConfiguration {
66
private final Map<String, String> defaultHeader;
77
private final String serviceAccountKey;
88
private final String serviceAccountKeyPath;
@@ -13,7 +13,7 @@ public class Configuration {
1313
private final String tokenCustomUrl;
1414
private final Long tokenExpirationLeeway;
1515

16-
Configuration(Builder builder) {
16+
CoreConfiguration(Builder builder) {
1717
this.defaultHeader = builder.defaultHeader;
1818
this.serviceAccountKey = builder.serviceAccountKey;
1919
this.serviceAccountKeyPath = builder.serviceAccountKeyPath;
@@ -117,8 +117,8 @@ public Builder tokenExpirationLeeway(Long tokenExpirationLeeway) {
117117
return this;
118118
}
119119

120-
public Configuration build() {
121-
return new Configuration(this);
120+
public CoreConfiguration build() {
121+
return new CoreConfiguration(this);
122122
}
123123
}
124124
}

services/resourcemanager/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ ext {
106106
}
107107

108108
dependencies {
109+
implementation 'com.auth0:java-jwt:4.5.0'
109110
implementation "com.google.code.findbugs:jsr305:3.0.2"
110111
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
111112
implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'

services/resourcemanager/src/main/java/cloud/stackit/sdk/resourcemanager/ApiClient.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package cloud.stackit.sdk.resourcemanager;
1515

16+
import cloud.stackit.sdk.core.auth.SetupAuth;
17+
import cloud.stackit.sdk.core.config.CoreConfiguration;
1618
import okhttp3.*;
1719
import okhttp3.internal.http.HttpMethod;
1820
import okhttp3.internal.tls.OkHostnameVerifier;
@@ -23,6 +25,7 @@
2325
import okio.Okio;
2426

2527
import javax.net.ssl.*;
28+
import javax.security.auth.login.CredentialNotFoundException;
2629
import java.io.File;
2730
import java.io.IOException;
2831
import java.io.InputStream;
@@ -39,7 +42,7 @@
3942
import java.security.cert.Certificate;
4043
import java.security.cert.CertificateException;
4144
import java.security.cert.CertificateFactory;
42-
import java.security.cert.X509Certificate;
45+
import java.security.spec.InvalidKeySpecException;
4346
import java.text.DateFormat;
4447
import java.time.LocalDate;
4548
import java.time.OffsetDateTime;
@@ -126,6 +129,22 @@ public ApiClient(OkHttpClient client) {
126129
authentications = Collections.unmodifiableMap(authentications);
127130
}
128131

132+
public ApiClient(CoreConfiguration config) throws IOException, InvalidKeySpecException, CredentialNotFoundException {
133+
init();
134+
135+
if (config.getCustomEndpoint() != null && !config.getCustomEndpoint().trim().isEmpty()) {
136+
basePath = config.getCustomEndpoint();
137+
}
138+
if (config.getDefaultHeader() != null) {
139+
defaultHeaderMap = config.getDefaultHeader();
140+
}
141+
SetupAuth auth;
142+
auth = new SetupAuth(config);
143+
List<Interceptor> interceptors = new LinkedList<>();
144+
interceptors.add(auth.getAuthHandler());
145+
initHttpClient(interceptors);
146+
}
147+
129148
protected void initHttpClient() {
130149
initHttpClient(Collections.<Interceptor>emptyList());
131150
}

services/resourcemanager/src/main/java/cloud/stackit/sdk/resourcemanager/api/DefaultApi.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package cloud.stackit.sdk.resourcemanager.api;
1515

16+
import cloud.stackit.sdk.core.config.CoreConfiguration;
1617
import cloud.stackit.sdk.resourcemanager.ApiCallback;
1718
import cloud.stackit.sdk.resourcemanager.ApiClient;
1819
import cloud.stackit.sdk.resourcemanager.ApiException;
@@ -37,13 +38,16 @@
3738
import cloud.stackit.sdk.resourcemanager.model.ListFoldersResponse;
3839
import cloud.stackit.sdk.resourcemanager.model.ListOrganizationsResponse;
3940
import cloud.stackit.sdk.resourcemanager.model.ListProjectsResponse;
41+
42+
import java.security.spec.InvalidKeySpecException;
4043
import java.time.OffsetDateTime;
4144
import cloud.stackit.sdk.resourcemanager.model.OrganizationResponse;
4245
import cloud.stackit.sdk.resourcemanager.model.PartialUpdateFolderPayload;
4346
import cloud.stackit.sdk.resourcemanager.model.PartialUpdateOrganizationPayload;
4447
import cloud.stackit.sdk.resourcemanager.model.PartialUpdateProjectPayload;
4548
import cloud.stackit.sdk.resourcemanager.model.Project;
4649

50+
import javax.security.auth.login.CredentialNotFoundException;
4751
import java.lang.reflect.Type;
4852
import java.util.ArrayList;
4953
import java.util.HashMap;
@@ -63,6 +67,13 @@ public DefaultApi(ApiClient apiClient) {
6367
this.localVarApiClient = apiClient;
6468
}
6569

70+
public DefaultApi(CoreConfiguration config) throws IOException, InvalidKeySpecException, CredentialNotFoundException {
71+
if (config.getCustomEndpoint() != null && !config.getCustomEndpoint().trim().isEmpty()) {
72+
localCustomBaseUrl = config.getCustomEndpoint();
73+
}
74+
this.localVarApiClient = new ApiClient(config);
75+
}
76+
6677
public ApiClient getApiClient() {
6778
return localVarApiClient;
6879
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cloud.stackit.sdk.resourcemanager;
2+
3+
import cloud.stackit.sdk.core.config.CoreConfiguration;
4+
import cloud.stackit.sdk.resourcemanager.api.DefaultApi;
5+
import cloud.stackit.sdk.resourcemanager.model.ListOrganizationsResponse;
6+
7+
import javax.security.auth.login.CredentialNotFoundException;
8+
import java.io.IOException;
9+
import java.security.spec.InvalidKeySpecException;
10+
11+
public class main {
12+
public static void main(String[] args) {
13+
String SERVICE_ACCOUNT_KEY_PATH = "/path/to/your/sa/key.json";
14+
String SERIVCE_ACCOUNT_MAIL = "name-1234@sa.stackit.cloud";
15+
16+
CoreConfiguration config = new CoreConfiguration
17+
.Builder()
18+
.serviceAccountKeyPath(SERVICE_ACCOUNT_KEY_PATH)
19+
.build();
20+
DefaultApi api = new DefaultApi(config);
21+
22+
ListOrganizationsResponse response = api.listOrganizations(null, SERIVCE_ACCOUNT_MAIL, null, null, null);
23+
24+
System.out.println(response);
25+
}
26+
}

0 commit comments

Comments
 (0)