Skip to content

Commit

Permalink
fix #4673 ensures token is shared by the same logical instance
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins authored and manusa committed Dec 15, 2022
1 parent fe1203c commit 1bd8e0e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
### 6.4-SNAPSHOT

#### Bugs
* Fix #4673: fixes a regression in sharing the OpenShiftOAuthInterceptor token

#### Improvements

Expand Down
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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());
Expand Down
Expand Up @@ -73,16 +73,17 @@ public class OpenShiftOAuthInterceptor implements Interceptor {

private final HttpClient client;
private final Config config;
private final AtomicReference<String> oauthToken = new AtomicReference<>();
private final AtomicReference<String> oauthToken;

public OpenShiftOAuthInterceptor(HttpClient client, Config config) {
public OpenShiftOAuthInterceptor(HttpClient client, Config config, AtomicReference<String> 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
Expand Down Expand Up @@ -188,4 +189,8 @@ private boolean shouldProceed(HttpRequest request, HttpResponse<?> response) {
}
return response.code() != HTTP_UNAUTHORIZED && response.code() != HTTP_FORBIDDEN;
}

AtomicReference<String> getOauthToken() {
return oauthToken;
}
}
@@ -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<String> reference = new AtomicReference<>();
OpenShiftOAuthInterceptor interceptor = new OpenShiftOAuthInterceptor(null, null, reference);
OpenShiftOAuthInterceptor other = interceptor.withConfig(Config.empty());
assertNotSame(interceptor, other);
assertSame(reference, other.getOauthToken());
}

}

0 comments on commit 1bd8e0e

Please sign in to comment.