Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void postInit(KeycloakSessionFactory factory) {

try {
if (clients.isEmpty()) {
policyStore.delete(removedClient.getRealm(), policy.getId());
policyStore.delete(policy.getId());
} else {
policy.putConfig("clients", JsonSerialization.writeValueAsString(clients));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ public void postInit(KeycloakSessionFactory factory) {
StoreFactory storeFactory = provider.getStoreFactory();
PolicyStore policyStore = storeFactory.getPolicyStore();
ClientScopeModel removedClientScope = ((ClientScopeRemovedEvent) event).getClientScope();
RealmModel realm = ((ClientScopeRemovedEvent) event).getClientScope().getRealm();

Map<Policy.FilterOption, String[]> filters = new HashMap<>();

filters.put(Policy.FilterOption.TYPE, new String[] { getId() });

policyStore.find(realm, null, filters, null, null).forEach(new Consumer<Policy>() {
policyStore.find(null, filters, null, null).forEach(new Consumer<Policy>() {

@Override
public void accept(Policy policy) {
Expand All @@ -94,7 +93,7 @@ public void accept(Policy policy) {
}

if (clientScopes.isEmpty()) {
policyStore.delete(realm, policy.getId());
policyStore.delete(policy.getId());
} else {
try {
policy.putConfig("clientScopes", JsonSerialization.writeValueAsString(clientScopes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public void onCreate(Policy policy, UmaPermissionRepresentation representation,
public void onUpdate(Policy policy, UmaPermissionRepresentation representation, AuthorizationProvider authorization) {
PolicyStore policyStore = authorization.getStoreFactory().getPolicyStore();
Set<Policy> associatedPolicies = policy.getAssociatedPolicies();
RealmModel realm = policy.getResourceServer().getRealm();

for (Policy associatedPolicy : associatedPolicies) {
AbstractPolicyRepresentation associatedRep = ModelToRepresentation.toRepresentation(associatedPolicy, authorization, false, false);
Expand All @@ -144,7 +143,7 @@ public void onUpdate(Policy policy, UmaPermissionRepresentation representation,
}

if (rep.getRoles().isEmpty()) {
policyStore.delete(realm, associatedPolicy.getId());
policyStore.delete(associatedPolicy.getId());
} else {
RepresentationToModel.toModel(rep, authorization, associatedPolicy);
}
Expand All @@ -155,7 +154,7 @@ public void onUpdate(Policy policy, UmaPermissionRepresentation representation,
rep.setType(representation.getCondition());
RepresentationToModel.toModel(rep, authorization, associatedPolicy);
} else {
policyStore.delete(realm, associatedPolicy.getId());
policyStore.delete(associatedPolicy.getId());
}
} else if ("group".equals(associatedRep.getType())) {
GroupPolicyRepresentation rep = GroupPolicyRepresentation.class.cast(associatedRep);
Expand All @@ -171,7 +170,7 @@ public void onUpdate(Policy policy, UmaPermissionRepresentation representation,
}

if (rep.getGroups().isEmpty()) {
policyStore.delete(realm, associatedPolicy.getId());
policyStore.delete(associatedPolicy.getId());
} else {
RepresentationToModel.toModel(rep, authorization, associatedPolicy);
}
Expand All @@ -189,7 +188,7 @@ public void onUpdate(Policy policy, UmaPermissionRepresentation representation,
}

if (rep.getClients().isEmpty()) {
policyStore.delete(realm, associatedPolicy.getId());
policyStore.delete(associatedPolicy.getId());
} else {
RepresentationToModel.toModel(rep, authorization, associatedPolicy);
}
Expand All @@ -207,7 +206,7 @@ public void onUpdate(Policy policy, UmaPermissionRepresentation representation,
}

if (rep.getUsers().isEmpty()) {
policyStore.delete(realm, associatedPolicy.getId());
policyStore.delete(associatedPolicy.getId());
} else {
RepresentationToModel.toModel(rep, authorization, associatedPolicy);
}
Expand Down Expand Up @@ -365,10 +364,9 @@ public Class<UmaPermissionRepresentation> getRepresentationType() {
@Override
public void onRemove(Policy policy, AuthorizationProvider authorization) {
PolicyStore policyStore = authorization.getStoreFactory().getPolicyStore();
RealmModel realm = policy.getResourceServer().getRealm();

for (Policy associatedPolicy : policy.getAssociatedPolicies()) {
policyStore.delete(realm, associatedPolicy.getId());
policyStore.delete(associatedPolicy.getId());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private void updateResourceServer(ClientModel clientModel, RoleModel removedRole

try {
if (roles.isEmpty()) {
policyStore.delete(clientModel.getRealm(), policy.getId());
policyStore.delete(policy.getId());
} else {
policy.putConfig("roles", JsonSerialization.writeValueAsString(roles));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ public class InfinispanCacheStoreFactoryProviderFactory implements CachedStorePr

protected volatile StoreFactoryCacheManager storeCache;

/**
* Legacy store doesn't store realm id for any entity and no method there is using new introduced RealmModel parameter.
* The parameter was introduced for usage only in the new storage. Therefore, in some cases we may break our rule specified in JavaDoc
* and use {@code null} value as parameter that otherwise cannot be {@code null}. We need to be careful and place such value only to a method call
* that cannot end up in the new store because it would end with {@link NullPointerException}. To mark all places where we do this,
* we use this variable so it is easily searchable.
*/
public static final RealmModel NULL_REALM = null;

@Override
public CachedStoreFactoryProvider create(KeycloakSession session) {
lazyInit(session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.keycloak.authorization.model.Resource;
import org.keycloak.authorization.model.ResourceServer;
import org.keycloak.authorization.model.Scope;
import org.keycloak.authorization.store.PermissionTicketStore;
import org.keycloak.models.cache.infinispan.authorization.entities.CachedPermissionTicket;

/**
Expand All @@ -43,8 +42,8 @@ public PermissionTicketAdapter(CachedPermissionTicket cached, StoreFactoryCacheS
@Override
public PermissionTicket getDelegateForUpdate() {
if (updated == null) {
ResourceServer resourceServer = cacheSession.getResourceServerStoreDelegate().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cached.getResourceServerId());
updated = cacheSession.getPermissionTicketStoreDelegate().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, resourceServer, cached.getId());
ResourceServer resourceServer = cacheSession.getResourceServerStoreDelegate().findById(cached.getResourceServerId());
updated = cacheSession.getPermissionTicketStoreDelegate().findById(resourceServer, cached.getId());
if (updated == null) throw new IllegalStateException("Not found in database");
cacheSession.registerPermissionTicketInvalidation(cached.getId(), cached.getOwner(), cached.getRequester(), cached.getResourceId(), updated.getResource().getName(), cached.getScopeId(), cached.getResourceServerId());
}
Expand All @@ -71,8 +70,8 @@ public long getCacheTimestamp() {
protected boolean isUpdated() {
if (updated != null) return true;
if (!invalidated) return false;
ResourceServer resourceServer = cacheSession.getResourceServerStoreDelegate().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cached.getResourceServerId());
updated = cacheSession.getPermissionTicketStoreDelegate().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, resourceServer, cached.getId());
ResourceServer resourceServer = cacheSession.getResourceServerStoreDelegate().findById(cached.getResourceServerId());
updated = cacheSession.getPermissionTicketStoreDelegate().findById(resourceServer, cached.getId());
if (updated == null) throw new IllegalStateException("Not found in database");
return true;
}
Expand Down Expand Up @@ -123,13 +122,13 @@ public void setGrantedTimestamp(Long millis) {

@Override
public ResourceServer getResourceServer() {
return cacheSession.getResourceServerStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cached.getResourceServerId());
return cacheSession.getResourceServerStore().findById(cached.getResourceServerId());
}

@Override
public Policy getPolicy() {
if (isUpdated()) return updated.getPolicy();
return cacheSession.getPolicyStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cacheSession.getResourceServerStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cached.getResourceServerId()), cached.getPolicy());
return cacheSession.getPolicyStore().findById(cacheSession.getResourceServerStore().findById(cached.getResourceServerId()), cached.getPolicy());
}

@Override
Expand All @@ -141,12 +140,12 @@ public void setPolicy(Policy policy) {

@Override
public Resource getResource() {
return cacheSession.getResourceStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, getResourceServer(), cached.getResourceId());
return cacheSession.getResourceStore().findById(getResourceServer(), cached.getResourceId());
}

@Override
public Scope getScope() {
return cacheSession.getScopeStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, getResourceServer(), cached.getScopeId());
return cacheSession.getScopeStore().findById(getResourceServer(), cached.getScopeId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.keycloak.authorization.model.Resource;
import org.keycloak.authorization.model.ResourceServer;
import org.keycloak.authorization.model.Scope;
import org.keycloak.authorization.store.PermissionTicketStore;
import org.keycloak.authorization.store.PolicyStore;
import org.keycloak.authorization.store.ResourceStore;
import org.keycloak.authorization.store.ScopeStore;
Expand Down Expand Up @@ -86,7 +85,7 @@ public long getCacheTimestamp() {
protected boolean isUpdated() {
if (updated != null) return true;
if (!invalidated) return false;
updated = cacheSession.getPolicyStoreDelegate().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cacheSession.getResourceServerStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cached.getResourceServerId()), cached.getId());
updated = cacheSession.getPolicyStoreDelegate().findById(cacheSession.getResourceServerStore().findById(cached.getResourceServerId()), cached.getId());
if (updated == null) throw new IllegalStateException("Not found in database");
return true;
}
Expand All @@ -113,7 +112,7 @@ public void setName(String name) {

@Override
public ResourceServer getResourceServer() {
return cacheSession.getResourceServerStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cached.getResourceServerId());
return cacheSession.getResourceServerStore().findById(cached.getResourceServerId());
}

@Override
Expand Down Expand Up @@ -209,7 +208,7 @@ public Set<Policy> getAssociatedPolicies() {
PolicyStore policyStore = cacheSession.getPolicyStore();
String resourceServerId = cached.getResourceServerId();
for (String id : cached.getAssociatedPoliciesIds(modelSupplier)) {
Policy policy = policyStore.findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cacheSession.getResourceServerStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, resourceServerId), id);
Policy policy = policyStore.findById(cacheSession.getResourceServerStore().findById(resourceServerId), id);
if (policy == null) {
// probably because the policy was removed
continue;
Expand All @@ -230,7 +229,7 @@ public Set<Resource> getResources() {
ResourceStore resourceStore = cacheSession.getResourceStore();
ResourceServer resourceServer = getResourceServer();
for (String resourceId : cached.getResourcesIds(modelSupplier)) {
Resource resource = resourceStore.findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, resourceServer, resourceId);
Resource resource = resourceStore.findById(resourceServer, resourceId);
cacheSession.cacheResource(resource);
resources.add(resource);
}
Expand Down Expand Up @@ -295,7 +294,7 @@ public Set<Scope> getScopes() {
ResourceServer resourceServer = getResourceServer();
ScopeStore scopeStore = cacheSession.getScopeStore();
for (String scopeId : cached.getScopesIds(modelSupplier)) {
Scope scope = scopeStore.findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, resourceServer, scopeId);
Scope scope = scopeStore.findById(resourceServer, scopeId);
cacheSession.cacheScope(scope);
scopes.add(scope);
}
Expand Down Expand Up @@ -330,6 +329,6 @@ public int hashCode() {
}

private Policy getPolicyModel() {
return cacheSession.getPolicyStoreDelegate().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cacheSession.getResourceServerStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cached.getResourceServerId()), cached.getId());
return cacheSession.getPolicyStoreDelegate().findById(cacheSession.getResourceServerStore().findById(cached.getResourceServerId()), cached.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public long getCacheTimestamp() {
protected boolean isUpdated() {
if (updated != null) return true;
if (!invalidated) return false;
updated = cacheSession.getResourceStoreDelegate().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, getResourceServer(), cached.getId());
updated = cacheSession.getResourceStoreDelegate().findById(getResourceServer(), cached.getId());
if (updated == null) throw new IllegalStateException("Not found in database");
return true;
}
Expand Down Expand Up @@ -134,7 +134,7 @@ public void setIconUri(String iconUri) {

@Override
public ResourceServer getResourceServer() {
return cacheSession.getResourceServerStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cached.getResourceServerId());
return cacheSession.getResourceServerStore().findById(cached.getResourceServerId());
}

@Override
Expand Down Expand Up @@ -172,7 +172,7 @@ public List<Scope> getScopes() {
if (scopes != null) return scopes;
scopes = new LinkedList<>();
for (String scopeId : cached.getScopesIds(modelSupplier)) {
scopes.add(cacheSession.getScopeStore().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, getResourceServer(), scopeId));
scopes.add(cacheSession.getScopeStore().findById(getResourceServer(), scopeId));
}
return scopes = Collections.unmodifiableList(scopes);
}
Expand Down Expand Up @@ -206,7 +206,7 @@ public void updateScopes(Set<Scope> scopes) {
List<PermissionTicket> permissions = permissionStore.findByScope(getResourceServer(), scope);

for (PermissionTicket permission : permissions) {
permissionStore.delete(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, permission.getId());
permissionStore.delete(permission.getId());
}
}
}
Expand Down Expand Up @@ -282,6 +282,6 @@ public int hashCode() {
}

private Resource getResourceModel() {
return cacheSession.getResourceStoreDelegate().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, getResourceServer(), cached.getId());
return cacheSession.getResourceStoreDelegate().findById(getResourceServer(), cached.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

import org.keycloak.authorization.model.CachedModel;
import org.keycloak.authorization.model.ResourceServer;
import org.keycloak.authorization.store.PermissionTicketStore;
import org.keycloak.models.ClientModel;
import org.keycloak.models.RealmModel;
import org.keycloak.models.cache.infinispan.authorization.entities.CachedResourceServer;
import org.keycloak.representations.idm.authorization.DecisionStrategy;
import org.keycloak.representations.idm.authorization.PolicyEnforcementMode;
Expand All @@ -32,21 +29,19 @@
public class ResourceServerAdapter implements ResourceServer, CachedModel<ResourceServer> {
protected CachedResourceServer cached;
protected StoreFactoryCacheSession cacheSession;
private RealmModel realm;
protected ResourceServer updated;

public ResourceServerAdapter(RealmModel realm, CachedResourceServer cached,
StoreFactoryCacheSession cacheSession) {
public ResourceServerAdapter(CachedResourceServer cached,
StoreFactoryCacheSession cacheSession) {
this.cached = cached;
this.cacheSession = cacheSession;
this.realm = realm;
}

@Override
public ResourceServer getDelegateForUpdate() {
if (updated == null) {
cacheSession.registerResourceServerInvalidation(cached.getId());
updated = cacheSession.getResourceServerStoreDelegate().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cached.getId());
updated = cacheSession.getResourceServerStoreDelegate().findById(cached.getId());
if (updated == null) throw new IllegalStateException("Not found in database");
}
return updated;
Expand All @@ -73,7 +68,7 @@ public long getCacheTimestamp() {
protected boolean isUpdated() {
if (updated != null) return true;
if (!invalidated) return false;
updated = cacheSession.getResourceServerStoreDelegate().findById(InfinispanCacheStoreFactoryProviderFactory.NULL_REALM, cached.getId());
updated = cacheSession.getResourceServerStoreDelegate().findById(cached.getId());
if (updated == null) throw new IllegalStateException("Not found in database");
return true;
}
Expand Down Expand Up @@ -127,11 +122,6 @@ public String getClientId() {
return getId();
}

@Override
public RealmModel getRealm() {
return realm;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Loading