Skip to content

Commit

Permalink
ref(utils): extract the oauth token logic into utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
jiridanek committed Oct 21, 2024
1 parent 22db3ee commit 462ff04
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 47 deletions.
59 changes: 59 additions & 0 deletions src/main/java/io/odh/test/platform/httpClient/OAuthToken.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
50 changes: 3 additions & 47 deletions src/test/java/io/odh/test/e2e/standard/DistributedST.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@
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;
import io.odh.test.TestUtils;
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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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", () -> {
Expand Down

0 comments on commit 462ff04

Please sign in to comment.