From 462ff04d83b8b254c10c5deb3a5fce0c781e52b6 Mon Sep 17 00:00:00 2001 From: Jiri Danek Date: Sat, 19 Oct 2024 18:22:37 +0200 Subject: [PATCH] ref(utils): extract the oauth token logic into utility method --- .../test/platform/httpClient/OAuthToken.java | 59 +++++++++++++++++++ .../odh/test/e2e/standard/DistributedST.java | 50 +--------------- 2 files changed, 62 insertions(+), 47 deletions(-) create mode 100644 src/main/java/io/odh/test/platform/httpClient/OAuthToken.java diff --git a/src/main/java/io/odh/test/platform/httpClient/OAuthToken.java b/src/main/java/io/odh/test/platform/httpClient/OAuthToken.java new file mode 100644 index 00000000..cfcc96bb --- /dev/null +++ b/src/main/java/io/odh/test/platform/httpClient/OAuthToken.java @@ -0,0 +1,59 @@ +/* + * Copyright Skodjob authors. + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). + */ +package io.odh.test.platform.httpClient; + +import io.fabric8.openshift.api.model.OAuthAccessToken; +import io.fabric8.openshift.api.model.OAuthAccessTokenBuilder; +import io.fabric8.openshift.api.model.OAuthClient; +import io.fabric8.openshift.api.model.OAuthClientBuilder; +import io.fabric8.openshift.api.model.User; +import io.skodjob.testframe.resources.KubeResourceManager; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; +import java.util.Random; + +public class OAuthToken { + public String getToken(String redirectUrl) throws NoSuchAlgorithmException { + // https://github.com/openshift/cluster-authentication-operator/blob/master/test/library/client.go#L35-L44 + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + String sha256Prefix = "sha256~"; + String randomToken = "nottoorandom%d".formatted(new Random().nextInt()); + byte[] hashed = digest.digest(randomToken.getBytes(StandardCharsets.UTF_8)); + String privateToken = sha256Prefix + randomToken; + String publicToken = sha256Prefix + Base64.getUrlEncoder().withoutPadding().encodeToString(hashed); + + User user = KubeResourceManager.getKubeClient().getOpenShiftClient().users().withName("kubeadmin").get(); + + final String oauthClientName = "oauth-client"; + OAuthClient client = new OAuthClientBuilder() + .withNewMetadata() + .withName(oauthClientName) + .endMetadata() + .withSecret("the-secret-for-oauth-client") + .withRedirectURIs("https://localhost") + .withGrantMethod("auto") + .withAccessTokenInactivityTimeoutSeconds(300) + .build(); + KubeResourceManager.getInstance().createResourceWithoutWait(client); + + OAuthAccessToken token = new OAuthAccessTokenBuilder() + .withNewMetadata() + .withName(publicToken) + .endMetadata() + .withExpiresIn(86400L) + .withScopes("user:full") + .withRedirectURI(redirectUrl) + .withClientName(oauthClientName) + .withUserName(user.getMetadata().getName()) + .withUserUID(user.getMetadata().getUid()) + .build(); + KubeResourceManager.getInstance().createResourceWithWait(token); + + return privateToken; + } +} diff --git a/src/test/java/io/odh/test/e2e/standard/DistributedST.java b/src/test/java/io/odh/test/e2e/standard/DistributedST.java index 70be0145..85a650e4 100644 --- a/src/test/java/io/odh/test/e2e/standard/DistributedST.java +++ b/src/test/java/io/odh/test/e2e/standard/DistributedST.java @@ -13,12 +13,7 @@ import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition; import io.fabric8.kubernetes.client.dsl.Resource; import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext; -import io.fabric8.openshift.api.model.OAuthAccessToken; -import io.fabric8.openshift.api.model.OAuthAccessTokenBuilder; -import io.fabric8.openshift.api.model.OAuthClient; -import io.fabric8.openshift.api.model.OAuthClientBuilder; import io.fabric8.openshift.api.model.Route; -import io.fabric8.openshift.api.model.User; import io.fabric8.openshift.client.OpenShiftClient; import io.odh.test.Environment; import io.odh.test.OdhAnnotationsLabels; @@ -26,6 +21,7 @@ import io.odh.test.install.InstallTypes; import io.odh.test.platform.RayClient; import io.odh.test.platform.TlsUtils; +import io.odh.test.platform.httpClient.OAuthToken; import io.odh.test.utils.CsvUtils; import io.odh.test.utils.DscUtils; import io.opendatahub.datasciencecluster.v1.DataScienceCluster; @@ -53,12 +49,8 @@ import org.slf4j.LoggerFactory; import java.net.http.HttpClient; -import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import java.util.Base64; import java.util.Map; import java.util.Objects; -import java.util.Random; import java.util.concurrent.TimeUnit; import java.util.function.Predicate; @@ -197,44 +189,8 @@ void testDistributedWorkloadWithKueue() throws Exception { final String clusterQueueName = "cluster-queue"; final String localQueueName = "local-queue"; - // https://github.com/openshift/cluster-authentication-operator/blob/master/test/library/client.go#L35-L44 - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - String sha256Prefix = "sha256~"; - String randomToken = "nottoorandom%d".formatted(new Random().nextInt()); - byte[] hashed = digest.digest(randomToken.getBytes(StandardCharsets.UTF_8)); - String privateToken = sha256Prefix + randomToken; - String publicToken = sha256Prefix + Base64.getUrlEncoder().withoutPadding().encodeToString(hashed); - - String oauthToken = Allure.step("Create OAuth Token", () -> { - User user = kubeClient.users().withName("kubeadmin").get(); - - final String oauthClientName = "oauth-client"; - OAuthClient client = new OAuthClientBuilder() - .withNewMetadata() - .withName(oauthClientName) - .endMetadata() - .withSecret("the-secret-for-oauth-client") - .withRedirectURIs("https://localhost") - .withGrantMethod("auto") - .withAccessTokenInactivityTimeoutSeconds(300) - .build(); - KubeResourceManager.getInstance().createResourceWithoutWait(client); - - OAuthAccessToken token = new OAuthAccessTokenBuilder() - .withNewMetadata() - .withName(publicToken) - .endMetadata() - .withExpiresIn(86400L) - .withScopes("user:full") - .withRedirectURI("https://ray-dashboard-koranteng-test-codeflare.apps-crc.testing/oauth/callback") - .withClientName(oauthClientName) - .withUserName(user.getMetadata().getName()) - .withUserUID(user.getMetadata().getUid()) - .build(); - KubeResourceManager.getInstance().createResourceWithWait(token); - - return privateToken; - }); + String redirectUrl = "https://ray-dashboard-koranteng-test-codeflare.apps-crc.testing/oauth/callback"; + String oauthToken = Allure.step("Create OAuth Token", () -> new OAuthToken().getToken(redirectUrl)); Allure.step("Setup resources", () -> { Allure.step("Create namespace", () -> {