8000 Enforce the remote management check by pedroigor · Pull Request #48111 · keycloak/keycloak · GitHub
[go: up one dir, main page]

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 @@ -58,14 +58,13 @@ public ResourceService(KeycloakSession session, ResourceServer resourceServer, I
this.identity = identity;
this.resourceServer = resourceServer;
this.resourceManager = resourceManager;
checkResourceServerSettings();
}

@POST
@Consumes("application/json")
@Produces("application/json")
public Response create(UmaResourceRepresentation resource) {
checkResourceServerSettings();

if (resource == null) {
return Response.status(Status.BAD_REQUEST).build();
}
Expand Down Expand Up @@ -103,7 +102,6 @@ public Response update(@PathParam("id") String id, ResourceRepresentation resour
@Path("/{id}")
@DELETE
public Response delete(@PathParam("id") String id) {
checkResourceServerSettings();
return this.resourceManager.delete(id);
}

Expand All @@ -128,7 +126,6 @@ public Response find(@QueryParam("_id") String id,
@QueryParam("deep") Boolean deep,
@QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResult) {

if(deep != null && deep) {
return resourceManager.find(id, name, uri, owner, type, scope, matchingUri, exactName, deep, firstResult, maxResult);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.keycloak.testsuite.authz;

import jakarta.ws.rs.core.Response.Status;

import org.keycloak.admin.client.resource.AuthorizationResource;
import org.keycloak.authorization.client.resource.ProtectionResource;
import org.keycloak.authorization.client.util.HttpResponseException;
import org.keycloak.common.util.Throwables;
import org.keycloak.models.utils.KeycloakModelUtils;
import org.keycloak.representations.idm.authorization.ResourceRepresentation;
import org.keycloak.representations.idm.authorization.ResourceServerRepresentation;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class ProtectionResourceTest extends AbstractResourceServerTest {

private ProtectionResource protection;

@Before
public void onBefore() {
protection = getAuthzClient().protection("marta", "password");
}

@Test
public void testAllowRemoteManagement() throws Exception {
AuthorizationResource authorization = getClient(getRealm()).authorization();
ResourceRepresentation existing = addResource("test", "marta", true);
ResourceServerRepresentation settings = authorization.getSettings();
settings.setAllowRemoteResourceManagement(false);
authorization.update(settings);
getCleanup().addCleanup(() -> {
settings.setAllowRemoteResourceManagement(true);
authorization.update(settings);
});

assertBadRequest(() -> {
ResourceRepresentation resource = new ResourceRepresentation();
resource.setName(KeycloakModelUtils.generateId());
protection.resource().create(resource);
});
assertBadRequest(() -> protection.resource().update(existing));
assertBadRequest(() -> protection.resource().delete(existing.getId()));
assertBadRequest(() -> protection.resource().findAll());
}

private void assertBadRequest(Runnable runnable) {
try {
runnable.run();
fail("Server should fail the request");
} catch (Exception e) {
Throwable error = e.getCause();
assertTrue(Throwables.isCausedBy(e, HttpResponseException.class));
if (!(error instanceof HttpResponseException)) {
error = error.getCause();
}
assertEquals(Status.BAD_REQUEST.getStatusCode(), ((HttpResponseException) error).getStatusCode());
assertTrue(new String(((HttpResponseException) error).getBytes()).contains("Remote management is disabled"));
}
}
}
Loading
0