-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Adding API for generating SAML SP metadata Resolve #49018 * Adding API for generating SAML SP metadata Resolves #49018 * Adding API for generating SAML SP metadata Resolves #49018 * Adding API for generating SAML SP metadata Resolves #49018 * Adding API for generating SAML SP metadata Resolves #49018 * Adding API for generating SAML SP metadata Resolves #49018 * Adding API for generating SAML SP metadata Resolves #49018 Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
be1a568
commit c6bfc96
Showing
12 changed files
with
327 additions
and
10 deletions.
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 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 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 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 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 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 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 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 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 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 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 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.