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.MoonRealm;
22 import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
23 import org.opendaylight.aaa.web.servlet.ServletSupport;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.ShiroConfiguration;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.shiro.configuration.Main;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.shiro.configuration.Urls;
28 import org.opendaylight.yangtools.util.ClassLoaderUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Extends <code>IniWebEnvironment</code> to provide the Ini configuration via a clustered app config,
34 * and sets the TCCL (x2) so that loading of classes by name (from aaa-app-config.xml) works even with
35 * ShiroWebContextSecurer.
37 * @author Ryan Goulding
38 * @author Thomas Pantelis
39 * @author Michael Vorburger - use of TCCL for ShiroWebContextSecurer
41 class AAAIniWebEnvironment extends IniWebEnvironment {
42 private static final Logger LOG = LoggerFactory.getLogger(AAAIniWebEnvironment.class);
44 private static final String MAIN_SECTION_HEADER = "main";
45 private static final String URLS_SECTION_HEADER = "urls";
47 private final ShiroConfiguration shiroConfiguration;
48 private final DataBroker dataBroker;
49 private final ICertificateManager certificateManager;
50 private final AuthenticationService authenticationService;
51 private final TokenAuthenticators tokenAuthenticators;
52 private final TokenStore tokenStore;
53 private final PasswordHashService passwordHashService;
54 private final ServletSupport servletSupport;
56 AAAIniWebEnvironment(final ShiroConfiguration shiroConfiguration, final DataBroker dataBroker,
57 final ICertificateManager certificateManager,
58 final AuthenticationService authenticationService,
59 final TokenAuthenticators tokenAuthenticators, final TokenStore tokenStore,
60 final PasswordHashService passwordHashService, final ServletSupport servletSupport) {
61 this.shiroConfiguration = shiroConfiguration;
62 this.dataBroker = dataBroker;
63 this.certificateManager = certificateManager;
64 this.authenticationService = authenticationService;
65 this.tokenAuthenticators = tokenAuthenticators;
66 this.tokenStore = tokenStore;
67 this.passwordHashService = passwordHashService;
68 this.servletSupport = servletSupport;
69 LOG.debug("AAAIniWebEnvironment created");
72 static Ini createIniFromClusteredAppConfig(final ShiroConfiguration shiroConfiguration) {
73 final Ini ini = new Ini();
75 final Ini.Section mainSection = ini.addSection(MAIN_SECTION_HEADER);
76 for (final Main main : shiroConfiguration.nonnullMain()) {
77 mainSection.put(main.getPairKey(), main.getPairValue());
80 final Ini.Section urlsSection = ini.addSection(URLS_SECTION_HEADER);
81 for (final Urls url : shiroConfiguration.nonnullUrls()) {
82 urlsSection.put(url.getPairKey(), url.getPairValue());
85 final Factory<SecurityManager> factory = new IniSecurityManagerFactory(ini);
86 final SecurityManager securityManager = ClassLoaderUtils.getWithClassLoader(
87 AAAIniWebEnvironment.class.getClassLoader(), (Supplier<SecurityManager>) factory::getInstance);
88 SecurityUtils.setSecurityManager(securityManager);
95 ThreadLocals.DATABROKER_TL.set(dataBroker);
96 ThreadLocals.CERT_MANAGER_TL.set(certificateManager);
97 ThreadLocals.AUTH_SETVICE_TL.set(authenticationService);
98 ThreadLocals.TOKEN_AUTHENICATORS_TL.set(tokenAuthenticators);
99 ThreadLocals.TOKEN_STORE_TL.set(tokenStore);
100 ThreadLocals.PASSWORD_HASH_SERVICE_TL.set(passwordHashService);
101 try (var moonLoad = MoonRealm.prepareForLoad(servletSupport)) {
102 // Initialize the Shiro environment from clustered-app-config
103 final Ini ini = createIniFromClusteredAppConfig(shiroConfiguration);
105 ClassLoaderUtils.getWithClassLoader(AAAIniWebEnvironment.class.getClassLoader(), (Supplier<Void>) () -> {
110 ThreadLocals.DATABROKER_TL.remove();
111 ThreadLocals.CERT_MANAGER_TL.remove();
112 ThreadLocals.AUTH_SETVICE_TL.remove();
113 ThreadLocals.TOKEN_AUTHENICATORS_TL.remove();
114 ThreadLocals.TOKEN_STORE_TL.remove();
115 ThreadLocals.PASSWORD_HASH_SERVICE_TL.remove();