[KEYCLOAK-14570] Resolve nullpointer issue in controller

Some ProviderFactory returns null as properties instead of
Collections.emptyList() and it leads to NPE.

Fix it with using Optional.ofNullable(...).orElse(Collections.emptyList())
This commit is contained in:
kurisumakise2011 2020-06-24 09:18:02 +03:00 committed by Marek Posolda
parent 541bc5124f
commit 738f24aa38
2 changed files with 11 additions and 1 deletions

View file

@ -1143,7 +1143,7 @@ public class AuthenticationManagementResource {
rep.setName(factory.getDisplayType());
rep.setHelpText(factory.getHelpText());
rep.setProperties(new LinkedList<>());
List<ProviderConfigProperty> configProperties = factory.getConfigProperties();
List<ProviderConfigProperty> configProperties = Optional.ofNullable(factory.getConfigProperties()).orElse(Collections.emptyList());
for (ProviderConfigProperty prop : configProperties) {
ConfigPropertyRepresentation propRep = getConfigPropertyRep(prop);
rep.getProperties().add(propRep);

View file

@ -24,6 +24,7 @@ import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.representations.idm.AuthenticationExecutionInfoRepresentation;
import org.keycloak.representations.idm.AuthenticationFlowRepresentation;
import org.keycloak.representations.idm.AuthenticatorConfigInfoRepresentation;
import org.keycloak.representations.idm.AuthenticatorConfigRepresentation;
import org.keycloak.testsuite.Assert;
import org.keycloak.testsuite.admin.ApiUtil;
@ -167,6 +168,15 @@ public class AuthenticatorConfigTest extends AbstractAuthenticationTest {
Assert.assertNull(execution.getAuthenticationConfig());
}
@Test
public void testNullsafetyIterationOverProperties() {
String providerId = "auth-cookie";
String providerName = "Cookie";
AuthenticatorConfigInfoRepresentation description = authMgmtResource.getAuthenticatorConfigDescription(providerId);
Assert.assertEquals(providerName, description.getName());
Assert.assertTrue(description.getProperties().isEmpty());
}
private String createConfig(String executionId, AuthenticatorConfigRepresentation cfg) {
Response resp = authMgmtResource.newExecutionConfig(executionId, cfg);