Improve error message when wrong KC profile is set (#32898)

Closes #30454

Signed-off-by: Martin Bartoš <mabartos@redhat.com>
Co-authored-by: Steven Hawkins <shawkins@redhat.com>
This commit is contained in:
Martin Bartoš 2024-09-16 08:37:20 +01:00 committed by GitHub
parent e6c5ee31e4
commit 7625e3b4ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 11 deletions

View file

@ -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

View file

@ -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