Ensure master realm can't be removed

Closes #28896

Signed-off-by: Stefan Guilhen <sguilhen@redhat.com>
This commit is contained in:
Stefan Guilhen 2024-05-10 16:04:38 -03:00 committed by Pedro Igor
parent 52c9e440d6
commit f0620353a4
2 changed files with 16 additions and 3 deletions

View file

@ -486,6 +486,10 @@ public class RealmAdminResource {
public void deleteRealm() { public void deleteRealm() {
auth.realm().requireManageRealm(); auth.realm().requireManageRealm();
if (Config.getAdminRealm().equals(realm.getName())) {
throw ErrorResponse.error("Can't rename master realm", Status.BAD_REQUEST);
}
if (!new RealmManager(session).removeRealm(realm)) { if (!new RealmManager(session).removeRealm(realm)) {
throw new NotFoundException("Realm doesn't exist"); throw new NotFoundException("Realm doesn't exist");
} }

View file

@ -524,12 +524,21 @@ public class RealmTest extends AbstractAdminTest {
private void reCreateRealm() { private void reCreateRealm() {
// Re-create realm // Re-create realm
RealmRepresentation realmRep = testContext.getTestRealmReps().stream().filter((RealmRepresentation realm) -> { RealmRepresentation realmRep = testContext.getTestRealmReps().stream()
return realm.getRealm().equals(REALM_NAME); .filter(realm -> realm.getRealm().equals(REALM_NAME)).findFirst().get();
}).findFirst().get();
importRealm(realmRep); importRealm(realmRep);
} }
@Test
public void removeMasterRealm() {
// any attempt to remove the master realm should fail.
try {
adminClient.realm("master").remove();
fail("It should not be possible to remove the master realm");
} catch(BadRequestException ignored) {
}
}
@Test @Test
public void loginAfterRemoveRealm() { public void loginAfterRemoveRealm() {
realm.remove(); realm.remove();