Skip to content

Commit

Permalink
secureCodeBox#121 Extract Proxy Credential Facotry from Intermediate …
Browse files Browse the repository at this point in the history
…Class

Signed-off-by: Sven Strittmatter <[email protected]>
  • Loading branch information
Weltraumschaf committed Feb 16, 2024
1 parent 574cf01 commit 2f23058
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,11 @@
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
* Placeholder to move duplicated code, will be named better later
*/
Expand All @@ -36,16 +27,13 @@ public Foo(@NonNull final Config config, @NonNull final ProxyConfig proxyConfig)
this.proxyConfig = proxyConfig;
}




public RestTemplate createRestTemplate() {
if (proxyConfig.isComplete()) {
// Configuring Proxy Authentication explicitly as it isn't done by default for spring rest templates :(
final var builder = HttpClientBuilder.create()
.useSystemProperties()
.setProxy(createHttpHost())
.setDefaultCredentialsProvider(createCredentialsProvider())
.setDefaultCredentialsProvider(new ProxyCredentialFactory(proxyConfig).createCredentialsProvider())
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());

final var factory = new HttpComponentsClientHttpRequestFactory();
Expand All @@ -57,20 +45,6 @@ public RestTemplate createRestTemplate() {
return new RestTemplate();
}

CredentialsProvider createCredentialsProvider() {
final var provider = new BasicCredentialsProvider();
provider.setCredentials(createAuthScope(), createCredentials());
return provider;
}

AuthScope createAuthScope() {
return new AuthScope(proxyConfig.getHost(), proxyConfig.getPort());
}

Credentials createCredentials() {
return new UsernamePasswordCredentials(proxyConfig.getUser(), proxyConfig.getPassword());
}

HttpHost createHttpHost() {
return new HttpHost(proxyConfig.getHost(), proxyConfig.getPort());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.securecodebox.persistence.defectdojo.http;

import lombok.NonNull;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;

/**
* Utility class to create credentials to authenticate against a HTTP proxy
*/
final class ProxyCredentialFactory {
private final ProxyConfig proxyConfig;

ProxyCredentialFactory(@NonNull ProxyConfig proxyConfig) {
super();
this.proxyConfig = proxyConfig;
}

CredentialsProvider createCredentialsProvider() {
final var provider = new BasicCredentialsProvider();
provider.setCredentials(createAuthScope(), createCredentials());
return provider;
}

AuthScope createAuthScope() {
return new AuthScope(proxyConfig.getHost(), proxyConfig.getPort());
}

Credentials createCredentials() {
return new UsernamePasswordCredentials(proxyConfig.getUser(), proxyConfig.getPassword());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,7 @@ class FooTest {
.build();
private final Foo sut = new Foo(config, proxyConfig);

@Test
void createCredentialsProvider() {
final var result = sut.createCredentialsProvider();
final var credentials = result.getCredentials(sut.createAuthScope());

assertAll(
() -> assertThat(credentials.getUserPrincipal().getName(), is(proxyConfig.getUser())),
() -> assertThat(credentials.getPassword(), is(proxyConfig.getPassword()))
);
}

@Test
void createAuthScope() {
final var result = sut.createAuthScope();

assertAll(
() -> assertThat(result.getHost(), is(proxyConfig.getHost())),
() -> assertThat(result.getPort(), is(proxyConfig.getPort()))
);
}

@Test
void createCredentials() {
final var result = sut.createCredentials();

assertAll(
() -> assertThat(result.getUserPrincipal().getName(), is(proxyConfig.getUser())),
() -> assertThat(result.getPassword(), is(proxyConfig.getPassword()))
);
}

@Test
void createHttpHost() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.securecodebox.persistence.defectdojo.http;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertAll;

/**
* Tests for {@link ProxyCredentialFactory}
*/
class ProxyCredentialFactoryTest {
private final ProxyConfig config = ProxyConfig.builder()
.user("user")
.password("pw")
.host("host")
.port(42)
.build();
private final ProxyCredentialFactory sut = new ProxyCredentialFactory(config);

@Test
void createCredentialsProvider() {
final var result = sut.createCredentialsProvider();
final var credentials = result.getCredentials(sut.createAuthScope());

assertAll(
() -> assertThat(credentials.getUserPrincipal().getName(), is(config.getUser())),
() -> assertThat(credentials.getPassword(), is(config.getPassword()))
);
}

@Test
void createAuthScope() {
final var result = sut.createAuthScope();

assertAll(
() -> assertThat(result.getHost(), is(config.getHost())),
() -> assertThat(result.getPort(), is(config.getPort()))
);
}

@Test
void createCredentials() {
final var result = sut.createCredentials();

assertAll(
() -> assertThat(result.getUserPrincipal().getName(), is(config.getUser())),
() -> assertThat(result.getPassword(), is(config.getPassword()))
);
}
}

0 comments on commit 2f23058

Please sign in to comment.