Skip to content

Commit d0f125f

Browse files
committed
Refactor: remove EventService; use Spring for events
1 parent 126e1ce commit d0f125f

11 files changed

Lines changed: 177 additions & 209 deletions

src/main/java/eu/openanalytics/containerproxy/auth/UserLogoutHandler.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import javax.servlet.http.HttpServletRequest;
2525
import javax.servlet.http.HttpServletResponse;
2626

27-
import eu.openanalytics.containerproxy.event.UserLogoutEvent;
28-
import org.springframework.context.ApplicationEventPublisher;
2927
import org.springframework.security.core.Authentication;
3028
import org.springframework.security.web.authentication.logout.LogoutHandler;
3129
import org.springframework.stereotype.Component;

src/main/java/eu/openanalytics/containerproxy/auth/impl/KerberosAuthenticationBackend.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import javax.inject.Inject;
2929

30+
import eu.openanalytics.containerproxy.event.UserLogoutEvent;
31+
import org.springframework.context.event.EventListener;
3032
import org.springframework.core.env.Environment;
3133
import org.springframework.core.io.FileSystemResource;
3234
import org.springframework.security.authentication.AuthenticationManager;
@@ -52,25 +54,22 @@
5254
import eu.openanalytics.containerproxy.auth.impl.kerberos.KRBClientCacheRegistry;
5355
import eu.openanalytics.containerproxy.auth.impl.kerberos.KRBTicketRenewalManager;
5456
import eu.openanalytics.containerproxy.model.spec.ContainerSpec;
55-
import eu.openanalytics.containerproxy.service.EventService;
56-
import eu.openanalytics.containerproxy.service.EventService.EventType;
5757

5858
public class KerberosAuthenticationBackend implements IAuthenticationBackend {
5959

6060
public static final String NAME = "kerberos";
6161

6262
private KRBClientCacheRegistry ccacheReg;
63-
63+
64+
private KRBTicketRenewalManager renewalManager;
65+
6466
@Inject
6567
Environment environment;
6668

6769
@Lazy
6870
@Inject
6971
AuthenticationManager authenticationManager;
7072

71-
@Inject
72-
EventService eventService;
73-
7473
@Override
7574
public String getName() {
7675
return NAME;
@@ -116,13 +115,9 @@ public void configureAuthenticationManagerBuilder(AuthenticationManagerBuilder a
116115
ticketValidator.setDebug(true);
117116
ticketValidator.afterPropertiesSet();
118117

119-
KRBTicketRenewalManager renewalManager = new KRBTicketRenewalManager(
118+
renewalManager = new KRBTicketRenewalManager(
120119
delegSvcPrinc, delegSvcKeytab, backendPrincipals, ccacheReg, ticketRenewInterval);
121120

122-
eventService.addListener(e -> {
123-
if (EventType.Logout.toString().equals(e.type)) renewalManager.stop(e.user);
124-
});
125-
126121
KerberosServiceAuthenticationProvider authProvider = new KerberosServiceAuthenticationProvider() {
127122
@Override
128123
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
@@ -138,6 +133,11 @@ public Authentication authenticate(Authentication authentication) throws Authent
138133
auth.authenticationProvider(authProvider);
139134
}
140135

136+
@EventListener
137+
public void on(UserLogoutEvent event) {
138+
renewalManager.stop(event.getUserId());
139+
}
140+
141141
@Override
142142
public void customizeContainer(ContainerSpec spec) {
143143
String principal = getCurrentPrincipal();

src/main/java/eu/openanalytics/containerproxy/service/EventService.java

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

src/main/java/eu/openanalytics/containerproxy/service/ProxyService.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import eu.openanalytics.containerproxy.model.runtime.ProxyStatus;
5353
import eu.openanalytics.containerproxy.model.runtime.RuntimeSetting;
5454
import eu.openanalytics.containerproxy.model.spec.ProxySpec;
55-
import eu.openanalytics.containerproxy.service.EventService.EventType;
5655
import eu.openanalytics.containerproxy.spec.IProxySpecMergeStrategy;
5756
import eu.openanalytics.containerproxy.spec.IProxySpecProvider;
5857
import eu.openanalytics.containerproxy.spec.ProxySpecException;
@@ -92,9 +91,6 @@ public class ProxyService {
9291
@Inject
9392
private UserService userService;
9493

95-
@Inject
96-
private EventService eventService;
97-
9894
@Inject
9995
private LogService logService;
10096

@@ -249,8 +245,6 @@ public Proxy startProxy(ProxySpec spec, boolean ignoreAccessControl) throws Cont
249245
}
250246

251247
log.info(String.format("Proxy activated [user: %s] [spec: %s] [id: %s]", proxy.getUserId(), spec.getId(), proxy.getId()));
252-
eventService.post(EventType.ProxyStart.toString(), proxy.getUserId(), spec.getId());
253-
254248
applicationEventPublisher.publishEvent(new ProxyStartEvent(this, proxy.getUserId(), spec.getId(), Duration.ofMillis(proxy.getStartupTimestamp() - proxy.getCreatedTimestamp())));
255249

256250
return proxy;
@@ -275,8 +269,6 @@ public void stopProxy(Proxy proxy, boolean async, boolean ignoreAccessControl) {
275269
backend.stopProxy(proxy);
276270
logService.detach(proxy);
277271
log.info(String.format("Proxy released [user: %s] [spec: %s] [id: %s]", proxy.getUserId(), proxy.getSpec().getId(), proxy.getId()));
278-
eventService.post(EventType.ProxyStop.toString(), proxy.getUserId(), proxy.getSpec().getId());
279-
280272
applicationEventPublisher.publishEvent(new ProxyStopEvent(this, proxy.getUserId(),
281273
proxy.getSpec().getId(),
282274
Duration.ofMillis(System.currentTimeMillis() - proxy.getStartupTimestamp())));

src/main/java/eu/openanalytics/containerproxy/service/UserService.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,18 @@
6060
import eu.openanalytics.containerproxy.backend.strategy.IProxyLogoutStrategy;
6161
import eu.openanalytics.containerproxy.model.runtime.Proxy;
6262
import eu.openanalytics.containerproxy.model.spec.ProxySpec;
63-
import eu.openanalytics.containerproxy.service.EventService.EventType;
6463
import eu.openanalytics.containerproxy.util.SessionHelper;
6564
import org.springframework.web.context.request.RequestContextHolder;
6665

6766

6867
@Service
69-
public class UserService implements ApplicationListener<AbstractAuthenticationEvent> {
68+
public class UserService {
7069

7170
private Logger log = LogManager.getLogger(UserService.class);
7271

7372
@Inject
7473
private Environment environment;
7574

76-
@Inject
77-
private EventService eventService;
78-
7975
@Inject
8076
@Lazy
8177
// Note: lazy needed to work around early initialization conflict
@@ -181,27 +177,25 @@ private String getUserId(Authentication auth) {
181177
}
182178
return auth.getName();
183179
}
184-
185-
@Override
186-
public void onApplicationEvent(AbstractAuthenticationEvent event) {
180+
181+
@EventListener
182+
public void onAbstractAuthenticationFailureEvent(AbstractAuthenticationFailureEvent event) {
187183
Authentication source = event.getAuthentication();
188-
if (event instanceof AbstractAuthenticationFailureEvent) {
189-
Exception e = ((AbstractAuthenticationFailureEvent) event).getException();
190-
log.info(String.format("Authentication failure [user: %s] [error: %s]", source.getName(), e.getMessage()));
191-
String userId = getUserId(source);
192-
applicationEventPublisher.publishEvent(new AuthFailedEvent(
193-
this,
194-
userId,
195-
RequestContextHolder.currentRequestAttributes().getSessionId()));
196-
}
184+
Exception e = event.getException();
185+
log.info(String.format("Authentication failure [user: %s] [error: %s]", source.getName(), e.getMessage()));
186+
String userId = getUserId(source);
187+
188+
applicationEventPublisher.publishEvent(new AuthFailedEvent(
189+
this,
190+
userId,
191+
RequestContextHolder.currentRequestAttributes().getSessionId()));
197192
}
198193

199194
public void logout(Authentication auth) {
200195
// TODO test for anonymous users
201196
String userId = getUserId(auth);
202197
if (userId == null) return;
203198

204-
eventService.post(EventType.Logout.toString(), userId, null);
205199
if (logoutStrategy != null) logoutStrategy.onLogout(userId);
206200
log.info(String.format("User logged out [user: %s]", userId));
207201

@@ -219,7 +213,6 @@ public void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event) {
219213
String userName = auth.getName();
220214

221215
log.info(String.format("User logged in [user: %s]", userName));
222-
eventService.post(EventType.Login.toString(), userName, null);
223216

224217
// TODO test for anonymous users
225218
String userId = getUserId(auth);

src/main/java/eu/openanalytics/containerproxy/stat/IStatCollector.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,35 @@
2020
*/
2121
package eu.openanalytics.containerproxy.stat;
2222

23+
import eu.openanalytics.containerproxy.event.*;
24+
import org.springframework.context.event.EventListener;
25+
2326
import java.io.IOException;
2427

25-
import org.springframework.core.env.Environment;
28+
public interface IStatCollector {
2629

27-
import eu.openanalytics.containerproxy.service.EventService.Event;
30+
@EventListener
31+
default public void onUserLogoutEvent(UserLogoutEvent event) throws IOException {
32+
}
2833

29-
public interface IStatCollector {
34+
@EventListener
35+
default public void onUserLoginEvent(UserLoginEvent event) throws IOException {
36+
}
37+
38+
@EventListener
39+
default public void onProxyStartEvent(ProxyStartEvent event) throws IOException {
40+
}
41+
42+
@EventListener
43+
default public void onProxyStopEvent(ProxyStopEvent event) throws IOException {
44+
}
45+
46+
@EventListener
47+
default public void onProxyStartFailedEvent(ProxyStartFailedEvent event) throws IOException {
48+
}
3049

31-
public void accept(Event event, Environment env) throws IOException;
50+
@EventListener
51+
default public void onAuthFailedEvent(AuthFailedEvent event) throws IOException {
52+
}
3253

3354
}

0 commit comments

Comments
 (0)