Inject ServletSupport into MoonRealm
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / shiro / web / env / AAAIniWebEnvironment.java
1 /*
2  * Copyright (c) 2018 Inocybe Technologies and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.aaa.shiro.web.env;
9
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;
31
32 /**
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.
36  *
37  * @author Ryan Goulding
38  * @author Thomas Pantelis
39  * @author Michael Vorburger - use of TCCL for ShiroWebContextSecurer
40  */
41 class AAAIniWebEnvironment extends IniWebEnvironment {
42     private static final Logger LOG = LoggerFactory.getLogger(AAAIniWebEnvironment.class);
43
44     private static final String MAIN_SECTION_HEADER = "main";
45     private static final String URLS_SECTION_HEADER = "urls";
46
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;
55
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");
70     }
71
72     static Ini createIniFromClusteredAppConfig(final ShiroConfiguration shiroConfiguration) {
73         final Ini ini = new Ini();
74
75         final Ini.Section mainSection = ini.addSection(MAIN_SECTION_HEADER);
76         for (final Main main : shiroConfiguration.nonnullMain()) {
77             mainSection.put(main.getPairKey(), main.getPairValue());
78         }
79
80         final Ini.Section urlsSection = ini.addSection(URLS_SECTION_HEADER);
81         for (final Urls url : shiroConfiguration.nonnullUrls()) {
82             urlsSection.put(url.getPairKey(), url.getPairValue());
83         }
84
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);
89
90         return ini;
91     }
92
93     @Override
94     public void init() {
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);
104             setIni(ini);
105             ClassLoaderUtils.getWithClassLoader(AAAIniWebEnvironment.class.getClassLoader(), (Supplier<Void>) () -> {
106                 super.init();
107                 return null;
108             });
109         } finally {
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();
116         }
117     }
118 }