fix: moving event enum expected values to quarkus logic

closes: #44679

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
This commit is contained in:
Steve Hawkins 2026-01-08 16:11:04 -05:00 committed by Martin Bartoš
parent aa5022aaf6
commit 967180b024
3 changed files with 22 additions and 41 deletions

View file

@ -23,22 +23,7 @@ public class EventOptions {
.category(OptionCategory.EVENTS)
.description("Comma-separated list of events to be collected for user event metrics. This option can be used to reduce the number of metrics created as by default all user events create a metric.")
.buildTime(false)
.expectedValues(sortedListOfEvents())
.deprecatedMetadata(DeprecatedMetadata.deprecateValues("Use `remove_credential` instead of `remove_totp`, and `update_credential` instead of `update_totp` and `update_password`.", "remove_totp", "update_totp", "update_password"))
.build();
private static List<String> sortedListOfEvents() {
List<String> events = new java.util.ArrayList<>(List.of("register", "login", "code_to_token", "logout", "client_login", "refresh_token", "introspect_token", "federated_identity_link",
"remove_federated_identity", "update_email", "update_profile",
"verify_email", "verify_profile", "grant_consent", "update_consent", "revoke_grant", "send_verify_email", "send_reset_password", "send_identity_provider_link",
"reset_password", "restart_authentication", "invalid_signature", "register_node", "unregister_node", "user_info_request", "identity_provider_link_account", "identity_provider_login",
"identity_provider_first_login", "identity_provider_post_login", "identity_provider_response", "identity_provider_retrieve_token", "impersonate", "custom_required_action",
"execute_actions", "execute_action_token", "client_info", "client_register", "client_update", "client_delete", "client_initiated_account_linking", "token_exchange",
"oauth2_device_auth", "oauth2_device_verify_user_code", "oauth2_device_code_to_token", "authreqid_to_token", "permission_token", "delete_account", "pushed_authorization_request",
"user_disabled_by_permanent_lockout", "user_disabled_by_temporary_lockout", "oauth2_extension_grant", "federated_identity_override_link", "update_credential", "remove_credential",
"invite_org", "remove_totp", "update_totp", "update_password", "user_session_deleted", "verifiable_credential_request", "verifiable_credential_offer_request", "verifiable_credential_nonce_request"));
events.sort(String::compareToIgnoreCase);
return events;
}
}

View file

@ -1,8 +1,11 @@
package org.keycloak.quarkus.runtime.configuration.mappers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.keycloak.common.Profile;
import org.keycloak.events.EventType;
import static org.keycloak.config.EventOptions.USER_EVENT_METRICS_ENABLED;
import static org.keycloak.config.EventOptions.USER_EVENT_METRICS_EVENTS;
@ -27,7 +30,7 @@ final class EventPropertyMappers implements PropertyMapperGrouping {
.paramLabel("tags")
.isEnabled(EventPropertyMappers::userEventsMetricsTags, "user event metrics are enabled")
.build(),
fromOption(USER_EVENT_METRICS_EVENTS)
fromOption(USER_EVENT_METRICS_EVENTS.toBuilder().expectedValues(expectedUserMetricEvents()).build())
.to("kc.spi-events-listener--micrometer-user-event-metrics--events")
.paramLabel("events")
.isEnabled(EventPropertyMappers::userEventsMetricsTags, "user event metrics are enabled")
@ -35,6 +38,23 @@ final class EventPropertyMappers implements PropertyMapperGrouping {
);
}
private static List<String> expectedUserMetricEvents() {
List<String> values = new ArrayList<>();
for (EventType event : EventType.values()) {
if (event.name().endsWith("_ERROR")) {
continue;
}
if (event == EventType.VALIDATE_ACCESS_TOKEN) {
// event is deprecated and no longer used in the code base
continue;
}
String value = event.name().toLowerCase();
values.add(value);
}
Collections.sort(values);
return values;
}
private static boolean userEventsMetricsEnabled() {
return metricsEnabled() && Profile.isFeatureEnabled(Profile.Feature.USER_EVENT_METRICS);
}

View file

@ -28,37 +28,13 @@ import static org.junit.Assert.fail;
public class EventOptionsTest {
/**
* This test ensures that list of CLI options is in-sync with the {@link EventType} enum.
* That enum is not directly accessible by the CLI classes, that's why there is this test.
*/
@Test
public void testAllEnumsArePresent() {
List<String> expectedValues = new ArrayList<>(EventOptions.USER_EVENT_METRICS_EVENTS.getExpectedValues());
public void testDeprecatedArePresent() {
List<String> deprecatedValues = new ArrayList<>(EventOptions.USER_EVENT_METRICS_EVENTS.getDeprecatedMetadata().get().getDeprecatedValues());
List<String> missingOptions = new ArrayList<>();
for (EventType event : EventType.values()) {
if (event.name().endsWith("_ERROR")) {
continue;
}
if (event == EventType.VALIDATE_ACCESS_TOKEN) {
// event is deprecated and no longer used in the code base
continue;
}
String value = event.name().toLowerCase();
if (expectedValues.contains(value)) {
expectedValues.remove(value);
} else {
missingOptions.add(value);
}
deprecatedValues.remove(value);
}
if (!missingOptions.isEmpty()) {
fail("Missing event types " + missingOptions + " in event-metrics-user-events");
}
if (!expectedValues.isEmpty()) {
fail("Unknown event types " + expectedValues + " found in event-metrics-user-events");
}
if (!deprecatedValues.isEmpty()) {
fail("Unknown event types " + deprecatedValues + " found in event-metrics-user-events");
}