Migrate more ThreadLocals
[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.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;
33
34 /**
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.
38  *
39  * @author Ryan Goulding
40  * @author Thomas Pantelis
41  * @author Michael Vorburger - use of TCCL for ShiroWebContextSecurer
42  */
43 class AAAIniWebEnvironment extends IniWebEnvironment {
44     private static final Logger LOG = LoggerFactory.getLogger(AAAIniWebEnvironment.class);
45
46     private static final String MAIN_SECTION_HEADER = "main";
47     private static final String URLS_SECTION_HEADER = "urls";
48
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;
57
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");
72     }
73
74     static Ini createIniFromClusteredAppConfig(final ShiroConfiguration shiroConfiguration) {
75         final Ini ini = new Ini();
76
77         final Ini.Section mainSection = ini.addSection(MAIN_SECTION_HEADER);
78         for (final Main main : shiroConfiguration.nonnullMain()) {
79             mainSection.put(main.getPairKey(), main.getPairValue());
80         }
81
82         final Ini.Section urlsSection = ini.addSection(URLS_SECTION_HEADER);
83         for (final Urls url : shiroConfiguration.nonnullUrls()) {
84             urlsSection.put(url.getPairKey(), url.getPairValue());
85         }
86
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);
91
92         return ini;
93     }
94
95     @Override
96     public void init() {
97         ThreadLocals.DATABROKER_TL.set(dataBroker);
98         ThreadLocals.PASSWORD_HASH_SERVICE_TL.set(passwordHashService);
99         try (
100             var keyStoneLoad = KeystoneAuthRealm.prepareForLoad(certificateManager);
101             var moonLoad = MoonRealm.prepareForLoad(servletSupport);
102             var tokenAuthLoad = TokenAuthRealm.prepareForLoad(authenticationService, tokenAuthenticators, tokenStore)) {
103
104             // Initialize the Shiro environment from clustered-app-config
105             final Ini ini = createIniFromClusteredAppConfig(shiroConfiguration);
106             setIni(ini);
107             ClassLoaderUtils.getWithClassLoader(AAAIniWebEnvironment.class.getClassLoader(), () -> {
108                 super.init();
109                 return null;
110             });
111         } finally {
112             ThreadLocals.DATABROKER_TL.remove();
113             ThreadLocals.PASSWORD_HASH_SERVICE_TL.remove();
114         }
115     }
116 }