Remove AAAWebEnvironment.create()
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / shiro / web / env / AAAWebEnvironment.java
1 /*
2  * Copyright (c) 2018 Inocybe Technologies and others.  All rights reserved.
3  * Copyright (c) 2022 PANTHEON.tech, s.r.o.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.aaa.shiro.web.env;
10
11 import org.apache.shiro.config.Ini;
12 import org.apache.shiro.web.env.IniWebEnvironment;
13 import org.opendaylight.aaa.api.AuthenticationService;
14 import org.opendaylight.aaa.api.TokenStore;
15 import org.opendaylight.aaa.api.password.service.PasswordHashService;
16 import org.opendaylight.aaa.cert.api.ICertificateManager;
17 import org.opendaylight.aaa.shiro.realm.KeystoneAuthRealm;
18 import org.opendaylight.aaa.shiro.realm.MDSALDynamicAuthorizationFilter;
19 import org.opendaylight.aaa.shiro.realm.MdsalRealm;
20 import org.opendaylight.aaa.shiro.realm.MoonRealm;
21 import org.opendaylight.aaa.shiro.realm.TokenAuthRealm;
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.ShiroIni;
26 import org.opendaylight.yangtools.util.ClassLoaderUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Extends {@code BasicInitEnvironment} to provide the {@link Ini} configuration via a clustered app config,
32  * Initialization happens in the context of this class's ClassLoader, with dependencies being injected into their
33  * thread-local variables.
34  */
35 public final class AAAWebEnvironment extends IniWebEnvironment implements AAAShiroWebEnvironment {
36     private static final Logger LOG = LoggerFactory.getLogger(AAAWebEnvironment.class);
37
38     public AAAWebEnvironment(final ShiroIni shiroConfiguration, final DataBroker dataBroker,
39             final ICertificateManager certificateManager, final AuthenticationService authenticationService,
40             final TokenAuthenticators tokenAuthenticators, final TokenStore tokenStore,
41             final PasswordHashService passwordHashService, final ServletSupport servletSupport) {
42         // Turn ShiroConfiguration into an Ini
43         final var ini = new Ini();
44
45         final var mainSection = ini.addSection("main");
46         for (var main : shiroConfiguration.nonnullMain()) {
47             mainSection.put(main.getPairKey(), main.getPairValue());
48         }
49
50         final var urlsSection = ini.addSection("urls");
51         for (var url : shiroConfiguration.nonnullUrls()) {
52             urlsSection.put(url.getPairKey(), url.getPairValue());
53         }
54
55         // Set the configuration
56         setIni(ini);
57
58         // Configure the instance with all known custom components prepared for loading via their thread locals and
59         // clean up afterwards. This needs to happen on our class loader so Shiro's ReflectionBuilder use of
60         // Class.forName() is happy.
61         ClassLoaderUtils.runWithClassLoader(AAAWebEnvironment.class.getClassLoader(), () -> {
62             try (var filterLoad = MDSALDynamicAuthorizationFilter.prepareForLoad(dataBroker);
63                  var keyStoneLoad = KeystoneAuthRealm.prepareForLoad(certificateManager, servletSupport);
64                  var mdsalLoad = MdsalRealm.prepareForLoad(passwordHashService, dataBroker);
65                  var moonLoad = MoonRealm.prepareForLoad(servletSupport);
66                  var tokenAuthLoad = TokenAuthRealm.prepareForLoad(authenticationService, tokenAuthenticators,
67                      tokenStore)) {
68                 configure();
69             }
70         });
71
72         LOG.debug("AAAWebEnvironment created");
73     }
74 }