8000 SSF: Honor pairwise subject identifier mappers of SSF receiver clients in SET subjects by thomasdarimont · Pull Request #55823 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Draft
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
22 changes: 22 additions & 0 deletions docs/guides/securing-apps/ssf-support.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,28 @@ Administrators can manage subjects from the *Subjects* sub-tab in the admin cons
an *ignore* operation that explicitly excludes a subject (distinct from removing them from the subscription
list), and a *check* action that surfaces what the gate would decide for a given user without emitting an event.

=== Pairwise subject identifiers

When the receiver client has a *Pairwise subject identifier* protocol mapper, the transmitter uses the pairwise
subject wherever a SET would otherwise carry the internal {project_name} user ID. The value is the same `sub` the
client receives in its ID tokens, access tokens and logout tokens, so the receiver can correlate events with the
users it knows. No additional receiver setting is required.

This applies to `iss_sub` subjects issued by the realm and to `opaque` subjects that identify a user, both as the
top-level `sub_id` and as the `user` member of a complex subject. Other formats, such as `email`, are not changed.
Subjects supplied through the <<synthetic-event-emitter,synthetic event emitter>> are rewritten in the same way,
because a receiver with a pairwise mapper is assumed to understand only the pairwise subject. If the pairwise
subject cannot be computed, the event is not delivered; the internal user ID is never sent as a fallback.

Only mappers defined directly on the receiver client are considered. If users log in through a different client
than the one that owns the stream, configure a pairwise mapper with the same sector identifier and salt on the
receiver client, so that both clients see the same subject.

A pairwise subject cannot be mapped back to a user. A receiver with a pairwise mapper therefore cannot subscribe
users through the subject management endpoints with an `iss_sub` subject. Use `default_subjects=ALL`,
*Auto notify on login*, subjects managed by an administrator, or an `email` subject instead.

[[synthetic-event-emitter]]
== Synthetic event emitter

Some security signals do not originate inside {project_name}: an LDAP password change against a federated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.keycloak.ssf.event.token;

import java.util.LinkedHashMap;

import org.keycloak.ssf.subject.SubjectId;
import org.keycloak.ssf.subject.SubjectIdJsonDeserializer;

Expand All @@ -20,6 +22,28 @@ public class SsfSecurityEventToken extends AbstractSecurityEventToken {
@JsonProperty("txn")
protected String txn;

/**
* Returns a shallow copy of this token: the copy has its own
* claims, {@code aud} array and {@code events} map, but shares the
* subject and the event payload objects with this token. Callers
* that need a variant of a token copy it and then replace
* individual claims.
*
* <p>Every claim added to this class or its superclass must be
* copied here.
*/
public SsfSecurityEventToken copy() {
SsfSecurityEventToken copy = new SsfSecurityEventToken();
copy.setJti(getJti());
copy.setIss(getIss());
copy.setIat(getIat());
copy.setAud(getAud() != null ? getAud().clone() : null);
copy.setEvents(new LinkedHashMap<>(getEvents()));
copy.setSubjectId(subjectId);
copy.setTxn(txn);
return copy;
}

public SubjectId getSubjectId() {
return subjectId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.keycloak.ssf.subject;

import java.util.HashMap;

import org.keycloak.ssf.event.caep.CaepSessionRevoked;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -75,6 +77,30 @@ public ComplexSubjectId() {
super(TYPE);
}

/**
* Returns a shallow copy of this complex subject: the copy has its
* own member slots and attribute map, but shares the member
* {@link SubjectId} instances with this subject. Callers that need
* a variant of a complex subject copy it and then replace
* individual members.
*
* <p>Every member added to this class must be copied here.
*/
public ComplexSubjectId copy() {
ComplexSubjectId copy = new ComplexSubjectId();
copy.setUser(user);
copy.setDevice(device);
copy.setSession(session);
copy.setApplication(application);
copy.setTenant(tenant);
copy.setOrgUnit(orgUnit);
copy.setGroup(group);
if (attributes != null) {
copy.setAttributes(new HashMap<>(attributes));
}
return copy;
}

public SubjectId getUser() {
return user;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.keycloak.ssf.event.token;

import java.util.LinkedHashMap;
import java.util.Map;

import org.keycloak.ssf.subject.OpaqueSubjectId;
import org.keycloak.util.JsonSerialization;

import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;

class SsfSecurityEventTokenTest {

/**
* Compares the serialized form, so a claim that is added later but
* forgotten in {@link SsfSecurityEventToken#copy()} fails this test
* as soon as it is set here.
*/
@Test
void copy_carriesAllClaims() {
SsfSecurityEventToken original = token();

SsfSecurityEventToken copy = original.copy();

assertNotSame(original, copy);
assertEquals(JsonSerialization.mapper.valueToTree(original),
(JsonNode) JsonSerialization.mapper.valueToTree(copy));
assertSame(original.getSubjectId(), copy.getSubjectId());
}

@Test
void copy_isIndependentOfTheOriginal() {
SsfSecurityEventToken original = token();
OpaqueSubjectId originalSubject = (OpaqueSubjectId) original.getSubjectId();

SsfSecurityEventToken copy = original.copy();
copy.setSubjectId(new OpaqueSubjectId());
copy.getEvents().put("urn:example:other-event", Map.of());
copy.getAud()[0] = "changed";

assertSame(originalSubject, original.getSubjectId());
assertEquals(1, original.getEvents().size());
assertEquals("https://receiver.example.test", original.getAud()[0]);
}

private static SsfSecurityEventToken token() {
OpaqueSubjectId subject = new OpaqueSubjectId();
subject.setId("subject-1");

Map<String, Object> events = new LinkedHashMap<>();
events.put("urn:example:event", Map.of("reason", "test"));

SsfSecurityEventToken token = new SsfSecurityEventToken();
token.setJti("jti-1");
token.setIss("https://kc.example.test/realms/test");
token.setIat(1700000000);
token.setAud(new String[]{"https://receiver.example.test"});
token.setTxn("txn-1");
token.setEvents(events);
token.setSubjectId(subject);
return token;
}
}
CF8B
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.keycloak.ssf.subject;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;

class ComplexSubjectIdTest {

/**
* Guards {@link ComplexSubjectId#copy()} against members that are
* added to the class later but forgotten in the copy: every member
* field is discovered reflectively, so a new one fails this test
* until {@code copy()} handles it.
*/
@Test
void copy_copiesEveryMember() throws Exception {
ComplexSubjectId original = new ComplexSubjectId();
List<Field> memberFields = memberFields();
assertFalse(memberFields.isEmpty(), "expected ComplexSubjectId to declare subject members");
for (Field field : memberFields) {
OpaqueSubjectId member = new OpaqueSubjectId();
member.setId(field.getName());
field.set(original, member);
}

ComplexSubjectId copy = original.copy();

assertNotSame(original, copy);
for (Field field : memberFields) {
assertSame(field.get(original), field.get(copy),
"copy() must copy the '" + field.getName() + "' member");
}
}

@Test
void copy_isIndependentOfTheOriginal() {
ComplexSubjectId original = new ComplexSubjectId();
OpaqueSubjectId user = new OpaqueSubjectId();
original.setUser(user);
original.setAttribute("custom", "value");

ComplexSubjectId copy = original.copy();
copy.setUser(new OpaqueSubjectId());
copy.setAttribute("custom", "changed");

assertSame(user, original.getUser(), "replacing a member on the copy must not affect the original");
assertEquals("value", original.getAttributes().get("custom"),
"changing an attribute on the copy must not affect the original");
assertEquals(ComplexSubjectId.TYPE, copy.getFormat());
}

private static List<Field> memberFields() {
List<Field> fields = new ArrayList<>();
for (Field field : ComplexSubjectId.class.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers()) && SubjectId.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
fields.add(field);
}
}
return fields;
}
}
Loading
Loading
0