|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.security.action.saml; |
| 8 | + |
| 9 | +import org.apache.logging.log4j.message.ParameterizedMessage; |
| 10 | +import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.action.support.ActionFilters; |
| 12 | +import org.elasticsearch.action.support.HandledTransportAction; |
| 13 | +import org.elasticsearch.common.inject.Inject; |
| 14 | +import org.elasticsearch.tasks.Task; |
| 15 | +import org.elasticsearch.transport.TransportService; |
| 16 | +import org.elasticsearch.xpack.core.security.action.saml.SamlSpMetadataAction; |
| 17 | +import org.elasticsearch.xpack.core.security.action.saml.SamlSpMetadataRequest; |
| 18 | +import org.elasticsearch.xpack.core.security.action.saml.SamlSpMetadataResponse; |
| 19 | +import org.elasticsearch.xpack.security.authc.Realms; |
| 20 | +import org.elasticsearch.xpack.security.authc.saml.SamlRealm; |
| 21 | +import org.elasticsearch.xpack.security.authc.saml.SamlSpMetadataBuilder; |
| 22 | +import org.elasticsearch.xpack.security.authc.saml.SamlUtils; |
| 23 | +import org.elasticsearch.xpack.security.authc.saml.SpConfiguration; |
| 24 | +import org.opensaml.saml.saml2.core.AuthnRequest; |
| 25 | +import org.opensaml.saml.saml2.metadata.EntityDescriptor; |
| 26 | +import org.opensaml.saml.saml2.metadata.impl.EntityDescriptorMarshaller; |
| 27 | +import org.w3c.dom.Element; |
| 28 | + |
| 29 | +import javax.xml.transform.Transformer; |
| 30 | +import javax.xml.transform.dom.DOMSource; |
| 31 | +import javax.xml.transform.stream.StreamResult; |
| 32 | +import java.io.StringWriter; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Locale; |
| 35 | + |
| 36 | +import static org.elasticsearch.xpack.security.authc.saml.SamlRealm.findSamlRealms; |
| 37 | + |
| 38 | +/** |
| 39 | + * Transport action responsible for generating a SAML SP Metadata. |
| 40 | + */ |
| 41 | +public class TransportSamlSpMetadataAction |
| 42 | + extends HandledTransportAction<SamlSpMetadataRequest, SamlSpMetadataResponse> { |
| 43 | + |
| 44 | + private final Realms realms; |
| 45 | + |
| 46 | + @Inject |
| 47 | + public TransportSamlSpMetadataAction(TransportService transportService, ActionFilters actionFilters, Realms realms) { |
| 48 | + super(SamlSpMetadataAction.NAME, transportService, actionFilters, SamlSpMetadataRequest::new |
| 49 | + ); |
| 50 | + this.realms = realms; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void doExecute(Task task, SamlSpMetadataRequest request, |
| 55 | + ActionListener<SamlSpMetadataResponse> listener) { |
| 56 | + List<SamlRealm> realms = findSamlRealms(this.realms, request.getRealmName(), null); |
| 57 | + if (realms.isEmpty()) { |
| 58 | + listener.onFailure(SamlUtils.samlException("Cannot find any matching realm for [{}]", request.getRealmName())); |
| 59 | + } else if (realms.size() > 1) { |
| 60 | + listener.onFailure(SamlUtils.samlException("Found multiple matching realms [{}] for [{}]", realms, request.getRealmName())); |
| 61 | + } else { |
| 62 | + prepareMetadata(realms.get(0), listener); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private void prepareMetadata(SamlRealm realm, ActionListener<SamlSpMetadataResponse> listener) { |
| 67 | + try { |
| 68 | + final EntityDescriptorMarshaller marshaller = new EntityDescriptorMarshaller(); |
| 69 | + final SpConfiguration spConfig = realm.getServiceProvider(); |
| 70 | + final SamlSpMetadataBuilder builder = new SamlSpMetadataBuilder(Locale.getDefault(), spConfig.getEntityId()) |
| 71 | + .assertionConsumerServiceUrl(spConfig.getAscUrl()) |
| 72 | + .singleLogoutServiceUrl(spConfig.getLogoutUrl()) |
| 73 | + .encryptionCredentials(spConfig.getEncryptionCredentials()) |
| 74 | + .signingCredential(spConfig.getSigningConfiguration().getCredential()) |
| 75 | + .authnRequestsSigned(spConfig.getSigningConfiguration().shouldSign(AuthnRequest.DEFAULT_ELEMENT_LOCAL_NAME)); |
| 76 | + final EntityDescriptor descriptor = builder.build(); |
| 77 | + final Element element = marshaller.marshall(descriptor); |
| 78 | + final StringWriter writer = new StringWriter(); |
| 79 | + final Transformer serializer = SamlUtils.getHardenedXMLTransformer(); |
| 80 | + serializer.transform(new DOMSource(element), new StreamResult(writer)); |
| 81 | + listener.onResponse(new SamlSpMetadataResponse(writer.toString())); |
| 82 | + } catch (Exception e) { |
| 83 | + logger.error(new ParameterizedMessage( |
| 84 | + "Error during SAML SP metadata generation for realm [{}]", realm.name()), e); |
| 85 | + listener.onFailure(e); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments