From c1b0b7b948af0ff6c6ae9a07720b596ffaac7ab7 Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Wed, 14 Dec 2022 13:27:11 -0500 Subject: [PATCH] fix #4673 ensures token is shared by the same logical instance --- CHANGELOG.md | 1 + .../client/impl/OpenShiftClientImpl.java | 4 +- .../internal/OpenShiftOAuthInterceptor.java | 13 +++++-- .../OpenShiftOAuthInterceptorTest.java | 37 +++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 openshift-client/src/test/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptorTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index d8a2d3b9c20..5195e9ba232 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 6.4-SNAPSHOT #### Bugs +* Fix #4673: fixes a regression in sharing the OpenShiftOAuthInterceptor token #### Improvements diff --git a/openshift-client/src/main/java/io/fabric8/openshift/client/impl/OpenShiftClientImpl.java b/openshift-client/src/main/java/io/fabric8/openshift/client/impl/OpenShiftClientImpl.java index 0eb4614621d..3931bbdc252 100644 --- a/openshift-client/src/main/java/io/fabric8/openshift/client/impl/OpenShiftClientImpl.java +++ b/openshift-client/src/main/java/io/fabric8/openshift/client/impl/OpenShiftClientImpl.java @@ -179,6 +179,7 @@ import java.text.ParseException; import java.util.List; import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; /** @@ -730,7 +731,8 @@ protected void setDerivedFields() { this.config = wrapped; HttpClient.DerivedClientBuilder builder = httpClient.newBuilder().authenticatorNone(); this.httpClient = builder - .addOrReplaceInterceptor(TokenRefreshInterceptor.NAME, new OpenShiftOAuthInterceptor(httpClient, wrapped)) + .addOrReplaceInterceptor(TokenRefreshInterceptor.NAME, + new OpenShiftOAuthInterceptor(httpClient, wrapped, new AtomicReference<>())) .build(); try { this.openShiftUrl = new URL(wrapped.getOpenShiftUrl()); diff --git a/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptor.java b/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptor.java index 95c67ba016d..fd9d90aae69 100644 --- a/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptor.java +++ b/openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptor.java @@ -73,16 +73,17 @@ public class OpenShiftOAuthInterceptor implements Interceptor { private final HttpClient client; private final Config config; - private final AtomicReference oauthToken = new AtomicReference<>(); + private final AtomicReference oauthToken; - public OpenShiftOAuthInterceptor(HttpClient client, Config config) { + public OpenShiftOAuthInterceptor(HttpClient client, Config config, AtomicReference oauthToken) { this.client = client; this.config = config; + this.oauthToken = oauthToken; } @Override - public Interceptor withConfig(Config config) { - return new OpenShiftOAuthInterceptor(client, config); + public OpenShiftOAuthInterceptor withConfig(Config config) { + return new OpenShiftOAuthInterceptor(client, config, oauthToken); } @Override @@ -188,4 +189,8 @@ private boolean shouldProceed(HttpRequest request, HttpResponse response) { } return response.code() != HTTP_UNAUTHORIZED && response.code() != HTTP_FORBIDDEN; } + + AtomicReference getOauthToken() { + return oauthToken; + } } diff --git a/openshift-client/src/test/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptorTest.java b/openshift-client/src/test/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptorTest.java new file mode 100644 index 00000000000..46fc430274e --- /dev/null +++ b/openshift-client/src/test/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptorTest.java @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.openshift.client.internal; + +import io.fabric8.kubernetes.client.Config; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; + +class OpenShiftOAuthInterceptorTest { + + @Test + void testTokenSharing() { + AtomicReference reference = new AtomicReference<>(); + OpenShiftOAuthInterceptor interceptor = new OpenShiftOAuthInterceptor(null, null, reference); + OpenShiftOAuthInterceptor other = interceptor.withConfig(Config.empty()); + assertNotSame(interceptor, other); + assertSame(reference, other.getOauthToken()); + } + +}