Skip to content

Commit 7e60121

Browse files
committed
Adjust resource manager to use CoreConfiguration for service account
1 parent ffae878 commit 7e60121

File tree

6 files changed

+74
-19
lines changed

6 files changed

+74
-19
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/src/main/java/cloud/stackit/sdk/resourcemanager/ApiClient.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,25 @@ public ApiClient(OkHttpClient client) {
118118
authentications = Collections.unmodifiableMap(authentications);
119119
}
120120

121-
protected void initHttpClient() {
122-
initHttpClient(Collections.<Interceptor>emptyList());
123-
}
121+
public ApiClient(CoreConfiguration config) throws IOException, InvalidKeySpecException, CredentialNotFoundException {
122+
init();
123+
124+
if (config.getCustomEndpoint() != null && !config.getCustomEndpoint().trim().isEmpty()) {
125+
basePath = config.getCustomEndpoint();
126+
}
127+
if (config.getDefaultHeader() != null) {
128+
defaultHeaderMap = config.getDefaultHeader();
129+
}
130+
SetupAuth auth;
131+
auth = new SetupAuth(config);
132+
List<Interceptor> interceptors = new LinkedList<>();
133+
interceptors.add(auth.getAuthHandler());
134+
initHttpClient(interceptors);
135+
}
136+
137+
protected void initHttpClient() {
138+
initHttpClient(Collections.<Interceptor>emptyList());
139+
}
124140

125141
protected void initHttpClient(List<Interceptor> interceptors) {
126142
OkHttpClient.Builder builder = new OkHttpClient.Builder();

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

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

15+
import cloud.stackit.sdk.core.config.CoreConfiguration;
1516
import cloud.stackit.sdk.resourcemanager.ApiCallback;
1617
import cloud.stackit.sdk.resourcemanager.ApiClient;
1718
import cloud.stackit.sdk.resourcemanager.ApiException;
@@ -26,12 +27,17 @@
2627
import cloud.stackit.sdk.resourcemanager.model.ListFoldersResponse;
2728
import cloud.stackit.sdk.resourcemanager.model.ListOrganizationsResponse;
2829
import cloud.stackit.sdk.resourcemanager.model.ListProjectsResponse;
30+
31+
import java.security.spec.InvalidKeySpecException;
32+
import java.time.OffsetDateTime;
2933
import cloud.stackit.sdk.resourcemanager.model.OrganizationResponse;
3034
import cloud.stackit.sdk.resourcemanager.model.PartialUpdateFolderPayload;
3135
import cloud.stackit.sdk.resourcemanager.model.PartialUpdateOrganizationPayload;
3236
import cloud.stackit.sdk.resourcemanager.model.PartialUpdateProjectPayload;
3337
import cloud.stackit.sdk.resourcemanager.model.Project;
3438
import com.google.gson.reflect.TypeToken;
39+
40+
import javax.security.auth.login.CredentialNotFoundException;
3541
import java.lang.reflect.Type;
3642
import java.math.BigDecimal;
3743
import java.time.OffsetDateTime;
@@ -53,9 +59,16 @@ public DefaultApi(ApiClient apiClient) {
5359
this.localVarApiClient = apiClient;
5460
}
5561

56-
public ApiClient getApiClient() {
57-
return localVarApiClient;
58-
}
62+
public DefaultApi(CoreConfiguration config) throws IOException, InvalidKeySpecException, CredentialNotFoundException {
63+
if (config.getCustomEndpoint() != null && !config.getCustomEndpoint().trim().isEmpty()) {
64+
localCustomBaseUrl = config.getCustomEndpoint();
65+
}
66+
this.localVarApiClient = new ApiClient(config);
67+
}
68+
69+
public ApiClient getApiClient() {
70+
return localVarApiClient;
71+
}
5972

6073
public void setApiClient(ApiClient apiClient) {
6174
this.localVarApiClient = apiClient;
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)