Encode role name parameter in the location header uri

The role is encoded to avoid template resolution by the URIBuilder. This fix avoids the exception when creating roles with names containing {patterns}.

Closes #27514

Signed-off-by: graziang <g.graziano94@gmail.com>
This commit is contained in:
graziang 2024-03-05 12:29:38 +01:00 committed by Marek Posolda
parent 82af0b6af6
commit 39299eeb38
2 changed files with 8 additions and 1 deletions

View file

@ -24,6 +24,7 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.jboss.resteasy.reactive.NoCache;
import jakarta.ws.rs.NotFoundException;
import org.keycloak.common.util.Encode;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.models.ClientModel;
@ -202,7 +203,7 @@ public class RoleContainerResource extends RoleResource {
adminEvent.operation(OperationType.CREATE).resourcePath(uriInfo, role.getName()).representation(rep).success();
return Response.created(uriInfo.getAbsolutePathBuilder().path(role.getName()).build()).build();
return Response.created(uriInfo.getAbsolutePathBuilder().path(Encode.encodePathSegmentAsIs(role.getName())).build()).build();
} catch (ModelDuplicateException e) {
throw ErrorResponse.exists("Role with name " + rep.getName() + " already exists");
}

View file

@ -110,6 +110,12 @@ public class ClientRolesTest extends AbstractClientTest {
rolesRsc.create(role);
}
@Test
public void createRoleWithNamePattern() {
RoleRepresentation role = RoleBuilder.create().name("role-a-{pattern}").build();
rolesRsc.create(role);
}
@Test
public void testRemoveRole() {
RoleRepresentation role2 = makeRole("role2");