-
Notifications
You must be signed in to change notification settings - Fork 319
Saml http metadata resolver refactor #3894
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
Closed
MaciejMierzwa
wants to merge
2
commits into
opensearch-project:main
from
MaciejMierzwa:SamlHTTPMetadataResolver-refactor
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
src/main/java/com/amazon/dlic/auth/http/saml/HTTPMetadataResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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"); | ||
} | ||
httpClient = client; | ||
|
||
try { | ||
metadataURI = new URI(metadataURL); | ||
} catch (final URISyntaxException e) { | ||
throw new ResolverException("Illegal URL syntax", e); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} | ||
httpClient = null; | ||
metadataURI = null; | ||
cachedMetadataETag = null; | ||
cachedMetadataLastModified = null; | ||
|
||
super.doDestroy(); | ||
} | ||
|
||
@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; | ||
} | ||
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); | ||
} | ||
|
||
processConditionalRetrievalHeaders(response); | ||
try { | ||
return getMetadataBytesFromResponse(response); | ||
} catch (ResolverException e) { | ||
final String errMsg = "Error retrieving metadata from " + metadataURI; | ||
throw new HttpException(errMsg, e); | ||
} | ||
}); | ||
} 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); | ||
} | ||
if (cachedMetadataLastModified != null) { | ||
getMethod.setHeader(HttpHeaders.IF_MODIFIED_SINCE, cachedMetadataLastModified); | ||
} | ||
|
||
return getMethod; | ||
} | ||
|
||
protected void processConditionalRetrievalHeaders(final ClassicHttpResponse response) { | ||
Header httpHeader = response.getFirstHeader(HttpHeaders.ETAG); | ||
if (httpHeader != null) { | ||
cachedMetadataETag = httpHeader.getValue(); | ||
} | ||
|
||
httpHeader = response.getFirstHeader(HttpHeaders.LAST_MODIFIED); | ||
if (httpHeader != null) { | ||
cachedMetadataLastModified = httpHeader.getValue(); | ||
} | ||
} | ||
|
||
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); | ||
} finally { | ||
// Make sure entity has been completely consumed. | ||
EntityUtils.consumeQuietly(response.getEntity()); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.