[KEYCLOAK-4034] - Invalidating policy cache when creating resources and scopes
This commit is contained in:
parent
40591cff25
commit
c9c8acd029
8 changed files with 75 additions and 9 deletions
|
@ -77,8 +77,11 @@ public class CachedPolicyStore implements PolicyStore {
|
||||||
Policy policy = getDelegate().create(name, type, getStoreFactory().getResourceServerStore().findById(resourceServer.getId()));
|
Policy policy = getDelegate().create(name, type, getStoreFactory().getResourceServerStore().findById(resourceServer.getId()));
|
||||||
String id = policy.getId();
|
String id = policy.getId();
|
||||||
|
|
||||||
this.transaction.whenCommit(() -> {
|
this.transaction.whenRollback(() -> {
|
||||||
cache.remove(getCacheKeyForPolicy(id));
|
cache.remove(getCacheKeyForPolicy(id));
|
||||||
|
});
|
||||||
|
|
||||||
|
this.transaction.whenCommit(() -> {
|
||||||
invalidateCache(resourceServer.getId());
|
invalidateCache(resourceServer.getId());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -88,6 +91,9 @@ public class CachedPolicyStore implements PolicyStore {
|
||||||
@Override
|
@Override
|
||||||
public void delete(String id) {
|
public void delete(String id) {
|
||||||
Policy policy = findById(id, null);
|
Policy policy = findById(id, null);
|
||||||
|
if (policy == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
ResourceServer resourceServer = policy.getResourceServer();
|
ResourceServer resourceServer = policy.getResourceServer();
|
||||||
getDelegate().delete(id);
|
getDelegate().delete(id);
|
||||||
this.transaction.whenCommit(() -> {
|
this.transaction.whenCommit(() -> {
|
||||||
|
@ -385,6 +391,9 @@ public class CachedPolicyStore implements PolicyStore {
|
||||||
cache.remove(getCacheKeyForPolicy(getId()));
|
cache.remove(getCacheKeyForPolicy(getId()));
|
||||||
invalidateCache(cached.getResourceServerId());
|
invalidateCache(cached.getResourceServerId());
|
||||||
});
|
});
|
||||||
|
transaction.whenRollback(() -> {
|
||||||
|
cache.remove(getCacheKeyForPolicy(getId()));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.updated;
|
return this.updated;
|
||||||
|
|
|
@ -62,6 +62,7 @@ public class CachedResourceStore implements ResourceStore {
|
||||||
this.transaction = transaction;
|
this.transaction = transaction;
|
||||||
cacheKeys = new ArrayList<>();
|
cacheKeys = new ArrayList<>();
|
||||||
cacheKeys.add("findByOwner");
|
cacheKeys.add("findByOwner");
|
||||||
|
cacheKeys.add("findByUri");
|
||||||
this.storeFactory = storeFactory;
|
this.storeFactory = storeFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +72,11 @@ public class CachedResourceStore implements ResourceStore {
|
||||||
|
|
||||||
this.transaction.whenRollback(() -> {
|
this.transaction.whenRollback(() -> {
|
||||||
cache.remove(getCacheKeyForResource(resource.getId()));
|
cache.remove(getCacheKeyForResource(resource.getId()));
|
||||||
|
});
|
||||||
|
|
||||||
|
this.transaction.whenCommit(() -> {
|
||||||
invalidateCache(resourceServer.getId());
|
invalidateCache(resourceServer.getId());
|
||||||
|
getCachedStoreFactory().getPolicyStore().notifyChange(resource);
|
||||||
});
|
});
|
||||||
|
|
||||||
return createAdapter(new CachedResource(resource));
|
return createAdapter(new CachedResource(resource));
|
||||||
|
@ -80,6 +85,9 @@ public class CachedResourceStore implements ResourceStore {
|
||||||
@Override
|
@Override
|
||||||
public void delete(String id) {
|
public void delete(String id) {
|
||||||
Resource resource = findById(id, null);
|
Resource resource = findById(id, null);
|
||||||
|
if (resource == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
ResourceServer resourceServer = resource.getResourceServer();
|
ResourceServer resourceServer = resource.getResourceServer();
|
||||||
getDelegate().delete(id);
|
getDelegate().delete(id);
|
||||||
this.transaction.whenCommit(() -> {
|
this.transaction.whenCommit(() -> {
|
||||||
|
@ -91,6 +99,7 @@ public class CachedResourceStore implements ResourceStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
invalidateCache(resourceServer.getId());
|
invalidateCache(resourceServer.getId());
|
||||||
|
getCachedStoreFactory().getPolicyStore().notifyChange(resource);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,6 +126,11 @@ public class CachedResourceStore implements ResourceStore {
|
||||||
return cacheResult(new StringBuilder("findByOwner").append(resourceServerId).append(ownerId).toString(), () -> getDelegate().findByOwner(ownerId, resourceServerId));
|
return cacheResult(new StringBuilder("findByOwner").append(resourceServerId).append(ownerId).toString(), () -> getDelegate().findByOwner(ownerId, resourceServerId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Resource> findByUri(String uri, String resourceServerId) {
|
||||||
|
return cacheResult(new StringBuilder("findByUri").append(resourceServerId).append(uri).toString(), () -> getDelegate().findByUri(uri, resourceServerId));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Resource> findByResourceServer(String resourceServerId) {
|
public List<Resource> findByResourceServer(String resourceServerId) {
|
||||||
return getDelegate().findByResourceServer(resourceServerId);
|
return getDelegate().findByResourceServer(resourceServerId);
|
||||||
|
@ -268,10 +282,13 @@ public class CachedResourceStore implements ResourceStore {
|
||||||
if (this.updated == null) {
|
if (this.updated == null) {
|
||||||
this.updated = getDelegate().findById(getId(), cached.getResourceServerId());
|
this.updated = getDelegate().findById(getId(), cached.getResourceServerId());
|
||||||
if (this.updated == null) throw new IllegalStateException("Not found in database");
|
if (this.updated == null) throw new IllegalStateException("Not found in database");
|
||||||
transaction.whenRollback(() -> {
|
transaction.whenCommit(() -> {
|
||||||
cache.remove(getCacheKeyForResource(cached.getId()));
|
cache.remove(getCacheKeyForResource(cached.getId()));
|
||||||
invalidateCache(cached.getResourceServerId());
|
invalidateCache(cached.getResourceServerId());
|
||||||
getCachedStoreFactory().getPolicyStore().notifyChange(cached);
|
getCachedStoreFactory().getPolicyStore().notifyChange(updated);
|
||||||
|
});
|
||||||
|
transaction.whenRollback(() -> {
|
||||||
|
cache.remove(getCacheKeyForResource(cached.getId()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.infinispan.Cache;
|
import org.infinispan.Cache;
|
||||||
|
import org.keycloak.authorization.model.Resource;
|
||||||
import org.keycloak.authorization.model.ResourceServer;
|
import org.keycloak.authorization.model.ResourceServer;
|
||||||
import org.keycloak.authorization.model.Scope;
|
import org.keycloak.authorization.model.Scope;
|
||||||
import org.keycloak.authorization.store.ScopeStore;
|
import org.keycloak.authorization.store.ScopeStore;
|
||||||
|
@ -61,12 +62,19 @@ public class CachedScopeStore implements ScopeStore {
|
||||||
Scope scope = getDelegate().create(name, getStoreFactory().getResourceServerStore().findById(resourceServer.getId()));
|
Scope scope = getDelegate().create(name, getStoreFactory().getResourceServerStore().findById(resourceServer.getId()));
|
||||||
|
|
||||||
this.transaction.whenRollback(() -> cache.remove(getCacheKeyForScope(scope.getId())));
|
this.transaction.whenRollback(() -> cache.remove(getCacheKeyForScope(scope.getId())));
|
||||||
|
this.transaction.whenCommit(() -> {
|
||||||
|
getCachedStoreFactory().getPolicyStore().notifyChange(scope);
|
||||||
|
});
|
||||||
|
|
||||||
return createAdapter(new CachedScope(scope));
|
return createAdapter(new CachedScope(scope));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(String id) {
|
public void delete(String id) {
|
||||||
|
Scope scope = findById(id, null);
|
||||||
|
if (scope == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
getDelegate().delete(id);
|
getDelegate().delete(id);
|
||||||
this.transaction.whenCommit(() -> {
|
this.transaction.whenCommit(() -> {
|
||||||
List<CachedScope> scopes = cache.remove(getCacheKeyForScope(id));
|
List<CachedScope> scopes = cache.remove(getCacheKeyForScope(id));
|
||||||
|
@ -75,6 +83,8 @@ public class CachedScopeStore implements ScopeStore {
|
||||||
CachedScope entry = scopes.get(0);
|
CachedScope entry = scopes.get(0);
|
||||||
cache.remove(getCacheKeyForScopeName(entry.getName(), entry.getResourceServerId()));
|
cache.remove(getCacheKeyForScopeName(entry.getName(), entry.getResourceServerId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getCachedStoreFactory().getPolicyStore().notifyChange(scope);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,6 +200,9 @@ public class CachedScopeStore implements ScopeStore {
|
||||||
cache.remove(getCacheKeyForScope(getId()));
|
cache.remove(getCacheKeyForScope(getId()));
|
||||||
getCachedStoreFactory().getPolicyStore().notifyChange(updated);
|
getCachedStoreFactory().getPolicyStore().notifyChange(updated);
|
||||||
});
|
});
|
||||||
|
transaction.whenRollback(() -> {
|
||||||
|
cache.remove(getCacheKeyForScope(cached.getId()));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.updated;
|
return this.updated;
|
||||||
|
|
|
@ -103,6 +103,16 @@ public class JPAResourceStore implements ResourceStore {
|
||||||
return query.getResultList();
|
return query.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Resource> findByUri(String uri, String resourceServerId) {
|
||||||
|
Query query = entityManager.createQuery("from ResourceEntity where resourceServer.id = :serverId and uri = :uri");
|
||||||
|
|
||||||
|
query.setParameter("uri", uri);
|
||||||
|
query.setParameter("serverId", resourceServerId);
|
||||||
|
|
||||||
|
return query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List findByResourceServer(String resourceServerId) {
|
public List findByResourceServer(String resourceServerId) {
|
||||||
Query query = entityManager.createQuery("from ResourceEntity where resourceServer.id = :serverId");
|
Query query = entityManager.createQuery("from ResourceEntity where resourceServer.id = :serverId");
|
||||||
|
|
|
@ -75,7 +75,7 @@ public class JPAScopeStore implements ScopeStore {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (true) {
|
if (resourceServerId == null) {
|
||||||
return entityManager.find(ScopeEntity.class, id);
|
return entityManager.find(ScopeEntity.class, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,17 @@ public class MongoResourceStore implements ResourceStore {
|
||||||
.map(scope -> findById(scope.getId(), resourceServerId)).collect(toList());
|
.map(scope -> findById(scope.getId(), resourceServerId)).collect(toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Resource> findByUri(String uri, String resourceServerId) {
|
||||||
|
DBObject query = new QueryBuilder()
|
||||||
|
.and("resourceServerId").is(resourceServerId)
|
||||||
|
.and("uri").is(uri)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
return getMongoStore().loadEntities(ResourceEntity.class, query, getInvocationContext()).stream()
|
||||||
|
.map(scope -> findById(scope.getId(), resourceServerId)).collect(toList());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List findByResourceServer(String resourceServerId) {
|
public List findByResourceServer(String resourceServerId) {
|
||||||
DBObject query = new QueryBuilder()
|
DBObject query = new QueryBuilder()
|
||||||
|
|
|
@ -63,6 +63,14 @@ public interface ResourceStore {
|
||||||
*/
|
*/
|
||||||
List<Resource> findByOwner(String ownerId, String resourceServerId);
|
List<Resource> findByOwner(String ownerId, String resourceServerId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds all {@link Resource} instances with the given uri.
|
||||||
|
*
|
||||||
|
* @param ownerId the identifier of the owner
|
||||||
|
* @return a list with all resource instances owned by the given owner
|
||||||
|
*/
|
||||||
|
List<Resource> findByUri(String uri, String resourceServerId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds all {@link Resource} instances associated with a given resource server.
|
* Finds all {@link Resource} instances associated with a given resource server.
|
||||||
*
|
*
|
||||||
|
|
|
@ -132,19 +132,17 @@ public class ResourceService {
|
||||||
|
|
||||||
|
|
||||||
if ("name".equals(filterType)) {
|
if ("name".equals(filterType)) {
|
||||||
resources.addAll(storeFactory.getResourceStore().findByResourceServer(this.resourceServer.getId()).stream().filter(description -> filterValue == null || filterValue.equals(description.getName())).collect(Collectors.toSet()).stream()
|
resources.add(ModelToRepresentation.toRepresentation(storeFactory.getResourceStore().findByName(filterValue, this.resourceServer.getId()), resourceServer, authorization));
|
||||||
.map(resource -> ModelToRepresentation.toRepresentation(resource, this.resourceServer, authorization))
|
|
||||||
.collect(Collectors.toList()));
|
|
||||||
} else if ("type".equals(filterType)) {
|
} else if ("type".equals(filterType)) {
|
||||||
resources.addAll(storeFactory.getResourceStore().findByResourceServer(this.resourceServer.getId()).stream().filter(description -> filterValue == null || filterValue.equals(description.getType())).collect(Collectors.toSet()).stream()
|
resources.addAll(storeFactory.getResourceStore().findByResourceServer(this.resourceServer.getId()).stream().filter(description -> filterValue == null || filterValue.equals(description.getType())).collect(Collectors.toSet()).stream()
|
||||||
.map(resource -> ModelToRepresentation.toRepresentation(resource, this.resourceServer, authorization))
|
.map(resource -> ModelToRepresentation.toRepresentation(resource, this.resourceServer, authorization))
|
||||||
.collect(Collectors.toList()));
|
.collect(Collectors.toList()));
|
||||||
} else if ("uri".equals(filterType)) {
|
} else if ("uri".equals(filterType)) {
|
||||||
resources.addAll(storeFactory.getResourceStore().findByResourceServer(this.resourceServer.getId()).stream().filter(description -> filterValue == null || filterValue.equals(description.getUri())).collect(Collectors.toSet()).stream()
|
resources.addAll(storeFactory.getResourceStore().findByUri(filterValue, this.resourceServer.getId()).stream()
|
||||||
.map(resource -> ModelToRepresentation.toRepresentation(resource, this.resourceServer, authorization))
|
.map(resource -> ModelToRepresentation.toRepresentation(resource, this.resourceServer, authorization))
|
||||||
.collect(Collectors.toList()));
|
.collect(Collectors.toList()));
|
||||||
} else if ("owner".equals(filterType)) {
|
} else if ("owner".equals(filterType)) {
|
||||||
resources.addAll(storeFactory.getResourceStore().findByResourceServer(this.resourceServer.getId()).stream().filter(description -> filterValue == null || filterValue.equals(description.getOwner())).collect(Collectors.toSet()).stream()
|
resources.addAll(storeFactory.getResourceStore().findByOwner(filterValue, this.resourceServer.getId()).stream()
|
||||||
.map(resource -> ModelToRepresentation.toRepresentation(resource, this.resourceServer, authorization))
|
.map(resource -> ModelToRepresentation.toRepresentation(resource, this.resourceServer, authorization))
|
||||||
.collect(Collectors.toList()));
|
.collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue