Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deps): update keycloak.version to v21 (major) #59

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
image: quay.io/keycloak/keycloak:21.0
command: [ 'start-dev', '--debug' ]
volumes:
- ./target/keycloak-event-listener-sns-1.0.2-SNAPSHOT.jar:/opt/keycloak/providers/keycloak-event-listener-sns.jar
- ./target/keycloak-event-listener-sns-1.1.1-SNAPSHOT.jar:/opt/keycloak/providers/keycloak-event-listener-sns.jar
environment:
- KEYCLOAK_ADMIN=admin
- KEYCLOAK_ADMIN_PASSWORD=admin
Expand Down
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@
<maven.javadoc.plugin.version>3.5.0</maven.javadoc.plugin.version>
<maven-shade.version>3.4.1</maven-shade.version>

<keycloak.version>20.0.5</keycloak.version>
<keycloak.version>21.1.2</keycloak.version>
<aws-java-sdk-sns.version>1.12.578</aws-java-sdk-sns.version>
<junit-jupiter.version>5.9.2</junit-jupiter.version>
<assertj.version>3.24.2</assertj.version>
<jboss-logging.version>3.5.3.Final</jboss-logging.version>
<mockito.version>5.1.1</mockito.version>
<jta.version>1.1</jta.version>
</properties>

<build>
Expand Down Expand Up @@ -250,7 +251,12 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>${jta.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/fr/redfroggy/keycloak/SnsEventListenerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@
import org.keycloak.events.EventListenerProvider;
import org.keycloak.events.EventListenerTransaction;
import org.keycloak.events.admin.AdminEvent;
import org.keycloak.models.KeycloakTransactionManager;
import org.keycloak.models.RealmProvider;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserProvider;
import org.keycloak.models.*;

import static org.keycloak.models.utils.KeycloakModelUtils.runJobInTransaction;

public class SnsEventListenerProvider implements EventListenerProvider {

private final SnsEventPublisher snsEventPublisher;

private final KeycloakSessionFactory sessionFactory;
private final EventListenerTransaction transaction = new EventListenerTransaction(this::sendAdminEvent,
this::sendEvent);

private final UserProvider userProvider;

private final RealmProvider realmProvider;

public SnsEventListenerProvider(SnsEventPublisher snsEventPublisher, KeycloakTransactionManager transactionManager,
UserProvider userProvider, RealmProvider realmProvider) {
public SnsEventListenerProvider(SnsEventPublisher snsEventPublisher,
KeycloakSessionFactory sessionFactory,
KeycloakTransactionManager transactionManager) {
this.snsEventPublisher = snsEventPublisher;
this.sessionFactory = sessionFactory;
transactionManager.enlistAfterCompletion(transaction);
this.userProvider = userProvider;
this.realmProvider = realmProvider;
}

@Override
Expand All @@ -44,19 +40,23 @@ public void close() {
}

private void sendEvent(Event event) {
snsEventPublisher.sendEvent(new SnsEvent(event, getUsername(event.getRealmId(), event.getUserId())));
runJobInTransaction(sessionFactory, session ->
snsEventPublisher.sendEvent(new SnsEvent(event, getUsername(session.realms(), session.users(), event.getRealmId(), event.getUserId()))));
}

private void sendAdminEvent(AdminEvent adminEvent, boolean includeRepresentation) {
String adminUserId = null;
if (adminEvent.getAuthDetails() != null) {
adminUserId = adminEvent.getAuthDetails().getUserId();
}
snsEventPublisher.sendAdminEvent(new SnsAdminEvent(adminEvent, getUsername(adminEvent.getRealmId(), adminUserId)));

runJobInTransaction(sessionFactory, session -> {
String adminUserId = null;
if (adminEvent.getAuthDetails() != null) {
adminUserId = adminEvent.getAuthDetails().getUserId();
}
snsEventPublisher.sendAdminEvent(
new SnsAdminEvent(adminEvent, getUsername(session.realms(), session.users(), adminEvent.getRealmId(), adminUserId)));
}
);
}

private String getUsername(String realmId, String userId) {
private String getUsername(RealmProvider realmProvider, UserProvider userProvider, String realmId, String userId) {
UserModel user;
if (userId != null) {
user = userProvider.getUserById(realmProvider.getRealm(realmId), userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public void close() {
public EventListenerProvider create(KeycloakSession session) {
AmazonSNSAsync snsClient = AmazonSNSAsyncClientBuilder.standard().build();
ObjectMapper mapper = new ObjectMapper();
return new SnsEventListenerProvider(new SnsEventPublisher(snsClient, snsEventListenerConfiguration, mapper), session.getTransactionManager(), session.users(), session.realms());
return new SnsEventListenerProvider(new SnsEventPublisher(snsClient, snsEventListenerConfiguration, mapper),
session.getKeycloakSessionFactory(),
session.getTransactionManager());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
import org.keycloak.events.EventListenerTransaction;
import org.keycloak.events.admin.AdminEvent;
import org.keycloak.events.admin.AuthDetails;
import org.keycloak.models.KeycloakTransactionManager;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RealmProvider;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserProvider;
import org.keycloak.models.*;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -47,6 +43,12 @@ class SnsEventListenerProviderTest {
@Mock
private KeycloakTransactionManager transactionManagerMock;

@Mock
private KeycloakSessionFactory sessionFactory;

@Mock
private KeycloakSession session;

@Mock
private SnsEventPublisher snsEventPublisherMock;

Expand Down Expand Up @@ -76,6 +78,11 @@ void shouldAddEventToTransaction(){
when(realmProviderMock.getRealm("realmId")).thenReturn(realmMock);
when(userProviderMock.getUserById(realmMock, "userId")).thenReturn(userMock);
when(userMock.getUsername()).thenReturn("username");
when(sessionFactory.create()).thenReturn(session);
when(session.getTransactionManager()).thenReturn(transactionManagerMock);
when(session.realms()).thenReturn(realmProviderMock);
when(session.users()).thenReturn(userProviderMock);

snsEventListenerProvider.onEvent(eventMock);
verify(transactionManagerMock).enlistAfterCompletion(transactionCaptor.capture());
EventListenerTransaction transaction = transactionCaptor.getValue();
Expand All @@ -91,6 +98,9 @@ void shouldAddEventToTransaction(){
void shouldAddEventToTransactionWithUsernameToNullBecauseUserIdNull(){
when(eventMock.getUserId()).thenReturn(null);
when(eventMock.getRealmId()).thenReturn("realmId");
when(sessionFactory.create()).thenReturn(session);
when(session.getTransactionManager()).thenReturn(transactionManagerMock);

snsEventListenerProvider.onEvent(eventMock);
verify(transactionManagerMock).enlistAfterCompletion(transactionCaptor.capture());
EventListenerTransaction transaction = transactionCaptor.getValue();
Expand All @@ -109,6 +119,11 @@ void shouldAddEventToTransactionWithUsernameToNullBecauseUserNull(){
when(realmProviderMock.getRealm("realmId")).thenReturn(realmMock);
when(userProviderMock.getUserById(realmMock, "userId")).thenReturn(null);
snsEventListenerProvider.onEvent(eventMock);
when(sessionFactory.create()).thenReturn(session);
when(session.getTransactionManager()).thenReturn(transactionManagerMock);
when(session.realms()).thenReturn(realmProviderMock);
when(session.users()).thenReturn(userProviderMock);

verify(transactionManagerMock).enlistAfterCompletion(transactionCaptor.capture());
EventListenerTransaction transaction = transactionCaptor.getValue();
transaction.begin();
Expand All @@ -127,6 +142,11 @@ void shouldAddAdminEventToTransaction(){
when(realmProviderMock.getRealm("realmId")).thenReturn(realmMock);
when(userProviderMock.getUserById(realmMock, "userId")).thenReturn(userMock);
when(userMock.getUsername()).thenReturn("username");
when(sessionFactory.create()).thenReturn(session);
when(session.getTransactionManager()).thenReturn(transactionManagerMock);
when(session.realms()).thenReturn(realmProviderMock);
when(session.users()).thenReturn(userProviderMock);

snsEventListenerProvider.onEvent(adminEventMock, true);
verify(transactionManagerMock).enlistAfterCompletion(transactionCaptor.capture());
EventListenerTransaction transaction = transactionCaptor.getValue();
Expand All @@ -141,6 +161,9 @@ void shouldAddAdminEventToTransaction(){
@Test
void shouldAddAdminEventToTransactionWithUsernameToNullBecauseAuthDetailsNull(){
when(adminEventMock.getAuthDetails()).thenReturn(null);
when(sessionFactory.create()).thenReturn(session);
when(session.getTransactionManager()).thenReturn(transactionManagerMock);

snsEventListenerProvider.onEvent(adminEventMock, true);
verify(transactionManagerMock).enlistAfterCompletion(transactionCaptor.capture());
EventListenerTransaction transaction = transactionCaptor.getValue();
Expand Down