From 4ec7c99b8e6574e592614b86883f8ec837ae783b Mon Sep 17 00:00:00 2001 From: toby Date: Tue, 16 Apr 2024 20:29:34 +0800 Subject: [PATCH] fix: updated OpenId access and refresh token persistend in in-memory config (5888) update updated OpenId access and refresh token in memory config --- fix style --- add test case --- fix comments --- fix NPE --- .../client/utils/OpenIDConnectionUtils.java | 19 +++++++++++++------ .../utils/OpenIDConnectionUtilsTest.java | 14 ++++++++++---- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/OpenIDConnectionUtils.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/OpenIDConnectionUtils.java index 72fe25c886a..3629937bef8 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/OpenIDConnectionUtils.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/OpenIDConnectionUtils.java @@ -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; @@ -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; @@ -242,6 +240,12 @@ static boolean persistKubeConfigWithUpdatedToken(Config currentConfig, Map 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; } @@ -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; diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/OpenIDConnectionUtilsTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/OpenIDConnectionUtilsTest.java index 47e8b9bc567..4cbbe946b5e 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/OpenIDConnectionUtilsTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/OpenIDConnectionUtilsTest.java @@ -183,10 +183,16 @@ void testPersistKubeConfigWithUpdatedToken() throws IOException { assertNotNull(currentNamedContext); int currentUserIndex = KubeConfigUtils.getNamedUserIndexFromConfig(config, currentNamedContext.getContext().getUser()); assertTrue(currentUserIndex > 0); - Map 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 authProviderConfigInFile = config.getUsers().get(currentUserIndex).getUser().getAuthProvider() + .getConfig(); + assertFalse(authProviderConfigInFile.isEmpty()); + Map 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