Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update updated OpenId access and refresh token in memory config #5888

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert this import related change?

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()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leads to a NPE if the current config has no auth provider set

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently working on some refactors and fixes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leads to a NPE if the current config has no auth provider set

I think the OpenIDConnectionUtils wouldn't be invoked if no auth provider set, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, but that's unclear in tests.
Also the method is reused elsewhere (i.e. this one is called from the OpenShiftOAuthInterceptor) because it was public.

Anyway, this might have some ramifications. I'll work on refactoring some of this stuff and maybe moving it elsewhere to avoid latent bugs.

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
Loading