use of io.fabric8.kubernetes.client.http.Interceptor in project che-server by eclipse-che.
the class OpenShiftClientFactory method buildKubernetesInterceptor.
@Override
protected Interceptor buildKubernetesInterceptor(Config config) {
final String oauthToken;
if (Utils.isNotNullOrEmpty(config.getUsername()) && Utils.isNotNullOrEmpty(config.getPassword())) {
synchronized (getHttpClient()) {
try {
OkHttpClient.Builder builder = getHttpClient().newBuilder();
builder.interceptors().clear();
OkHttpClient clone = builder.build();
String credential = Credentials.basic(config.getUsername(), config.getPassword());
URL url = new URL(URLUtils.join(config.getMasterUrl(), AUTHORIZE_PATH));
Response response = clone.newCall(new Request.Builder().get().url(url).header(AUTHORIZATION, credential).build()).execute();
// False positive warn: according to javadocs response.body() returns non-null value
// if called after Call.execute()
response.body().close();
response = response.priorResponse() != null ? response.priorResponse() : response;
response = response.networkResponse() != null ? response.networkResponse() : response;
String token = response.header(LOCATION);
if (token == null || token.isEmpty()) {
throw new KubernetesClientException("Unexpected response (" + response.code() + " " + response.message() + "), to the authorization request. Missing header:[" + LOCATION + "]!");
}
token = token.substring(token.indexOf(BEFORE_TOKEN) + BEFORE_TOKEN.length());
token = token.substring(0, token.indexOf(AFTER_TOKEN));
oauthToken = token;
} catch (Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
}
} else if (Utils.isNotNullOrEmpty(config.getOauthToken())) {
oauthToken = config.getOauthToken();
} else {
oauthToken = null;
}
return chain -> {
Request request = chain.request();
if (isNotNullOrEmpty(oauthToken)) {
Request authReq = chain.request().newBuilder().addHeader("Authorization", "Bearer " + oauthToken).build();
return chain.proceed(authReq);
}
return chain.proceed(request);
};
}
use of io.fabric8.kubernetes.client.http.Interceptor in project kubernetes-client by fabric8io.
the class JdkHttpClientImpl method buildAsync.
public CompletableFuture<WebSocket> buildAsync(JdkWebSocketImpl.BuilderImpl webSocketBuilder, Listener listener) {
JdkWebSocketImpl.BuilderImpl copy = webSocketBuilder.copy();
for (Interceptor interceptor : builder.interceptors.values()) {
interceptor.before(copy, new JdkHttpRequestImpl(null, copy.asRequest()));
}
CompletableFuture<WebSocket> result = new CompletableFuture<>();
CompletableFuture<WebSocketResponse> cf = internalBuildAsync(copy, listener);
for (Interceptor interceptor : builder.interceptors.values()) {
cf = cf.thenCompose(response -> {
if (response.wshse != null && response.wshse.getResponse() != null && interceptor.afterFailure(copy, new JdkHttpResponseImpl<>(response.wshse.getResponse()))) {
return this.internalBuildAsync(copy, listener);
}
return CompletableFuture.completedFuture(response);
});
}
// map back to the expected convention with the future completed by the response exception
cf.whenComplete((r, t) -> {
if (t != null) {
result.completeExceptionally(t);
} else if (r != null) {
if (r.wshse != null) {
result.completeExceptionally(new io.fabric8.kubernetes.client.http.WebSocketHandshakeException(new JdkHttpResponseImpl<>(r.wshse.getResponse())).initCause(r.wshse));
} else {
result.complete(r.webSocket);
}
} else {
// shouldn't happen
result.complete(null);
}
});
return result;
}
use of io.fabric8.kubernetes.client.http.Interceptor in project kubernetes-client by fabric8io.
the class JdkHttpClientImpl method sendAsync.
@Override
public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, Class<T> type) {
JdkHttpRequestImpl jdkRequest = (JdkHttpRequestImpl) request;
JdkHttpRequestImpl.BuilderImpl builderImpl = jdkRequest.newBuilder();
for (Interceptor interceptor : builder.interceptors.values()) {
interceptor.before(builderImpl, jdkRequest);
jdkRequest = builderImpl.build();
}
BodyHandler<T> bodyHandler;
if (type == null) {
bodyHandler = (BodyHandler<T>) BodyHandlers.discarding();
} else if (type == InputStream.class) {
bodyHandler = (BodyHandler<T>) BodyHandlers.ofInputStream();
} else if (type == String.class) {
bodyHandler = (BodyHandler<T>) BodyHandlers.ofString();
} else {
bodyHandler = responseInfo -> {
BodySubscriber<InputStream> upstream = BodyHandlers.ofInputStream().apply(responseInfo);
BodySubscriber<Reader> downstream = BodySubscribers.mapping(upstream, (InputStream is) -> new InputStreamReader(is, StandardCharsets.UTF_8));
return (BodySubscriber<T>) downstream;
};
}
CompletableFuture<java.net.http.HttpResponse<T>> cf = this.httpClient.sendAsync(builderImpl.build().request, bodyHandler);
for (Interceptor interceptor : builder.interceptors.values()) {
cf = cf.thenCompose(response -> {
if (response != null && !HttpResponse.isSuccessful(response.statusCode()) && interceptor.afterFailure(builderImpl, new JdkHttpResponseImpl<>(response))) {
return this.httpClient.sendAsync(builderImpl.build().request, bodyHandler);
}
return CompletableFuture.completedFuture(response);
});
}
return cf.thenApply(JdkHttpResponseImpl::new);
}
use of io.fabric8.kubernetes.client.http.Interceptor in project kubernetes-client by fabric8io.
the class HttpClientUtils method createApplicableInterceptors.
public static Map<String, io.fabric8.kubernetes.client.http.Interceptor> createApplicableInterceptors(Config config, HttpClient.Factory factory) {
Map<String, io.fabric8.kubernetes.client.http.Interceptor> interceptors = new LinkedHashMap<>();
// Header Interceptor
interceptors.put(HEADER_INTERCEPTOR, new Interceptor() {
@Override
public void before(BasicBuilder builder, HttpHeaders headers) {
if (Utils.isNotNullOrEmpty(config.getUsername()) && Utils.isNotNullOrEmpty(config.getPassword())) {
builder.header("Authorization", basicCredentials(config.getUsername(), config.getPassword()));
} else if (Utils.isNotNullOrEmpty(config.getOauthToken())) {
builder.header("Authorization", "Bearer " + config.getOauthToken());
}
if (config.getCustomHeaders() != null && !config.getCustomHeaders().isEmpty()) {
for (Map.Entry<String, String> entry : config.getCustomHeaders().entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
}
if (config.getUserAgent() != null && !config.getUserAgent().isEmpty()) {
builder.setHeader("User-Agent", config.getUserAgent());
}
}
});
// Impersonator Interceptor
interceptors.put(ImpersonatorInterceptor.NAME, new ImpersonatorInterceptor(config));
// Token Refresh Interceptor
interceptors.put(TokenRefreshInterceptor.NAME, new TokenRefreshInterceptor(config, factory));
// Backwards Compatibility Interceptor
String shouldDisableBackwardsCompatibilityInterceptor = Utils.getSystemPropertyOrEnvVar(KUBERNETES_BACKWARDS_COMPATIBILITY_INTERCEPTOR_DISABLE, "false");
if (!Boolean.parseBoolean(shouldDisableBackwardsCompatibilityInterceptor)) {
interceptors.put(BackwardsCompatibilityInterceptor.NAME, new BackwardsCompatibilityInterceptor());
}
return interceptors;
}
use of io.fabric8.kubernetes.client.http.Interceptor in project kubernetes-client by fabric8io.
the class HttpClientUtilsTest method testCreateApplicableInterceptors.
@Test
void testCreateApplicableInterceptors() {
// Given
Config config = new ConfigBuilder().build();
// When
Collection<Interceptor> interceptorList = HttpClientUtils.createApplicableInterceptors(config, null).values();
// Then
assertThat(interceptorList).isNotNull().hasSize(4).hasAtLeastOneElementOfType(BackwardsCompatibilityInterceptor.class).hasAtLeastOneElementOfType(ImpersonatorInterceptor.class).hasAtLeastOneElementOfType(TokenRefreshInterceptor.class);
}
Aggregations