|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.saml2.core; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | +import java.util.function.Consumer; |
| 23 | +import javax.xml.XMLConstants; |
| 24 | + |
| 25 | +import net.shibboleth.utilities.java.support.xml.BasicParserPool; |
| 26 | +import org.apache.commons.logging.Log; |
| 27 | +import org.apache.commons.logging.LogFactory; |
| 28 | +import org.opensaml.core.config.ConfigurationService; |
| 29 | +import org.opensaml.core.config.InitializationService; |
| 30 | +import org.opensaml.core.xml.config.XMLObjectProviderRegistry; |
| 31 | + |
| 32 | +import org.springframework.security.saml2.Saml2Exception; |
| 33 | + |
| 34 | +import static java.lang.Boolean.FALSE; |
| 35 | +import static java.lang.Boolean.TRUE; |
| 36 | +import static org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport.setParserPool; |
| 37 | + |
| 38 | +/** |
| 39 | + * An initialization service for initializing OpenSAML. Each Spring Security OpenSAML-based component invokes |
| 40 | + * the {@link #initialize()} method at static initialization time. |
| 41 | + * |
| 42 | + * {@link #initialize()} is idempotent and may be safely called in custom classes that need OpenSAML to be |
| 43 | + * initialized in order to function correctly. It's recommended that you call this {@link #initialize()} method |
| 44 | + * when using Spring Security and OpenSAML instead of OpenSAML's {@link InitializationService#initialize()}. |
| 45 | + * |
| 46 | + * The primary purpose of {@link #initialize()} is to prepare OpenSAML's {@link XMLObjectProviderRegistry} |
| 47 | + * with some reasonable defaults. Any changes that Spring Security makes to the registry happen in this method. |
| 48 | + * |
| 49 | + * To override those defaults, call {@link #requireInitialize(Consumer)} and change the registry: |
| 50 | + * |
| 51 | + * <pre> |
| 52 | + * static { |
| 53 | + * OpenSamlInitializationService.requireInitialize(registry -> { |
| 54 | + * registry.setParserPool(...); |
| 55 | + * registry.getBuilderFactory().registerBuilder(...); |
| 56 | + * }); |
| 57 | + * } |
| 58 | + * </pre> |
| 59 | + * |
| 60 | + * {@link #requireInitialize(Consumer)} may only be called once per application. |
| 61 | + * |
| 62 | + * If the application already initialized OpenSAML before {@link #requireInitialize(Consumer)} was called, |
| 63 | + * then the configuration changes will not be applied and an exception will be thrown. The reason for this is to |
| 64 | + * alert you to the fact that there are likely some initialization ordering problems in your application that |
| 65 | + * would otherwise lead to an unpredictable state. |
| 66 | + * |
| 67 | + * If you must change the registry's configuration in multiple places in your application, you are expected |
| 68 | + * to handle the initialization ordering issues yourself instead of trying to call {@link #requireInitialize(Consumer)} |
| 69 | + * multiple times. |
| 70 | + * |
| 71 | + * @author Josh Cummings |
| 72 | + * @since 5.4 |
| 73 | + */ |
| 74 | +public class OpenSamlInitializationService { |
| 75 | + private static final Log log = LogFactory.getLog(OpenSamlInitializationService.class); |
| 76 | + private static final AtomicBoolean initialized = new AtomicBoolean(false); |
| 77 | + |
| 78 | + /** |
| 79 | + * Ready OpenSAML for use and configure it with reasonable defaults. |
| 80 | + * |
| 81 | + * Initialization is guaranteed to happen only once per application. This method will passively return |
| 82 | + * {@code false} if initialization already took place earlier in the application. |
| 83 | + * |
| 84 | + * @return whether or not initialization was performed. The first thread to initialize OpenSAML will |
| 85 | + * return {@code true} while the rest will return {@code false}. |
| 86 | + * @throws Saml2Exception if OpenSAML failed to initialize |
| 87 | + */ |
| 88 | + public static boolean initialize() { |
| 89 | + return initialize(registry -> {}); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Ready OpenSAML for use, configure it with reasonable defaults, and modify the {@link XMLObjectProviderRegistry} |
| 94 | + * using the provided {@link Consumer}. |
| 95 | + * |
| 96 | + * Initialization is guaranteed to happen only once per application. This method will throw an exception |
| 97 | + * if initialization already took place earlier in the application. |
| 98 | + * |
| 99 | + * @param registryConsumer the {@link Consumer} to further configure the {@link XMLObjectProviderRegistry} |
| 100 | + * @throws Saml2Exception if initialization already happened previously or if OpenSAML failed to initialize |
| 101 | + */ |
| 102 | + public static void requireInitialize(Consumer<XMLObjectProviderRegistry> registryConsumer) { |
| 103 | + if (!initialize(registryConsumer)) { |
| 104 | + throw new Saml2Exception("OpenSAML was already initialized previously"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static boolean initialize(Consumer<XMLObjectProviderRegistry> registryConsumer) { |
| 109 | + if (initialized.compareAndSet(false, true)) { |
| 110 | + log.trace("Initializing OpenSAML"); |
| 111 | + |
| 112 | + try { |
| 113 | + InitializationService.initialize(); |
| 114 | + } catch (Exception e) { |
| 115 | + throw new Saml2Exception(e); |
| 116 | + } |
| 117 | + |
| 118 | + BasicParserPool parserPool = new BasicParserPool(); |
| 119 | + parserPool.setMaxPoolSize(50); |
| 120 | + |
| 121 | + Map<String, Boolean> parserBuilderFeatures = new HashMap<>(); |
| 122 | + parserBuilderFeatures.put("http://apache.org/xml/features/disallow-doctype-decl", TRUE); |
| 123 | + parserBuilderFeatures.put(XMLConstants.FEATURE_SECURE_PROCESSING, TRUE); |
| 124 | + parserBuilderFeatures.put("http://xml.org/sax/features/external-general-entities", FALSE); |
| 125 | + parserBuilderFeatures.put("http://apache.org/xml/features/validation/schema/normalized-value", FALSE); |
| 126 | + parserBuilderFeatures.put("http://xml.org/sax/features/external-parameter-entities", FALSE); |
| 127 | + parserBuilderFeatures.put("http://apache.org/xml/features/dom/defer-node-expansion", FALSE); |
| 128 | + parserPool.setBuilderFeatures(parserBuilderFeatures); |
| 129 | + |
| 130 | + try { |
| 131 | + parserPool.initialize(); |
| 132 | + } catch (Exception e) { |
| 133 | + throw new Saml2Exception(e); |
| 134 | + } |
| 135 | + setParserPool(parserPool); |
| 136 | + |
| 137 | + registryConsumer.accept(ConfigurationService.get(XMLObjectProviderRegistry.class)); |
| 138 | + |
| 139 | + log.debug("Initialized OpenSAML"); |
| 140 | + return true; |
| 141 | + } else { |
| 142 | + log.debug("Refused to re-initialize OpenSAML"); |
| 143 | + return false; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments