Search in sources :

Example 6 with EventStoreProvider

use of org.keycloak.events.EventStoreProvider in project keycloak by keycloak.

the class TestingResourceProvider method clearAdminEventStore.

@GET
@Path("/clear-admin-event-store-older-than")
@Produces(MediaType.APPLICATION_JSON)
public Response clearAdminEventStore(@QueryParam("realmId") String realmId, @QueryParam("olderThan") long olderThan) {
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    eventStore.clearAdmin(realmId, olderThan);
    return Response.noContent().build();
}
Also used : EventStoreProvider(org.keycloak.events.EventStoreProvider) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 7 with EventStoreProvider

use of org.keycloak.events.EventStoreProvider in project keycloak by keycloak.

the class AdminEventQueryTest method testQuery.

@Test
public void testQuery() {
    inRolledBackTransaction(null, (session, t) -> {
        EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
        RealmModel realm = session.realms().createRealm("realm");
        ClientConnection cc = new DummyClientConnection();
        eventStore.onEvent(new EventBuilder(realm, null, cc).event(EventType.LOGIN).user("u1").getEvent());
        eventStore.onEvent(new EventBuilder(realm, null, cc).event(EventType.LOGIN).user("u2").getEvent());
        eventStore.onEvent(new EventBuilder(realm, null, cc).event(EventType.LOGIN).user("u3").getEvent());
        eventStore.onEvent(new EventBuilder(realm, null, cc).event(EventType.LOGIN).user("u4").getEvent());
        assertThat(eventStore.createQuery().firstResult(2).getResultStream().collect(Collectors.counting()), is(2L));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) EventBuilder(org.keycloak.events.EventBuilder) ClientConnection(org.keycloak.common.ClientConnection) EventStoreProvider(org.keycloak.events.EventStoreProvider) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest) Test(org.junit.Test)

Example 8 with EventStoreProvider

use of org.keycloak.events.EventStoreProvider in project keycloak by keycloak.

the class RealmAdminResource method getEvents.

/**
 * Get admin events
 *
 * Returns all admin events, or filters events based on URL query parameters listed here
 *
 * @param operationTypes
 * @param authRealm
 * @param authClient
 * @param authUser user id
 * @param authIpAddress
 * @param resourcePath
 * @param dateTo
 * @param dateFrom
 * @param firstResult
 * @param maxResults Maximum results size (defaults to 100)
 * @return
 */
@Path("admin-events")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public Stream<AdminEventRepresentation> getEvents(@QueryParam("operationTypes") List<String> operationTypes, @QueryParam("authRealm") String authRealm, @QueryParam("authClient") String authClient, @QueryParam("authUser") String authUser, @QueryParam("authIpAddress") String authIpAddress, @QueryParam("resourcePath") String resourcePath, @QueryParam("dateFrom") String dateFrom, @QueryParam("dateTo") String dateTo, @QueryParam("first") Integer firstResult, @QueryParam("max") Integer maxResults, @QueryParam("resourceTypes") List<String> resourceTypes) {
    auth.realm().requireViewEvents();
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    AdminEventQuery query = eventStore.createAdminQuery().realm(realm.getId());
    ;
    if (authRealm != null) {
        query.authRealm(authRealm);
    }
    if (authClient != null) {
        query.authClient(authClient);
    }
    if (authUser != null) {
        query.authUser(authUser);
    }
    if (authIpAddress != null) {
        query.authIpAddress(authIpAddress);
    }
    if (resourcePath != null) {
        query.resourcePath(resourcePath);
    }
    if (operationTypes != null && !operationTypes.isEmpty()) {
        OperationType[] t = new OperationType[operationTypes.size()];
        for (int i = 0; i < t.length; i++) {
            t[i] = OperationType.valueOf(operationTypes.get(i));
        }
        query.operation(t);
    }
    if (resourceTypes != null && !resourceTypes.isEmpty()) {
        ResourceType[] t = new ResourceType[resourceTypes.size()];
        for (int i = 0; i < t.length; i++) {
            t[i] = ResourceType.valueOf(resourceTypes.get(i));
        }
        query.resourceType(t);
    }
    if (dateFrom != null) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date from = null;
        try {
            from = df.parse(dateFrom);
        } catch (ParseException e) {
            throw new BadRequestException("Invalid value for 'Date(From)', expected format is yyyy-MM-dd");
        }
        query.fromTime(from);
    }
    if (dateTo != null) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date to = null;
        try {
            to = df.parse(dateTo);
        } catch (ParseException e) {
            throw new BadRequestException("Invalid value for 'Date(To)', expected format is yyyy-MM-dd");
        }
        query.toTime(to);
    }
    if (firstResult != null) {
        query.firstResult(firstResult);
    }
    if (maxResults != null) {
        query.maxResults(maxResults);
    } else {
        query.maxResults(Constants.DEFAULT_MAX_RESULTS);
    }
    return query.getResultStream().map(ModelToRepresentation::toRepresentation);
}
Also used : AdminEventQuery(org.keycloak.events.admin.AdminEventQuery) BadRequestException(javax.ws.rs.BadRequestException) ResourceType(org.keycloak.events.admin.ResourceType) OperationType(org.keycloak.events.admin.OperationType) ParseException(java.text.ParseException) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) EventStoreProvider(org.keycloak.events.EventStoreProvider) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 9 with EventStoreProvider

use of org.keycloak.events.EventStoreProvider in project keycloak by keycloak.

the class RealmAdminResource method clearEvents.

/**
 * Delete all events
 */
@Path("events")
@DELETE
public void clearEvents() {
    auth.realm().requireManageEvents();
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    eventStore.clear(realm.getId());
}
Also used : EventStoreProvider(org.keycloak.events.EventStoreProvider) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 10 with EventStoreProvider

use of org.keycloak.events.EventStoreProvider in project keycloak by keycloak.

the class RealmAdminResource method clearAdminEvents.

/**
 * Delete all admin events
 */
@Path("admin-events")
@DELETE
public void clearAdminEvents() {
    auth.realm().requireManageEvents();
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    eventStore.clearAdmin(realm.getId());
}
Also used : EventStoreProvider(org.keycloak.events.EventStoreProvider) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Aggregations

EventStoreProvider (org.keycloak.events.EventStoreProvider)17 Path (javax.ws.rs.Path)14 GET (javax.ws.rs.GET)10 Produces (javax.ws.rs.Produces)9 NoCache (org.jboss.resteasy.annotations.cache.NoCache)4 Date (java.util.Date)3 AdminEventQuery (org.keycloak.events.admin.AdminEventQuery)3 Consumes (javax.ws.rs.Consumes)2 DELETE (javax.ws.rs.DELETE)2 Test (org.junit.Test)2 OperationType (org.keycloak.events.admin.OperationType)2 ModelToRepresentation (org.keycloak.models.utils.ModelToRepresentation)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1