+
Skip to content

added a non-null check to check whether the to be exported realm exists #40655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

boehmalex
Copy link
Contributor

In the SingleFileExportProvider.exportRealm(...) (used when exporting to a single file) we are checking if the realm exists. In case it does not, we throw a proper error message:

2025-06-19 21:41:40,483 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) Error details:: java.lang.NullPointerException: realm not found by realm name 'non-existent-realm'
at java.base/java.util.Objects.requireNonNull(Objects.java:235)
at org.keycloak.exportimport.singlefile.SingleFileExportProvider.exportRealm(SingleFileExportProvider.java:88)

I copied this non-null check to the MultipleStepsExportProvider.runExportImportTask(...) (used when exporting multiple files to a directory) to have the same check and proper error message if the realm does not exist.

Closes #39122

…leFileExportProvider to have a proper error message in case the realm does not exist

Closes keycloak#39122

Signed-off-by: Alexander Böhm <boehm.alexanderb@gmail.com>
@boehmalex boehmalex requested a review from a team as a code owner June 22, 2025 16:07
@shawkins
Copy link
Contributor

@boehmalex thank you for the fix. Can you see if it's easy to add a test for this case in ExportImportTest?

@boehmalex
Copy link
Contributor Author

@shawkins sorry for the late response. I tried to add a test when creating the fix, but with the usage of testRealmExportImport i couldn't manage to check for the expected exception, because the exception is converted to a standard UncaughtServerError in KeycloakErrorHandler.getResponse(...). And in this exception there is no cause/reason or something else left to get to the root cause.

But i did a little more testing and this could be a test (its actually working):

public void checkForRealmNotFoundExceptionWhenDirExportingNonExistentRealm() throws IOException {
        String realmName = "non-existent-realm";
        String targetDirPath = testingClient.testing().exportImport().getExportImportTestDirectory() + File.separator + "dirRealmExport";
        testingClient.testing().exportImport().setDir(targetDirPath);
        testingClient.testing().exportImport().setRealmName(realmName);
        Config.Scope config = new Config.SystemPropertiesScope("");

        DirExportProviderFactory dirExportProviderFactory = new DirExportProviderFactory();
        dirExportProviderFactory.init(config);

        KeycloakSessionFactory factory = KeycloakApplication.getSessionFactory();
        KeycloakSession keycloakSession = factory.create();
        ExportProvider exportProvider = dirExportProviderFactory.create(keycloakSession);

        try {
            exportProvider.exportModel();
        } catch (NullPointerException e) {
            assertEquals(String.format("realm not found by realm name '%s'", realmName), e.getMessage());
        }
    }

But i don't know if this is considered as an OK test, because its a little puzzled together and not using the testingClient.testing().exportImport().runExport();

The same test base could be used to test the Exception in SingleFileExportProvider.

@shawkins
Copy link
Contributor

shawkins commented Jul 2, 2025

Thank you for looking more into this @boehmalex

And in this exception there is no cause/reason or something else left to get to the root cause.

Right, you'd have to inspect the server log instead.

But i don't know if this is considered as an OK test, because its a little puzzled together and not using the testingClient.testing().exportImport().runExport();

I'm fine with this more direct style of test. It would be clearer though to just by-pass using testingClient.testing().exportImport() methods and instead use ExportImportConfig methods.

@boehmalex
Copy link
Contributor Author

@shawkins Thanks for your input - valid point to use ExportImportConfig directly! I will update this PR and provide the tests at the end of next week at the earliest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Export fails with an unexpected error if the realm does not exist
2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载