Skip to content

Commit ea8ae4c

Browse files
committed
SK-1802 Fix inconsistencies in SDK v2
- Fix inconsistency between vault and connection client - Added unit tests for vault and connection clients
1 parent f015151 commit ea8ae4c

File tree

7 files changed

+410
-7
lines changed

7 files changed

+410
-7
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
json: ${{ secrets.TEST_CREDENTIALS_FILE_STRING }}
2424

2525
- name: Build & Run tests with Maven
26-
run: mvn -B package -DTEST_VAULT_ID=${{ secrets.TEST_VAULT_ID }} -DTEST_VAULT_URL=${{ secrets.TEST_VAULT_URL }} -DTEST_SKYFLOW_ID=${{ secrets.TEST_SKYFLOW_ID }} -DTEST_TOKEN=${{ secrets.TEST_TOKEN }} -DTEST_CREDENTIALS=${{ secrets.TEST_CREDENTIALS_FILE_STRING }} -DTEST_EXPIRED_TOKEN=${{ secrets.TEST_EXPIRED_TOKEN }} -f pom.xml
26+
run: mvn -B package -DTEST_VAULT_ID=${{ secrets.TEST_VAULT_ID }} -DTEST_VAULT_URL=${{ secrets.TEST_VAULT_URL }} -DTEST_SKYFLOW_ID=${{ secrets.TEST_SKYFLOW_ID }} -DTEST_TOKEN=${{ secrets.TEST_TOKEN }} -DTEST_CREDENTIALS=${{ secrets.TEST_CREDENTIALS_FILE_STRING }} -DTEST_EXPIRED_TOKEN=${{ secrets.TEST_EXPIRED_TOKEN }} -DTEST_HUNDRED_TOKEN=${{ secrets.TEST_HUNDRED_TOKEN }} -f pom.xml
2727

2828
- name: Codecov
2929
uses: codecov/codecov-action@v2.1.0

.github/workflows/pr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
touch .env
3838
echo SKYFLOW_CREDENTIALS=${{ secrets.SKYFLOW_CREDENTIALS }} >> .env
3939
echo TEST_EXPIRED_TOKEN=${{ secrets.TEST_EXPIRED_TOKEN }} >> .env
40+
echo TEST_HUNDRED_TOKEN=${{ secrets.TEST_HUNDRED_TOKEN }} >> .env
4041
4142
- name: Build & Run tests with Maven
4243
run: mvn -B package -DTEST_EXPIRED_TOKEN=${{ secrets.TEST_EXPIRED_TOKEN }} -DTEST_DATA_CREDENTIALS_FILE=${{ secrets.TEST_DATA_CREDENTIALS_FILE }} -f pom.xml

.github/workflows/shared-build-and-deploy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ jobs:
8989
touch .env
9090
echo SKYFLOW_CREDENTIALS=${{ secrets.SKYFLOW_CREDENTIALS }} >> .env
9191
echo TEST_EXPIRED_TOKEN=${{ secrets.TEST_EXPIRED_TOKEN }} >> .env
92+
echo TEST_HUNDRED_TOKEN=${{ secrets.TEST_HUNDRED_TOKEN }} >> .env
9293
9394
- name: Create credentials json
9495
id: create-json

src/main/java/com/skyflow/ConnectionClient.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.skyflow.utils.logger.LogUtil;
1313
import com.skyflow.utils.validations.Validations;
1414
import io.github.cdimascio.dotenv.Dotenv;
15+
import io.github.cdimascio.dotenv.DotenvException;
1516

1617
public class ConnectionClient {
1718
private final ConnectionConfig connectionConfig;
@@ -30,21 +31,19 @@ protected ConnectionConfig getConnectionConfig() {
3031
return connectionConfig;
3132
}
3233

33-
protected void setCommonCredentials(Credentials commonCredentials) {
34+
protected void setCommonCredentials(Credentials commonCredentials) throws SkyflowException {
3435
this.commonCredentials = commonCredentials;
3536
prioritiseCredentials();
3637
}
3738

38-
protected void updateConnectionConfig(ConnectionConfig connectionConfig) {
39+
protected void updateConnectionConfig(ConnectionConfig connectionConfig) throws SkyflowException {
3940
prioritiseCredentials();
4041
}
4142

4243
protected void setBearerToken() throws SkyflowException {
43-
prioritiseCredentials();
4444
Validations.validateCredentials(this.finalCredentials);
4545
if (this.finalCredentials.getApiKey() != null) {
4646
setApiKey();
47-
return;
4847
} else if (Token.isExpired(token)) {
4948
LogUtil.printInfoLog(InfoLogs.BEARER_TOKEN_EXPIRED.getLog());
5049
token = Utils.generateBearerToken(this.finalCredentials);
@@ -61,7 +60,7 @@ private void setApiKey() {
6160
}
6261
}
6362

64-
private void prioritiseCredentials() {
63+
private void prioritiseCredentials() throws SkyflowException {
6564
try {
6665
if (this.connectionConfig.getCredentials() != null) {
6766
this.finalCredentials = this.connectionConfig.getCredentials();
@@ -78,6 +77,11 @@ private void prioritiseCredentials() {
7877
this.finalCredentials.setCredentialsString(sysCredentials);
7978
}
8079
}
80+
token = null;
81+
apiKey = null;
82+
} catch (DotenvException e) {
83+
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(),
84+
ErrorMessage.EmptyCredentials.getMessage());
8185
} catch (Exception e) {
8286
throw new RuntimeException(e);
8387
}

src/main/java/com/skyflow/VaultClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ protected V1TokenizePayload getTokenizePayload(TokenizeRequest request) {
169169
}
170170

171171
protected void setBearerToken() throws SkyflowException {
172-
prioritiseCredentials();
173172
Validations.validateCredentials(this.finalCredentials);
174173
if (this.finalCredentials.getApiKey() != null) {
175174
setApiKey();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.skyflow;
2+
3+
import com.skyflow.config.ConnectionConfig;
4+
import com.skyflow.config.Credentials;
5+
import io.github.cdimascio.dotenv.Dotenv;
6+
import org.junit.Assert;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
public class ConnectionClientTests {
11+
private static final String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception";
12+
private static final String EXCEPTION_NOT_THROWN = "Should have thrown an exception";
13+
private static ConnectionClient connectionClient;
14+
private static String connectionID = null;
15+
private static String connectionURL = null;
16+
private static String apiKey = null;
17+
private static ConnectionConfig connectionConfig;
18+
19+
@BeforeClass
20+
public static void setup() {
21+
connectionID = "connection123";
22+
connectionURL = "https://test.connection.url";
23+
apiKey = "sky-ab123-abcd1234cdef1234abcd4321cdef4321";
24+
25+
Credentials credentials = new Credentials();
26+
credentials.setApiKey(apiKey);
27+
28+
connectionConfig = new ConnectionConfig();
29+
connectionConfig.setConnectionId(connectionID);
30+
connectionConfig.setConnectionUrl(connectionURL);
31+
connectionClient = new ConnectionClient(connectionConfig, credentials);
32+
}
33+
34+
@Test
35+
public void getConnectionConfig() {
36+
try {
37+
ConnectionConfig config = connectionClient.getConnectionConfig();
38+
Assert.assertEquals(connectionID, config.getConnectionId());
39+
Assert.assertEquals(connectionURL, config.getConnectionUrl());
40+
} catch (Exception e) {
41+
Assert.fail(INVALID_EXCEPTION_THROWN);
42+
}
43+
}
44+
45+
@Test
46+
public void testSetBearerToken() {
47+
try {
48+
Dotenv dotenv = Dotenv.load();
49+
String bearerToken = dotenv.get("TEST_HUNDRED_TOKEN");
50+
Credentials credentials = new Credentials();
51+
credentials.setToken(bearerToken);
52+
connectionConfig.setCredentials(credentials);
53+
connectionClient.updateConnectionConfig(connectionConfig);
54+
55+
// regular scenario
56+
connectionClient.setBearerToken();
57+
58+
// re-use scenario
59+
connectionClient.setBearerToken();
60+
} catch (Exception e) {
61+
Assert.fail(INVALID_EXCEPTION_THROWN);
62+
}
63+
}
64+
65+
@Test
66+
public void testSetBearerTokenWithApiKey() {
67+
try {
68+
Credentials credentials = new Credentials();
69+
credentials.setApiKey(apiKey);
70+
connectionConfig.setCredentials(null);
71+
connectionClient.updateConnectionConfig(connectionConfig);
72+
connectionClient.setCommonCredentials(credentials);
73+
74+
// regular scenario
75+
connectionClient.setBearerToken();
76+
77+
// re-use scenario
78+
connectionClient.setBearerToken();
79+
} catch (Exception e) {
80+
Assert.fail(INVALID_EXCEPTION_THROWN);
81+
}
82+
}
83+
84+
@Test
85+
public void testSetBearerTokenWithEnvCredentials() {
86+
try {
87+
connectionConfig.setCredentials(null);
88+
connectionClient.updateConnectionConfig(connectionConfig);
89+
connectionClient.setCommonCredentials(null);
90+
Assert.assertNull(connectionClient.getConnectionConfig().getCredentials());
91+
} catch (Exception e) {
92+
Assert.fail(INVALID_EXCEPTION_THROWN);
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)