This topic describes how to enable LDAP authentication for EMR Kafka and provides a detailed example.
Prerequisites
You have created a Dataflow cluster and selected the Kafka and OpenLDAP services. For more information, see Create a Dataflow Kafka cluster.
Limits
-
This feature is available for EMR Kafka clusters that run EMR V3.44.0 or later, or EMR V5.10.0 or later.
-
An EMR OpenLDAP service or an external LDAP service is available in the cluster.
This topic uses the EMR OpenLDAP service as an example.
Precautions
If you want to use user group authentication, ensure that the memberOf overlay feature is enabled for the LDAP service or that the memberOf attribute is supported for LDAP users.
Enable LDAP authentication
-
Create a superuser.
NoteIf you have already created a superuser, you can skip this step.
A Kafka superuser can access all Kafka resources. The superuser handles communication between broker nodes and other components. This example uses the EMR OpenLDAP service to show how to add a Kafka superuser.
-
Connect to the master-1-1 node of the cluster over SSH. For more information, see Log on to a cluster.
-
Create a file named kafka.ldif with the following content.
This topic uses the
ldapcommand to add an LDAP user with the UIDkafkaand the passwordkafka-secret.dn: uid=kafka,ou=people,o=emr cn: kafka sn: kafka objectClass: inetOrgPerson userPassword: kafka-secret uid: kafka -
Run the following command to add the LDAP user.
ldapadd -H ldap://master-1-1:10389 -f kafka.ldif -D ${uid} -w ${rootDnPW}Note-
${uid}: The value of the admin_dn parameter on the Configure page of the OpenLDAP service in the EMR console. -
${rootDnPW}: The value of the admin_pwd parameter on the Configure page of the OpenLDAP service in the EMR console. -
10389: The listening port of the OpenLDAP service.
After you add the user, run the following command to view their information.
ldapsearch -w ${rootDnPW} -D ${uid} -H ldap://master-1-1:10389 -b uid=kafka,ou=people,o=emr -
-
-
Go to the Kafka configuration page.
-
Log on to the EMR on ECS console.
-
In the top navigation bar, select the required region and resource group.
-
In the Actions column of the target cluster, click Services.
-
On the Services page, click Configure in the Kafka service section.
-
-
Modify and save the configuration.
-
On the Configure page, click the server.properties tab.
-
Change the value of the kafka.ssl.config.type configuration item to CUSTOM.
-
Change the value of the authorizer.class.name configuration item to kafka.security.ldap.authorizer.SimpleLdapAuthorizer.
-
Save the configuration.
-
Click Save.
-
In the dialog box, enter a reason in the Execution Reason field.
-
-
-
Add and save custom parameters.
-
On the server.properties tab, click Add Configuration Item.
-
In the Add Configuration Item dialog box, add the following items.
Parameter
Value
Description
super.users
User:kafka
kafkais the username that you created. Replace it as needed.For more information, see Step 1.
listener.name.${listener}.sasl.enabled.mechanisms
PLAIN
Replace ${listener} with the name of a specific listener, such as
sasl_ssl.listener.name.${listener}.plain.sasl.jaas.config
org.apache.kafka.common.security.plain.PlainLoginModule required username="kafka" password="kafka-secret" emr.kafka.security.ldap.user.name.attribute="uid" emr.kafka.security.ldap.user.base.dn="ou=people,o=emr" emr.kafka.security.ldap.group.name.attribute="cn" emr.kafka.security.ldap.group.base.dn="ou=groups,o=emr" emr.kafka.security.ldap.admin.name.attribute="uid" emr.kafka.security.ldap.admin.base.dn="o=emr" emr.kafka.security.ldap.url="ldaps://master-1-1:10636" emr.kafka.security.ldap.bind.user="admin" emr.kafka.security.ldap.bind.user.password="WMMuhh3P**********" emr.kafka.security.ldap.user.member.of.attribute="memberOf" emr.kafka.security.ldap.group.authorization.support="true" ;Replace ${listener} with the name of a specific listener, such as
sasl_ssl.You must also replace the following LDAP-related configuration values:
-
username: The superuser that the Kafka service uses for communication between nodes. -
emr.kafka.security.ldap.user.name.attribute: The LDAP user name attribute that is used to obtain the username. -
emr.kafka.security.ldap.user.base.dn: The base distinguished name (DN) of the LDAP user. -
emr.kafka.security.ldap.group.name.attribute: The attribute for the LDAP user group name.
-
emr.kafka.security.ldap.group.base.dn: The base DN of the LDAP user group. -
emr.kafka.security.ldap.admin.name.attribute: The LDAP admin user attribute. -
emr.kafka.security.ldap.admin.base.dn: The base DN of the LDAP admin user. -
emr.kafka.security.ldap.url: The LDAP URL. -
emr.kafka.security.ldap.bind.user: The LDAP administrator username that is used for user group authentication. -
emr.kafka.security.ldap.bind.user.password: The password of the LDAP administrator. -
emr.kafka.security.ldap.user.member.of.attribute: The attribute that indicates the groups to which a user belongs. -
emr.kafka.security.ldap.group.authorization.support: Specifies whether to enable group authorization. If this parameter is set totrue, a user inherits permissions from their assigned user groups.
listener.name.${listener}.plain.sasl.server.callback.handler.class
kafka.security.ldap.authenticator.LdapAuthenticateCallbackHandler
Replace ${listener} with the name of a specific listener, such as
sasl_ssl.sasl.mechanism.inter.broker.protocol
PLAIN
None
sasl.enabled.mechanisms
PLAIN
None
-
-
Modify other parameters.
Modify the following configurations based on your requirements.
NoteYou must replace the username and password in the value of the kafka.client.jaas.content parameter as needed. We recommend that you use the superuser.
Configuration file
Parameter
Value
Description
kafka_client_jaas.conf
kafka.client.jaas.content
KafkaClient { org.apache.kafka.common.security.plain.PlainLoginModule required username="kafka" password="kafka-secret"; };The value must end with a semicolon (;).
schema-registry.properties
schema_registry_opts
-Djava.security.auth.login.config=/etc/taihao-apps/kafka-conf/kafka-conf/kafka_client_jaas.confIf this parameter already has a value, append the new content.
kafka-rest.properties
kafkarest_opts
-Djava.security.auth.login.config=/etc/taihao-apps/kafka-conf/kafka-conf/kafka_client_jaas.confIf this parameter already has a value, append the new content.
-
-
Restart the Kafka service.
-
On the Configure page of the Kafka service, choose .
-
In the dialog box that appears, enter a reason for the action and click OK.
-
In the Confirm dialog box, click OK.
-
Usage example
The operations in this example apply to open source Kafka 2.4.1. For other Kafka versions, see Apache Kafka.
-
Create test users and user groups.
-
If the memberOf overlay feature is not enabled for OpenLDAP, follow these steps to enable it.
Note-
The following configuration method is for reference only. The method to enable the memberOf overlay varies based on the LDAP service.
-
Apply this configuration to all OpenLDAP nodes.
-
Modify the parameters in all the following configuration files as needed. For example:
-
olcModulepath: For a 32-bit operating system, the path is /usr/lib/openldap. -
dn: cn=module{0},cn=config: If the /etc/openldap/slapd.d/cn=config directory already contains a cn=module{0}.ldif file, change the number aftermoduleto 1. If it contains a cn=module{1}.ldif file, change the number to 2, and so on. -
dn: olcOverlay={0}memberof,olcDatabase={2}hdb,cn=config: Configure this value based on the olcDatabase value in the /etc/openldap/slapd.d/cn=config directory. For example, ifolcDatabase={2}hdb.ldifexists, set the value tohdb.
-
-
Run the command
vim memberof_config.ldifto create a new configuration file memberof_config.ldif. The content of the file is as follows.dn: cn=module{0},cn=config cn: module{0} objectClass: olcModuleList objectclass: top olcModuleload: memberof.la olcModulePath: /usr/lib64/openldap dn: olcOverlay={0}memberof,olcDatabase={2}hdb,cn=config objectClass: olcConfig objectClass: olcMemberOf objectClass: olcOverlayConfig objectClass: top olcOverlay: memberof olcMemberOfDangling: ignore olcMemberOfRefInt: TRUE olcMemberOfGroupOC: groupOfNames olcMemberOfMemberAD: member olcMemberOfMemberOfAD: memberOf -
Run the command
vim refint1.ldifto create the configuration file refint1.ldif. The file content is as follows.dn: cn=module{0},cn=config add: olcmoduleload olcmoduleload: refint -
Run the command
vim refint2.ldifto create a new configuration file refint2.ldif with the following content.dn: olcOverlay=refint,olcDatabase={2}hdb,cn=config objectClass: olcConfig objectClass: olcOverlayConfig objectClass: olcRefintConfig objectClass: top olcOverlay: refint olcRefintAttribute: memberof member manager owner -
Run the following commands to load the configuration files.
ldapadd -Q -Y EXTERNAL -H ldapi:/// -f memberof_config.ldif ldapmodify -Q -Y EXTERNAL -H ldapi:/// -f refint1.ldif ldapadd -Q -Y EXTERNAL -H ldapi:/// -f refint2.ldif
-
-
Create test users.
Create a file named kafka-users.ldif with the following content, and then use the ldapadd command to add the test users.
dn: uid=kafka-user1,ou=people,o=emr cn: kafka-user1 sn: kafka-user1 uid: kafka-user1 objectClass: inetOrgPerson userPassword: kafka-secret dn: uid=kafka-user2,ou=people,o=emr cn: kafka-user2 sn: kafka-user2 uid: kafka-user2 objectClass: inetOrgPerson userPassword: kafka-secret -
Create test user groups.
Create a file named kafka-groups.ldif with the following content, and then use the ldapadd command to add the test user groups.
dn: cn=kafka-group1,ou=groups,o=emr cn: kafka-group1 objectClass: groupOfNames member: uid=kafka-user1,ou=people,o=emr member: uid=kafka-user2,ou=people,o=emr dn: cn=kafka-group2,ou=groups,o=emr cn: kafka-group2 objectClass: groupOfNames member: uid=kafka-user1,ou=people,o=emr -
On the server where LDAP is running, run the following command to check the
memberOfattribute of the test users.ldapsearch -Q -Y EXTERNAL -H ldapi:/// -b ou=people,o=emr memberOfThe output indicates that the
memberOfattribute ofkafka-user1includeskafka-group1andkafka-group2, and thememberOfattribute ofkafka-user2includeskafka-group1.
-
-
Create client configuration files.
-
Create a client configuration file named client.properties with the following content.
security.protocol=SASL_PLAINTEXT sasl.mechanism=PLAIN #Replace the username and password based on the client identity. sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="kafka" password="kafka-secret"; -
Create a client configuration file named kafka-user1.properties with the following content.
security.protocol=SASL_PLAINTEXT sasl.mechanism=PLAIN #Replace the username and password based on the client identity. sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="kafka-user1" password="kafka-secret"; -
Create a client configuration file named kafka-user2.properties with the following content.
security.protocol=SASL_PLAINTEXT sasl.mechanism=PLAIN #Replace the username and password based on the client identity. sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="kafka-user2" password="kafka-secret";
-
-
Create a test topic named test.
kafka-topics.sh --bootstrap-server core-1-1:9092 --command-config client.properties --create --topic test --replication-factor 3 --partitions 2 -
Grant permissions.
For more information about authorization commands, see Authorization and ACLs.
#Permissions for kafka-group2 kafka-acls.sh --authorizer-properties zookeeper.connect=$KAFKA_ZOOKEEPER --add --allow-principal User:kafka-group2 --allow-host "*" --operation All --cluster kafka-cluster kafka-acls.sh --authorizer-properties zookeeper.connect=$KAFKA_ZOOKEEPER --add --allow-principal User:kafka-group2 --allow-host "*" --operation All --topic test #Permissions for kafka-group1 kafka-acls.sh --authorizer-properties zookeeper.connect=$KAFKA_ZOOKEEPER --add --deny-principal User:kafka-group1 --deny-host "*" --operation All --topic test #Permissions for kafka-user1 kafka-acls.sh --authorizer-properties zookeeper.connect=$KAFKA_ZOOKEEPER --add --deny-principal User:kafka-user1 --deny-host "*" --operation Read --topic test #Permissions for kafka-user2 kafka-acls.sh --authorizer-properties zookeeper.connect=$KAFKA_ZOOKEEPER --add --allow-principal User:kafka-user2 --allow-host "*" --operation Read --topic testYou can run the following command to view the authorization results.
kafka-acls.sh --authorizer-properties zookeeper.connect=$KAFKA_ZOOKEEPER --list --topic testThe following information is returned:
Current ACLs for resource `Topic:LITERAL:test`: User:kafka-group2 has Allow permission for operations: All from hosts: * User:kafka-user2 has Allow permission for operations: Read from hosts: * User:kafka-user1 has Deny permission for operations: Read from hosts: * User:kafka-group1 has Deny permission for operations: All from hosts: * -
Verify permissions.
-
User
kafka-user1can write to thetesttopic throughkafka-group2.kafka-console-producer.sh --broker-list core-1-1:9092 --producer.config ./kafka-user1.properties --topic testEnter test messages at the prompt. The write operation succeeds.
#Enter test messages after the prompt. >a >b >c >d -
User
kafka-user1cannot read from thetesttopic.#Grant permission to the consumer group. kafka-acls.sh --authorizer-properties zookeeper.connect=$KAFKA_ZOOKEEPER --add --allow-principal User:kafka-user1 --allow-host "*" --operation All --group kka-user1-consumer kafka-console-consumer.sh --bootstrap-server core-1-1:9092 --consumer.config ./kafka-user1.properties --topic test --group kafka-user1-consumerAuthentication fails, and the user cannot access the
testtopic. -
User
kafka-user2cannot write to thetesttopic.kafka-console-producer.sh --broker-list core-1-1:9092 --producer.config ./kafka-user2.properties --topic test #Enter a test message after the prompt. >a #Authentication fails.Authentication fails.
-
User
kafka-user2can read from thetesttopic.#Grant permission to the consumer group. kafka-acls.sh --authorizer-properties zookeeper.connect=$KAFKA_ZOOKEEPER --add --allow-principal User:kafka-user2 --allow-host "*" --operation All --group kafka-user2-consumer kafka-console-consumer.sh --bootstrap-server core-1-1:9092 --consumer.config ./kafka-user2.properties --topic test --group kafka-user2-consumer --from-beginning #Data is consumed as expected.
-