Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #4673 ensures token is shared by the same logical instance #4679

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 6.4-SNAPSHOT

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

#### Improvements

Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}