-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Adding API for generating SAML SP metadata #64517
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
860fd30
Adding API for generating SAML SP metadata
75b6c95
Merge branch 'master' into SAML_SP_API
elasticmachine 1180e64
Adding API for generating SAML SP metadata
1b18d9f
Adding API for generating SAML SP metadata
82f663f
Merge branch 'master' into SAML_SP_API
elasticmachine b076e6c
Adding API for generating SAML SP metadata
7ac8d28
Merge branch 'master' into SAML_SP_API
elasticmachine 9d1a098
Adding API for generating SAML SP metadata
aaa823b
Merge branch 'SAML_SP_API' of github.com:BigPandaToo/elasticsearch in…
337f23c
Adding API for generating SAML SP metadata
6567e0f
Adding API for generating SAML SP metadata
1687517
Merge branch 'master' into SAML_SP_API
elasticmachine 71f3899
Merge branch 'master' into SAML_SP_API
elasticmachine a3bd935
Merge branch 'master' into SAML_SP_API
elasticmachine 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
18 changes: 18 additions & 0 deletions
18
...src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataAction.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,18 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.action.saml; | ||
|
||
import org.elasticsearch.action.ActionType; | ||
|
||
public class SamlSpMetadataAction extends ActionType<SamlSpMetadataResponse> { | ||
public static final String NAME = "cluster:monitor/xpack/security/saml/metadata"; | ||
public static final SamlSpMetadataAction INSTANCE = new SamlSpMetadataAction(); | ||
|
||
private SamlSpMetadataAction() { | ||
super(NAME, SamlSpMetadataResponse::new); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...rc/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataRequest.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,61 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.action.saml; | ||
|
||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.elasticsearch.action.ValidateActions.addValidationError; | ||
|
||
public class SamlSpMetadataRequest extends ActionRequest { | ||
|
||
private String realmName; | ||
|
||
public SamlSpMetadataRequest(StreamInput in) throws IOException { | ||
super(in); | ||
realmName = in.readOptionalString(); | ||
} | ||
|
||
public SamlSpMetadataRequest(String realmName) { | ||
this.realmName = realmName; | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
ActionRequestValidationException validationException = null; | ||
if (Strings.hasText(realmName) == false) { | ||
validationException = addValidationError("Realm name may not be empty", validationException); | ||
} | ||
return validationException; | ||
} | ||
|
||
public String getRealmName() { | ||
return realmName; | ||
} | ||
|
||
public void setRealmName(String realmName) { | ||
this.realmName = realmName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "{" + | ||
"realmName=" + realmName + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeOptionalString(realmName); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...c/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataResponse.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,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.action.saml; | ||
|
||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Response containing a SAML SP metadata for a specific realm as XML. | ||
*/ | ||
public class SamlSpMetadataResponse extends ActionResponse { | ||
public String getXMLString() { | ||
return XMLString; | ||
} | ||
|
||
private String XMLString; | ||
|
||
public SamlSpMetadataResponse(StreamInput in) throws IOException { | ||
super(in); | ||
XMLString = in.readString(); | ||
} | ||
|
||
public SamlSpMetadataResponse(String XMLString) { | ||
this.XMLString = XMLString; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(XMLString); | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
...st/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataRequestTests.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,41 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.action.saml; | ||
|
||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
|
||
public class SamlSpMetadataRequestTests extends ESTestCase { | ||
|
||
public void testValidateFailsWhenRealmEmpty() { | ||
final SamlSpMetadataRequest samlSPMetadataRequest = new SamlSpMetadataRequest(""); | ||
final ActionRequestValidationException validationException = samlSPMetadataRequest.validate(); | ||
assertThat(validationException.getMessage(), containsString("Realm name may not be empty")); | ||
} | ||
|
||
public void testValidateSerialization() throws IOException { | ||
final SamlSpMetadataRequest samlSPMetadataRequest = new SamlSpMetadataRequest("saml1"); | ||
try (BytesStreamOutput out = new BytesStreamOutput()) { | ||
samlSPMetadataRequest.writeTo(out); | ||
try (StreamInput in = out.bytes().streamInput()) { | ||
final SamlSpMetadataRequest serialized = new SamlSpMetadataRequest(in); | ||
assertEquals(samlSPMetadataRequest.getRealmName(), serialized.getRealmName()); | ||
} | ||
} | ||
} | ||
|
||
public void testValidateToString() { | ||
final SamlSpMetadataRequest samlSPMetadataRequest = new SamlSpMetadataRequest("saml1"); | ||
assertThat(samlSPMetadataRequest.toString(), containsString("{realmName=saml1}")); | ||
} | ||
} |
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
88 changes: 88 additions & 0 deletions
88
...main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlSpMetadataAction.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,88 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.security.action.saml; | ||
|
||
import org.apache.logging.log4j.message.ParameterizedMessage; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.action.support.HandledTransportAction; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.tasks.Task; | ||
import org.elasticsearch.transport.TransportService; | ||
import org.elasticsearch.xpack.core.security.action.saml.SamlSpMetadataAction; | ||
import org.elasticsearch.xpack.core.security.action.saml.SamlSpMetadataRequest; | ||
import org.elasticsearch.xpack.core.security.action.saml.SamlSpMetadataResponse; | ||
import org.elasticsearch.xpack.security.authc.Realms; | ||
import org.elasticsearch.xpack.security.authc.saml.SamlRealm; | ||
import org.elasticsearch.xpack.security.authc.saml.SamlSpMetadataBuilder; | ||
import org.elasticsearch.xpack.security.authc.saml.SamlUtils; | ||
import org.elasticsearch.xpack.security.authc.saml.SpConfiguration; | ||
import org.opensaml.saml.saml2.core.AuthnRequest; | ||
import org.opensaml.saml.saml2.metadata.EntityDescriptor; | ||
import org.opensaml.saml.saml2.metadata.impl.EntityDescriptorMarshaller; | ||
import org.w3c.dom.Element; | ||
|
||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.dom.DOMSource; | ||
import javax.xml.transform.stream.StreamResult; | ||
import java.io.StringWriter; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import static org.elasticsearch.xpack.security.authc.saml.SamlRealm.findSamlRealms; | ||
|
||
/** | ||
* Transport action responsible for generating a SAML SP Metadata. | ||
*/ | ||
public class TransportSamlSpMetadataAction | ||
extends HandledTransportAction<SamlSpMetadataRequest, SamlSpMetadataResponse> { | ||
|
||
private final Realms realms; | ||
|
||
@Inject | ||
public TransportSamlSpMetadataAction(TransportService transportService, ActionFilters actionFilters, Realms realms) { | ||
super(SamlSpMetadataAction.NAME, transportService, actionFilters, SamlSpMetadataRequest::new | ||
); | ||
this.realms = realms; | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, SamlSpMetadataRequest request, | ||
ActionListener<SamlSpMetadataResponse> listener) { | ||
List<SamlRealm> realms = findSamlRealms(this.realms, request.getRealmName(), null); | ||
if (realms.isEmpty()) { | ||
listener.onFailure(SamlUtils.samlException("Cannot find any matching realm for [{}]", request.getRealmName())); | ||
} else if (realms.size() > 1) { | ||
listener.onFailure(SamlUtils.samlException("Found multiple matching realms [{}] for [{}]", realms, request.getRealmName())); | ||
} else { | ||
prepareMetadata(realms.get(0), listener); | ||
} | ||
} | ||
|
||
private void prepareMetadata(SamlRealm realm, ActionListener<SamlSpMetadataResponse> listener) { | ||
try { | ||
final EntityDescriptorMarshaller marshaller = new EntityDescriptorMarshaller(); | ||
final SpConfiguration spConfig = realm.getServiceProvider(); | ||
final SamlSpMetadataBuilder builder = new SamlSpMetadataBuilder(Locale.getDefault(), spConfig.getEntityId()) | ||
.assertionConsumerServiceUrl(spConfig.getAscUrl()) | ||
.singleLogoutServiceUrl(spConfig.getLogoutUrl()) | ||
.encryptionCredentials(spConfig.getEncryptionCredentials()) | ||
.signingCredential(spConfig.getSigningConfiguration().getCredential()) | ||
.authnRequestsSigned(spConfig.getSigningConfiguration().shouldSign(AuthnRequest.DEFAULT_ELEMENT_LOCAL_NAME)); | ||
final EntityDescriptor descriptor = builder.build(); | ||
final Element element = marshaller.marshall(descriptor); | ||
final StringWriter writer = new StringWriter(); | ||
final Transformer serializer = SamlUtils.getHardenedXMLTransformer(); | ||
serializer.transform(new DOMSource(element), new StreamResult(writer)); | ||
listener.onResponse(new SamlSpMetadataResponse(writer.toString())); | ||
} catch (Exception e) { | ||
logger.error(new ParameterizedMessage( | ||
"Error during SAML SP metadata generation for realm [{}]", realm.name()), e); | ||
listener.onFailure(e); | ||
} | ||
} | ||
} |
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
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.