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

fix (kubernetes-client-api) : OpenIDConnectionUtils uses caCertFile and caCertData as a fallback option when idp-certificate-authority-data is not specified #5818

Merged
merged 1 commit into from
Apr 9, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### Bugs
* Fix #5853: [java-generator] Gracefully handle colliding enum definitions
* Fix #5817: NPE on EKS OIDC cluster when token needs to be refreshed

#### Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
Expand Down Expand Up @@ -89,7 +91,7 @@ public static CompletableFuture<String> resolveOIDCTokenFromAuthConfig(Config cu
String clientId = currentAuthProviderConfig.get(CLIENT_ID_KUBECONFIG);
String refreshToken = currentAuthProviderConfig.get(REFRESH_TOKEN_KUBECONFIG);
String clientSecret = currentAuthProviderConfig.getOrDefault(CLIENT_SECRET_KUBECONFIG, "");
String idpCert = currentAuthProviderConfig.getOrDefault(IDP_CERT_DATA, currentConfig.getCaCertData());
String idpCert = currentAuthProviderConfig.getOrDefault(IDP_CERT_DATA, getClientCertDataFromConfig(currentConfig));
if (isTokenRefreshSupported(currentAuthProviderConfig)) {
return getOIDCProviderTokenEndpointAndRefreshToken(issuer, clientId, refreshToken, clientSecret, idpCert, clientBuilder)
.thenApply(map -> {
Expand Down Expand Up @@ -352,4 +354,18 @@ private static boolean isValidJwt(String token) {
}
return false;
}

private static String getClientCertDataFromConfig(Config config) {
if (config.getCaCertData() != null && !config.getCaCertData().isEmpty()) {
return config.getCaCertData();
}
try {
if (config.getCaCertFile() != null) {
return java.util.Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(config.getCaCertFile())));
}
} catch (IOException e) {
LOGGER.debug("Failure in reading certificate data from {}", config.getCaCertFile());
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.fabric8.kubernetes.client.internal.KubeConfigUtils;
import io.fabric8.kubernetes.client.internal.SSLUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

Expand Down Expand Up @@ -229,6 +230,34 @@ void resolveOIDCTokenFromAuthConfig_whenIDPCertNotPresentInAuthConfig_thenUseCer
}
}

@Test
void resolveOIDCTokenFromAuthConfig_whenIDPCertNotPresentInAuthConfig_thenUseCertFileFromConfig(@TempDir File temporaryFolder)
throws Exception {
try (MockedStatic<SSLUtils> sslUtilsMockedStatic = mockStatic(SSLUtils.class)) {
// Given
File caCertFile = new File(temporaryFolder, "ca.crt");
Files.write(caCertFile.toPath(), "cert".getBytes(StandardCharsets.UTF_8));
Map<String, String> currentAuthProviderConfig = new HashMap<>();
currentAuthProviderConfig.put(CLIENT_ID_KUBECONFIG, "client-id");
currentAuthProviderConfig.put(CLIENT_SECRET_KUBECONFIG, "client-secret");
currentAuthProviderConfig.put(ID_TOKEN_KUBECONFIG, "id-token");
currentAuthProviderConfig.put(REFRESH_TOKEN_KUBECONFIG, "refresh-token");
currentAuthProviderConfig.put(ISSUER_KUBECONFIG, "https://iam.cloud.example.com/identity");
Config config = new ConfigBuilder(Config.empty()).withCaCertFile(caCertFile.getAbsolutePath()).build();
HttpClient.Builder builder = mock(HttpClient.Builder.class);
HttpClient httpClient = mock(HttpClient.class, RETURNS_DEEP_STUBS);
when(builder.build()).thenReturn(httpClient);

// When
OpenIDConnectionUtils.resolveOIDCTokenFromAuthConfig(config, currentAuthProviderConfig, builder).get();

// Then
sslUtilsMockedStatic.verify(() -> SSLUtils.trustManagers(eq("cert"), isNull(), anyBoolean(), isNull(), isNull()));
sslUtilsMockedStatic.verify(
() -> SSLUtils.keyManagers(eq("cert"), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull()));
}
}

@Test
void testgetParametersFromDiscoveryResponse() {
// Given
Expand Down
Loading