Search code examples
javaldapkeycloakkeycloak-serviceskeycloak-rest-api

How to configure LDAP in Keycloak dynamically


I am using Keycloak as UAA for my project, and I am able to create a new realm in keycloak dynamically via java code. Now i would like to add LDAP support for the relam which is created in keycloak.Is there any option to do it dynamically using java ?

I able to drill down and identified the attributes that i need to set. basically i created ComponentRepresentation object with all ldap values and added into the realm,

ComponentRepresentation ldapComponentRep = new ComponentRepresentation();

String componentId = UUID.randomUUID().toString();

ldapComponentRep.setId(componentId);
ldapComponentRep.setName("testldap");
ldapComponentRep.setProviderId("testldap");
ldapComponentRep.setParentId("realmname");
ldapComponentRep.setProviderType("org.keycloak.storage.UserStorageProvider");

MultivaluedHashMap config = new MultivaluedHashMap<>();

config.putSingle("fullSyncPeriod", "-1");
config.putSingle("pagination", "true");
config.putSingle("connectionPooling", "true");
config.putSingle("usersDn", "o=758ldifojencdjks,dc=test,dc=com");
config.putSingle("cachePolicy", "DEFAULT");
config.putSingle("useKerberosForPasswordAuthentication","false");
config.putSingle("importEnabled","true");
config.putSingle("enabled","true");
config.putSingle("bindCredential","*********");
config.putSingle("usernameLDAPAttribute","uid");
config.putSingle("bindDn","uid=ldap.connector,ou=Users,o=758ldifojencdjks,dc=test,dc=com");
config.putSingle("changedSyncPeriod","-1");
config.putSingle("vendor","other");
config.putSingle("uuidLDAPAttribute","entryUUID");
config.putSingle("allowKerberosAuthentication","false");
config.putSingle("connectionUrl","ldap://ldap.test.com:389");
config.putSingle("syncRegistrations","false");
config.putSingle("authType","simple");
config.putSingle("debug","false");
config.putSingle("searchScope","2");
config.putSingle("useTruststoreSpi","ldapsOnly");
config.putSingle("priority","1");
config.putSingle("trustEmail","false");
config.putSingle("userObjectClasses","inetOrgPerson, organizationalPerson");
config.putSingle("rdnLDAPAttribute","uid");
config.putSingle("editMode","READ_ONLY");
config.putSingle("validatePasswordPolicy","false");
config.putSingle("batchSizeForSync","1000");

ldapComponentRep.setConfig(config);

keycloak.realms().realm("realmname").components().add(ldapComponentRep).getStatus();

The output of the last line 201 but there is no exception and the configuration are not saved either.

Please help me to resolve this issue.

Thanks in Advance,


Solution

  • Yes finally I am able to create LDAP config dynamically, there was a wrong input passed in the providerId

    ldapComponentRep.setName("testldap");   
    ldapComponentRep.setProviderId("testldap");
    

    it should be as below,

    ldapComponentRep.setProviderId("ldap");