Skip to content

Commit a46c44b

Browse files
committed
Fix #33620: allow additional attributes in statistics
1 parent 1354e23 commit a46c44b

11 files changed

Lines changed: 234 additions & 48 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/event/ProxyStartEvent.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package eu.openanalytics.containerproxy.event;
2222

2323
import com.fasterxml.jackson.annotation.JsonCreator;
24+
import com.fasterxml.jackson.annotation.JsonIgnore;
2425
import com.fasterxml.jackson.annotation.JsonProperty;
2526
import eu.openanalytics.containerproxy.model.runtime.Proxy;
2627
import eu.openanalytics.containerproxy.model.runtime.ProxyStartupLog;
@@ -30,6 +31,7 @@
3031
import lombok.EqualsAndHashCode;
3132
import lombok.NoArgsConstructor;
3233
import lombok.Value;
34+
import org.springframework.security.core.Authentication;
3335

3436
@Value
3537
@EqualsAndHashCode(callSuper = true)
@@ -43,6 +45,8 @@ public class ProxyStartEvent extends BridgeableEvent {
4345
Long createdTimestamp;
4446
BackendContainerName backendContainerName;
4547
ProxyStartupLog proxyStartupLog;
48+
@JsonIgnore
49+
Authentication authentication;
4650

4751
@JsonCreator
4852
public ProxyStartEvent(@JsonProperty("source") String source,
@@ -53,6 +57,18 @@ public ProxyStartEvent(@JsonProperty("source") String source,
5357
@JsonProperty("createdTimestamp") Long createdTimestamp,
5458
@JsonProperty("backendContainerName") BackendContainerName backendContainerName,
5559
@JsonProperty("proxyStartupLog") ProxyStartupLog proxyStartupLog) {
60+
this(source, proxyId, userId, specId, instance, createdTimestamp, backendContainerName, proxyStartupLog, null);
61+
}
62+
63+
public ProxyStartEvent(String source,
64+
String proxyId,
65+
String userId,
66+
String specId,
67+
String instance,
68+
long createdTimestamp,
69+
BackendContainerName backendContainerName,
70+
ProxyStartupLog proxyStartupLog,
71+
Authentication authentication) {
5672
super(source);
5773
this.proxyId = proxyId;
5874
this.userId = userId;
@@ -61,16 +77,19 @@ public ProxyStartEvent(@JsonProperty("source") String source,
6177
this.createdTimestamp = createdTimestamp;
6278
this.backendContainerName = backendContainerName;
6379
this.proxyStartupLog = proxyStartupLog;
80+
this.authentication = authentication;
6481
}
6582

66-
public ProxyStartEvent(Proxy proxy, ProxyStartupLog proxyStartupLog) {
67-
this(SOURCE_NOT_AVAILABLE, proxy.getId(),
83+
public ProxyStartEvent(Proxy proxy, ProxyStartupLog proxyStartupLog, Authentication authentication) {
84+
this(SOURCE_NOT_AVAILABLE,
85+
proxy.getId(),
6886
proxy.getUserId(),
6987
proxy.getSpecId(),
7088
proxy.getRuntimeValue("SHINYPROXY_APP_INSTANCE"),
7189
proxy.getCreatedTimestamp(),
7290
proxy.getContainers().isEmpty() ? null : proxy.getContainers().get(0).getRuntimeObjectOrNull(BackendContainerNameKey.inst),
73-
proxyStartupLog);
91+
proxyStartupLog,
92+
authentication);
7493
}
7594

7695
@Override

src/main/java/eu/openanalytics/containerproxy/event/ProxyStopEvent.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
package eu.openanalytics.containerproxy.event;
2222

2323
import com.fasterxml.jackson.annotation.JsonCreator;
24+
import com.fasterxml.jackson.annotation.JsonIgnore;
2425
import com.fasterxml.jackson.annotation.JsonProperty;
2526
import eu.openanalytics.containerproxy.model.runtime.Proxy;
2627
import eu.openanalytics.containerproxy.model.runtime.ProxyStopReason;
2728
import lombok.AccessLevel;
2829
import lombok.EqualsAndHashCode;
2930
import lombok.NoArgsConstructor;
3031
import lombok.Value;
32+
import org.springframework.security.core.Authentication;
3133

3234
import java.time.Duration;
3335

@@ -41,6 +43,8 @@ public class ProxyStopEvent extends BridgeableEvent {
4143
String specId;
4244
ProxyStopReason proxyStopReason;
4345
Duration usageTime;
46+
@JsonIgnore
47+
Authentication authentication;
4448

4549
@JsonCreator
4650
public ProxyStopEvent(@JsonProperty("source") String source,
@@ -49,17 +53,33 @@ public ProxyStopEvent(@JsonProperty("source") String source,
4953
@JsonProperty("specId") String specId,
5054
@JsonProperty("proxyStopReason") ProxyStopReason proxyStopReason,
5155
@JsonProperty("usageTime") Duration usageTime) {
56+
this(source, proxyId, userId, specId, proxyStopReason, usageTime, null);
57+
}
58+
59+
public ProxyStopEvent(String source,
60+
String proxyId,
61+
String userId,
62+
String specId,
63+
ProxyStopReason proxyStopReason,
64+
Duration usageTime,
65+
Authentication authentication) {
5266
super(source);
5367
this.proxyId = proxyId;
5468
this.userId = userId;
5569
this.specId = specId;
5670
this.proxyStopReason = proxyStopReason;
5771
this.usageTime = usageTime;
72+
this.authentication = authentication;
5873
}
5974

60-
public ProxyStopEvent(Proxy proxy, ProxyStopReason proxyStopReason) {
61-
this(SOURCE_NOT_AVAILABLE, proxy.getId(), proxy.getUserId(), proxy.getSpecId(), proxyStopReason,
62-
proxy.getStartupTimestamp() == 0 ? null : Duration.ofMillis(System.currentTimeMillis() - proxy.getStartupTimestamp()));
75+
public ProxyStopEvent(Proxy proxy, ProxyStopReason proxyStopReason, Authentication authentication) {
76+
this(SOURCE_NOT_AVAILABLE,
77+
proxy.getId(),
78+
proxy.getUserId(),
79+
proxy.getSpecId(),
80+
proxyStopReason,
81+
proxy.getStartupTimestamp() == 0 ? null : Duration.ofMillis(System.currentTimeMillis() - proxy.getStartupTimestamp()),
82+
authentication);
6383
}
6484

6585
@Override

src/main/java/eu/openanalytics/containerproxy/event/UserLoginEvent.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,24 @@
2121
package eu.openanalytics.containerproxy.event;
2222

2323
import org.springframework.context.ApplicationEvent;
24+
import org.springframework.security.core.Authentication;
2425

2526
public class UserLoginEvent extends ApplicationEvent {
2627

2728
private final String userId;
29+
private final Authentication authentication;
2830

29-
public UserLoginEvent(Object source, String userId) {
31+
public UserLoginEvent(Object source, String userId, Authentication authentication) {
3032
super(source);
3133
this.userId = userId;
34+
this.authentication = authentication;
3235
}
3336

34-
3537
public String getUserId() {
3638
return userId;
3739
}
3840

41+
public Authentication getAuthentication() {
42+
return authentication;
43+
}
3944
}

src/main/java/eu/openanalytics/containerproxy/event/UserLogoutEvent.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,24 @@
2121
package eu.openanalytics.containerproxy.event;
2222

2323
import org.springframework.context.ApplicationEvent;
24+
import org.springframework.security.core.Authentication;
2425

2526
public class UserLogoutEvent extends ApplicationEvent {
2627

2728
private final String userId;
2829
private final Boolean wasExpired;
30+
private final Authentication authentication;
2931

3032
/**
3133
* @param source
3234
* @param userId
3335
* @param wasExpired whether the user is logged automatically because the session has expired
3436
*/
35-
public UserLogoutEvent(Object source, String userId, Boolean wasExpired) {
37+
public UserLogoutEvent(Object source, String userId, Boolean wasExpired, Authentication authentication) {
3638
super(source);
3739
this.userId = userId;
3840
this.wasExpired = wasExpired;
41+
this.authentication = authentication;
3942
}
4043

4144
public String getUserId() {
@@ -45,4 +48,8 @@ public String getUserId() {
4548
public Boolean getWasExpired() {
4649
return wasExpired;
4750
}
51+
52+
public Authentication getAuthentication() {
53+
return authentication;
54+
}
4855
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public Command startProxy(Authentication user, ProxySpec spec, List<RuntimeValue
299299

300300
if (result != null) {
301301
slog.info(result, "Proxy activated");
302-
applicationEventPublisher.publishEvent(new ProxyStartEvent(result, proxyStartupLog.succeeded()));
302+
applicationEventPublisher.publishEvent(new ProxyStartEvent(result, proxyStartupLog.succeeded(), user));
303303

304304
// final check to see if the app was stopped
305305
cleanupIfPendingAppWasStopped(result);
@@ -355,7 +355,7 @@ public Command stopProxy(Authentication user, Proxy proxy, boolean ignoreAccessC
355355
}
356356
slog.info(stoppedProxy, "Proxy released");
357357

358-
applicationEventPublisher.publishEvent(new ProxyStopEvent(stoppedProxy, proxyStopReason));
358+
applicationEventPublisher.publishEvent(new ProxyStopEvent(stoppedProxy, proxyStopReason, user));
359359
});
360360
}
361361

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ public void logout(Authentication auth) {
233233
applicationEventPublisher.publishEvent(new UserLogoutEvent(
234234
this,
235235
userId,
236-
false));
236+
false,
237+
auth));
237238
}
238239

239240
@EventListener
@@ -246,7 +247,8 @@ public void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event) {
246247

247248
applicationEventPublisher.publishEvent(new UserLoginEvent(
248249
this,
249-
userId));
250+
userId,
251+
auth));
250252
}
251253

252254
@EventListener
@@ -263,14 +265,16 @@ public void onHttpSessionDestroyedEvent(HttpSessionDestroyedEvent event) {
263265
SecurityContext securityContext = event.getSecurityContexts().get(0);
264266
if (securityContext == null || authNull(securityContext.getAuthentication())) return;
265267

268+
Authentication auth = securityContext.getAuthentication();
266269
String userId = securityContext.getAuthentication().getName();
267270
if (logoutStrategy != null) logoutStrategy.onLogout(userId);
268271

269272
log.info(String.format("User logged out [user: %s]", userId));
270273
applicationEventPublisher.publishEvent(new UserLogoutEvent(
271274
this,
272275
userId,
273-
true
276+
true,
277+
auth
274278
));
275279
} else if (authBackend.getName().equals("none")) {
276280
String userId = NoAuthenticationBackend.extractUserId(event.getSession());
@@ -281,7 +285,8 @@ public void onHttpSessionDestroyedEvent(HttpSessionDestroyedEvent event) {
281285
applicationEventPublisher.publishEvent(new UserLogoutEvent(
282286
this,
283287
userId,
284-
true
288+
true,
289+
null
285290
));
286291
}
287292
}

src/main/java/eu/openanalytics/containerproxy/spec/expression/SpecExpressionContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import lombok.Builder;
3333
import lombok.EqualsAndHashCode;
3434
import lombok.Value;
35+
import org.springframework.context.ApplicationEvent;
3536
import org.springframework.security.core.Authentication;
3637
import org.springframework.security.ldap.userdetails.LdapUserDetails;
3738

@@ -55,6 +56,7 @@ public class SpecExpressionContext {
5556
List<String> groups;
5657
String userId;
5758
JsonNode json;
59+
ApplicationEvent event;
5860

5961
public static SpecExpressionContext create(Object... objects) {
6062
SpecExpressionContextBuilder builder = SpecExpressionContext.builder();
@@ -79,6 +81,8 @@ public static SpecExpressionContext create(SpecExpressionContextBuilder builder,
7981
builder.webServiceUser = (WebServiceAuthenticationBackend.WebServiceUser) o;
8082
} else if (o instanceof JsonNode) {
8183
builder.json = (JsonNode) o;
84+
} else if (o instanceof ApplicationEvent) {
85+
builder.event = (ApplicationEvent) o;
8286
}
8387
if (o instanceof Authentication) {
8488
builder.groups = UserService.getGroups((Authentication) o);

0 commit comments

Comments
 (0)