diff --git a/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionConverter.java b/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionConverter.java index 43befa6049..d3aec8cc30 100644 --- a/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionConverter.java +++ b/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionConverter.java @@ -38,7 +38,7 @@ public class SessionConverter { SessionUtils.AUTH_CONTEXT_SESSION_KEY ); - public static SessionDTO simpleSessionToSessionDTO(SimpleSession simpleSession) { + public static SessionDTO.Builder simpleSessionToSessionDTOBuilder(SimpleSession simpleSession) { final var unknownKeys = unknownSessionKeys(simpleSession); if (!unknownKeys.isEmpty()) { @@ -60,8 +60,7 @@ public class SessionConverter { .authenticationRealm(principalInfo.map(PrincipalInfo::realm).orElse(null)) .authenticated((Boolean) simpleSession.getAttribute(DefaultSubjectContext.AUTHENTICATED_SESSION_KEY)) .userName((String) simpleSession.getAttribute(SessionUtils.USERNAME_SESSION_KEY)) - .authContext((SessionAuthContext) simpleSession.getAttribute(SessionUtils.AUTH_CONTEXT_SESSION_KEY)) - .build(); + .authContext((SessionAuthContext) simpleSession.getAttribute(SessionUtils.AUTH_CONTEXT_SESSION_KEY)); } private static Set unknownSessionKeys(SimpleSession simpleSession) { diff --git a/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionDAO.java b/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionDAO.java index c39f491589..0ab6da8462 100644 --- a/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionDAO.java +++ b/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionDAO.java @@ -72,7 +72,7 @@ public class SessionDAO extends CachingSessionDAO { assignSessionId(session, sessionId); - final var primaryKey = sessionService.create(SessionDTO.fromSimpleSession(session)); + final var primaryKey = sessionService.create(SessionDTO.builderFromSimpleSession(session).build()); LOG.debug("Created session {}", primaryKey); return sessionId; @@ -93,7 +93,7 @@ public class SessionDAO extends CachingSessionDAO { private void doUpdate(SimpleSession session) { LOG.debug("Updating session"); - final var sessionDTO = SessionDTO.fromSimpleSession(session); + final var sessionDTO = SessionDTO.builderFromSimpleSession(session).build(); sessionService.updateBySessionId(sessionDTO); } diff --git a/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionDTO.java b/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionDTO.java index 3c9b0357bb..ff4705c349 100644 --- a/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionDTO.java +++ b/graylog2-server/src/main/java/org/graylog2/security/sessions/SessionDTO.java @@ -33,6 +33,7 @@ import java.util.Optional; public abstract class SessionDTO implements BuildableMongoEntity { @Override + @Nullable @JsonInclude(JsonInclude.Include.NON_NULL) public abstract String id(); @@ -130,8 +131,8 @@ public abstract class SessionDTO implements BuildableMongoEntity SessionConverter.simpleSessionToSessionDTO(simpleSession)) + assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("unknown attribute keys"); } @@ -122,25 +122,25 @@ class SessionConverterTest { simpleSession.setId("session-id"); simpleSession.setHost("localhost"); - assertThat(SessionConverter.simpleSessionToSessionDTO(simpleSession).userId()).isEmpty(); + assertThat(SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build().userId()).isEmpty(); simpleSession.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY, new SimplePrincipalCollection()); - assertThat(SessionConverter.simpleSessionToSessionDTO(simpleSession).userId()).isEmpty(); + assertThat(SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build().userId()).isEmpty(); simpleSession.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY, "not-a-collection"); - assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTO(simpleSession)) + assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Unexpected type"); simpleSession.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY, new SimplePrincipalCollection(List.of("a", "b", "c"), "realm")); - assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTO(simpleSession)) + assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Expected a single principal"); simpleSession.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY, new SimplePrincipalCollection(1, "realm")); - assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTO(simpleSession)) + assertThatThrownBy(() -> SessionConverter.simpleSessionToSessionDTOBuilder(simpleSession).build()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Unexpected type"); }