Skip to content
This repository has been archived by the owner on Dec 31, 2021. It is now read-only.

Commit

Permalink
Merge pull request #62 from MS3Inc/master
Browse files Browse the repository at this point in the history
- Add debug logs  when creating a client from a `keycloak.json`.
- Support the option `disable-trust-manager` and `allow-any-hostname` of `keycloak.json` to disable SSL verification in test environments.
  • Loading branch information
flytreeleft authored Dec 19, 2020
2 parents 8fd053d + b5c29b7 commit 7135c80
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class KeycloakAuthenticatingRealm extends AuthorizingRealm {

private final Logger logger = LoggerFactory.getLogger(getClass());

private NexusKeycloakClient client;
private final NexusKeycloakClient client;

public KeycloakAuthenticatingRealm() {
this(NexusKeycloakClientLoader.loadDefaultClient());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;

import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.shiro.util.StringUtils;
import org.github.flytreeleft.nexus3.keycloak.plugin.internal.http.ClientAuthenticator;
import org.github.flytreeleft.nexus3.keycloak.plugin.internal.http.Http;
Expand Down Expand Up @@ -302,7 +309,23 @@ public AdapterConfig getConfig() {

public synchronized Http getHttp() {
if (this.http == null) {
HttpClient httpClient = HttpClients.createDefault();
HttpClient httpClient = null;

try {
HttpClientBuilder builder = HttpClients.custom();

if (this.config.isDisableTrustManager()) {
builder.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build());
}
if (this.config.isAllowAnyHostname()) {
builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
}

httpClient = builder.build();
} catch (Exception e) {
throw new IllegalArgumentException(e);
}

ClientAuthenticator clientAuthenticator = (HttpMethod httpMethod) -> {
String token = getTokenManager().getAccessTokenString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import org.apache.commons.io.FileUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.nexus.security.role.Role;
import org.sonatype.nexus.security.user.User;
import org.sonatype.nexus.security.user.UserSearchCriteria;
Expand All @@ -17,6 +19,7 @@ public class NexusKeycloakClientLoader {
public static final String DEFAULT_0_CONFIG = "keycloak.0.json";
public static final String DEFAULT_1_CONFIG = "keycloak.1.json";
public static final String DEFAULT_2_CONFIG = "keycloak.2.json";
private static final Logger LOGGER = LoggerFactory.getLogger(NexusKeycloakClientLoader.class);

private static final Map<String, NexusKeycloakClient> clientMap = new HashMap<>();

Expand All @@ -42,11 +45,14 @@ public synchronized static NexusKeycloakClient loadClient(
NexusKeycloakClient client = clientMap.get(keycloakConfigName);

if (client == null) {
LOGGER.debug("Attempting to instantiate new client...");
File config = FileUtils.getFile(".", "etc", keycloakConfigName);
if (config.exists()) {
client = new NexusKeycloakClient(source, sourceCode, config);

clientMap.put(keycloakConfigName, client);
} else {
LOGGER.debug(config.getAbsolutePath() + " file not found, will create no-op client");
}
}
return client != null ? client : new NoopNexusKeycloakClient(source);
Expand Down

0 comments on commit 7135c80

Please sign in to comment.