Skip to content

Commit

Permalink
Configured value for voPersonId
Browse files Browse the repository at this point in the history
  • Loading branch information
cgeorgilakis-grnet committed Sep 23, 2024
1 parent 3827d56 commit 61a7889
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
For Keycloak upstream changelog please see https://www.keycloak.org/docs/latest/release_notes/index.html.
Full Keycloak upstream jira issue can be shown if filtered by Fix version. For example [Keycloak jira issue for 15.0.2 version](https://issues.redhat.com/browse/KEYCLOAK-19161?jql=project%20%3D%20keycloak%20and%20fixVersion%20%3D%2015.0.2)

## [2.1.0] - 23-09-2024

### Changed
- Configured value for voPersonId

## [2.1.0] - 10-7-2024
### Added
- Add idpName together with authnAuthority
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You need to define the following realm attributes in order Keycloak metric plugi
- tenenvId : environment parameter
- source : Keycloak
- keycloakUrl : Keycloak main url
- metricsUserIdAttribute : key of event details that value will be set to voPersonId, default to username

Moreover, you need to add in Events Config -> Event Listeners the "metrics-communication".

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<okhttp.version>4.12.0</okhttp.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<metrics-version>2.1.0</metrics-version>
<metrics-version>2.2.0</metrics-version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.keycloak.events.Details;
import org.keycloak.events.Event;
import org.keycloak.metrics.utils.MetricsUtils;
import org.keycloak.models.ClientModel;
Expand Down Expand Up @@ -46,7 +47,7 @@ public MetricsDto() {

}

public MetricsDto(Event event, RealmModel realm) throws IOException {
public MetricsDto(Event event, RealmModel realm) throws Exception {
this.date = LocalDateTime.ofInstant(Instant.ofEpochMilli(event.getTime()), ZoneId.systemDefault());
this.tenenvId = realm.getAttribute(MetricsUtils.TENENV_ID);
this.source = realm.getAttribute(MetricsUtils.SOURCE);
Expand Down Expand Up @@ -82,11 +83,14 @@ public MetricsDto(Event event, RealmModel realm) throws IOException {
}
}

private void setLogin(Event event, RealmModel realm) throws IOException {
private void setLogin(Event event, RealmModel realm) throws Exception {
this.ipAddress = event.getIpAddress();
if (event.getDetails() == null)
event.setDetails(new HashMap<>());
this.voPersonId = event.getDetails().get(MetricsUtils.VO_PERSON_ID);
String userIdentifier = realm.getAttribute(MetricsUtils.METRICS_USER_ID_ATTRIBUTE);
this.voPersonId = event.getDetails().get(userIdentifier != null ? userIdentifier : Details.USERNAME);
if ( this.voPersonId == null)
throw new Exception(userIdentifier + " as userIdentifier does not exist in");
if (event.getDetails().get(MetricsUtils.IDENTITY_PROVIDER_AUTHN_AUTHORITIES) != null) {
AuthnAuthorityRepresentation lastAuthnAuthority = JsonSerialization.readValue(event.getDetails().get(MetricsUtils.IDENTITY_PROVIDER_AUTHN_AUTHORITIES),new TypeReference<LinkedList<AuthnAuthorityRepresentation>>(){}).getLast();
this.entityId = lastAuthnAuthority.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class AmsCommunication {

private static final List<EventType> groupEvents = Stream.of(EventType.valueOf(MetricsUtils.GROUP_MEMBERSHIP_CREATE), EventType.valueOf(MetricsUtils.GROUP_MEMBERSHIP_SUSPEND), EventType.valueOf(MetricsUtils.GROUP_MEMBERSHIP_DELETE)).collect(Collectors.toList());

public static void communicate(RealmModel realm, Event event) throws BadRequestException, IOException {
public static void communicate(RealmModel realm, Event event) throws Exception {
//groupEvents are only allowed for top level groups
if ((!groupEvents.contains(event.getType())) || StringUtils.countMatches(event.getDetails().get(MetricsUtils.EVENT_GROUP), "/") == 1) {
MetricsDto metricsDto = new MetricsDto(event, realm);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/keycloak/metrics/utils/MetricsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class MetricsUtils {
public static final String KEYCLOAK_URL = "keycloakUrl";

public static final String EVENT_GROUP = "group";
public static final String GROUP_MEMBERSHIP_CREATE = "GROUP_MEMBERSHIP_CREATE";
public static final String METRICS_USER_ID_ATTRIBUTE = "metricsUserIdAttribute";
public static final String GROUP_MEMBERSHIP_CREATE = "GROUP_MEMBEmetricsUserIdAttributeRSHIP_CREATE";
public static final String GROUP_MEMBERSHIP_DELETE = "GROUP_MEMBERSHIP_DELETE";
public static final String GROUP_MEMBERSHIP_SUSPEND = "GROUP_MEMBERSHIP_SUSPEND";
public static final String LOGIN = "LOGIN";
Expand Down

0 comments on commit 61a7889

Please sign in to comment.