Synchronize data between MongoDB and IDM
This sample uses the Groovy Connector Toolkit to implement a scripted connector that interacts with a MongoDB database. You can use the connector to provision MongoDB database users and roles from an IDM managed repository.
The Groovy Connector Toolkit is bundled with IDM in the JAR openidm/connectors/groovy-connector-1.5.20.9.jar
.
Sample overview
The Groovy scripts required for the sample are bundled within the MongoDB connector. If you want to customize these scripts, you can specify different scripts by adjusting the scriptRoots
property and script names in provisioner.openicf-mongodb.json
.
This sample lets you to synchronize from IDM Managed User to an external MongoDB database.
There is currently no way to synchronize passwords from an external MongoDB database to IDM. Because of this, it is recommended that IDM be used for user creation and password management. |
While not demonstrated in this sample, the MongoDB connector can also:
-
Synchronize from a dedicated store of IDM Managed MongoDB Roles to an external MongoDB database.
-
Synchronize from an external MongoDB database to a dedicated IDM store of Managed MongoDB Roles.
Configure the MongoDB database
This sample assumes a MongoDB database, running on the localhost system. Follow these steps to install and configure the MongoDB database:
-
Use the instructions for downloading and installing MongoDB in the MongoDB Manual. For the supported version of MongoDB, see MongoDB connector.
-
Set up MongoDB, based on the
configurationProperties
described in MongoDB connector. By default, MongoDB listens on localhost, port 27017. For the purpose of this sample, set up an administrative user ofmyUserAdmin
with a password ofPassw0rd
in theadmin
database. Then create a database in MongoDB namedhrdb
.The MongoDB administrative user must have the userAdminAnyDatabase
role, or attempts to update users will fail.If want to use an existing MongoDB instance that runs on a different host or port, or you want to change the database credentials, adjust the
configurationProperties
in the connector configuration file (samples/sync-with-mongodb/conf/provisioner.openicf-mongodb.json
) before you start the sample, as described in connector-reference:mongodb.adoc#configuring-mongodb-connector. -
Set up the MongoDB database, with which IDM will synchronize its managed user repository, by:
-
Enabling authentication, as described in the following MongoDB document: Enable Auth.
-
Setting up users and roles, as described in this MongoDB document: Manage Users and Roles.
-
Run the sample
In this section, you will start IDM with the sample configuration, test the connection to the MongoDB database, and populate the database with sample data.
The mapping configuration file (sync.json
) for this sample includes one mapping: managedUser_systemMongodbAccount
. You will use this mapping to synchronize users between the IDM repository and the MongoDB database:
-
Update
samples/sync-with-mongodb/conf/provisioner.openicf-mongodb.json
with the credentials and database information you created when configuring MongoDB. In our example,database
would be set tohrdb
, whileuser
would bemyUserAdmin
withuserDatabase
set toadmin
. -
Start IDM with the configuration for the MongoDB sample:
cd /path/to/openidm/ ./startup.sh -p samples/sync-with-mongodb
-
Create at least one assignment and role to assign roles to users. In this example, we are creating a role to assign read privileges to users. The role created is conditional, and only assigned to active users:
curl \ --header "X-OpenIDM-Username: openidm-admin" \ --header "X-OpenIDM-Password: openidm-admin" \ --header "Accept-API-Version: resource=1.0" \ --header "Content-type: application/json" \ --request POST \ --data '{ "name" : "MongoDB Read Access", "description": "Basic Read Access to HRDB", "mapping" : "managedUser_systemMongodbAccount", "attributes": [ { "name": "roles", "value": [ { "role": "read", "db": "hrdb" } ], "assignmentOperation" : "mergeWithTarget", "unassignmentOperation" : "removeFromTarget" } ] }' \ "http://localhost:8080/openidm/managed/assignment?_action=create" { "_id": "fb98f4a5-0f4d-4e22-9e17-79c45c11fe20", "_rev": "000000005c2da0eb", "name": "MongoDB Read Access", "description": "Basic Read Access to HRDB", "mapping": "managedUser_systemMongodbAccount", "attributes": [ { "name": "roles", "value": [ { "role": "read", "db": "hrdb" } ], "assignmentOperation": "mergeWithTarget", "unassignmentOperation": "removeFromTarget" } ] }
curl \ --header "Content-type: application/json" \ --header "X-OpenIDM-Username: openidm-admin" \ --header "X-OpenIDM-Password: openidm-admin" \ --header "Accept-API-Version: resource=1.0" \ --request POST \ --data '{ "name" : "MongoDB Read Access", "description": "Role for accounts with read access in MongoDB.", "condition": "/accountStatus eq \"active\"", "assignments": [ { "_ref": "managed/assignment/fb98f4a5-0f4d-4e22-9e17-79c45c11fe20", "_refResourceCollection": "managed/assignment", "_refResourceId": "fb98f4a5-0f4d-4e22-9e17-79c45c11fe20" } ] }' \ "http://localhost:8080/openidm/managed/role?_action=create" { "_id": "5f16238e-39e1-4f8c-8b16-27d39dc64dc3", "_rev": "0000000011e566a2", "name": "MongoDB Read Access", "description": "Role for accounts with read access in MongoDB.", "condition": "/accountStatus eq \"active\"" }
-
Create new users in IDM. Note that MongoDB requires user name, password, and roles properties to successfully create a user. In this example, the
read
role is assigned to new users automatically. -
Reconcile the managed user repository with the external MongoDB database.
-
To reconcile the repository using the admin UI:
-
Log in to the admin UI at the URL
https://localhost:8443/admin
as the default administrative user (openidm-admin
) with passwordopenidm-admin
. -
Select Configure > Mappings.
The Mappings page shows one mapping: From the IDM Managed User repository to the MongoDB database (
managedUser_systemMongodbAccount
). -
Select the
managedUser_systemMongodbAccount
mapping, and choose the Reconcile option.
-
-
To reconcile the repository by using the command-line, launch the reconciliation operation with the following command:
curl \ --header "X-OpenIDM-Username: openidm-admin" \ --header "X-OpenIDM-Password: openidm-admin" \ --header "Accept-API-Version: resource=1.0" \ --request POST \ "http://localhost:8080/openidm/recon?_action=recon&mapping=managedUser_systemMongodbAccount&waitForCompletion=true" { "_id": "e5bf074e-4da6-4ea7-8203-d4ec6f5a814a-24344", "state": "SUCCESS" }
The reconciliation operation creates MongoDB users from the users found in
managed/user
. -