forked from Hithomelabs/CFTunnels
feat(portainer-automation): add SSL-friendly Portainer client config
This commit is contained in:
parent
934869a108
commit
7942f47eb8
@ -0,0 +1,60 @@
|
|||||||
|
package com.hithomelabs.portainer.config;
|
||||||
|
|
||||||
|
import com.hithomelabs.common.portainer.client.PortainerApiClient;
|
||||||
|
|
||||||
|
import org.apache.hc.client5.http.classic.HttpClient;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||||
|
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
|
||||||
|
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
|
||||||
|
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
|
||||||
|
import org.apache.hc.client5.http.ssl.TrustAllStrategy;
|
||||||
|
import org.apache.hc.core5.ssl.SSLContexts;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.KeyStoreException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class PortainerClientConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestTemplate portainerRestTemplate() {
|
||||||
|
try {
|
||||||
|
SSLContext sslContext = SSLContexts.custom()
|
||||||
|
.loadTrustMaterial(TrustAllStrategy.INSTANCE)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
SSLConnectionSocketFactory sslSocketFactory =
|
||||||
|
new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
|
||||||
|
|
||||||
|
HttpClient httpClient = HttpClients.custom()
|
||||||
|
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
|
||||||
|
.setSSLSocketFactory(sslSocketFactory)
|
||||||
|
.build())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpComponentsClientHttpRequestFactory factory =
|
||||||
|
new HttpComponentsClientHttpRequestFactory(httpClient);
|
||||||
|
factory.setConnectTimeout(10_000);
|
||||||
|
factory.setConnectionRequestTimeout(10_000);
|
||||||
|
|
||||||
|
return new RestTemplate(factory);
|
||||||
|
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
|
||||||
|
throw new IllegalStateException("Failed to create SSL-friendly RestTemplate", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PortainerApiClient portainerApiClient(PortainerAutomationProperties properties,
|
||||||
|
RestTemplate portainerRestTemplate) {
|
||||||
|
PortainerApiClient client =
|
||||||
|
new PortainerApiClient(portainerRestTemplate, properties.getBaseUrl());
|
||||||
|
client.authenticateWithApiKey(properties.getApiKey());
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user