Skip to content

Commit

Permalink
Merge pull request #59 from samagra-comms/development
Browse files Browse the repository at this point in the history
[Bug Fix] - Removed access token from cache (#58)
  • Loading branch information
pankajjangid05 authored Sep 5, 2023
2 parents 565fb49 + a49e706 commit 56ee091
Showing 1 changed file with 7 additions and 61 deletions.
68 changes: 7 additions & 61 deletions src/main/java/com/uci/adapter/cdn/service/MinioClientService.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public String uploadFileFromPath(String filePath, String name) {

MinioClient minioClient = getMinioClient();
if (minioClient != null) {
log.info("uploadFileFromPath:: filePath: " + filePath + " Name : " + name);
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(this.minioBucketId)
Expand All @@ -172,6 +173,8 @@ public String uploadFileFromPath(String filePath, String name) {
.build());

return getFileSignedUrl(name);
} else {
log.error("uploadFileFromPath:: Minio client is null : " + minioClient);
}
} catch (Exception ex) {
log.error("Exception in minio uploadFileFromPath: " + ex.getMessage());
Expand All @@ -189,7 +192,7 @@ private MinioClient getMinioClient() {
if (this.minioUrl != null) {
try {
StaticProvider provider = getMinioCredentialsProvider();
log.info("provider: " + provider + ", url: " + this.minioUrl);
log.info("getMinioClient:: provider: " + provider + ", url: " + this.minioUrl);
if (provider != null) {
return MinioClient.builder()
.endpoint(this.minioUrl)
Expand All @@ -210,16 +213,9 @@ private MinioClient getMinioClient() {
*/
private StaticProvider getMinioCredentialsProvider() {
try {
/* Get credentials in cache */
Map<String, String> cacheData = getMinioCredentialsCache();
if (cacheData.get("sessionToken") != null && cacheData.get("accessKey") != null && cacheData.get("secretAccessKey") != null) {
return new StaticProvider(cacheData.get("accessKey"), cacheData.get("secretAccessKey"), cacheData.get("sessionToken"));
}

String token = getFusionAuthToken();
log.info("token: " + token);
log.info("getMinioCredentialsProvider:: token : " + token);
if (!token.isEmpty()) {
Integer duration = 36000;
OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(90, TimeUnit.SECONDS)
.writeTimeout(90, TimeUnit.SECONDS).readTimeout(90, TimeUnit.SECONDS).build();
MediaType mediaType = MediaType.parse("application/json");
Expand All @@ -238,7 +234,7 @@ private StaticProvider getMinioCredentialsProvider() {
try {
Response callResponse = client.newCall(request).execute();
String response = callResponse.body().string();

log.info("Minio getMinioCredentialsProvider Response::" + response);
JSONObject xmlJSONObj = XML.toJSONObject(response);
String jsonPrettyPrintString = xmlJSONObj.toString(4);

Expand All @@ -251,14 +247,10 @@ private StaticProvider getMinioCredentialsProvider() {
String accessKey = credentials.get("AccessKeyId").asText();
String secretAccessKey = credentials.get("SecretAccessKey").asText();

log.info("sessionToken: " + sessionToken + ", accessKey: " + accessKey + ",secretAccessKey: " + secretAccessKey);
log.info("getMinioCredentialsProvider:: sessionToken: " + sessionToken + ", accessKey: " + accessKey + ", secretAccessKey: " + secretAccessKey);

if (!accessKey.isEmpty() && !secretAccessKey.isEmpty() && !sessionToken.isEmpty()) {
/* Set credentials in cache */
setMinioCredentialsCache(sessionToken, accessKey, secretAccessKey);

return new StaticProvider(accessKey, secretAccessKey, sessionToken);
// return new StaticProvider("test", secretAccessKey, sessionToken);
}
} else {
if (node.path("ErrorResponse") != null
Expand All @@ -277,52 +269,6 @@ private StaticProvider getMinioCredentialsProvider() {
return null;
}

/**
* Set Minio Credentials Cache
*
* @param sessionToken
* @param accessKey
* @param secretAccessKey
*/
private void setMinioCredentialsCache(String sessionToken, String accessKey, String secretAccessKey) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

/* local date time */
LocalDateTime localTomorrow = LocalDateTime.now().plusDays(1);
String expiryDateString = fmt.format(localTomorrow).toString();

redisCacheService.setMinioCDNCache("sessionToken", sessionToken);
redisCacheService.setMinioCDNCache("accessKey", accessKey);
redisCacheService.setMinioCDNCache("secretAccessKey", secretAccessKey);
redisCacheService.setMinioCDNCache("expiresAt", expiryDateString);
}

/**
* Get Minio Credentials from Redis Cache
*
* @return
*/
private Map<String, String> getMinioCredentialsCache() {
Map<String, String> credentials = new HashMap();

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

/* local date time */
LocalDateTime localNow = LocalDateTime.now();
/* Expiry Date time */
String expiry = (String) redisCacheService.getMinioCDNCache("expiresAt");
if (expiry != null) {
LocalDateTime expiryDateTime = LocalDateTime.parse(expiry, fmt);

if (localNow.compareTo(expiryDateTime) < 0) {
credentials.put("sessionToken", (String) redisCacheService.getMinioCDNCache("sessionToken"));
credentials.put("accessKey", (String) redisCacheService.getMinioCDNCache("accessKey"));
credentials.put("secretAccessKey", (String) redisCacheService.getMinioCDNCache("secretAccessKey"));
}
}
return credentials;
}

/**
* Get Fustion Auth Token
*
Expand Down

0 comments on commit 56ee091

Please sign in to comment.