Skip to content

Commit bec9223

Browse files
committed
wip: add tests
1 parent 6a63a9f commit bec9223

File tree

7 files changed

+395
-23
lines changed

7 files changed

+395
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private String generateSelfSignedJWT() throws InvalidKeySpecException, NoSuchAlg
178178

179179
return JWT.create()
180180
.withIssuer(saKey.getCredentials().getIss())
181-
.withSubject(saKey.getCredentials().getSub().toString())
181+
.withSubject(saKey.getCredentials().getSub())
182182
.withJWTId(UUID.randomUUID().toString())
183183
.withAudience(saKey.getCredentials().getAud())
184184
.withIssuedAt(new Date())

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public class SetupAuth {
3636
* Set up the KeyFlow Authentication and can be integrated in an OkHttp client, by adding `SetupAuth().getAuthHandler()` as interceptor.
3737
* This relies on the configuration methods via ENVs or the credentials file in `$HOME/.stackit/credentials.json`
3838
* @throws IOException when a file can be found
39-
* @throws CredentialNotFoundException when no configuration is set or can be found
39+
* @throws CredentialsInFileNotFoundException when no configuration is set or can be found
4040
* @throws InvalidKeySpecException when the private key can not be parsed
4141
* @throws ApiException when access token creation failed
4242
*/
43-
public SetupAuth() throws IOException, InvalidKeySpecException, CredentialNotFoundException, ApiException {
43+
public SetupAuth() throws IOException, InvalidKeySpecException, CredentialsInFileNotFoundException, ApiException {
4444
this(new CoreConfiguration.Builder().build());
4545
}
4646

core/src/main/java/cloud/stackit/sdk/core/model/ServiceAccountCredentials.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public class ServiceAccountCredentials {
1313
private final String iss;
1414
private final String kid;
1515
private String privateKey;
16-
private final UUID sub;
16+
private final String sub;
1717

18-
public ServiceAccountCredentials(String aud, String iss, String kid, String privateKey, UUID sub) {
18+
public ServiceAccountCredentials(String aud, String iss, String kid, String privateKey, String sub) {
1919
this.aud = aud;
2020
this.iss = iss;
2121
this.kid = kid;
@@ -47,7 +47,7 @@ public boolean isPrivateKeySet() {
4747
return privateKey != null && !privateKey.trim().isEmpty();
4848
}
4949

50-
public UUID getSub() {
50+
public String getSub() {
5151
return sub;
5252
}
5353

core/src/main/java/cloud/stackit/sdk/core/model/ServiceAccountKey.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
import com.google.gson.Gson;
44
import com.google.gson.JsonSyntaxException;
55

6-
import java.security.KeyFactory;
7-
import java.security.NoSuchAlgorithmException;
8-
import java.security.interfaces.RSAPublicKey;
9-
import java.security.spec.InvalidKeySpecException;
10-
import java.security.spec.X509EncodedKeySpec;
11-
import java.util.Base64;
126
import java.util.Date;
137

148
public class ServiceAccountKey {
@@ -70,17 +64,6 @@ public ServiceAccountCredentials getCredentials() {
7064
return credentials;
7165
}
7266

73-
public RSAPublicKey getPublicKeyParsed() throws NoSuchAlgorithmException, InvalidKeySpecException {
74-
String trimmedKey = publicKey.replaceFirst("-----BEGIN PUBLIC KEY-----", "");
75-
trimmedKey = trimmedKey.replaceFirst("-----END PUBLIC KEY-----", "");
76-
trimmedKey = trimmedKey.replaceAll("\n","");
77-
78-
byte[] publicBytes = Base64.getDecoder().decode(trimmedKey);
79-
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
80-
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
81-
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
82-
}
83-
8467
public static ServiceAccountKey loadFromJson(String json) throws JsonSyntaxException {
8568
ServiceAccountKey saKey = new Gson().fromJson(json, ServiceAccountKey.class);
8669
if (!saKey.isCredentialsSet()) {
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package cloud.stackit.sdk.core.config;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
class CoreConfigurationTest {
11+
12+
@Test
13+
void getDefaultHeader() {
14+
HashMap<String, String> map = new HashMap<String, String>();
15+
map.put("key", "value");
16+
CoreConfiguration cfg =
17+
new CoreConfiguration.Builder().
18+
defaultHeader(map).
19+
build();
20+
Map<String, String> cfgHeader = cfg.getDefaultHeader();
21+
22+
assertEquals(map, cfgHeader);
23+
}
24+
25+
@Test
26+
void getServiceAccountKey() {
27+
final String saKey = "<sa-key>";
28+
29+
CoreConfiguration cfg = new CoreConfiguration.Builder()
30+
.serviceAccountKey(saKey)
31+
.build();
32+
33+
String cfgSaKey = cfg.getServiceAccountKey();
34+
35+
assertEquals(saKey, cfgSaKey);
36+
}
37+
38+
@Test
39+
void getServiceAccountKeyPath() {
40+
final String saKeyPath = "<sa-key-path>";
41+
42+
CoreConfiguration cfg = new CoreConfiguration.Builder()
43+
.serviceAccountKeyPath(saKeyPath)
44+
.build();
45+
46+
String cfgSaKeyPath = cfg.getServiceAccountKeyPath();
47+
48+
assertEquals(saKeyPath, cfgSaKeyPath);
49+
}
50+
51+
@Test
52+
void getPrivateKeyPath() {
53+
final String privateKeyPath = "<private-key-path>";
54+
55+
CoreConfiguration cfg = new CoreConfiguration.Builder()
56+
.privateKeyPath(privateKeyPath)
57+
.build();
58+
59+
String cfgPrivateKeyPath = cfg.getPrivateKeyPath();
60+
61+
assertEquals(privateKeyPath, cfgPrivateKeyPath);
62+
}
63+
64+
@Test
65+
void getPrivateKey() {
66+
final String privateKey = "<private-key>";
67+
68+
CoreConfiguration cfg = new CoreConfiguration.Builder()
69+
.privateKey(privateKey)
70+
.build();
71+
72+
String cfgPrivateKey = cfg.getPrivateKey();
73+
74+
assertEquals(privateKey, cfgPrivateKey);
75+
}
76+
77+
@Test
78+
void getCustomEndpoint() {
79+
final String customEndpoint = "<custom-endpoint>";
80+
81+
CoreConfiguration cfg = new CoreConfiguration.Builder()
82+
.customEndpoint(customEndpoint)
83+
.build();
84+
85+
String cfgCustomEndpoint = cfg.getCustomEndpoint();
86+
87+
assertEquals(customEndpoint, cfgCustomEndpoint);
88+
}
89+
90+
@Test
91+
void getCredentialsFilePath() {
92+
final String credFilePath = "<cred-file-path>";
93+
94+
CoreConfiguration cfg = new CoreConfiguration.Builder()
95+
.credentialsFilePath(credFilePath)
96+
.build();
97+
98+
String cfgCredentialsFilePath = cfg.getCredentialsFilePath();
99+
100+
assertEquals(credFilePath, cfgCredentialsFilePath);
101+
}
102+
103+
@Test
104+
void getTokenCustomUrl() {
105+
final String tokenCustomUrl = "<token-custom-url>";
106+
107+
CoreConfiguration cfg = new CoreConfiguration.Builder()
108+
.tokenCustomUrl(tokenCustomUrl)
109+
.build();
110+
111+
String cfgTokenUrl = cfg.getTokenCustomUrl();
112+
113+
assertEquals(tokenCustomUrl, cfgTokenUrl);
114+
}
115+
116+
@Test
117+
void getTokenExpirationLeeway() {
118+
final long tokenExpireLeeway = 100;
119+
120+
CoreConfiguration cfg = new CoreConfiguration.Builder()
121+
.tokenExpirationLeeway(tokenExpireLeeway)
122+
.build();
123+
124+
Long cfgTokenExpirationLeeway = cfg.getTokenExpirationLeeway();
125+
126+
assertEquals(tokenExpireLeeway, cfgTokenExpirationLeeway);
127+
}
128+
129+
@Test
130+
void getDefaultHeader_not_set() {
131+
CoreConfiguration cfg = new CoreConfiguration.Builder().build();
132+
Map<String, String> defaultHeader = cfg.getDefaultHeader();
133+
134+
assertNull(defaultHeader);
135+
}
136+
137+
@Test
138+
void getServiceAccountKey_not_set() {
139+
CoreConfiguration cfg = new CoreConfiguration.Builder().build();
140+
String serviceAccountKey = cfg.getServiceAccountKey();
141+
142+
assertNull(serviceAccountKey);
143+
}
144+
145+
@Test
146+
void getServiceAccountKeyPath_not_set() {
147+
CoreConfiguration cfg = new CoreConfiguration.Builder().build();
148+
String serviceAccountKeyPath = cfg.getServiceAccountKeyPath();
149+
150+
assertNull(serviceAccountKeyPath);
151+
}
152+
153+
@Test
154+
void getPrivateKeyPath_not_set() {
155+
CoreConfiguration cfg = new CoreConfiguration.Builder().build();
156+
String privateKeyPath = cfg.getPrivateKeyPath();
157+
158+
assertNull(privateKeyPath);
159+
}
160+
161+
@Test
162+
void getPrivateKey_not_set() {
163+
CoreConfiguration cfg = new CoreConfiguration.Builder().build();
164+
String privateKey = cfg.getPrivateKey();
165+
166+
assertNull(privateKey);
167+
}
168+
169+
@Test
170+
void getCustomEndpoint_not_set() {
171+
CoreConfiguration cfg = new CoreConfiguration.Builder().build();
172+
String customEndpoint = cfg.getCustomEndpoint();
173+
174+
assertNull(customEndpoint);
175+
}
176+
177+
@Test
178+
void getCredentialsFilePath_not_set() {
179+
CoreConfiguration cfg = new CoreConfiguration.Builder().build();
180+
String credentialsFilePath = cfg.getCredentialsFilePath();
181+
182+
assertNull(credentialsFilePath);
183+
}
184+
185+
@Test
186+
void getTokenCustomUrl_not_set() {
187+
CoreConfiguration cfg = new CoreConfiguration.Builder().build();
188+
String tokenCustomUrl = cfg.getTokenCustomUrl();
189+
190+
assertNull(tokenCustomUrl);
191+
}
192+
193+
@Test
194+
void getTokenExpirationLeeway_not_set() {
195+
CoreConfiguration cfg = new CoreConfiguration.Builder().build();
196+
Long tokenExpirationLeeway = cfg.getTokenExpirationLeeway();
197+
198+
assertNull(tokenExpirationLeeway);
199+
}
200+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package cloud.stackit.sdk.core.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.security.spec.InvalidKeySpecException;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class ServiceAccountCredentialsTest {
10+
11+
@Test
12+
void isPrivateKeySet_null_returnsFalse() {
13+
ServiceAccountCredentials saCreds = new ServiceAccountCredentials(null, null, null, null, null);
14+
15+
assertFalse(
16+
saCreds.isPrivateKeySet()
17+
);
18+
}
19+
20+
@Test
21+
void isPrivateKeySet_emptyString_returnsFalse() {
22+
ServiceAccountCredentials saCreds = new ServiceAccountCredentials(null, null, null, "", null);
23+
24+
assertFalse(
25+
saCreds.isPrivateKeySet()
26+
);
27+
}
28+
29+
@Test
30+
void isPrivateKeySet_emptyStringWhitespaces_returnsFalse() {
31+
ServiceAccountCredentials saCreds = new ServiceAccountCredentials(null, null, null, " ", null);
32+
33+
assertFalse(
34+
saCreds.isPrivateKeySet()
35+
);
36+
}
37+
38+
@Test
39+
void isPrivateKeySet_string_returnsFalse() {
40+
ServiceAccountCredentials saCreds = new ServiceAccountCredentials(null, null, null, "my-private-key", null);
41+
42+
assertTrue(
43+
saCreds.isPrivateKeySet()
44+
);
45+
}
46+
47+
@Test
48+
void getPrivateKeyParsed_notBase64Key_throwsException() {
49+
ServiceAccountCredentials saCreds = new ServiceAccountCredentials(null, null, null, "my-private-key", null);
50+
51+
assertThrows(
52+
IllegalArgumentException.class,
53+
saCreds::getPrivateKeyParsed
54+
);
55+
}
56+
57+
@Test
58+
void getPrivateKeyParsed_invalidKey_throwsException() {
59+
ServiceAccountCredentials saCreds = new ServiceAccountCredentials(null, null, null, "bXktcHJpdmF0ZS1rZXk=", null);
60+
61+
assertThrows(
62+
InvalidKeySpecException.class,
63+
saCreds::getPrivateKeyParsed
64+
);
65+
}
66+
67+
@Test
68+
void getPrivateKeyParsed_validKey_returnsRsaKey() {
69+
final String privateKey = "-----BEGIN PRIVATE KEY-----\n" +
70+
"MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqPfgaTEWEP3S9w0t\n" +
71+
"gsicURfo+nLW09/0KfOPinhYZ4ouzU+3xC4pSlEp8Ut9FgL0AgqNslNaK34Kq+NZ\n" +
72+
"jO9DAQIDAQABAkAgkuLEHLaqkWhLgNKagSajeobLS3rPT0Agm0f7k55FXVt743hw\n" +
73+
"Ngkp98bMNrzy9AQ1mJGbQZGrpr4c8ZAx3aRNAiEAoxK/MgGeeLui385KJ7ZOYktj\n" +
74+
"hLBNAB69fKwTZFsUNh0CIQEJQRpFCcydunv2bENcN/oBTRw39E8GNv2pIcNxZkcb\n" +
75+
"NQIgbYSzn3Py6AasNj6nEtCfB+i1p3F35TK/87DlPSrmAgkCIQDJLhFoj1gbwRbH\n" +
76+
"/bDRPrtlRUDDx44wHoEhSDRdy77eiQIgE6z/k6I+ChN1LLttwX0galITxmAYrOBh\n" +
77+
"BVl433tgTTQ=\n" +
78+
"-----END PRIVATE KEY-----";
79+
80+
ServiceAccountCredentials saCreds = new ServiceAccountCredentials(null, null, null, privateKey, null);
81+
82+
assertDoesNotThrow(saCreds::getPrivateKeyParsed);
83+
}
84+
}

0 commit comments

Comments
 (0)