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