Skip to content

Commit ad4ff4f

Browse files
Merge pull request #207 from skyflowapi/skyflow-vivek/SK-2267-fix-bearer-token-logs
SK-2267 Fix log behaviour for bearer token
2 parents a014706 + b5bb89e commit ad4ff4f

File tree

10 files changed

+24
-258
lines changed

10 files changed

+24
-258
lines changed

common/src/main/java/com/skyflow/errors/ErrorMessage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public enum ErrorMessage {
5252
JwtDecodeError("%s0 Validation error. Invalid access token. Verify your credentials."),
5353
MissingAccessToken("%s0 Validation error. Access token not present in the response from bearer token generation. Verify your credentials."),
5454
MissingTokenType("%s0 Validation error. Token type not present in the response from bearer token generation. Verify your credentials."),
55+
BearerTokenExpired("%s0 Validation error. Bearer token is invalid or expired. Please provide a valid bearer token."),
5556

5657
// Insert
5758
TableKeyError("%s0 Validation error. 'table' key is missing from the payload. Specify a 'table' key."),

common/src/main/java/com/skyflow/logs/ErrorLogs.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ public enum ErrorLogs {
129129

130130
UNEXPECTED_ERROR_DURING_BATCH_PROCESSING("Unexpected error occurred during batch processing. Error: %s1"),
131131

132-
PROCESSING_ERROR_RESPONSE("Processing error response.")
133-
;
132+
PROCESSING_ERROR_RESPONSE("Processing error response.");
134133

135134
private final String log;
136135

common/src/main/java/com/skyflow/logs/InfoLogs.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public enum InfoLogs {
2020
GET_SIGNED_DATA_TOKENS_TRIGGERED("getSignedDataTokens method triggered."),
2121
GET_SIGNED_DATA_TOKEN_SUCCESS("Signed data tokens generated."),
2222
REUSE_BEARER_TOKEN("Reusing bearer token."),
23-
REUSE_API_KEY("Reusing api key."),
23+
USE_CLIENT_PROVIDED_BEARER_TOKEN("Using bearer token provided by client."),
24+
USE_API_KEY("Using api key."),
2425
GENERATE_BEARER_TOKEN_FROM_CREDENTIALS_TRIGGERED("generateBearerTokenFromCredentials method triggered."),
2526
GENERATE_BEARER_TOKEN_FROM_CREDENTIALS_STRING_TRIGGERED("generateBearerTokenFromCredentialString method triggered."),
2627
GENERATE_SIGNED_TOKENS_FROM_CREDENTIALS_FILE_TRIGGERED("generateSignedTokensFromCredentialsFile method triggered."),

common/src/main/java/com/skyflow/utils/BaseUtils.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.skyflow.logs.ErrorLogs;
1010
import com.skyflow.logs.InfoLogs;
1111
import com.skyflow.serviceaccount.util.BearerToken;
12+
import com.skyflow.serviceaccount.util.Token;
1213
import com.skyflow.utils.logger.LogUtil;
1314
import org.apache.commons.codec.binary.Base64;
1415

@@ -43,23 +44,31 @@ public static String getVaultURL(String clusterId, Env env, String vaultDomain)
4344
}
4445

4546
public static String generateBearerToken(Credentials credentials) throws SkyflowException {
47+
String bearerToken;
4648
if (credentials.getPath() != null) {
47-
return BearerToken.builder()
49+
bearerToken = BearerToken.builder()
4850
.setCredentials(new File(credentials.getPath()))
4951
.setRoles(credentials.getRoles())
5052
.setCtx(credentials.getContext())
5153
.build()
5254
.getBearerToken();
5355
} else if (credentials.getCredentialsString() != null) {
54-
return BearerToken.builder()
56+
bearerToken = BearerToken.builder()
5557
.setCredentials(credentials.getCredentialsString())
5658
.setRoles(credentials.getRoles())
5759
.setCtx(credentials.getContext())
5860
.build()
5961
.getBearerToken();
6062
} else {
61-
return credentials.getToken();
63+
LogUtil.printInfoLog(InfoLogs.USE_CLIENT_PROVIDED_BEARER_TOKEN.getLog());
64+
bearerToken = credentials.getToken();
6265
}
66+
// check expiry for generated token
67+
if (Token.isExpired(bearerToken)) {
68+
LogUtil.printErrorLog(ErrorLogs.INVALID_BEARER_TOKEN.getLog());
69+
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.BearerTokenExpired.getMessage());
70+
}
71+
return bearerToken;
6372
}
6473

6574
public static PrivateKey getPrivateKeyFromPem(String pemKey) throws SkyflowException {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private void setApiKey() {
5757
if (apiKey == null) {
5858
apiKey = this.finalCredentials.getApiKey();
5959
} else {
60-
LogUtil.printInfoLog(InfoLogs.REUSE_API_KEY.getLog());
60+
LogUtil.printInfoLog(InfoLogs.USE_API_KEY.getLog());
6161
}
6262
}
6363

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ protected void setBearerToken() throws SkyflowException {
213213
prioritiseCredentials();
214214
Validations.validateCredentials(this.finalCredentials);
215215
if (this.finalCredentials.getApiKey() != null) {
216-
LogUtil.printInfoLog(InfoLogs.REUSE_API_KEY.getLog());
216+
LogUtil.printInfoLog(InfoLogs.USE_API_KEY.getLog());
217217
token = this.finalCredentials.getApiKey();
218218
} else if (Token.isExpired(token)) {
219219
LogUtil.printInfoLog(InfoLogs.BEARER_TOKEN_EXPIRED.getLog());

v2/src/main/java/com/skyflow/logs/ErrorLogs.java

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)