Skip to content

Commit

Permalink
update updated OpenId access and refresh token in memory config
Browse files Browse the repository at this point in the history
  • Loading branch information
ttbadr committed Apr 13, 2024
1 parent 7d5fd62 commit 2d475e0
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.KeyManager;
import javax.net.ssl.TrustManager;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -44,9 +46,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

import javax.net.ssl.KeyManager;
import javax.net.ssl.TrustManager;

/**
* Utility class for OpenID token refresh.
*/
Expand Down Expand Up @@ -102,8 +101,10 @@ public static CompletableFuture<String> resolveOIDCTokenFromAuthConfig(Config cu
return accessToken;
}

// Persist new config and if successful, update the in memory config.
try {
//update in memory config
updateInMemoryConfigWithUpdatedToken(currentConfig, map);
//persist kubeConfig
persistKubeConfigWithUpdatedToken(currentConfig, map);
} catch (IOException e) {
LOGGER.warn("oidc: failure while persisting new tokens into KUBECONFIG", e);
Expand All @@ -115,6 +116,21 @@ public static CompletableFuture<String> resolveOIDCTokenFromAuthConfig(Config cu
return CompletableFuture.completedFuture(accessToken);
}

/**
* update Updated Access and Refresh token in memory config.
* @param currentConfig config
* @param map updated access and refresh token
*/
private static void updateInMemoryConfigWithUpdatedToken(Config currentConfig, Map<String, Object> map) {
Map<String, String> authProviderConfig = currentConfig.getAuthProvider().getConfig();
if (map.containsKey(ID_TOKEN_KUBECONFIG)) {
authProviderConfig.put(ID_TOKEN_KUBECONFIG, String.valueOf(map.get(ID_TOKEN_PARAM)));
}
if (map.containsKey(REFRESH_TOKEN_KUBECONFIG)) {
authProviderConfig.put(REFRESH_TOKEN_KUBECONFIG, String.valueOf(map.get(REFRESH_TOKEN_PARAM)));
}
}

/**
* Whether we should try to do token refresh or not, checks whether refresh-token key is set in
* HashMap or not
Expand Down

0 comments on commit 2d475e0

Please sign in to comment.