Skip to content

Commit

Permalink
token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
v1r3n committed Jun 28, 2023
1 parent 9368482 commit 04190ec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
21 changes: 11 additions & 10 deletions src/main/java/io/orkes/conductor/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public ApiClient(String basePath, String keyId, String keySecret) {
this.httpClient.setRetryOnConnectionFailure(true);
this.verifyingSsl = true;
this.json = new JSON();
String refreshInterval = System.getenv("CONDUCTOR_SECURITY_TOKEN_REFRESH_INTERVAL");
if(refreshInterval == null) {
refreshInterval = System.getProperty("CONDUCTOR_SECURITY_TOKEN_REFRESH_INTERVAL");
}
if(refreshInterval != null) {
try {
this.tokenRefreshInSeconds = Integer.parseInt(refreshInterval);
}catch (Exception ignored){}
}
LOGGER.info("Setting token refresh interval to {} seconds", this.tokenRefreshInSeconds);
this.tokenCache = CacheBuilder.newBuilder().expireAfterWrite(tokenRefreshInSeconds, TimeUnit.SECONDS).build();
if(useSecurity()) {
scheduleTokenRefresh();
Expand All @@ -135,19 +145,10 @@ public ApiClient(String basePath, String keyId, String keySecret) {
}
}

public void setTokenRefreshTime(long duration, TimeUnit timeUnit) {
this.tokenRefreshInSeconds = timeUnit.toSeconds(duration);
Cache<String, String> tokenCacheNew = CacheBuilder.newBuilder().expireAfterWrite(tokenRefreshInSeconds, TimeUnit.SECONDS).build();
synchronized (tokenCache) {
tokenCache = tokenCacheNew;
tokenRefreshService.shutdownNow();
scheduleTokenRefresh();
}
}

private void scheduleTokenRefresh() {
this.tokenRefreshService = Executors.newSingleThreadScheduledExecutor();
long refreshInterval = Math.max(30, tokenRefreshInSeconds - 30);
LOGGER.info("Starting token refresh thread to run at every {} seconds", refreshInterval);
this.tokenRefreshService.scheduleAtFixedRate(()-> {
refreshToken();
}, refreshInterval,refreshInterval, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package io.orkes.conductor.client.spring;


import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
Expand All @@ -31,9 +30,6 @@ public class ApiClientAutoConfiguration {
public static final String CONDUCTOR_SERVER_URL = "conductor.server.url";
public static final String CONDUCTOR_CLIENT_KEY_ID = "conductor.security.client.key-id";
public static final String CONDUCTOR_CLIENT_SECRET = "conductor.security.client.secret";

public static final String CONDUCTOR_TOKEN_REFRESH_SEC = "conductor.security.token.refresh.seconds";

public static final String CONDUCTOR_GRPC_SERVER = "conductor.grpc.host";

public static final String CONDUCTOR_GRPC_PORT = "conductor.grpc.port";
Expand All @@ -45,7 +41,6 @@ public ApiClient getApiClient(Environment env) {
String rootUri = env.getProperty(CONDUCTOR_SERVER_URL);
String keyId = env.getProperty(CONDUCTOR_CLIENT_KEY_ID);
String secret = env.getProperty(CONDUCTOR_CLIENT_SECRET);
Integer tokenRefreshInterval = env.getProperty(CONDUCTOR_TOKEN_REFRESH_SEC, Integer.class);
if (rootUri.endsWith("/")) {
rootUri = rootUri.substring(0, rootUri.length() - 1);
}
Expand All @@ -57,9 +52,6 @@ public ApiClient getApiClient(Environment env) {

ApiClient apiClient = new ApiClient(rootUri);
apiClient = configureGrpc(apiClient, env);
if(tokenRefreshInterval != null) {
apiClient.setTokenRefreshTime(tokenRefreshInterval, TimeUnit.SECONDS);
}
return apiClient;
}

Expand Down

0 comments on commit 04190ec

Please sign in to comment.