[java-plugin-shibd-saml] branch main updated: Use Spring to load metadata templates as a Resource.
Codeberg
noreply at shibboleth.net
Wed Nov 11 17:19:30 UTC 2026
This is an automated email from the git hooks/post-receive script.
codeberg pushed a commit to branch main
in repository java-plugin-shibd-saml.
View the commit online:
https://codeberg.org/Shibboleth/java-plugin-shibd-saml/commit/a4ccb98664894e359a761e5834a69a7bb6c4ba51
The following commit(s) were added to refs/heads/main by this push:
new a4ccb98 Use Spring to load metadata templates as a Resource.
a4ccb98 is described below
commit a4ccb98664894e359a761e5834a69a7bb6c4ba51
Author: Scott Cantor <scott at restingparrotsoftware.com>
AuthorDate: Wed Nov 11 12:19:10 2026 -0500
Use Spring to load metadata templates as a Resource.
---
.../MetadataPublishingProfileConfiguration.java | 4 +-
.../sp/saml/flows/saml2/MetadataFlowTest.java | 44 ++++++++---------
.../idp/module/conf/sp/saml-test-agents.xml | 10 +++-
.../sp/saml/saml2/profile/impl/AddMetadata.java | 55 +++++++++++++++++-----
4 files changed, 73 insertions(+), 40 deletions(-)
diff --git a/sp-saml-api/src/main/java/net/shibboleth/sp/saml/saml2/profile/config/MetadataPublishingProfileConfiguration.java b/sp-saml-api/src/main/java/net/shibboleth/sp/saml/saml2/profile/config/MetadataPublishingProfileConfiguration.java
index c8d10f3..ee71979 100644
--- a/sp-saml-api/src/main/java/net/shibboleth/sp/saml/saml2/profile/config/MetadataPublishingProfileConfiguration.java
+++ b/sp-saml-api/src/main/java/net/shibboleth/sp/saml/saml2/profile/config/MetadataPublishingProfileConfiguration.java
@@ -35,7 +35,7 @@ public interface MetadataPublishingProfileConfiguration extends ProfileConfigura
@Nonnull @NotEmpty static final String PROFILE_ID = "http://shibboleth.net/ns/profiles/saml2/metadata";
/**
- * Gets the path to a template XML file to source for the metadata.
+ * Gets the path to a template XML resource to source for the metadata.
*
* <p>Defaults to null, resulting in a query to the configured {@link MetadataResolver} service
* for the configured "issuer" value as entityID to obtain metadata loaded into the Hub.</p>
@@ -51,7 +51,7 @@ public interface MetadataPublishingProfileConfiguration extends ProfileConfigura
*
* @param profileRequestContext profile request context
*
- * @return templated path to XML file to serve
+ * @return templated path to XML resource to serve
*/
@ConfigurationSetting(name="templatePath")
@Nullable @NotEmpty String getTemplatePath(@Nullable final ProfileRequestContext profileRequestContext);
diff --git a/sp-saml-conf-impl/src/test/java/net/shibboleth/sp/saml/flows/saml2/MetadataFlowTest.java b/sp-saml-conf-impl/src/test/java/net/shibboleth/sp/saml/flows/saml2/MetadataFlowTest.java
index 8ae25bd..ee314b9 100644
--- a/sp-saml-conf-impl/src/test/java/net/shibboleth/sp/saml/flows/saml2/MetadataFlowTest.java
+++ b/sp-saml-conf-impl/src/test/java/net/shibboleth/sp/saml/flows/saml2/MetadataFlowTest.java
@@ -16,59 +16,34 @@ package net.shibboleth.sp.saml.flows.saml2;
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
import org.opensaml.core.xml.XMLObject;
import org.opensaml.core.xml.util.XMLObjectSupport;
-import org.opensaml.messaging.context.MessageContext;
-import org.opensaml.messaging.decoder.MessageDecodingException;
import org.opensaml.profile.action.EventIds;
import org.opensaml.profile.context.ProfileRequestContext;
-import org.opensaml.saml.common.SAMLObject;
-import org.opensaml.saml.common.binding.SAMLBindingSupport;
-import org.opensaml.saml.common.profile.SAMLEventIds;
-import org.opensaml.saml.common.xml.SAMLConstants;
-import org.opensaml.saml.saml2.binding.decoding.impl.HTTPRedirectDeflateDecoder;
-import org.opensaml.saml.saml2.core.AuthnContext;
-import org.opensaml.saml.saml2.core.AuthnContextComparisonTypeEnumeration;
-import org.opensaml.saml.saml2.core.AuthnRequest;
-import org.opensaml.saml.saml2.core.Issuer;
-import org.opensaml.saml.saml2.core.NameIDPolicy;
-import org.opensaml.saml.saml2.core.NameIDType;
-import org.opensaml.saml.saml2.core.RequestedAuthnContext;
import org.opensaml.saml.saml2.metadata.EntityDescriptor;
import org.opensaml.security.credential.Credential;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
-import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.webflow.executor.FlowExecutionResult;
import org.testng.Assert;
import org.testng.annotations.Test;
-import net.shibboleth.idp.authn.AuthnEventIds;
import net.shibboleth.idp.profile.IdPEventIds;
import net.shibboleth.idp.spring.IdPPropertiesApplicationContextInitializer;
import net.shibboleth.idp.test.PreferFileSystemApplicationContextInitializer;
-import net.shibboleth.shared.collection.Pair;
-import net.shibboleth.shared.component.ComponentInitializationException;
-import net.shibboleth.shared.net.URISupport;
-import net.shibboleth.shared.primitive.NonnullSupplier;
import net.shibboleth.shared.xml.ParserPool;
import net.shibboleth.sp.context.AgentRequestContext;
import net.shibboleth.sp.ddf.DDF;
import net.shibboleth.sp.flows.AbstractSPFlowTest;
import net.shibboleth.sp.messaging.RemotedHttpServletRequest;
import net.shibboleth.sp.messaging.RemotedHttpServletResponse;
-import net.shibboleth.sp.profile.InitiatorConstants;
-import net.shibboleth.sp.profile.SPConstants;
-import net.shibboleth.sp.saml.saml2.profile.SAML2InitiatorConstants;
/**
* Unit test for the metadata publishing flow.
@@ -179,6 +154,25 @@ public class MetadataFlowTest extends AbstractSPFlowTest {
assertFlowExecutionOutcome(result.getOutcome());
assertOutputMessageEvent(result, EventIds.MESSAGE_PROC_ERROR);
}
+
+ /**
+ * Test flow with absent IdP but no metadata via resolver.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testNoMetadataViaTemplate() throws IOException {
+ setDefaultAuth();
+
+ final DDF input = new DDF(null).structure();
+ input.addmember(RemotedHttpServletRequest.STRUCTURE_NAME).structure();
+ setApplicationRequest("no-metadata-template", input);
+
+ final FlowExecutionResult result = flowExecutor.launchExecution(FLOW_ID, null, externalContext);
+ assertFlowExecutionResult(result, FLOW_ID);
+ assertFlowExecutionOutcome(result.getOutcome());
+ assertOutputMessageEvent(result, EventIds.IO_ERROR);
+ }
/**
* Decode returned metadata and run sanity checks against it.
diff --git a/sp-saml-conf-impl/src/test/resources/net/shibboleth/idp/module/conf/sp/saml-test-agents.xml b/sp-saml-conf-impl/src/test/resources/net/shibboleth/idp/module/conf/sp/saml-test-agents.xml
index 1b49ef4..b3e74cc 100644
--- a/sp-saml-conf-impl/src/test/resources/net/shibboleth/idp/module/conf/sp/saml-test-agents.xml
+++ b/sp-saml-conf-impl/src/test/resources/net/shibboleth/idp/module/conf/sp/saml-test-agents.xml
@@ -49,6 +49,9 @@
<bean p:id="no-initiators" parent="shibboleth.sp.Application"
p:profileConfigurations-ref="test.ProfileConfigurations"
p:sessionInitiators="#{{}}" />
+
+ <bean p:id="no-metadata-template" parent="shibboleth.sp.Application"
+ p:profileConfigurations-ref="test.MissingTemplateProfileConfigurations" />
</set>
</property>
</bean>
@@ -60,7 +63,12 @@
<ref bean="SAML2.SSO" />
<ref bean="SAML2.ECP" />
<ref bean="SAML2.Logout" />
- <ref bean="SAML2.Metadata" />
+ <bean parent="SAML2.Metadata"
+ p:templatePath="%{idp.home}/metadata/example-template.xml" />
+ </util:list>
+
+ <util:list id="test.MissingTemplateProfileConfigurations">
+ <bean parent="SAML2.Metadata" p:templatePath="%{idp.home}/metadata/missing.xml" />
</util:list>
<util:list id="test.responseBindingProfileConfigurations">
diff --git a/sp-saml-impl/src/main/java/net/shibboleth/sp/saml/saml2/profile/impl/AddMetadata.java b/sp-saml-impl/src/main/java/net/shibboleth/sp/saml/saml2/profile/impl/AddMetadata.java
index cba4d34..8594c56 100644
--- a/sp-saml-impl/src/main/java/net/shibboleth/sp/saml/saml2/profile/impl/AddMetadata.java
+++ b/sp-saml-impl/src/main/java/net/shibboleth/sp/saml/saml2/profile/impl/AddMetadata.java
@@ -14,11 +14,8 @@
package net.shibboleth.sp.saml.saml2.profile.impl;
-import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
@@ -28,6 +25,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.shibboleth.idp.profile.IdPEventIds;
+import net.shibboleth.idp.profile.context.SpringRequestContext;
import net.shibboleth.profile.context.RelyingPartyContext;
import net.shibboleth.profile.context.navigate.IssuerLookupFunction;
import net.shibboleth.shared.annotation.constraint.NonnullAfterInit;
@@ -58,6 +56,8 @@ import org.opensaml.profile.context.ProfileRequestContext;
import org.opensaml.saml.metadata.resolver.MetadataResolver;
import org.opensaml.saml.saml2.metadata.EntityDescriptor;
import org.slf4j.Logger;
+import org.springframework.core.io.Resource;
+import org.springframework.webflow.execution.RequestContext;
import net.shibboleth.shared.primitive.LoggerFactory;
import net.shibboleth.shared.resolver.CriteriaSet;
@@ -241,7 +241,6 @@ public class AddMetadata extends AbstractApplicationAction {
final String templatePath = profileConfiguration.getTemplatePath(profileRequestContext);
if (templatePath != null) {
- log.debug("{} Evaluating template path to metadata: {}", getLogPrefix(), templatePath);
entityDescriptor = doTemplate(profileRequestContext,
Template.fromTemplate(velocityEngine, templatePath, StandardCharsets.UTF_8));
} else {
@@ -254,7 +253,6 @@ public class AddMetadata extends AbstractApplicationAction {
log.info("{} Populated Metadata for {}", getLogPrefix(), entityDescriptor.getEntityID());
} else {
log.error("{} No metadata produced for request", getLogPrefix());
- ActionSupport.buildEvent(profileRequestContext, EventIds.MESSAGE_PROC_ERROR);
}
}
@@ -273,15 +271,20 @@ public class AddMetadata extends AbstractApplicationAction {
String finalPath = null;
try {
finalPath = template.merge(ctx);
- log.debug("{} Loading metadata from {}", getLogPrefix(), finalPath);
+ log.debug("{} Loading metadata from '{}'", getLogPrefix(), finalPath);
final String metadataContent;
- final String metadataTemplateContent = Files.readString(Path.of(finalPath));
+ final String metadataTemplateContent = resourceToString(profileRequestContext, finalPath);
+ if (metadataTemplateContent == null) {
+ ActionSupport.buildEvent(profileRequestContext, EventIds.IO_ERROR);
+ return null;
+ }
+
if (profileConfiguration.isUseVelocity(profileRequestContext)) {
log.debug("{} Evaluating metadata content from {} with Velocity", getLogPrefix(), finalPath);
metadataContent = Template.fromTemplate(velocityEngine, metadataTemplateContent,
StandardCharsets.UTF_8).merge(ctx);
} else {
- log.debug("{} Parsing metadata content from {} directly", getLogPrefix(), finalPath);
+ log.debug("{} Parsing metadata content from '{}' directly", getLogPrefix(), finalPath);
metadataContent = metadataTemplateContent;
}
@@ -291,15 +294,13 @@ public class AddMetadata extends AbstractApplicationAction {
return entity;
}
- log.error("{} Metadata root element was invalid: {}", ret.getElementQName());
+ log.warn("{} Metadata root element was invalid: {}", ret.getElementQName());
+ ActionSupport.buildEvent(profileRequestContext, EventIds.UNABLE_TO_DECODE);
return null;
}
} catch (final VelocityException e) {
log.error("{} Velocity error evaluating metadata template", getLogPrefix(), e);
ActionSupport.buildEvent(profileRequestContext, EventIds.MESSAGE_PROC_ERROR);
- } catch (final IOException e) {
- log.error("{} Error loading metadata template from path: {}", getLogPrefix(), finalPath, e);
- ActionSupport.buildEvent(profileRequestContext, EventIds.IO_ERROR);
} catch (final XMLParserException|UnmarshallingException e) {
log.error("{} Error parsing metadata from {}", getLogPrefix(), finalPath, e);
ActionSupport.buildEvent(profileRequestContext, EventIds.UNABLE_TO_DECODE);
@@ -308,6 +309,36 @@ public class AddMetadata extends AbstractApplicationAction {
return null;
}
+ /**
+ * Loads a resource via Spring and returns the contents as a String.
+ *
+ * @param profileRequestContext profile request context
+ * @param path resource path
+ *
+ * @return resource contents
+ */
+ @Nullable private String resourceToString(@Nonnull final ProfileRequestContext profileRequestContext,
+ @Nonnull @NotEmpty final String path) {
+
+ final SpringRequestContext springContext = profileRequestContext.getSubcontext(SpringRequestContext.class);
+ if (springContext != null) {
+ final RequestContext swfContext = springContext.getRequestContext();
+ if (swfContext != null) {
+ try {
+ final Resource resource =
+ swfContext.getFlowExecutionContext().getDefinition().getApplicationContext().getResource(path);
+ return resource.getContentAsString(StandardCharsets.UTF_8);
+ } catch (final Exception e) {
+ log.error("{} Failed to load metadata from resource '{}'", getLogPrefix(), path, e);
+ return null;
+ }
+ }
+ }
+
+ log.error("{} Unable to access Spring ApplicationContext to load metadata resource", getLogPrefix());
+ return null;
+ }
+
/**
* Creates and populates a {@link VelocityContext} for template evaluation.
*
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list