2 * Copyright (c) 2018 Inocybe Technologies and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.aaa.shiro.web.env;
10 import java.util.function.Supplier;
11 import org.apache.shiro.SecurityUtils;
12 import org.apache.shiro.config.Ini;
13 import org.apache.shiro.config.IniSecurityManagerFactory;
14 import org.apache.shiro.mgt.SecurityManager;
15 import org.apache.shiro.util.Factory;
16 import org.apache.shiro.web.env.IniWebEnvironment;
17 import org.opendaylight.aaa.api.AuthenticationService;
18 import org.opendaylight.aaa.api.TokenStore;
19 import org.opendaylight.aaa.api.password.service.PasswordHashService;
20 import org.opendaylight.aaa.cert.api.ICertificateManager;
21 import org.opendaylight.aaa.shiro.realm.KeystoneAuthRealm;
22 import org.opendaylight.aaa.shiro.realm.MoonRealm;
23 import org.opendaylight.aaa.shiro.realm.TokenAuthRealm;
24 import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
25 import org.opendaylight.aaa.web.servlet.ServletSupport;
26 import org.opendaylight.mdsal.binding.api.DataBroker;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.ShiroConfiguration;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.shiro.configuration.Main;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.shiro.configuration.Urls;
30 import org.opendaylight.yangtools.util.ClassLoaderUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Extends <code>IniWebEnvironment</code> to provide the Ini configuration via a clustered app config,
36 * and sets the TCCL (x2) so that loading of classes by name (from aaa-app-config.xml) works even with
37 * ShiroWebContextSecurer.
39 * @author Ryan Goulding
40 * @author Thomas Pantelis
41 * @author Michael Vorburger - use of TCCL for ShiroWebContextSecurer
43 class AAAIniWebEnvironment extends IniWebEnvironment {
44 private static final Logger LOG = LoggerFactory.getLogger(AAAIniWebEnvironment.class);
46 private static final String MAIN_SECTION_HEADER = "main";
47 private static final String URLS_SECTION_HEADER = "urls";
49 private final ShiroConfiguration shiroConfiguration;
50 private final DataBroker dataBroker;
51 private final ICertificateManager certificateManager;
52 private final AuthenticationService authenticationService;
53 private final TokenAuthenticators tokenAuthenticators;
54 private final TokenStore tokenStore;
55 private final PasswordHashService passwordHashService;
56 private final ServletSupport servletSupport;
58 AAAIniWebEnvironment(final ShiroConfiguration shiroConfiguration, final DataBroker dataBroker,
59 final ICertificateManager certificateManager,
60 final AuthenticationService authenticationService,
61 final TokenAuthenticators tokenAuthenticators, final TokenStore tokenStore,
62 final PasswordHashService passwordHashService, final ServletSupport servletSupport) {
63 this.shiroConfiguration = shiroConfiguration;
64 this.dataBroker = dataBroker;
65 this.certificateManager = certificateManager;
66 this.authenticationService = authenticationService;
67 this.tokenAuthenticators = tokenAuthenticators;
68 this.tokenStore = tokenStore;
69 this.passwordHashService = passwordHashService;
70 this.servletSupport = servletSupport;
71 LOG.debug("AAAIniWebEnvironment created");
74 static Ini createIniFromClusteredAppConfig(final ShiroConfiguration shiroConfiguration) {
75 final Ini ini = new Ini();
77 final Ini.Section mainSection = ini.addSection(MAIN_SECTION_HEADER);
78 for (final Main main : shiroConfiguration.nonnullMain()) {
79 mainSection.put(main.getPairKey(), main.getPairValue());
82 final Ini.Section urlsSection = ini.addSection(URLS_SECTION_HEADER);
83 for (final Urls url : shiroConfiguration.nonnullUrls()) {
84 urlsSection.put(url.getPairKey(), url.getPairValue());
87 final Factory<SecurityManager> factory = new IniSecurityManagerFactory(ini);
88 final SecurityManager securityManager = ClassLoaderUtils.getWithClassLoader(
89 AAAIniWebEnvironment.class.getClassLoader(), (Supplier<SecurityManager>) factory::getInstance);
90 SecurityUtils.setSecurityManager(securityManager);
97 ThreadLocals.DATABROKER_TL.set(dataBroker);
98 ThreadLocals.PASSWORD_HASH_SERVICE_TL.set(passwordHashService);
100 var keyStoneLoad = KeystoneAuthRealm.prepareForLoad(certificateManager);
101 var moonLoad = MoonRealm.prepareForLoad(servletSupport);
102 var tokenAuthLoad = TokenAuthRealm.prepareForLoad(authenticationService, tokenAuthenticators, tokenStore)) {
104 // Initialize the Shiro environment from clustered-app-config
105 final Ini ini = createIniFromClusteredAppConfig(shiroConfiguration);
107 ClassLoaderUtils.getWithClassLoader(AAAIniWebEnvironment.class.getClassLoader(), () -> {
112 ThreadLocals.DATABROKER_TL.remove();
113 ThreadLocals.PASSWORD_HASH_SERVICE_TL.remove();