-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add Issuer to failed SAML Signature validation logs when available #126310
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
Changes from 3 commits
39fe357
8d4d813
8533c8e
f714d35
312b2ab
198cdf9
7de3149
f5ff8b1
301f6a0
fa9b48c
16cb4b8
de44463
42ebafc
474a79c
4893144
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,13 +161,13 @@ protected static String describe(Collection<X509Credential> credentials) { | |
| return credentials.stream().map(credential -> describe(credential.getEntityCertificate())).collect(Collectors.joining(",")); | ||
| } | ||
|
|
||
| void validateSignature(Signature signature) { | ||
| void validateSignature(Signature signature, Issuer issuer) { | ||
| final String signatureText = text(signature, 32); | ||
| SAMLSignatureProfileValidator profileValidator = new SAMLSignatureProfileValidator(); | ||
| try { | ||
| profileValidator.validate(signature); | ||
| } catch (SignatureException e) { | ||
| throw samlSignatureException(idp.getSigningCredentials(), signatureText, e); | ||
| throw samlSignatureException(issuer, idp.getSigningCredentials(), signatureText, e); | ||
| } | ||
|
|
||
| checkIdpSignature(credential -> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the warning in this method also include the And maybe here too: https://github.com/elastic/elasticsearch/pull/126310/files#diff-19351c3f19f00d928645d11a61ef3c3dea10982b13f8f7ed47773c18d034b33dL234 |
||
|
|
@@ -207,14 +207,14 @@ void validateSignature(Signature signature) { | |
| } catch (PrivilegedActionException e) { | ||
| throw new SecurityException("SecurityException while attempting to validate SAML signature", e); | ||
| } | ||
| }, signatureText); | ||
| }, signatureText, issuer); | ||
| } | ||
|
|
||
| /** | ||
| * Tests whether the provided function returns {@code true} for any of the IdP's signing credentials. | ||
| * @throws ElasticsearchSecurityException - A SAML exception if not matching credential is found. | ||
| * @throws ElasticsearchSecurityException - A SAML exception if no matching credential is found. | ||
| */ | ||
| protected void checkIdpSignature(CheckedFunction<Credential, Boolean, Exception> check, String signatureText) { | ||
| protected void checkIdpSignature(CheckedFunction<Credential, Boolean, Exception> check, String signatureText, Issuer issuer) { | ||
| final Predicate<Credential> predicate = credential -> { | ||
| try { | ||
| return check.apply(credential); | ||
|
|
@@ -237,29 +237,35 @@ protected void checkIdpSignature(CheckedFunction<Credential, Boolean, Exception> | |
| }; | ||
| final List<Credential> credentials = idp.getSigningCredentials(); | ||
| if (credentials.stream().anyMatch(predicate) == false) { | ||
| throw samlSignatureException(credentials, signatureText); | ||
| throw samlSignatureException(issuer, credentials, signatureText); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a SAML specific exception with a consistent message regarding SAML Signature validation failures | ||
| */ | ||
| private ElasticsearchSecurityException samlSignatureException(List<Credential> credentials, String signature, Exception cause) { | ||
| private ElasticsearchSecurityException samlSignatureException( | ||
| Issuer issuer, | ||
| List<Credential> credentials, | ||
| String signature, | ||
| Exception cause | ||
| ) { | ||
| final String issuerValue = issuer != null ? issuer.getValue() : ""; | ||
| logger.warn( | ||
| "The XML Signature of this SAML message cannot be validated. Please verify that the saml realm uses the correct SAML" | ||
| + "metadata file/URL for this Identity Provider" | ||
| "The XML Signature of this SAML message cannot be validated. Please verify that the saml realm uses the correct SAML " | ||
| + "metadata file/URL for this Identity Provider {}", | ||
|
||
| issuerValue | ||
| ); | ||
| final String msg = "SAML Signature [{}] could not be validated against [{}]"; | ||
| return samlException(msg, cause, signature, describeCredentials(credentials)); | ||
| if (cause != null) { | ||
| return samlException(msg, cause, signature, describeCredentials(credentials)); | ||
| } else { | ||
| return samlException(msg, signature, describeCredentials(credentials)); | ||
| } | ||
| } | ||
|
|
||
| private ElasticsearchSecurityException samlSignatureException(List<Credential> credentials, String signature) { | ||
| logger.warn( | ||
| "The XML Signature of this SAML message cannot be validated. Please verify that the saml realm uses the correct SAML" | ||
| + "metadata file/URL for this Identity Provider" | ||
| ); | ||
| final String msg = "SAML Signature [{}] could not be validated against [{}]"; | ||
| return samlException(msg, signature, describeCredentials(credentials)); | ||
| private ElasticsearchSecurityException samlSignatureException(Issuer issuer, List<Credential> credentials, String signature) { | ||
| return samlSignatureException(issuer, credentials, signature, null); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove duplication (since I need to edit the exact same log message twice) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| } | ||
|
|
||
| private static String describeCredentials(List<Credential> credentials) { | ||
|
|
@@ -423,7 +429,7 @@ private void validateSignature(String inputString, String signatureAlgorithm, St | |
| ); | ||
| return false; | ||
| } | ||
| }, signatureText); | ||
| }, signatureText, null); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the context of this is SAML Logout with a query string; the message gets parsed into a SAML document after the signature has been verified, so I'm not sure it's possible to pass an Issuer here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree. We could change the order of things here so the issuer is parsed from the message before throwing the error, but if that parsing fails, it ends up causing more confusion than help so I think it makes sense to leave it as it. |
||
| } | ||
|
|
||
| protected byte[] decodeBase64(String content) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,6 +106,9 @@ public class SamlAuthenticatorTests extends SamlResponseHandlerTests { | |
| + "Attributes with a name clash may prevent authentication or interfere will role mapping. " | ||
| + "Change your IdP configuration to use a different attribute *" | ||
| + " that will not clash with any of [*]"; | ||
| private static final String SIGNATURE_VALIDATION_FAILED_LOG_MESSAGE = "The XML Signature of this SAML message cannot be validated. " | ||
| + "Please verify that the saml realm uses the correct SAML metadata file/URL for this Identity Provider " | ||
| + "https://idp.saml.elastic.test/"; | ||
|
|
||
| private SamlAuthenticator authenticator; | ||
|
|
||
|
|
@@ -741,16 +744,29 @@ public void testIncorrectSigningKeyIsRejected() throws Exception { | |
| // check that the content is valid when signed by the correct key-pair | ||
| assertThat(authenticator.authenticate(token(signer.transform(xml, idpSigningCertificatePair))), notNullValue()); | ||
|
|
||
| // check is rejected when signed by a different key-pair | ||
| final Tuple<X509Certificate, PrivateKey> wrongKey = readKeyPair("RSA_4096_updated"); | ||
| final ElasticsearchSecurityException exception = expectThrows( | ||
| ElasticsearchSecurityException.class, | ||
| () -> authenticator.authenticate(token(signer.transform(xml, wrongKey))) | ||
| ); | ||
| assertThat(exception.getMessage(), containsString("SAML Signature")); | ||
| assertThat(exception.getMessage(), containsString("could not be validated")); | ||
| assertThat(exception.getCause(), nullValue()); | ||
| assertThat(SamlUtils.isSamlException(exception), is(true)); | ||
| try (var mockLog = MockLog.capture(authenticator.getClass())) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update existing tests to check for log message There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| mockLog.addExpectation( | ||
| new MockLog.SeenEventExpectation( | ||
| "Invalid Signature", | ||
| authenticator.getClass().getName(), | ||
| Level.WARN, | ||
| SIGNATURE_VALIDATION_FAILED_LOG_MESSAGE | ||
| ) | ||
| ); | ||
|
|
||
| // check is rejected when signed by a different key-pair | ||
| final Tuple<X509Certificate, PrivateKey> wrongKey = readKeyPair("RSA_4096_updated"); | ||
| final ElasticsearchSecurityException exception = expectThrows( | ||
| ElasticsearchSecurityException.class, | ||
| () -> authenticator.authenticate(token(signer.transform(xml, wrongKey))) | ||
| ); | ||
| assertThat(exception.getMessage(), containsString("SAML Signature")); | ||
| assertThat(exception.getMessage(), containsString("could not be validated")); | ||
| assertThat(exception.getCause(), nullValue()); | ||
| assertThat(SamlUtils.isSamlException(exception), is(true)); | ||
|
|
||
| mockLog.assertAllExpectationsMatched(); | ||
| } | ||
| } | ||
|
|
||
| public void testSigningKeyIsReloadedForEachRequest() throws Exception { | ||
|
|
@@ -1301,24 +1317,54 @@ public void testFailureWhenIdPCredentialsAreEmpty() throws Exception { | |
| authenticator = buildAuthenticator(() -> emptyList(), emptyList()); | ||
| final String xml = getSimpleResponseAsString(clock.instant()); | ||
| final SamlToken token = token(signResponse(xml)); | ||
| final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); | ||
| assertThat(exception.getCause(), nullValue()); | ||
| assertThat(exception.getMessage(), containsString("SAML Signature")); | ||
| assertThat(exception.getMessage(), containsString("could not be validated")); | ||
| // Restore the authenticator with credentials for the rest of the test cases | ||
| authenticator = buildAuthenticator(() -> buildOpenSamlCredential(idpSigningCertificatePair), emptyList()); | ||
|
|
||
| try (var mockLog = MockLog.capture(authenticator.getClass())) { | ||
| mockLog.addExpectation( | ||
| new MockLog.SeenEventExpectation( | ||
| "Invalid signature", | ||
| authenticator.getClass().getName(), | ||
| Level.WARN, | ||
| SIGNATURE_VALIDATION_FAILED_LOG_MESSAGE | ||
| ) | ||
| ); | ||
|
|
||
| final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); | ||
| assertThat(exception.getCause(), nullValue()); | ||
| assertThat(exception.getMessage(), containsString("SAML Signature")); | ||
| assertThat(exception.getMessage(), containsString("could not be validated")); | ||
|
|
||
| mockLog.awaitAllExpectationsMatched(); | ||
| } finally { | ||
| // Restore the authenticator with credentials for the rest of the test cases | ||
richard-dennehy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| authenticator = buildAuthenticator(() -> buildOpenSamlCredential(idpSigningCertificatePair), emptyList()); | ||
richard-dennehy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| public void testFailureWhenIdPCredentialsAreNull() throws Exception { | ||
| authenticator = buildAuthenticator(() -> singletonList(null), emptyList()); | ||
| final String xml = getSimpleResponseAsString(clock.instant()); | ||
| final SamlToken token = token(signResponse(xml)); | ||
| final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); | ||
| assertThat(exception.getCause(), nullValue()); | ||
| assertThat(exception.getMessage(), containsString("SAML Signature")); | ||
| assertThat(exception.getMessage(), containsString("could not be validated")); | ||
| // Restore the authenticator with credentials for the rest of the test cases | ||
| authenticator = buildAuthenticator(() -> buildOpenSamlCredential(idpSigningCertificatePair), emptyList()); | ||
|
|
||
| try (var mockLog = MockLog.capture(authenticator.getClass())) { | ||
| mockLog.addExpectation( | ||
| new MockLog.SeenEventExpectation( | ||
| "Invalid signature", | ||
| authenticator.getClass().getName(), | ||
| Level.WARN, | ||
| SIGNATURE_VALIDATION_FAILED_LOG_MESSAGE | ||
| ) | ||
| ); | ||
|
|
||
| final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); | ||
| assertThat(exception.getCause(), nullValue()); | ||
| assertThat(exception.getMessage(), containsString("SAML Signature")); | ||
| assertThat(exception.getMessage(), containsString("could not be validated")); | ||
|
|
||
| mockLog.awaitAllExpectationsMatched(); | ||
| } finally { | ||
| // Restore the authenticator with credentials for the rest of the test cases | ||
richard-dennehy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| authenticator = buildAuthenticator(() -> buildOpenSamlCredential(idpSigningCertificatePair), emptyList()); | ||
| } | ||
| } | ||
|
|
||
| private interface CryptoTransform { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.