Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saml http metadata resolver refactor #3894

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package com.amazon.dlic.auth.http.saml;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Timer;

import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.shibboleth.utilities.java.support.resolver.ResolverException;
import org.opensaml.saml.metadata.resolver.impl.AbstractReloadingMetadataResolver;

public class HTTPMetadataResolver extends AbstractReloadingMetadataResolver {
MaciejMierzwa marked this conversation as resolved.
Show resolved Hide resolved
private final static Logger log = LogManager.getLogger(HTTPMetadataResolver.class);
private HttpClient httpClient;
private URI metadataURI;
private String cachedMetadataETag;
private String cachedMetadataLastModified;

public HTTPMetadataResolver(final HttpClient client, final String metadataURL) throws ResolverException {
this(null, client, metadataURL);
}

public HTTPMetadataResolver(final Timer backgroundTaskTimer, final HttpClient client, final String metadataURL)
throws ResolverException {
super(backgroundTaskTimer);

if (client == null) {
throw new ResolverException("HTTP client may not be null");

Check warning on line 48 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L48

Added line #L48 was not covered by tests
}
httpClient = client;

try {
metadataURI = new URI(metadataURL);
} catch (final URISyntaxException e) {
throw new ResolverException("Illegal URL syntax", e);

Check warning on line 55 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L54-L55

Added lines #L54 - L55 were not covered by tests
}
}

public String getMetadataURI() {
return metadataURI.toASCIIString();
}

@Override
protected void doDestroy() {
if (httpClient instanceof AutoCloseable) {
try {
((AutoCloseable) httpClient).close();
} catch (final Exception e) {
log.error("Error closing HTTP client", e);
}

Check warning on line 70 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L67-L70

Added lines #L67 - L70 were not covered by tests
}
httpClient = null;
metadataURI = null;
cachedMetadataETag = null;
cachedMetadataLastModified = null;

Check warning on line 75 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L72-L75

Added lines #L72 - L75 were not covered by tests

super.doDestroy();
}

Check warning on line 78 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L77-L78

Added lines #L77 - L78 were not covered by tests

@Override
protected String getMetadataIdentifier() {
return metadataURI.toString();
}

@Override
protected byte[] fetchMetadata() throws ResolverException {
final HttpGet httpGet = buildHttpGet();
final HttpClientContext context = HttpClientContext.create();

try {
log.debug("{} Attempting to fetch metadata document from '{}'", getLogPrefix(), metadataURI);
return httpClient.execute(httpGet, context, response -> {
final int httpStatusCode = response.getCode();
if (httpStatusCode == HttpStatus.SC_NOT_MODIFIED) {
log.debug("{} Metadata document from '{}' has not changed since last retrieval", getLogPrefix(), getMetadataURI());
return null;

Check warning on line 96 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L95-L96

Added lines #L95 - L96 were not covered by tests
}
if (httpStatusCode != HttpStatus.SC_OK) {
final String errMsg = "Non-ok status code " + httpStatusCode + " returned from remote metadata source " + metadataURI;
log.error("{} " + errMsg, getLogPrefix());
throw new HttpException(errMsg);

Check warning on line 101 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L99-L101

Added lines #L99 - L101 were not covered by tests
}

processConditionalRetrievalHeaders(response);
try {
return getMetadataBytesFromResponse(response);
} catch (ResolverException e) {
final String errMsg = "Error retrieving metadata from " + metadataURI;
throw new HttpException(errMsg, e);

Check warning on line 109 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L107-L109

Added lines #L107 - L109 were not covered by tests
}
});
} catch (final IOException e) {
final String errMsg = "Error retrieving metadata from " + metadataURI;
log.error("{} {}: {}", getLogPrefix(), errMsg, e.getMessage());
throw new ResolverException(errMsg, e);
}
}

protected HttpGet buildHttpGet() {
final HttpGet getMethod = new HttpGet(getMetadataURI());

if (cachedMetadataETag != null) {
getMethod.setHeader(HttpHeaders.IF_NONE_MATCH, cachedMetadataETag);

Check warning on line 123 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L123

Added line #L123 was not covered by tests
}
if (cachedMetadataLastModified != null) {
getMethod.setHeader(HttpHeaders.IF_MODIFIED_SINCE, cachedMetadataLastModified);

Check warning on line 126 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L126

Added line #L126 was not covered by tests
}

return getMethod;
}

protected void processConditionalRetrievalHeaders(final ClassicHttpResponse response) {
Header httpHeader = response.getFirstHeader(HttpHeaders.ETAG);
if (httpHeader != null) {
cachedMetadataETag = httpHeader.getValue();

Check warning on line 135 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L135

Added line #L135 was not covered by tests
}

httpHeader = response.getFirstHeader(HttpHeaders.LAST_MODIFIED);
if (httpHeader != null) {
cachedMetadataLastModified = httpHeader.getValue();

Check warning on line 140 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L140

Added line #L140 was not covered by tests
}
}

protected byte[] getMetadataBytesFromResponse(final ClassicHttpResponse response) throws ResolverException {
log.debug("{} Attempting to extract metadata from response to request for metadata from '{}'", getLogPrefix(), getMetadataURI());
try {
final InputStream ins = response.getEntity().getContent();
return inputstreamToByteArray(ins);
} catch (final IOException e) {
log.error("{} Unable to read response: {}", getLogPrefix(), e.getMessage());
throw new ResolverException("Unable to read response", e);

Check warning on line 151 in src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java#L149-L151

Added lines #L149 - L151 were not covered by tests
} finally {
// Make sure entity has been completely consumed.
EntityUtils.consumeQuietly(response.getEntity());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.HttpStatus;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
import java.security.PrivilegedExceptionAction;
import java.time.Duration;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder;

import org.opensearch.SpecialPermission;
import org.opensearch.common.settings.Settings;

import com.amazon.dlic.util.SettingsBasedSSLConfiguratorV4;
import net.shibboleth.utilities.java.support.resolver.ResolverException;
import org.opensaml.saml.metadata.resolver.impl.HTTPMetadataResolver;

public class SamlHTTPMetadataResolver extends HTTPMetadataResolver {

Expand All @@ -38,10 +43,9 @@
}

@Override
@SuppressWarnings("removal")
protected byte[] fetchMetadata() throws ResolverException {
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<byte[]>) () -> SamlHTTPMetadataResolver.super.fetchMetadata());
return AccessController.doPrivileged((PrivilegedExceptionAction<byte[]>) SamlHTTPMetadataResolver.super::fetchMetadata);
} catch (PrivilegedActionException e) {

if (e.getCause() instanceof ResolverException) {
Expand All @@ -56,7 +60,6 @@
return new SettingsBasedSSLConfiguratorV4(settings, configPath, "idp").buildSSLConfig();
}

@SuppressWarnings("removal")
private static HttpClient createHttpClient(Settings settings, Path configPath) throws Exception {
try {
final SecurityManager sm = System.getSecurityManager();
Expand Down Expand Up @@ -89,7 +92,13 @@
SettingsBasedSSLConfiguratorV4.SSLConfig sslConfig = getSSLConfig(settings, configPath);

if (sslConfig != null) {
builder.setSSLSocketFactory(sslConfig.toSSLConnectionSocketFactory());
SSLConnectionSocketFactory sslConnectionSocketFactory = sslConfig.toSSLConnectionSocketFactory();
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register(URIScheme.HTTPS.id, sslConnectionSocketFactory)
.build();

Check warning on line 98 in src/main/java/com/amazon/dlic/auth/http/saml/SamlHTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/SamlHTTPMetadataResolver.java#L95-L98

Added lines #L95 - L98 were not covered by tests

BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);
builder.setConnectionManager(connectionManager);

Check warning on line 101 in src/main/java/com/amazon/dlic/auth/http/saml/SamlHTTPMetadataResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/auth/http/saml/SamlHTTPMetadataResolver.java#L100-L101

Added lines #L100 - L101 were not covered by tests
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

package com.amazon.dlic.util;

import java.net.Socket;
import java.nio.file.Path;
import java.security.KeyManagementException;
import java.security.KeyStore;
Expand All @@ -25,22 +24,18 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import com.google.common.collect.ImmutableList;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
import org.apache.http.ssl.PrivateKeyDetails;
import org.apache.http.ssl.PrivateKeyStrategy;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -203,20 +198,16 @@
if (enableSslClientAuth) {
if (effectiveKeystore != null) {
try {
sslContextBuilder.loadKeyMaterial(effectiveKeystore, effectiveKeyPassword, new PrivateKeyStrategy() {

@Override
public String chooseAlias(Map<String, PrivateKeyDetails> aliases, Socket socket) {
if (aliases == null || aliases.isEmpty()) {
return effectiveKeyAlias;
}

if (effectiveKeyAlias == null || effectiveKeyAlias.isEmpty()) {
return aliases.keySet().iterator().next();
}

sslContextBuilder.loadKeyMaterial(effectiveKeystore, effectiveKeyPassword, (aliases, socket) -> {
if (aliases == null || aliases.isEmpty()) {
return effectiveKeyAlias;
}

if (effectiveKeyAlias == null || effectiveKeyAlias.isEmpty()) {
return aliases.keySet().iterator().next();

Check warning on line 207 in src/main/java/com/amazon/dlic/util/SettingsBasedSSLConfiguratorV4.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/amazon/dlic/util/SettingsBasedSSLConfiguratorV4.java#L207

Added line #L207 was not covered by tests
}

return effectiveKeyAlias;
});
} catch (UnrecoverableKeyException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -470,10 +461,6 @@
return hostnameVerifier;
}

public SSLIOSessionStrategy toSSLIOSessionStrategy() {
return new SSLIOSessionStrategy(sslContext, supportedProtocols, supportedCipherSuites, hostnameVerifier);
}

public SSLConnectionSocketFactory toSSLConnectionSocketFactory() {
return new SSLConnectionSocketFactory(sslContext, supportedProtocols, supportedCipherSuites, hostnameVerifier);
}
Expand Down
Loading
Loading