Skip to content

Commit c08f556

Browse files
committed
Modernize AAA authorization
Since JettyWebServer is being used, the entire AAA authorization can be modernized to use WebContext.builder() instead of handlers. Classes related to moon endpoins can also be ditched since now its part ShiroProvider using WebContext.builder(). JIRA: LIGHTY-329 Signed-off-by: tobias.pobocik <[email protected]>
1 parent 6816ac9 commit c08f556

File tree

4 files changed

+69
-153
lines changed

4 files changed

+69
-153
lines changed

lighty-modules/lighty-aaa-aggregator/lighty-aaa/src/main/java/io/lighty/aaa/AAALighty.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.concurrent.CountDownLatch;
1515
import org.opendaylight.aaa.api.CredentialAuth;
1616
import org.opendaylight.aaa.api.PasswordCredentials;
17+
import org.opendaylight.aaa.web.WebContextSecurer;
1718
import org.opendaylight.mdsal.binding.api.DataBroker;
1819

1920
public final class AAALighty extends AbstractLightyModule {
@@ -22,6 +23,7 @@ public final class AAALighty extends AbstractLightyModule {
2223
private final LightyJettyServerProvider server;
2324
private final CredentialAuth<PasswordCredentials> credentialAuth;
2425
private final DataBroker dataBroker;
26+
private WebContextSecurer webContextSecurer;
2527

2628
private final AAAConfiguration aaaConfiguration;
2729

@@ -41,6 +43,7 @@ protected boolean initProcedure() throws InterruptedException {
4143
final CountDownLatch cdl = new CountDownLatch(1);
4244
newInstance.whenComplete((t, u) -> {
4345
AAALighty.this.aaaShiroProviderHandler.setAaaLightyShiroProvider(t);
46+
this.webContextSecurer = aaaShiroProviderHandler.getAaaLightyShiroProvider().getWebContextSecurer();
4447
cdl.countDown();
4548
});
4649

@@ -70,4 +73,8 @@ AAALightyShiroProvider getAaaLightyShiroProvider() {
7073
return this.aaaLightyShiroProvider;
7174
}
7275
}
76+
77+
public WebContextSecurer getWebContextSecurer() {
78+
return webContextSecurer;
79+
}
7380
}

lighty-modules/lighty-aaa-aggregator/lighty-aaa/src/main/java/io/lighty/aaa/AAALightyShiroProvider.java

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,14 @@
88
package io.lighty.aaa;
99

1010
import io.lighty.aaa.config.AAAConfiguration;
11-
import java.util.ArrayList;
1211
import io.lighty.server.LightyJettyServerProvider;
1312
import java.util.HashMap;
14-
import java.util.List;
1513
import java.util.Map;
1614
import java.util.concurrent.CompletableFuture;
17-
import org.eclipse.jetty.server.Handler;
18-
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
19-
import org.eclipse.jetty.servlet.FilterHolder;
20-
import org.eclipse.jetty.servlet.ServletContextHandler;
21-
import org.eclipse.jetty.servlet.ServletHolder;
15+
import javax.servlet.ServletException;
16+
import org.apache.shiro.mgt.DefaultSecurityManager;
17+
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
2218
import org.eclipse.jetty.servlets.CrossOriginFilter;
23-
import org.glassfish.jersey.internal.guava.Preconditions;
24-
import org.glassfish.jersey.server.ResourceConfig;
25-
import org.glassfish.jersey.servlet.ServletContainer;
2619
import org.opendaylight.aaa.api.AuthenticationService;
2720
import org.opendaylight.aaa.api.ClaimCache;
2821
import org.opendaylight.aaa.api.CredentialAuth;
@@ -39,20 +32,23 @@
3932
import org.opendaylight.aaa.filterchain.configuration.impl.CustomFilterAdapterConfigurationImpl;
4033
import org.opendaylight.aaa.filterchain.filters.CustomFilterAdapter;
4134
import org.opendaylight.aaa.impl.password.service.DefaultPasswordHashService;
42-
import org.opendaylight.aaa.shiro.filters.AAAShiroFilter;
4335
import org.opendaylight.aaa.shiro.idm.IdmLightApplication;
4436
import org.opendaylight.aaa.shiro.idm.IdmLightProxy;
45-
import org.opendaylight.aaa.shiro.moon.MoonTokenEndpoint;
4637
import org.opendaylight.aaa.shiro.web.env.AAAWebEnvironment;
38+
import org.opendaylight.aaa.shiro.web.env.ShiroWebContextSecurer;
4739
import org.opendaylight.aaa.tokenauthrealm.auth.AuthenticationManager;
4840
import org.opendaylight.aaa.tokenauthrealm.auth.HttpBasicAuth;
4941
import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
42+
import org.opendaylight.aaa.web.FilterDetails;
43+
import org.opendaylight.aaa.web.ServletDetails;
44+
import org.opendaylight.aaa.web.WebContext;
5045
import org.opendaylight.aaa.web.servlet.jersey2.JerseyServletSupport;
5146
import org.opendaylight.mdsal.binding.api.DataBroker;
5247
import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.DatastoreConfig;
5348
import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.ShiroConfiguration;
5449
import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.password.service.config.rev170619.PasswordServiceConfig;
5550
import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.password.service.config.rev170619.PasswordServiceConfigBuilder;
51+
import org.opendaylight.yangtools.concepts.Registration;
5652
import org.slf4j.Logger;
5753
import org.slf4j.LoggerFactory;
5854

@@ -62,7 +58,6 @@ public final class AAALightyShiroProvider {
6258

6359
private static AAALightyShiroProvider INSTANCE;
6460

65-
private final List<Handler> handlers;
6661
private final DataBroker dataBroker;
6762
private final ICertificateManager certificateManager;
6863
private final ShiroConfiguration shiroConfiguration;
@@ -73,6 +68,8 @@ public final class AAALightyShiroProvider {
7368
private ClaimCache claimCache;
7469
private PasswordHashService passwordHashService;
7570
private IIDMStore iidmStore;
71+
private Registration registration;
72+
private ShiroWebContextSecurer webContextSecurer;
7673

7774
private AAAWebEnvironment aaaWebEnvironment;
7875

@@ -84,7 +81,6 @@ private AAALightyShiroProvider(final DataBroker dataBroker,
8481
this.certificateManager = aaaConfiguration.getCertificateManager();
8582
this.credentialAuth = credentialAuth;
8683
this.shiroConfiguration = aaaConfiguration.getShiroConf();
87-
this.handlers = new ArrayList<>();
8884
this.authenticationService = new AuthenticationManager();
8985
final DatastoreConfig datastoreConfig = aaaConfiguration.getDatastoreConf();
9086

@@ -121,47 +117,60 @@ private AAALightyShiroProvider(final DataBroker dataBroker,
121117
} catch (final IDMStoreException e) {
122118
LOG.error("Failed to initialize data in store", e);
123119
}
124-
final LocalHttpServer httpService = new LocalHttpServer(server);
125-
registerServletContexts(httpService, aaaConfiguration.getMoonEndpointPath());
126-
127120
initAAAonServer(server);
128121
}
129122

130123
private void initAAAonServer(final LightyJettyServerProvider server) {
131-
final ContextHandlerCollection contexts = new ContextHandlerCollection();
132-
final ServletContextHandler mainHandler = new ServletContextHandler(contexts, "/auth", true, false);
133-
final IdmLightApplication idmLightApplication = new IdmLightApplication(iidmStore, claimCache);
134-
final ServletHolder idmLightServlet = new ServletHolder(new ServletContainer(ResourceConfig.forApplication(
135-
idmLightApplication)));
136-
idmLightServlet.setInitParameter("jersey.config.server.provider.packages",
137-
"org.opendaylight.aaa.impl.provider");
138-
mainHandler.addServlet(idmLightServlet, "/*");
139-
server.addContextHandler(contexts);
140-
this.handlers.add(contexts);
141-
this.handlers.add(mainHandler);
142-
this.aaaWebEnvironment = new AAAWebEnvironment(shiroConfiguration,
143-
dataBroker,
144-
certificateManager,
145-
authenticationService,
146-
tokenAuthenticators,
147-
passwordHashService,
148-
new JerseyServletSupport());
149-
150124
final Map<String, String> properties = new HashMap<>();
151125
final CustomFilterAdapterConfigurationImpl customFilterAdapterConfig =
152-
new CustomFilterAdapterConfigurationImpl();
126+
new CustomFilterAdapterConfigurationImpl();
153127
customFilterAdapterConfig.update(properties);
154-
final FilterHolder customFilterAdapter = new FilterHolder(new CustomFilterAdapter(customFilterAdapterConfig));
155-
server.addCommonFilter(customFilterAdapter, "/*");
156128

157-
final FilterHolder shiroFilter = new FilterHolder(new AAAShiroFilter(aaaWebEnvironment));
158-
server.addCommonFilter(shiroFilter, "/*");
129+
this.aaaWebEnvironment = new AAAWebEnvironment(
130+
shiroConfiguration,
131+
dataBroker,
132+
certificateManager,
133+
authenticationService,
134+
tokenAuthenticators,
135+
passwordHashService,
136+
new JerseyServletSupport());
137+
138+
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
139+
((DefaultSecurityManager) aaaWebEnvironment.getSecurityManager()).setSessionManager(sessionManager);
140+
141+
final var webContextBuilder = WebContext.builder()
142+
.name("RealmManagement")
143+
.contextPath("/auth")
144+
.supportsSessions(true)
159145

160-
final FilterHolder crossOriginFilter = new FilterHolder(new CrossOriginFilter());
161-
crossOriginFilter.setInitParameter("allowedMethods", "GET,POST,OPTIONS,DELETE,PUT,HEAD");
162-
crossOriginFilter.setInitParameter("allowedHeaders",
163-
"origin, content-type, accept, authorization, Authorization");
164-
server.addCommonFilter(crossOriginFilter, "/*");
146+
// Add servlet
147+
.addServlet(ServletDetails.builder()
148+
.servlet(new JerseyServletSupport().createHttpServletBuilder(
149+
new IdmLightApplication(iidmStore, claimCache)).build())
150+
.addUrlPattern("/*")
151+
.build())
152+
153+
// CustomFilterAdapter
154+
.addFilter(FilterDetails.builder()
155+
.filter(new CustomFilterAdapter(customFilterAdapterConfig))
156+
.addUrlPattern("/*")
157+
.build())
158+
159+
// CORS filter
160+
.addFilter(FilterDetails.builder()
161+
.filter(new CrossOriginFilter())
162+
.addUrlPattern("/*")
163+
.putInitParam("allowedMethods", "GET,POST,OPTIONS,DELETE,PUT,HEAD")
164+
.putInitParam("allowedHeaders", "origin, content-type, accept, authorization, Authorization")
165+
.build());
166+
this.webContextSecurer = new ShiroWebContextSecurer(aaaWebEnvironment);
167+
webContextSecurer.requireAuthentication(webContextBuilder, "/*", "/moon/*");
168+
169+
try {
170+
this.registration = server.build().registerWebContext(webContextBuilder.build());
171+
} catch (ServletException e) {
172+
LOG.error("Failed to register AAA web context: {}!", server.getClass(), e);
173+
}
165174
}
166175

167176
public static CompletableFuture<AAALightyShiroProvider> newInstance(final DataBroker dataBroker,
@@ -225,6 +234,10 @@ public static IIDMStore getIdmStore() {
225234
return INSTANCE.iidmStore;
226235
}
227236

237+
public ShiroWebContextSecurer getWebContextSecurer() {
238+
return webContextSecurer;
239+
}
240+
228241
/**
229242
* Set IDM data store, only used for test.
230243
*
@@ -236,26 +249,13 @@ public static void setIdmStore(final IIDMStore store) {
236249

237250
@SuppressWarnings("IllegalCatch")
238251
public void close() {
239-
this.handlers.forEach((handler) -> {
240-
try {
241-
handler.stop();
242-
} catch (Exception e) {
243-
LOG.error("Failed to close AAA handler [{}]", handler, e);
244-
} finally {
245-
handler.destroy();
246-
}
247-
});
252+
if (registration != null) {
253+
registration.close();
254+
}
248255
}
249256

250257
private static TokenAuthenticators buildTokenAuthenticators(
251258
final CredentialAuth<PasswordCredentials> credentialAuth) {
252259
return new TokenAuthenticators(new HttpBasicAuth(credentialAuth));
253260
}
254-
255-
private void registerServletContexts(final LocalHttpServer httpService, final String moonEndpointPath) {
256-
LOG.info("attempting registration of AAA moon and auth servlets");
257-
258-
Preconditions.checkNotNull(httpService, "httpService cannot be null");
259-
httpService.registerServlet(moonEndpointPath, new MoonTokenEndpoint(), null);
260-
}
261261
}

lighty-modules/lighty-aaa-aggregator/lighty-aaa/src/main/java/io/lighty/aaa/LocalHttpServer.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

lighty-modules/lighty-aaa-aggregator/lighty-aaa/src/test/java/io/lighty/aaa/LocalHttpServerTest.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)