Adopt Artifactory client to changed authentication scheme.

Closes #11
This commit is contained in:
Mark Paluch
2022-10-13 17:14:21 +02:00
parent 665b49473b
commit 53edb1b833
2 changed files with 118 additions and 30 deletions

View File

@@ -15,20 +15,14 @@
*/
package org.springframework.data.release.deployment;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.io.IOException;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.release.model.Password;
import org.springframework.data.release.utils.HttpBasicCredentials;
import org.springframework.data.release.utils.HttpComponentsClientHttpRequestFactoryBuilder;
import org.springframework.data.release.utils.Logger;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
@@ -50,28 +44,15 @@ class DeploymentConfiguration {
public RestTemplate artifactoryRestTemplate() {
RestTemplate template = new RestTemplate();
template.setInterceptors(Arrays.asList(new AuthenticatingClientHttpRequestInterceptor(properties)));
HttpComponentsClientHttpRequestFactory factory = HttpComponentsClientHttpRequestFactoryBuilder.builder()
.withAuthentication(properties.getServer().getUri(),
new HttpBasicCredentials(properties.getUsername(), Password.of(properties.getApiKey())))
.build();
template.setRequestFactory(factory);
return template;
}
@RequiredArgsConstructor
private static class AuthenticatingClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
private final @NonNull DeploymentProperties properties;
/*
* (non-Javadoc)
* @see org.springframework.http.client.ClientHttpRequestInterceptor#intercept(org.springframework.http.HttpRequest, byte[], org.springframework.http.client.ClientHttpRequestExecution)
*/
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
request.getHeaders().add("X-Api-Key", properties.getApiKey());
request.getHeaders().add("Authentication", properties.getCredentials().toString());
return execution.execute(request, body);
}
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2022 the original author or authors.
*
* 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 org.springframework.data.release.utils;
import java.io.IOException;
import java.net.URI;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.data.util.Lazy;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
/**
* Builder for {@link HttpComponentsClientHttpRequestFactory}.
*
* @author Mark Paluch
*/
public class HttpComponentsClientHttpRequestFactoryBuilder {
private final CredentialsProvider credsProvider = new BasicCredentialsProvider();
private final AuthCache authCache = new BasicAuthCache();
private HttpComponentsClientHttpRequestFactoryBuilder() {
}
public static HttpComponentsClientHttpRequestFactoryBuilder builder() {
return new HttpComponentsClientHttpRequestFactoryBuilder();
}
/**
* Add pre-emptive authentication to the {@link HttpComponentsClientHttpRequestFactory}.
*
* @param uri
* @param credentials
* @return
*/
public HttpComponentsClientHttpRequestFactoryBuilder withAuthentication(String uri,
HttpBasicCredentials credentials) {
addPreemptiveAuth(credsProvider, authCache, uri, credentials);
return this;
}
/**
* Build the {@link HttpComponentsClientHttpRequestFactory}.
*
* @return
*/
public HttpComponentsClientHttpRequestFactory build() {
Lazy<CloseableHttpClient> lazy = Lazy
.of(() -> HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build());
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory() {
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
setHttpClient(lazy.get());
return super.createRequest(uri, httpMethod);
}
};
factory.setHttpContextFactory((httpMethod, uri) -> {
HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
return context;
});
return factory;
}
private static void addPreemptiveAuth(CredentialsProvider credsProvider, AuthCache authCache, String requestUrl,
HttpBasicCredentials credentials) {
HttpHost host = HttpHost.create(requestUrl);
credsProvider.setCredentials(new AuthScope(host),
new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword().toString()));
authCache.put(host, new BasicScheme());
}
}