Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
Software Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Accessing a Kafka Instance with SASL

Updated on 2023-05-06 GMT+08:00

If you enable SASL_SSL when creating an instance, data will be encrypted before transmission for enhanced security.

For security purposes, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, and TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 are supported.

This section describes how to use an open-source Kafka client to access a Kafka instance if SASL has been enabled for the instance.

NOTE:

Each Kafka broker allows a maximum of 1000 connections from each IP address by default. Excess connections will be rejected. You can change the limit by referring to Modifying Kafka Parameters.

Prerequisites

  • Security group rules have been correctly configured.

    To access a Kafka instance with SASL enabled, configure correct security group rules. For details about security group configuration requirements, see Table 2.

  • The instance connection address has been obtained.
    • For intra-VPC access, use port 9093. Obtain the instance connection address in the Connection section of the Basic Information tab page.
      Figure 1 Kafka instance connection addresses for intra-VPC access with SASL
    • For public access, use port 9095. Obtain the instance connection address in the Connection section of the Basic Information tab page.
      Figure 2 Kafka instance connection addresses for public access with SASL

  • The SASL mechanism in use is known.

    In the Connection area on the Kafka instance details page, view SASL Mechanism. If both SCRAM-SHA-512 and PLAIN are enabled, configure either of them for connections. If SASL Mechanism is not displayed, PLAIN is used by default.

    Figure 3 SASL mechanism in use
  • If automatic topic creation is not enabled for the Kafka instance, create a topic before connecting to the instance.
  • The client.truststore.jks certificate has been downloaded. Click the Kafka instance to go to the Basic Information tab page. Click Download next to SSL Certificate in the Connection area. Download and decompress the package to obtain the client certificate file client.truststore.jks.
  • Kafka CLI v1.1.0, v2.3.0, or v2.7.2 is available. Ensure that the Kafka instance and the CLI are of the same version.
  • An ECS has been created. For intra-VPC access, ensure that its VPC, subnet, and security group configurations are the same as those of the Kafka instance. JDK v1.8.111 or later has been installed on the ECS, and the JAVA_HOME and PATH environment variables have been configured as follows:

    Add the following lines to the .bash_profile file in the home directory as an authorized user: In this command, /opt/java/jdk1.8.0_151 is the JDK installation path. Change it to the path where you install JDK.

    export JAVA_HOME=/opt/java/jdk1.8.0_151 
    export PATH=$JAVA_HOME/bin:$PATH

    Run the source .bash_profile command for the modification to take effect.

Accessing the Instance Using CLI

The following uses Linux as an example.

  1. Map hosts to IP addresses in the /etc/hosts file on the host where the client is located, so that the client can quickly parse the instance brokers.

    Set IP addresses to the instance connection addresses obtained in Prerequisites. Set hosts to the names of instance hosts. Specify a unique name for each host.

    For example:

    10.154.48.120 server01

    10.154.48.121 server02

    10.154.48.122 server03

  2. Decompress the Kafka CLI package.

    Access the directory where the CLI package is stored and run the following command to decompress the package:

    tar -zxf [kafka_tar]

    In the preceding command, [kafka_tar] indicates the name of the CLI package.

    For example:

    tar -zxf kafka_2.12-2.7.2.tgz

  3. Modify the Kafka CLI configuration file based on the SASL mechanism.

    • If PLAIN is used, find the consumer.properties and producer.properties files in the /config directory of the Kafka CLI and add the following content to the files:
      sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
      username="**********" \
      password="**********";        
      sasl.mechanism=PLAIN
      
      security.protocol=SASL_SSL
      ssl.truststore.location={ssl_truststore_path}
      ssl.truststore.password=dms@kafka
      ssl.endpoint.identification.algorithm=

      Parameter description:

      • username and password: username and password you set when enabling SASL_SSL during Kafka instance creation or when creating a SASL_SSL user.
      • ssl.truststore.location: path for storing the client.truststore.jks certificate. Even in Windows, you need to use slashes (/) for the certificate path. Do not use backslashes (\), which are used by default for paths in Windows. Otherwise, the client will fail to obtain the certificate.
      • ssl.truststore.password: server certificate password, which must be set to dms@kafka and cannot be changed.
      • ssl.endpoint.identification.algorithm: whether to verify the certificate domain name. This parameter must be left blank, which indicates disabling domain name verification.
    • If SCRAM-SHA-512 is used, find the consumer.properties and producer.properties files in the /config directory of the Kafka CLI and add the following content to the files:
      sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \
      username="**********" \
      password="**********";        
      sasl.mechanism=SCRAM-SHA-512
      
      security.protocol=SASL_SSL
      ssl.truststore.location={ssl_truststore_path}
      ssl.truststore.password=dms@kafka
      ssl.endpoint.identification.algorithm=

      Parameter description:

      • username and password: username and password you set when enabling SASL_SSL during Kafka instance creation or when creating a SASL_SSL user.
      • ssl.truststore.location: path for storing the client.truststore.jks certificate. Even in Windows, you need to use slashes (/) for the certificate path. Do not use backslashes (\), which are used by default for paths in Windows. Otherwise, the client will fail to obtain the certificate.
      • ssl.truststore.password: server certificate password, which must be set to dms@kafka and cannot be changed.
      • ssl.endpoint.identification.algorithm: whether to verify the certificate domain name. This parameter must be left blank, which indicates disabling domain name verification.

  4. Access the /bin directory of the Kafka CLI.

    In Windows, you need to access the /bin/windows directory.

  5. Run the following command to create messages:

    ./kafka-console-producer.sh --broker-list ${connection-address} --topic ${topic-name} --producer.config ../config/producer.properties

    Parameter description:

    • {connection-address}: the address obtained in Prerequisites. For public access, use Instance Address (Public Network). For intra-VPC access, use Instance Address (Private Network).
    • {topic-name}: the name of the topic created for the Kafka instance If automatic topic creation has enabled for the Kafka instance, set this parameter to the name of a created topic or a topic that has not been created.

    The following example uses connection addresses 10.3.196.45:9095,10.78.42.127:9095,10.4.49.103:9095.

    After running the preceding command, you can send a message to the Kafka instance by writing it and pressing Enter. Each line of content is sent as a message.

    [root@ecs-kafka bin]#./kafka-console-producer.sh --broker-list 10.3.196.45:9095,10.78.42.127:9095,10.4.49.103:9095  --topic topic-demo --producer.config ../config/producer.properties
    >Hello
    >DMS
    >Kafka!
    >^C[root@ecs-kafka bin]# 

    To stop creating messages, press Ctrl+C to exit.

  6. Run the following command to retrieve messages:

    ./kafka-console-consumer.sh --bootstrap-server ${connection-address} --topic ${topic-name} --group ${consumer-group-name} --from-beginning  --consumer.config ../config/consumer.properties

    Parameter description:

    • {connection-address}: the address obtained in Prerequisites. For public access, use Instance Address (Public Network). For intra-VPC access, use Instance Address (Private Network).
    • {topic-name}: the name of the topic created for the Kafka instance
    • {consumer-group-name}: the consumer group name set based on your service requirements. If a consumer group name has been specified in the configuration file, ensure that you use the same name in the command line. Otherwise, consumption may fail. If a consumer group name starts with a special character, such as an underscore (_) or a number sign (#), the monitoring data cannot be displayed.

    Example:

    [root@ecs-kafka bin]#  ./kafka-console-consumer.sh --bootstrap-server 10.3.196.45:9095,10.78.42.127:9095,10.4.49.103:9095 --topic topic-demo --group order-test --from-beginning --consumer.config ../config/consumer.properties
    Hello
    DMS
    Kafka!
    ^CProcessed a total of 3 messages
    [root@ecs-kafka bin]# 

    To stop retrieving messages, press Ctrl+C to exit.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback