2 * Copyright (c) 2018 Inocybe Technologies and others. All rights reserved.
3 * Copyright (c) 2022 PANTHEON.tech, s.r.o.
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
9 package org.opendaylight.aaa.shiro.web.env;
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;
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.
35 public final class AAAWebEnvironment extends IniWebEnvironment implements AAAShiroWebEnvironment {
36 private static final Logger LOG = LoggerFactory.getLogger(AAAWebEnvironment.class);
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();
45 final var mainSection = ini.addSection("main");
46 for (var main : shiroConfiguration.nonnullMain()) {
47 mainSection.put(main.getPairKey(), main.getPairValue());
50 final var urlsSection = ini.addSection("urls");
51 for (var url : shiroConfiguration.nonnullUrls()) {
52 urlsSection.put(url.getPairKey(), url.getPairValue());
55 // Set the configuration
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,
72 LOG.debug("AAAWebEnvironment created");