Skip to content

Commit

Permalink
Attempt to fix duplicate clientProvider apply
Browse files Browse the repository at this point in the history
  • Loading branch information
Janelle Law committed Dec 17, 2021
1 parent 620c9b7 commit 82fba94
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/io/cryostat/net/OpenShiftAuthManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.openshift.api.model.OAuthAccessToken;
import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftClient;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.WebClient;
Expand Down Expand Up @@ -314,8 +315,16 @@ public Future<Boolean> validateWebSocketSubProtocol(
private Future<Boolean> deleteToken(String token) {
try (OpenShiftClient client = clientProvider.apply(getServiceAccountToken())) {
String serviceAccountAsOAuthClient = this.getServiceAccountName();
Future<TokenReviewStatus> fStatus = performTokenReview(token);
TokenReviewStatus status = fStatus.get();

// FIXME reuse performTokenReview instead of copying it here
TokenReview review =
new TokenReviewBuilder().withNewSpec().withToken(token).endSpec().build();
review = client.tokenReviews().create(review);
TokenReviewStatus status = review.getStatus();
if (StringUtils.isNotBlank(status.getError())) {
return CompletableFuture.failedFuture(
new AuthorizationErrorException(status.getError()));
}
String uid = status.getUser().getUid();

List<OAuthAccessToken> userOauthAccessTokens =
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/io/cryostat/net/OpenShiftAuthManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

Expand All @@ -64,6 +65,7 @@
import com.google.gson.Gson;
import io.fabric8.kubernetes.api.model.authentication.TokenReview;
import io.fabric8.kubernetes.api.model.authentication.TokenReviewBuilder;
import io.fabric8.kubernetes.api.model.authentication.TokenReviewStatus;
import io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectAccessReview;
import io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectAccessReviewBuilder;
import io.fabric8.kubernetes.client.Config;
Expand All @@ -76,7 +78,9 @@
import io.fabric8.openshift.client.server.mock.OpenShiftMockServerExtension;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.HttpResponse;
Expand Down Expand Up @@ -153,6 +157,8 @@ void setup() {
client = Mockito.spy(client);
tokenProvider = new TokenProvider(client);
mgr = new OpenShiftAuthManager(env, logger, fs, tokenProvider, webClient);
MultiMap headers = MultiMap.caseInsensitiveMultiMap();
headers.set(HttpHeaders.AUTHORIZATION, "abcd1234==");
}

@Test
Expand Down Expand Up @@ -452,10 +458,27 @@ void shouldReturnLogoutRedirectUrl() throws Exception {
List<OAuthAccessToken> tokens = new ArrayList<OAuthAccessToken>();
tokens.add(token);

TokenReview tokenReview =
new TokenReviewBuilder()
.withNewStatus()
.withAuthenticated(true)
.withNewUser()
.withUsername("fooUser")
.withUid("uid")
.endUser()
.endStatus()
.build();
server.expect()
.post()
.withPath(TOKEN_REVIEW_API_PATH)
.andReturn(HttpURLConnection.HTTP_CREATED, tokenReview)
.once();

Mockito.when(client.oAuthAccessTokens()).thenReturn(nonNamespaceOperation);
Mockito.when(nonNamespaceOperation.list()).thenReturn(oAuthAccessTokenList);
Mockito.when(oAuthAccessTokenList.getItems()).thenReturn(tokens);
Mockito.when(token.getClientName()).thenReturn(SERVICE_ACCOUNT);
Mockito.when(token.getUserUID()).thenReturn("uid");
Mockito.when(nonNamespaceOperation.delete(tokens)).thenReturn(true);

HttpRequest<Buffer> req = Mockito.mock(HttpRequest.class);
Expand Down Expand Up @@ -502,10 +525,27 @@ void shouldThrowWhenTokenDeletionFailsOnLogout(Boolean deletionFailure) throws E
List<OAuthAccessToken> tokens = new ArrayList<OAuthAccessToken>();
tokens.add(token);

TokenReview tokenReview =
new TokenReviewBuilder()
.withNewStatus()
.withAuthenticated(true)
.withNewUser()
.withUsername("fooUser")
.withUid("uid")
.endUser()
.endStatus()
.build();
server.expect()
.post()
.withPath(TOKEN_REVIEW_API_PATH)
.andReturn(HttpURLConnection.HTTP_CREATED, tokenReview)
.once();

Mockito.when(client.oAuthAccessTokens()).thenReturn(nonNamespaceOperation);
Mockito.when(nonNamespaceOperation.list()).thenReturn(oAuthAccessTokenList);
Mockito.when(oAuthAccessTokenList.getItems()).thenReturn(tokens);
Mockito.when(token.getClientName()).thenReturn(SERVICE_ACCOUNT);
Mockito.when(token.getUserUID()).thenReturn("uid");
Mockito.when(nonNamespaceOperation.delete(tokens)).thenReturn(deletionFailure);

ExecutionException ee =
Expand Down

0 comments on commit 82fba94

Please sign in to comment.