Skip to content

Commit 1bd8e0e

Browse files
shawkinsmanusa
authored andcommitted
fix #4673 ensures token is shared by the same logical instance
1 parent fe1203c commit 1bd8e0e

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### 6.4-SNAPSHOT
44

55
#### Bugs
6+
* Fix #4673: fixes a regression in sharing the OpenShiftOAuthInterceptor token
67

78
#### Improvements
89

openshift-client/src/main/java/io/fabric8/openshift/client/impl/OpenShiftClientImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
import java.text.ParseException;
180180
import java.util.List;
181181
import java.util.Objects;
182+
import java.util.concurrent.atomic.AtomicReference;
182183
import java.util.function.Supplier;
183184

184185
/**
@@ -730,7 +731,8 @@ protected void setDerivedFields() {
730731
this.config = wrapped;
731732
HttpClient.DerivedClientBuilder builder = httpClient.newBuilder().authenticatorNone();
732733
this.httpClient = builder
733-
.addOrReplaceInterceptor(TokenRefreshInterceptor.NAME, new OpenShiftOAuthInterceptor(httpClient, wrapped))
734+
.addOrReplaceInterceptor(TokenRefreshInterceptor.NAME,
735+
new OpenShiftOAuthInterceptor(httpClient, wrapped, new AtomicReference<>()))
734736
.build();
735737
try {
736738
this.openShiftUrl = new URL(wrapped.getOpenShiftUrl());

openshift-client/src/main/java/io/fabric8/openshift/client/internal/OpenShiftOAuthInterceptor.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,17 @@ public class OpenShiftOAuthInterceptor implements Interceptor {
7373

7474
private final HttpClient client;
7575
private final Config config;
76-
private final AtomicReference<String> oauthToken = new AtomicReference<>();
76+
private final AtomicReference<String> oauthToken;
7777

78-
public OpenShiftOAuthInterceptor(HttpClient client, Config config) {
78+
public OpenShiftOAuthInterceptor(HttpClient client, Config config, AtomicReference<String> oauthToken) {
7979
this.client = client;
8080
this.config = config;
81+
this.oauthToken = oauthToken;
8182
}
8283

8384
@Override
84-
public Interceptor withConfig(Config config) {
85-
return new OpenShiftOAuthInterceptor(client, config);
85+
public OpenShiftOAuthInterceptor withConfig(Config config) {
86+
return new OpenShiftOAuthInterceptor(client, config, oauthToken);
8687
}
8788

8889
@Override
@@ -188,4 +189,8 @@ private boolean shouldProceed(HttpRequest request, HttpResponse<?> response) {
188189
}
189190
return response.code() != HTTP_UNAUTHORIZED && response.code() != HTTP_FORBIDDEN;
190191
}
192+
193+
AtomicReference<String> getOauthToken() {
194+
return oauthToken;
195+
}
191196
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.openshift.client.internal;
17+
18+
import io.fabric8.kubernetes.client.Config;
19+
import org.junit.jupiter.api.Test;
20+
21+
import java.util.concurrent.atomic.AtomicReference;
22+
23+
import static org.junit.Assert.assertNotSame;
24+
import static org.junit.Assert.assertSame;
25+
26+
class OpenShiftOAuthInterceptorTest {
27+
28+
@Test
29+
void testTokenSharing() {
30+
AtomicReference<String> reference = new AtomicReference<>();
31+
OpenShiftOAuthInterceptor interceptor = new OpenShiftOAuthInterceptor(null, null, reference);
32+
OpenShiftOAuthInterceptor other = interceptor.withConfig(Config.empty());
33+
assertNotSame(interceptor, other);
34+
assertSame(reference, other.getOauthToken());
35+
}
36+
37+
}

0 commit comments

Comments
 (0)