Add realm to session context when exporting to prevent NPE when vault is enabled. (#27911)

Closes #22617

Signed-off-by: Stefan Guilhen <sguilhen@redhat.com>
This commit is contained in:
Stefan Guilhen 2024-03-15 08:24:22 -03:00 committed by GitHub
parent 9b967e3ee9
commit 0e717f735e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 0 deletions

View file

@ -71,6 +71,7 @@ public class SingleFileExportProvider implements ExportProvider {
@Override @Override
protected void runExportImportTask(KeycloakSession session) throws IOException { protected void runExportImportTask(KeycloakSession session) throws IOException {
Stream<RealmRepresentation> realms = session.realms().getRealmsStream() Stream<RealmRepresentation> realms = session.realms().getRealmsStream()
.peek(realm -> session.getContext().setRealm(realm))
.map(realm -> ExportUtils.exportRealm(session, realm, true, true)); .map(realm -> ExportUtils.exportRealm(session, realm, true, true));
writeToFile(realms); writeToFile(realms);
@ -88,6 +89,7 @@ public class SingleFileExportProvider implements ExportProvider {
protected void runExportImportTask(KeycloakSession session) throws IOException { protected void runExportImportTask(KeycloakSession session) throws IOException {
RealmModel realm = session.realms().getRealmByName(realmName); RealmModel realm = session.realms().getRealmByName(realmName);
Objects.requireNonNull(realm, "realm not found by realm name '" + realmName + "'"); Objects.requireNonNull(realm, "realm not found by realm name '" + realmName + "'");
session.getContext().setRealm(realm);
RealmRepresentation realmRep = ExportUtils.exportRealm(session, realm, true, true); RealmRepresentation realmRep = ExportUtils.exportRealm(session, realm, true, true);
writeToFile(realmRep); writeToFile(realmRep);
} }

View file

@ -95,6 +95,7 @@ public abstract class MultipleStepsExportProvider<T extends MultipleStepsExportP
@Override @Override
protected void runExportImportTask(KeycloakSession session) throws IOException { protected void runExportImportTask(KeycloakSession session) throws IOException {
RealmModel realm = session.realms().getRealmByName(realmName); RealmModel realm = session.realms().getRealmByName(realmName);
session.getContext().setRealm(realm);
RealmRepresentation rep = ExportUtils.exportRealm(session, realm, exportUsersIntoRealmFile, true); RealmRepresentation rep = ExportUtils.exportRealm(session, realm, exportUsersIntoRealmFile, true);
writeRealm(realmName + "-realm.json", rep); writeRealm(realmName + "-realm.json", rep);
logger.info("Realm '" + realmName + "' - data exported"); logger.info("Realm '" + realmName + "' - data exported");
@ -131,6 +132,7 @@ public abstract class MultipleStepsExportProvider<T extends MultipleStepsExportP
@Override @Override
protected void runExportImportTask(KeycloakSession session) throws IOException { protected void runExportImportTask(KeycloakSession session) throws IOException {
RealmModel realm = session.realms().getRealmByName(realmName); RealmModel realm = session.realms().getRealmByName(realmName);
session.getContext().setRealm(realm);
usersHolder.users = session.users() usersHolder.users = session.users()
.searchForUserStream(realm, Collections.emptyMap(), usersHolder.currentPageStart, usersHolder.currentPageEnd - usersHolder.currentPageStart) .searchForUserStream(realm, Collections.emptyMap(), usersHolder.currentPageStart, usersHolder.currentPageEnd - usersHolder.currentPageStart)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -164,6 +166,7 @@ public abstract class MultipleStepsExportProvider<T extends MultipleStepsExportP
@Override @Override
protected void runExportImportTask(KeycloakSession session) throws IOException { protected void runExportImportTask(KeycloakSession session) throws IOException {
RealmModel realm = session.realms().getRealmByName(realmName); RealmModel realm = session.realms().getRealmByName(realmName);
session.getContext().setRealm(realm);
federatedUsersHolder.users = UserStorageUtil.userFederatedStorage(session) federatedUsersHolder.users = UserStorageUtil.userFederatedStorage(session)
.getStoredUsersStream(realm, federatedUsersHolder.currentPageStart, federatedUsersHolder.currentPageEnd - federatedUsersHolder.currentPageStart) .getStoredUsersStream(realm, federatedUsersHolder.currentPageStart, federatedUsersHolder.currentPageEnd - federatedUsersHolder.currentPageStart)
.collect(Collectors.toList()); .collect(Collectors.toList());