Improve message for failing partial import of realm (#32667)
Closes #28017 Signed-off-by: Keshav Deshpande <keshavprashantdeshpande@gmail.com> Signed-off-by: Alexander Schwartz <aschwart@redhat.com> Co-authored-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
parent
866101e72e
commit
9f5f8e017e
7 changed files with 15 additions and 56 deletions
|
@ -21,9 +21,9 @@ import org.keycloak.models.KeycloakSession;
|
||||||
import org.keycloak.models.RealmModel;
|
import org.keycloak.models.RealmModel;
|
||||||
import org.keycloak.representations.idm.PartialImportRepresentation;
|
import org.keycloak.representations.idm.PartialImportRepresentation;
|
||||||
import org.keycloak.services.ErrorResponse;
|
import org.keycloak.services.ErrorResponse;
|
||||||
|
import org.keycloak.services.ErrorResponseException;
|
||||||
import org.keycloak.services.ServicesLogger;
|
import org.keycloak.services.ServicesLogger;
|
||||||
|
|
||||||
import jakarta.ws.rs.core.Response;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -50,7 +50,7 @@ public abstract class AbstractPartialImport<T> implements PartialImport<T> {
|
||||||
@Override
|
@Override
|
||||||
public void prepare(PartialImportRepresentation partialImportRep,
|
public void prepare(PartialImportRepresentation partialImportRep,
|
||||||
RealmModel realm,
|
RealmModel realm,
|
||||||
KeycloakSession session) throws ErrorResponseException {
|
KeycloakSession session) {
|
||||||
List<T> repList = getRepList(partialImportRep);
|
List<T> repList = getRepList(partialImportRep);
|
||||||
if ((repList == null) || repList.isEmpty()) return;
|
if ((repList == null) || repList.isEmpty()) return;
|
||||||
|
|
||||||
|
@ -89,7 +89,7 @@ public abstract class AbstractPartialImport<T> implements PartialImport<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PartialImportResults doImport(PartialImportRepresentation partialImportRep, RealmModel realm, KeycloakSession session) throws ErrorResponseException {
|
public PartialImportResults doImport(PartialImportRepresentation partialImportRep, RealmModel realm, KeycloakSession session) {
|
||||||
PartialImportResults results = new PartialImportResults();
|
PartialImportResults results = new PartialImportResults();
|
||||||
List<T> repList = getRepList(partialImportRep);
|
List<T> repList = getRepList(partialImportRep);
|
||||||
if ((repList == null) || repList.isEmpty()) return results;
|
if ((repList == null) || repList.isEmpty()) return results;
|
||||||
|
|
|
@ -26,6 +26,8 @@ import org.keycloak.representations.idm.RoleRepresentation;
|
||||||
import org.keycloak.services.ErrorResponse;
|
import org.keycloak.services.ErrorResponse;
|
||||||
|
|
||||||
import jakarta.ws.rs.core.Response;
|
import jakarta.ws.rs.core.Response;
|
||||||
|
import org.keycloak.services.ErrorResponseException;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -108,7 +110,7 @@ public class ClientRolesPartialImport {
|
||||||
client.removeRole(role);
|
client.removeRole(role);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void prepare(PartialImportRepresentation partialImportRep, RealmModel realm, KeycloakSession session) throws ErrorResponseException {
|
public void prepare(PartialImportRepresentation partialImportRep, RealmModel realm, KeycloakSession session) {
|
||||||
Map<String, List<RoleRepresentation>> repList = getRepList(partialImportRep);
|
Map<String, List<RoleRepresentation>> repList = getRepList(partialImportRep);
|
||||||
if (repList == null || repList.isEmpty()) return;
|
if (repList == null || repList.isEmpty()) return;
|
||||||
|
|
||||||
|
|
|
@ -1,38 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
|
||||||
* and other contributors as indicated by the @author tags.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.keycloak.partialimport;
|
|
||||||
|
|
||||||
import jakarta.ws.rs.core.Response;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An exception that can hold a Response object.
|
|
||||||
*
|
|
||||||
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
|
|
||||||
*/
|
|
||||||
public class ErrorResponseException extends RuntimeException {
|
|
||||||
private final Response response;
|
|
||||||
|
|
||||||
public ErrorResponseException(Response response) {
|
|
||||||
this.response = response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Response getResponse() {
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -35,12 +35,12 @@ public interface PartialImport<T> {
|
||||||
* @param rep Everything in the PartialImport request.
|
* @param rep Everything in the PartialImport request.
|
||||||
* @param realm Realm to be imported into.
|
* @param realm Realm to be imported into.
|
||||||
* @param session The KeycloakSession.
|
* @param session The KeycloakSession.
|
||||||
* @throws ErrorResponseException If the PartialImport can not be performed,
|
* @ If the PartialImport can not be performed,
|
||||||
* throw this exception.
|
* throw this exception.
|
||||||
*/
|
*/
|
||||||
void prepare(PartialImportRepresentation rep,
|
void prepare(PartialImportRepresentation rep,
|
||||||
RealmModel realm,
|
RealmModel realm,
|
||||||
KeycloakSession session) throws ErrorResponseException;
|
KeycloakSession session);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete resources that will be overwritten. This is done separately so
|
* Delete resources that will be overwritten. This is done separately so
|
||||||
|
@ -61,9 +61,9 @@ public interface PartialImport<T> {
|
||||||
* @param realm Realm to be imported into.
|
* @param realm Realm to be imported into.
|
||||||
* @param session The KeycloakSession.
|
* @param session The KeycloakSession.
|
||||||
* @return The final results of the PartialImport request.
|
* @return The final results of the PartialImport request.
|
||||||
* @throws ErrorResponseException if an error was detected trying to doImport a resource.
|
* @ if an error was detected trying to doImport a resource.
|
||||||
*/
|
*/
|
||||||
PartialImportResults doImport(PartialImportRepresentation rep,
|
PartialImportResults doImport(PartialImportRepresentation rep,
|
||||||
RealmModel realm,
|
RealmModel realm,
|
||||||
KeycloakSession session) throws ErrorResponseException;
|
KeycloakSession session);
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ public class PartialImportManager {
|
||||||
partialImports.add(new UsersPartialImport());
|
partialImports.add(new UsersPartialImport());
|
||||||
}
|
}
|
||||||
|
|
||||||
public PartialImportResults saveResources() throws ErrorResponseException {
|
public PartialImportResults saveResources() {
|
||||||
PartialImportResults results = new PartialImportResults();
|
PartialImportResults results = new PartialImportResults();
|
||||||
|
|
||||||
for (PartialImport partialImport : partialImports) {
|
for (PartialImport partialImport : partialImports) {
|
||||||
|
|
|
@ -59,12 +59,12 @@ public class RolesPartialImport implements PartialImport<RolesRepresentation> {
|
||||||
private RoleRepresentation newDefaultRole;
|
private RoleRepresentation newDefaultRole;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void prepare(PartialImportRepresentation rep, RealmModel realm, KeycloakSession session) throws ErrorResponseException {
|
public void prepare(PartialImportRepresentation rep, RealmModel realm, KeycloakSession session) {
|
||||||
prepareRealmRoles(rep, realm, session);
|
prepareRealmRoles(rep, realm, session);
|
||||||
prepareClientRoles(rep, realm, session);
|
prepareClientRoles(rep, realm, session);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareRealmRoles(PartialImportRepresentation rep, RealmModel realm, KeycloakSession session) throws ErrorResponseException {
|
private void prepareRealmRoles(PartialImportRepresentation rep, RealmModel realm, KeycloakSession session) {
|
||||||
if (!rep.hasRealmRoles()) return;
|
if (!rep.hasRealmRoles()) return;
|
||||||
|
|
||||||
realmRolesPI.prepare(rep, realm, session);
|
realmRolesPI.prepare(rep, realm, session);
|
||||||
|
@ -82,7 +82,7 @@ public class RolesPartialImport implements PartialImport<RolesRepresentation> {
|
||||||
this.realmRolesToSkip = realmRolesPI.getToSkip();
|
this.realmRolesToSkip = realmRolesPI.getToSkip();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareClientRoles(PartialImportRepresentation rep, RealmModel realm, KeycloakSession session) throws ErrorResponseException {
|
private void prepareClientRoles(PartialImportRepresentation rep, RealmModel realm, KeycloakSession session) {
|
||||||
if (!rep.hasClientRoles()) return;
|
if (!rep.hasClientRoles()) return;
|
||||||
|
|
||||||
clientRolesPI.prepare(rep, realm, session);
|
clientRolesPI.prepare(rep, realm, session);
|
||||||
|
@ -97,7 +97,7 @@ public class RolesPartialImport implements PartialImport<RolesRepresentation> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PartialImportResults doImport(PartialImportRepresentation rep, RealmModel realm, KeycloakSession session) throws ErrorResponseException {
|
public PartialImportResults doImport(PartialImportRepresentation rep, RealmModel realm, KeycloakSession session) {
|
||||||
PartialImportResults results = new PartialImportResults();
|
PartialImportResults results = new PartialImportResults();
|
||||||
if (!rep.hasRealmRoles() && !rep.hasClientRoles()) return results;
|
if (!rep.hasRealmRoles() && !rep.hasClientRoles()) return results;
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,6 @@ import org.keycloak.models.UserSessionModel;
|
||||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||||
import org.keycloak.models.utils.ModelToRepresentation;
|
import org.keycloak.models.utils.ModelToRepresentation;
|
||||||
import org.keycloak.models.utils.RepresentationToModel;
|
import org.keycloak.models.utils.RepresentationToModel;
|
||||||
import org.keycloak.partialimport.ErrorResponseException;
|
|
||||||
import org.keycloak.partialimport.PartialImportResult;
|
import org.keycloak.partialimport.PartialImportResult;
|
||||||
import org.keycloak.partialimport.PartialImportResults;
|
import org.keycloak.partialimport.PartialImportResults;
|
||||||
import org.keycloak.representations.adapters.action.GlobalRequestResult;
|
import org.keycloak.representations.adapters.action.GlobalRequestResult;
|
||||||
|
@ -1120,10 +1119,6 @@ public class RealmAdminResource {
|
||||||
).build();
|
).build();
|
||||||
} catch (ModelDuplicateException e) {
|
} catch (ModelDuplicateException e) {
|
||||||
throw ErrorResponse.exists(e.getLocalizedMessage());
|
throw ErrorResponse.exists(e.getLocalizedMessage());
|
||||||
} catch (ErrorResponseException error) {
|
|
||||||
return error.getResponse();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw ErrorResponse.error(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue