Skip to content
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 keycloak-event-emitter-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.cloudtrust</groupId>
<artifactId>kc-cloudtrust-testsuite</artifactId>
<version>26.0.0</version>
<version>26.6.0-SNAPSHOT</version>
<relativePath />
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.keycloak.events.Event;
import org.keycloak.events.EventType;
import org.keycloak.events.admin.AdminEvent;
Expand All @@ -20,6 +22,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

class SerializationUtilsTest {
private long UID = 123456789L;
Expand All @@ -46,13 +49,13 @@ void testEventMinimalToJson() throws JsonProcessingException {
@Test
void testAdminEventToJson() throws JsonProcessingException {
String jsonEvent = SerializationUtils.toJson(createExtendedAdminEvent());
Assertions.assertEquals("{\"id\":null,\"time\":120000,\"realmId\":\"realmId\",\"realmName\":null,\"resourceType\":\"AUTHORIZATION_RESOURCE\",\"operationType\":\"CREATE\",\"resourcePath\":\"resource/path\",\"representation\":\"representation\",\"error\":\"error\",\"uid\":123456789,\"extAuthDetails\":{\"realmId\":\"authDetails-realmId\",\"realmName\":null,\"clientId\":\"authDetails-clientId\",\"userId\":\"authDetails-userId\",\"ipAddress\":\"authDetails-ipAddress\",\"username\":\"authDetails-username\"},\"details\":{\"user_id\":\"userid\",\"username\":\"username\"}}", jsonEvent);
Assertions.assertEquals("{\"id\":null,\"time\":120000,\"realmId\":\"realmId\",\"realmName\":null,\"resourceType\":\"AUTHORIZATION_RESOURCE\",\"operationType\":\"CREATE\",\"resourcePath\":\"resource/path\",\"representation\":\"representation\",\"error\":\"error\",\"uid\":123456789,\"resourceId\":\"path\",\"extAuthDetails\":{\"realmId\":\"authDetails-realmId\",\"realmName\":null,\"clientId\":\"authDetails-clientId\",\"userId\":\"authDetails-userId\",\"ipAddress\":\"authDetails-ipAddress\",\"username\":\"authDetails-username\"},\"details\":{\"user_id\":\"userid\",\"username\":\"username\"}}", jsonEvent);
}

@Test
void testMinimalAdminEventToJson() throws JsonProcessingException {
String jsonEvent = SerializationUtils.toJson(createMinimalExtendedAdminEvent());
Assertions.assertEquals("{\"id\":null,\"time\":120000,\"realmId\":null,\"realmName\":null,\"resourceType\":null,\"operationType\":null,\"resourcePath\":null,\"representation\":null,\"error\":null,\"uid\":123456789,\"extAuthDetails\":null,\"details\":{}}", jsonEvent);
Assertions.assertEquals("{\"id\":null,\"time\":120000,\"realmId\":null,\"realmName\":null,\"resourceType\":null,\"operationType\":null,\"resourcePath\":null,\"representation\":null,\"error\":null,\"uid\":123456789,\"resourceId\":null,\"extAuthDetails\":null,\"details\":{}}", jsonEvent);
}

@Test
Expand All @@ -64,6 +67,17 @@ void testEventToFlatbuffers() {
Assertions.assertEquals(UID, deserializedEvent.uid());
}

@ParameterizedTest
@MethodSource("eventTypes")
void testEventToFlatbuffersIntSerialization(EventType eventType) {
Event event = new Event();
event.setType(eventType);
ByteBuffer buffer = SerializationUtils.toFlat(new IdentifiedEvent(UID, event));
flatbuffers.events.Event deserializedEvent = flatbuffers.events.Event.getRootAsEvent(buffer);
Assertions.assertTrue(equals(event, deserializedEvent));
Assertions.assertEquals(UID, deserializedEvent.uid());
}

@Test
void testMinimalEventToFlatbuffers() {
Event event = createMinimalEvent();
Expand Down Expand Up @@ -198,4 +212,14 @@ private static boolean equals(ExtendedAuthDetails adminEventDetails, flatbuffers
.append(adminEventDetails.getRealmId(), adminEventFlatDetails.realmId())
.build();
}

private static Stream<EventType> eventTypes() {
return Stream.of(
EventType.CLIENT_LOGIN,
EventType.VERIFIABLE_CREDENTIAL_NONCE_REQUEST,
EventType.VERIFIABLE_CREDENTIAL_CREATE_OFFER_ERROR,
EventType.VERIFIABLE_CREDENTIAL_PRE_AUTHORIZED_GRANT,
EventType.JWT_AUTHORIZATION_GRANT
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void initMock() {
RealmModel realm = new RealmAdapter(null, null, realmEntity);
Mockito.when(keycloakSession.realms().getRealm(Mockito.any())).thenReturn(realm);

mockProducer = new MockProducer<>(true, new StringSerializer(), new StringSerializer());
mockProducer = new MockProducer<>(true, null, new StringSerializer(), new StringSerializer());
}

@Test
Expand Down Expand Up @@ -105,7 +105,7 @@ void testAdminEventFlatbufferFormatOutput() {
@Test
void testNoConnection() {
IdGenerator idGenerator = new IdGenerator(1, 1);
try (MockProducer<String, String> stepMockProducer = new MockProducer<>(false, new StringSerializer(), new StringSerializer())) {
try (MockProducer<String, String> stepMockProducer = new MockProducer<>(false, null, new StringSerializer(), new StringSerializer())) {
LinkedBlockingQueue<ProducerRecord<String, String>> pendingEvents = new LinkedBlockingQueue<>(50);

KafkaEventEmitterState state = new KafkaEventEmitterState();
Expand Down
18 changes: 17 additions & 1 deletion keycloak-event-emitter/src/main/flatbuffers/event.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace flatbuffers.events;

enum EventType:byte {
enum EventType:ubyte {
LOGIN,
LOGIN_ERROR,
REGISTER,
Expand Down Expand Up @@ -121,6 +121,20 @@ enum EventType:byte {
REMOVE_CREDENTIAL_ERROR,
INVITE_ORG,
INVITE_ORG_ERROR,
USER_SESSION_DELETED,
USER_SESSION_DELETED_ERROR,
VERIFIABLE_CREDENTIAL_REQUEST,
VERIFIABLE_CREDENTIAL_REQUEST_ERROR,
VERIFIABLE_CREDENTIAL_OFFER_REQUEST,
VERIFIABLE_CREDENTIAL_OFFER_REQUEST_ERROR,
VERIFIABLE_CREDENTIAL_NONCE_REQUEST,
VERIFIABLE_CREDENTIAL_NONCE_REQUEST_ERROR,
VERIFIABLE_CREDENTIAL_CREATE_OFFER,
VERIFIABLE_CREDENTIAL_CREATE_OFFER_ERROR,
VERIFIABLE_CREDENTIAL_PRE_AUTHORIZED_GRANT,
VERIFIABLE_CREDENTIAL_PRE_AUTHORIZED_GRANT_ERROR,
JWT_AUTHORIZATION_GRANT,
JWT_AUTHORIZATION_GRANT_ERROR,
UNKNOWN // Default case
}

Expand Down Expand Up @@ -162,6 +176,8 @@ enum ResourceType:byte {
USER_PROFILE,
ORGANIZATION,
ORGANIZATION_MEMBERSHIP,
ORGANIZATION_GROUP,
ORGANIZATION_GROUP_MEMBERSHIP,
UNKNOWN // Default case
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class Event extends Table {

public long uid() { int o = __offset(4); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
public long time() { int o = __offset(6); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
public byte type() { int o = __offset(8); return o != 0 ? bb.get(o + bb_pos) : 0; }
public int type() { int o = __offset(8); return o != 0 ? bb.get(o + bb_pos) & 0xFF : 0; }
public String realmId() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; }
public ByteBuffer realmIdAsByteBuffer() { return __vector_as_bytebuffer(10, 1); }
public ByteBuffer realmIdInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 10, 1); }
Expand Down Expand Up @@ -57,7 +57,7 @@ public final class Event extends Table {
public static int createEvent(FlatBufferBuilder builder,
long uid,
long time,
byte type,
int type,
int realmIdOffset,
int clientIdOffset,
int userIdOffset,
Expand All @@ -82,7 +82,7 @@ public static int createEvent(FlatBufferBuilder builder,
public static void startEvent(FlatBufferBuilder builder) { builder.startTable(10); }
public static void addUid(FlatBufferBuilder builder, long uid) { builder.addLong(0, uid, 0L); }
public static void addTime(FlatBufferBuilder builder, long time) { builder.addLong(1, time, 0L); }
public static void addType(FlatBufferBuilder builder, byte type) { builder.addByte(2, type, 0); }
public static void addType(FlatBufferBuilder builder, int type) { builder.addByte(2, (byte) type, (byte) 0); }
public static void addRealmId(FlatBufferBuilder builder, int realmIdOffset) { builder.addOffset(3, realmIdOffset, 0); }
public static void addClientId(FlatBufferBuilder builder, int clientIdOffset) { builder.addOffset(4, clientIdOffset, 0); }
public static void addUserId(FlatBufferBuilder builder, int userIdOffset) { builder.addOffset(5, userIdOffset, 0); }
Expand Down
Loading