Skip to content

Commit

Permalink
fix: updated OpenId access and refresh token persistend in in-memory …
Browse files Browse the repository at this point in the history
…config (5888)

update updated OpenId access and refresh token in memory config
---
fix style
---
add test case
---
fix comments
---
fix NPE
  • Loading branch information
ttbadr committed Apr 16, 2024
1 parent 6f63909 commit 4ec7c99
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.fabric8.kubernetes.client.utils;

import io.fabric8.kubernetes.api.model.AuthInfo;
import io.fabric8.kubernetes.api.model.AuthProviderConfig;
import io.fabric8.kubernetes.api.model.NamedAuthInfo;
import io.fabric8.kubernetes.api.model.NamedContext;
import io.fabric8.kubernetes.client.Config;
Expand All @@ -37,10 +38,7 @@
import java.security.cert.CertificateException;
import java.security.spec.InvalidKeySpecException;
import java.time.Instant;
import java.util.Base64;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

Expand Down Expand Up @@ -242,6 +240,12 @@ static boolean persistKubeConfigWithUpdatedToken(Config currentConfig, Map<Strin
*/
public static boolean persistKubeConfigWithUpdatedAuthInfo(Config currentConfig, Consumer<AuthInfo> updateAction)
throws IOException {
AuthInfo authInfo = new AuthInfo();
authInfo.setAuthProvider(new AuthProviderConfig(new HashMap<>(2), currentConfig.getAuthProvider().getName()));
updateAction.accept(authInfo);
//update new auth info to in-memory config
currentConfig.getAuthProvider().getConfig().putAll(authInfo.getAuthProvider().getConfig());

if (currentConfig.getFile() == null) {
return false;
}
Expand All @@ -259,10 +263,13 @@ public static boolean persistKubeConfigWithUpdatedAuthInfo(Config currentConfig,
config.getUsers().add(result);
return result;
});
//update new auth info to kubeConfig
if (namedAuthInfo.getUser() == null) {
namedAuthInfo.setUser(new AuthInfo());
namedAuthInfo.setUser(authInfo);
} else {
Optional.ofNullable(authInfo.getToken()).ifPresent(t -> namedAuthInfo.getUser().setToken(t));
namedAuthInfo.getUser().getAuthProvider().getConfig().putAll(authInfo.getAuthProvider().getConfig());
}
updateAction.accept(namedAuthInfo.getUser());
// Persist changes to KUBECONFIG
KubeConfigUtils.persistKubeConfigIntoFile(config, currentConfig.getFile().getAbsolutePath());
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,16 @@ void testPersistKubeConfigWithUpdatedToken() throws IOException {
assertNotNull(currentNamedContext);
int currentUserIndex = KubeConfigUtils.getNamedUserIndexFromConfig(config, currentNamedContext.getContext().getUser());
assertTrue(currentUserIndex > 0);
Map<String, String> authProviderConfig = config.getUsers().get(currentUserIndex).getUser().getAuthProvider().getConfig();
assertFalse(authProviderConfig.isEmpty());
assertEquals("id-token-updated", authProviderConfig.get(ID_TOKEN_KUBECONFIG));
assertEquals("refresh-token-updated", authProviderConfig.get(REFRESH_TOKEN_KUBECONFIG));
Map<String, String> authProviderConfigInFile = config.getUsers().get(currentUserIndex).getUser().getAuthProvider()
.getConfig();
assertFalse(authProviderConfigInFile.isEmpty());
Map<String, String> authProviderConfigInMemory = theConfig.getAuthProvider().getConfig();
//auth info should be updated in memory
assertEquals("id-token-updated", authProviderConfigInMemory.get(ID_TOKEN_KUBECONFIG));
assertEquals("refresh-token-updated", authProviderConfigInMemory.get(REFRESH_TOKEN_KUBECONFIG));
//auth info should be updated in kubeConfig
assertEquals("id-token-updated", authProviderConfigInFile.get(ID_TOKEN_KUBECONFIG));
assertEquals("refresh-token-updated", authProviderConfigInFile.get(REFRESH_TOKEN_KUBECONFIG));
}

@Test
Expand Down

0 comments on commit 4ec7c99

Please sign in to comment.