From 7625e3b4eac49abe0b6807749be40fb7c899ecc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Barto=C5=A1?= Date: Mon, 16 Sep 2024 08:37:20 +0100 Subject: [PATCH] Improve error message when wrong KC profile is set (#32898) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #30454 Signed-off-by: Martin Bartoš Co-authored-by: Steven Hawkins --- .../PropertiesProfileConfigResolver.java | 10 ++++++- .../java/org/keycloak/common/ProfileTest.java | 26 ++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/org/keycloak/common/profile/PropertiesProfileConfigResolver.java b/common/src/main/java/org/keycloak/common/profile/PropertiesProfileConfigResolver.java index 751cff9a92..804582805a 100644 --- a/common/src/main/java/org/keycloak/common/profile/PropertiesProfileConfigResolver.java +++ b/common/src/main/java/org/keycloak/common/profile/PropertiesProfileConfigResolver.java @@ -21,7 +21,15 @@ public class PropertiesProfileConfigResolver implements ProfileConfigResolver { @Override public Profile.ProfileName getProfileName() { String profile = getter.apply("keycloak.profile"); - return profile != null ? Profile.ProfileName.valueOf(profile.toUpperCase()) : null; + + if (profile != null) { + try { + return Profile.ProfileName.valueOf(profile.toUpperCase()); + } catch (IllegalArgumentException e) { + throw new ProfileException(String.format("Invalid profile '%s' specified via 'keycloak.profile' property", profile)); + } + } + return null; } @Override diff --git a/common/src/test/java/org/keycloak/common/ProfileTest.java b/common/src/test/java/org/keycloak/common/ProfileTest.java index ec3d9007dc..afa64768ee 100644 --- a/common/src/test/java/org/keycloak/common/ProfileTest.java +++ b/common/src/test/java/org/keycloak/common/ProfileTest.java @@ -85,11 +85,9 @@ public class ProfileTest { Properties properties = new Properties(); properties.setProperty("keycloak.profile.feature.account_api", "disabled"); - try { - Profile.configure(new PropertiesProfileConfigResolver(properties)); - } catch (ProfileException e) { - Assert.assertEquals("Feature account3 depends on disabled feature account-api", e.getMessage()); - } + Assert.assertEquals("Feature account3 depends on disabled feature account-api", + assertThrows(ProfileException.class, + () -> Profile.configure(new PropertiesProfileConfigResolver(properties))).getMessage()); } @Test @@ -107,11 +105,19 @@ public class ProfileTest { Properties properties = new Properties(); properties.setProperty("keycloak.profile.feature.account_api", "invalid"); - try { - Profile.configure(new PropertiesProfileConfigResolver(properties)); - } catch (ProfileException e) { - Assert.assertEquals("Invalid config value 'invalid' for feature key keycloak.profile.feature.account_api", e.getMessage()); - } + Assert.assertEquals("Invalid config value 'invalid' for feature key keycloak.profile.feature.account_api", + assertThrows(ProfileException.class, + () -> Profile.configure(new PropertiesProfileConfigResolver(properties))).getMessage()); + } + + @Test + public void wrongProfileInProperties() { + Properties properties = new Properties(); + properties.setProperty("keycloak.profile", "experimental"); + + Assert.assertEquals("Invalid profile 'experimental' specified via 'keycloak.profile' property", + assertThrows(ProfileException.class, + () -> Profile.configure(new PropertiesProfileConfigResolver(properties))).getMessage()); } @Test