Eliminate 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.MDSALDynamicAuthorizationFilter;
23 import org.opendaylight.aaa.shiro.realm.MdsalRealm;
24 import org.opendaylight.aaa.shiro.realm.MoonRealm;
25 import org.opendaylight.aaa.shiro.realm.TokenAuthRealm;
26 import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
27 import org.opendaylight.aaa.web.servlet.ServletSupport;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.ShiroConfiguration;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.shiro.configuration.Main;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.shiro.configuration.Urls;
32 import org.opendaylight.yangtools.util.ClassLoaderUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Extends <code>IniWebEnvironment</code> to provide the Ini configuration via a clustered app config,
38  * and sets the TCCL (x2) so that loading of classes by name (from aaa-app-config.xml) works even with
39  * ShiroWebContextSecurer.
40  *
41  * @author Ryan Goulding
42  * @author Thomas Pantelis
43  * @author Michael Vorburger - use of TCCL for ShiroWebContextSecurer
44  */
45 class AAAIniWebEnvironment extends IniWebEnvironment {
46     private static final Logger LOG = LoggerFactory.getLogger(AAAIniWebEnvironment.class);
47
48     private static final String MAIN_SECTION_HEADER = "main";
49     private static final String URLS_SECTION_HEADER = "urls";
50
51     private final ShiroConfiguration shiroConfiguration;
52     private final DataBroker dataBroker;
53     private final ICertificateManager certificateManager;
54     private final AuthenticationService authenticationService;
55     private final TokenAuthenticators tokenAuthenticators;
56     private final TokenStore tokenStore;
57     private final PasswordHashService passwordHashService;
58     private final ServletSupport servletSupport;
59
60     AAAIniWebEnvironment(final ShiroConfiguration shiroConfiguration, final DataBroker dataBroker,
61                          final ICertificateManager certificateManager,
62                          final AuthenticationService authenticationService,
63                          final TokenAuthenticators tokenAuthenticators, final TokenStore tokenStore,
64                          final PasswordHashService passwordHashService, final ServletSupport servletSupport) {
65         this.shiroConfiguration = shiroConfiguration;
66         this.dataBroker = dataBroker;
67         this.certificateManager = certificateManager;
68         this.authenticationService = authenticationService;
69         this.tokenAuthenticators = tokenAuthenticators;
70         this.tokenStore = tokenStore;
71         this.passwordHashService = passwordHashService;
72         this.servletSupport = servletSupport;
73         LOG.debug("AAAIniWebEnvironment created");
74     }
75
76     static Ini createIniFromClusteredAppConfig(final ShiroConfiguration shiroConfiguration) {
77         final Ini ini = new Ini();
78
79         final Ini.Section mainSection = ini.addSection(MAIN_SECTION_HEADER);
80         for (final Main main : shiroConfiguration.nonnullMain()) {
81             mainSection.put(main.getPairKey(), main.getPairValue());
82         }
83
84         final Ini.Section urlsSection = ini.addSection(URLS_SECTION_HEADER);
85         for (final Urls url : shiroConfiguration.nonnullUrls()) {
86             urlsSection.put(url.getPairKey(), url.getPairValue());
87         }
88
89         final Factory<SecurityManager> factory = new IniSecurityManagerFactory(ini);
90         final SecurityManager securityManager = ClassLoaderUtils.getWithClassLoader(
91                 AAAIniWebEnvironment.class.getClassLoader(), (Supplier<SecurityManager>) factory::getInstance);
92         SecurityUtils.setSecurityManager(securityManager);
93
94         return ini;
95     }
96
97     @Override
98     public void init() {
99         try (
100             var filterLoad = MDSALDynamicAuthorizationFilter.prepareForLoad(dataBroker);
101             var keyStoneLoad = KeystoneAuthRealm.prepareForLoad(certificateManager);
102             var mdsalLoad = MdsalRealm.prepareForLoad(passwordHashService, dataBroker);
103             var moonLoad = MoonRealm.prepareForLoad(servletSupport);
104             var tokenAuthLoad = TokenAuthRealm.prepareForLoad(authenticationService, tokenAuthenticators, tokenStore)) {
105             // Initialize the Shiro environment from clustered-app-config
106             final Ini ini = createIniFromClusteredAppConfig(shiroConfiguration);
107             setIni(ini);
108             ClassLoaderUtils.getWithClassLoader(AAAIniWebEnvironment.class.getClassLoader(), () -> {
109                 super.init();
110                 return null;
111             });
112         }
113     }
114 }