Giter VIP home page Giter VIP logo

keycloak_jsonlog_eventlistener's Introduction

Java CI

Keycloak JSON Log Eventlistener

Primarily written for the Jboss Keycloak docker image, it will output Keycloak events as JSON into the keycloak server log.

Tested with Keycloak version 12.0.1 Compatible Java 11

Dockerfile implementation

COPY ./jsonlog-event-listener.jar /opt/jboss/keycloak/standalone/deployments/jsonlog-event-listener.jar

Configuration

Change the default log prefix

If you want to change the default log prefix (which is "JSON_EVENT::"), set the following environmental variable:

KEYCLOAK_JSONLOG_PREFIX

.. for example:

sudo docker run -it --rm -v "$(pwd)/jsonlog-event-listener.jar:/opt/jboss/keycloak/standalone/deployments/jsonlog-event-listener.jar" -e 'KEYCLOAK_USER=admin' -e 'KEYCLOAK_PASSWORD=admin' -e 'KEYCLOAK_JSONLOG_PREFIX=FOO' -p 8080:8080 jboss/keycloak:8.0.1

Once the event handler is enabled for the realm (in the admin console see Events -> Config), you should see something like this in the server log:

16:30:11,662 INFO  [org.keycloak.events] (default task-6) FOO{"type":"ADMIN_EVENT","operationType":"CREATE","realmId":"master","clientId":"01b98c9f-a2c3-418a-9c5e-a766cd51c4a7","userId":"6f87b3d2-108f-4234-8fd2-9fdccf700fce","ipAddress":"172.17.0.1","resourceType":"USER","resourcePath":"users/633ff1ce-a695-4c07-a083-1af595a839e8"}
16:30:18,063 INFO  [org.keycloak.events] (default task-8) FOO{"type":"ADMIN_EVENT","operationType":"ACTION","realmId":"master","clientId":"01b98c9f-a2c3-418a-9c5e-a766cd51c4a7","userId":"6f87b3d2-108f-4234-8fd2-9fdccf700fce","ipAddress":"172.17.0.1","resourceType":"USER","resourcePath":"users/633ff1ce-a695-4c07-a083-1af595a839e8/reset-password"}

Log user groups and attributes

If you want to log user's attributes and/or groups, set:

KEYCLOAK_JSONLOG_SHOW_GROUPS="true"
KEYCLOAK_JSONLOG_SHOW_ATTRIBUTES="true"

See above for activation with docker run, example output:

17:19:46,209 INFO  [org.keycloak.events] (default task-3) JSON_EVENT::{"type":"LOGIN","realmId":"myrealm","clientId":"test.client","userId":"10b2f010-6d87-4ece-aecd-61e7fe3bde3b","userGroups":["Group1"],"userAttributes":{"gender":["Other"],"grade":["example_grade"],"birth_year":["1998"]},"ipAddress":"192.168.3.3","auth_method":"openid-connect","auth_type":"code","redirect_uri":"https://test.app/oauth/callback","consent":"no_consent_required","code_id":"629888b4-49eb-4659-93da-32fc1ce73abd","username":"sam"}

Examples

15:58:08,150 INFO  [org.keycloak.events] (default task-7) JSON_EVENT::{"type":"LOGIN","realmId":"master","clientId":"security-admin-console","userId":"804807dd-c991-4712-8ec1-c6d4ff3f0e73","ipAddress":"172.17.0.1","auth_method":"openid-connect","auth_type":"code","redirect_uri":"http://localhost:8080/auth/admin/master/console/","consent":"no_consent_required","code_id":"d1a1cc64-4b31-43e6-a083-cc84d7a45cb3","username":"admin"}

15:58:08,312 INFO  [org.keycloak.events] (default task-7) JSON_EVENT::{"type":"CODE_TO_TOKEN","realmId":"master","clientId":"security-admin-console","userId":"804807dd-c991-4712-8ec1-c6d4ff3f0e73","ipAddress":"172.17.0.1","token_id":"733a2efb-4094-4dc1-90a4-194858ce08b0","grant_type":"authorization_code","refresh_token_type":"Refresh","scope":"openid profile email","refresh_token_id":"fe31ebc2-2621-434d-a1e9-300c03b66568","code_id":"d1a1cc64-4b31-43e6-a083-cc84d7a45cb3","client_auth_method":"client-secret"}

15:58:14,940 INFO  [org.keycloak.events] (default task-4) JSON_EVENT::{"type":"AdminEvent","operationType":"UPDATE","realmId":"master","clientId":"beec8c69-34b2-4d7e-870d-c84e008fab23","userId":"804807dd-c991-4712-8ec1-c6d4ff3f0e73","ipAddress":"172.17.0.1","resourceType":"REALM","resourcePath":"events/config"}

15:58:23,428 INFO  [org.keycloak.events] (default task-4) JSON_EVENT::{"type":"LOGOUT","realmId":"master","userId":"804807dd-c991-4712-8ec1-c6d4ff3f0e73","ipAddress":"172.17.0.1","redirect_uri":"http://localhost:8080/auth/admin/master/console/#/realms/test/events-settings"}

keycloak_jsonlog_eventlistener's People

Contributors

kill9zombie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

keycloak_jsonlog_eventlistener's Issues

Compatibility with Keycloak 20

Are there any plans for making the project compatible with Keycloak 20?

We are currently experiencing the following errors if we try using it with Keycloak 20.0.3:

Failed to send type to uk.me.neon.jsonlogeventlistenerprovider.provider.JSONLogEventListenerProvider

failing with a java.lang.NoSuchMethodError and a stacktrace:

{
  "class": "uk.me.neon.jsonlogeventlistenerprovider.provider.JSONLogEventListenerProvider",
  "method": "getUserModelById",
  "line": 63
},
{
  "class": "uk.me.neon.jsonlogeventlistenerprovider.provider.JSONLogEventListenerProvider",
  "method": "toJsonString",
  "line": 121
},
{
  "class": "uk.me.neon.jsonlogeventlistenerprovider.provider.JSONLogEventListenerProvider",
  "method": "onEvent",
  "line": 39
},
{
  "class": "org.keycloak.events.EventBuilder",
  "method": "send",
  "line": 225
},
{
  "class": "org.keycloak.events.EventBuilder",
  "method": "success",
  "line": 192
},
{
  "class": "org.keycloak.protocol.oidc.endpoints.TokenEndpoint",
  "method": "clientCredentialsGrant",
  "line": 789
},
{
  "class": "org.keycloak.protocol.oidc.endpoints.TokenEndpoint",
  "method": "processGrantRequest",
  "line": 210
},
{
  "class": "jdk.internal.reflect.NativeMethodAccessorImpl",
  "method": "invoke0"
},
{
  "class": "jdk.internal.reflect.NativeMethodAccessorImpl",
  "method": "invoke",
  "line": 62
},
{
  "class": "jdk.internal.reflect.DelegatingMethodAccessorImpl",
  "method": "invoke",
  "line": 43
},
{
  "class": "java.lang.reflect.Method",
  "method": "invoke",
  "line": 566
},
{
  "class": "org.jboss.resteasy.core.MethodInjectorImpl",
  "method": "invoke",
  "line": 170
},
{
  "class": "org.jboss.resteasy.core.MethodInjectorImpl",
  "method": "invoke",
  "line": 130
},
{
  "class": "org.jboss.resteasy.core.ResourceMethodInvoker",
  "method": "internalInvokeOnTarget",
  "line": 660
},
{
  "class": "org.jboss.resteasy.core.ResourceMethodInvoker",
  "method": "invokeOnTargetAfterFilter",
  "line": 524
},
{
  "class": "org.jboss.resteasy.core.ResourceMethodInvoker",
  "method": "lambda$invokeOnTarget$2",
  "line": 474
},
{
  "class": "org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext",
  "method": "filter",
  "line": 364
},
{
  "class": "org.jboss.resteasy.core.ResourceMethodInvoker",
  "method": "invokeOnTarget",
  "line": 476
},
{
  "class": "org.jboss.resteasy.core.ResourceMethodInvoker",
  "method": "invoke",
  "line": 434
},
{
  "class": "org.jboss.resteasy.core.ResourceLocatorInvoker",
  "method": "invokeOnTargetObject",
  "line": 192
},
{
  "class": "org.jboss.resteasy.core.ResourceLocatorInvoker",
  "method": "invoke",
  "line": 152
},
{
  "class": "org.jboss.resteasy.core.ResourceLocatorInvoker",
  "method": "invokeOnTargetObject",
  "line": 183
},
{
  "class": "org.jboss.resteasy.core.ResourceLocatorInvoker",
  "method": "invoke",
  "line": 141
},
{
  "class": "org.jboss.resteasy.core.ResourceLocatorInvoker",
  "method": "invoke",
  "line": 32
},
{
  "class": "org.jboss.resteasy.core.SynchronousDispatcher",
  "method": "invoke",
  "line": 492
},
{
  "class": "org.jboss.resteasy.core.SynchronousDispatcher",
  "method": "lambda$invoke$4",
  "line": 261
},
{
  "class": "org.jboss.resteasy.core.SynchronousDispatcher",
  "method": "lambda$preprocess$0",
  "line": 161
},
{
  "class": "org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext",
  "method": "filter",
  "line": 364
},
{
  "class": "org.jboss.resteasy.core.SynchronousDispatcher",
  "method": "preprocess",
  "line": 164
},
{
  "class": "org.jboss.resteasy.core.SynchronousDispatcher",
  "method": "invoke",
  "line": 247
},
{
  "class": "io.quarkus.resteasy.runtime.standalone.RequestDispatcher",
  "method": "service",
  "line": 73
},
{
  "class": "io.quarkus.resteasy.runtime.standalone.VertxRequestHandler",
  "method": "dispatch",
  "line": 151
},
{
  "class": "io.quarkus.resteasy.runtime.standalone.VertxRequestHandler",
  "method": "handle",
  "line": 82
},
{
  "class": "io.quarkus.resteasy.runtime.standalone.VertxRequestHandler",
  "method": "handle",
  "line": 42
},
{
  "class": "io.vertx.ext.web.impl.RouteState",
  "method": "handleContext",
  "line": 1284
},
{
  "class": "io.vertx.ext.web.impl.RoutingContextImplBase",
  "method": "iterateNext",
  "line": 173
},
{
  "class": "io.vertx.ext.web.impl.RoutingContextWrapper",
  "method": "next",
  "line": 200
},
{
  "class": "io.quarkus.vertx.http.runtime.StaticResourcesRecorder$2",
  "method": "handle",
  "line": 84
},
{
  "class": "io.quarkus.vertx.http.runtime.StaticResourcesRecorder$2",
  "method": "handle",
  "line": 71
},
{
  "class": "io.vertx.ext.web.impl.RouteState",
  "method": "handleContext",
  "line": 1284
},
{
  "class": "io.vertx.ext.web.impl.RoutingContextImplBase",
  "method": "iterateNext",
  "line": 173
},
{
  "class": "io.vertx.ext.web.impl.RoutingContextWrapper",
  "method": "next",
  "line": 200
},
{
  "class": "io.quarkus.vertx.http.runtime.VertxHttpRecorder$6",
  "method": "handle",
  "line": 430
},
{
  "class": "io.quarkus.vertx.http.runtime.VertxHttpRecorder$6",
  "method": "handle",
  "line": 408
},
{
  "class": "io.vertx.ext.web.impl.RouteState",
  "method": "handleContext",
  "line": 1284
},
{
  "class": "io.vertx.ext.web.impl.RoutingContextImplBase",
  "method": "iterateNext",
  "line": 173
},
{
  "class": "io.vertx.ext.web.impl.RoutingContextWrapper",
  "method": "next",
  "line": 200
},
{
  "class": "org.keycloak.quarkus.runtime.integration.web.QuarkusRequestFilter",
  "method": "lambda$createBlockingHandler$0",
  "line": 82
},
{
  "class": "io.quarkus.vertx.core.runtime.VertxCoreRecorder$14",
  "method": "runWith",
  "line": 564
},
{
  "class": "org.jboss.threads.EnhancedQueueExecutor$Task",
  "method": "run",
  "line": 2449
},
{
  "class": "org.jboss.threads.EnhancedQueueExecutor$ThreadBody",
  "method": "run",
  "line": 1478
},
{
  "class": "org.jboss.threads.DelegatingRunnable",
  "method": "run",
  "line": 29
},
{
  "class": "org.jboss.threads.ThreadLocalResettingRunnable",
  "method": "run",
  "line": 29
},
{
  "class": "io.netty.util.concurrent.FastThreadLocalRunnable",
  "method": "run",
  "line": 30
},
{
  "class": "java.lang.Thread",
  "method": "run",
  "line": 829
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.