Cấu hình xác thực LDAP cho Impala trên CentOS 7

Thanhdc
Edumall Engineering
6 min readNov 21, 2019

Để thực hiện việc cấu hình xác thực LDAP cho Impala chúng ta chia thành 2 phần

Phần 1. Cài đặt và cấu hình LDAP (Impala yêu cầu LDAP cần hỗ trợ SSL/TLS), ở đây mình chọn TLS

Do bài này mình tập trung vào hướng dẫn cài đặt xác thực LDAP cho Impala nên mình sẽ không đi vào giải thích lại LDAP là gì, cấu trúc và mô hình của LDAP (Những thông tin này mọi người có thể hỏi bác Google) mà chỉ tập trung vào các bước cài đặt cụ thể

Phần 1.1. Cài đặt LDAP trên server

Bước 1. Cấu hình name resolution cho server. Ở đây server mình sử dụng là dwtl-gamma-prod.edumall.io với địa chỉ IP là 167.71.207.xxx, hệ điều hành là CentOS 7

Sửa file /etc/hosts thêm dòng sau

167.71.207.xxx dwtl-gamma-prod.edumall.io dwtl-gamma-prod

Bước 2. Cài đặt các gói phần mềm cho openldap (Các gói rpm)

yum -y install openldap compat-openldap openldap-clients openldap-servers openldap-servers-sql openldap-devel

Bước 3. Start gói dịch vụ LDAP và cấu hình cho dịch vụ khởi động cùng hệ điều hành (Ở đây tên gói là slapd)

systemctl start slapd
systemctl enable slapd

Bước 4. Kiểm tra lại việc cài LDAP có thành công không bằng câu lệnh

netstat -antup | grep -i 389

Nếu có kết quả trả về tương tự như sau nghĩa là đã cài đặt LDAP thành công

tcp 0 0 0.0.0.0:389 0.0.0.0:* LISTEN 25355/slapd
tcp6 0 0 :::389 :::* LISTEN 25355/slapd

Phần 1.2. Thực hiện cấu hình LDAP Server

Bước 1. Thiết lập LDAP Admin password

Bước 1.1. Tạo mật khẩu bằng câu lệnh như sau

slappasswd -h {SSHA} -s ldappassword

Output đầu ra sẽ có dạng như sau

{SSHA}JGaIK235Vhvu5X/r1a8H2k3vmI5To+J+

Bước 1.2. Di chuyển vào thư mục /etc/openldap/slapd.d/

cd /etc/openldap/slapd.d/

Bước 1.3. Tạo file db.ldif với nội dung như sau (chú ý phần in đậm bạn phải thay bằng giá trị password được băm tương ứng)

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=topica,dc=local

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=ldapadm,dc=topica,dc=local

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootPW
olcRootPW: {SSHA}JGaIK235Vhvu5X/r1a8H2k3vmI5To+J+

Bước 1.4. Thực hiện đẩy cấu hình file vừa tạo vào LDAP server bằng cách thực hiện lệnh

ldapmodify -Y EXTERNAL -H ldapi:/// -f db.ldif

Bước 2. Hạn chế quyền truy cập tài khoản ldpadm

Bước 2.1. Tạo file monitor.ldif với nội dung như sau

dn: olcDatabase={1}monitor,cn=config
changetype: modify
replace: olcAccess
olcAccess: {0}to * by dn.base=”gidNumber=0+uidNumber=0,cn=peercred,cn=external, cn=auth” read by dn.base=”cn=ldapadm,dc=topica,dc=local” read by * none

Bước 2.2. Gửi cấu hình tới LDAP server

ldapmodify -Y EXTERNAL -H ldapi:/// -f monitor.ldif

Bước 3. Cấu hình LDAP Database

Bước 3.1. Copy cấu hình database mẫu

cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

Bước 3.2. Phân quyền thư mục cho tài khoản ldap
chown ldap:ldap /var/lib/ldap/*

Bước 3.3. Thực hiện thêm schema

ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif

Bước 4. Tạo cấu trúc thư mục LDAP

Bước 4.1. Tạo file base.ldif với nội dung như sau

dn: dc=topica,dc=local
dc: topica
objectClass: top
objectClass: domain

dn: cn=ldapadm ,dc=topica,dc=local
objectClass: organizationalRole
cn: ldapadm
description: LDAP Manager

dn: ou=People,dc=topica,dc=local
objectClass: organizationalUnit
ou: People

dn: ou=Group,dc=topica,dc=local
objectClass: organizationalUnit
ou: Group

Bước 4.2. Build cấu trúc thư mục

ldapadd -x -W -D “cn=ldapadm,dc=topica,dc=local” -f base.ldif

Bước 5. Tạo user để xác thực trên LDAP

Bước 5.1. Tạo user và mật khẩu cho user (Ở đây tôi lấy ví dụ user là ldapdw)

useradd ldapdw

slappasswd -h {SSHA} -s ldapdw
{SSHA}KlEYqqNLmLsAXi2d7B0mBuQ8RoY/7jvI

Bước 5.2. Tạo file ldapdw.ldif với nội dung như sau

dn: uid=ldapdw,ou=People,dc=topica,dc=local
objectClass: top
objectClass: account
objectClass: posixAccount
objectClass: shadowAccount
cn: ldapdw
uid: ldapdw
uidNumber: 1001
gidNumber: 1001
homeDirectory: /home/ldapdw
loginShell: /bin/bash
gecos: ldapdw
userPassword: {SSHA}KlEYqqNLmLsAXi2d7B0mBuQ8RoY/7jvI
shadowLastChange: 0
shadowMin: 0
shadowMax: 0
shadowWarning: 0

Bước 5.3. Đẩy cấu hình vào LDAP Server

ldapadd -x -W -D “cn=ldapadm,dc=topica,dc=local” -f ldapdw.ldif

Chú ý: Có thể assign mật khẩu cho tài khoản bằng lệnh

ldappasswd -s ldapdw -W -D “cn=ldapadm,dc=topica,dc=local” -x “uid=ldapdw,ou=People,dc=topica,dc=local”

Bước 5.4. Verify tài khoản bằng cách chạy lệnh

ldapsearch -x cn=ldapdw -b dc=topica,dc=local

Phần 1.3. Tích hợp TLS cho LDAP

Bước 1. Cài đặt TLS

yum -y install openssl

Kiểm tra bằng cách liệt kê thư mục sau

ls -l /etc/pki/CA/

Bước 2. Tạo tệp index.txt và serial

cd /etc/pki/CA
echo 0001 > serial
touch index.txt

Bước 3. Tạo key cho CA, chú ý nhớ giá trị passphrase nhập vào để sử dụng sau này

openssl genrsa -aes256 -out /etc/pki/CA/private/ca.key.pem
openssl req -new -x509 -days 3650 -key /etc/pki/CA/private/ca.key.pem -extensions v3_ca -out /etc/pki/CA/certs/ca.cert.pem

Country Name (2 letter code) [XX]:VN
State or Province Name (full name) []:HANOI
Locality Name (eg, city) [Default City]:HANOI
Organization Name (eg, company) [Default Company Ltd]:Topica
Organizational Unit Name (eg, section) []:TST
Common Name (eg, your name or your server’s hostname) []:dwtl-gamma-prod.edumall.io
Email Address []:thanhdc4@topica.edu.vn

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:ldapdw
An optional company name []:Topica

Bước 4. Tạo private key

openssl genrsa -out private/dwtl-gamma-prod.edumall.io.key
openssl req -new -key private/dwtl-gamma-prod.edumall.io.key -out certs/dwtl-gamma-prod.edumall.io.csr

Chú ý: Tên file có chỗ in đậm trùng với hostname của server

Bước 5. Ký CA

openssl ca -keyfile private/ca.key.pem -cert certs/ca.cert.pem -in certs/dwtl-gamma-prod.edumall.io.csr -out certs/dwtl-gamma-prod.edumall.io.crt

Bước 6. Kiểm tra lại việc tạo có thành công bằng cách

Bước 6.1. Kiểm tra nội dung tệp index.txt có thông tin là OK

cat index.txt

Bước 6.2. Có thể chạy lệnh sau để verify

openssl verify -CAfile certs/ca.cert.pem certs/dwtl-gamma-prod.edumall.io.crt

Bước 7. Copy các thông tin khóa xác thực vào LDAP

cp -v certs/* /etc/openldap/certs/
cp -v private/dwtl-gamma-prod.edumall.io.key /etc/openldap/certs/

mkdir /etc/openldap/cacerts
cp -v certs/ca.cert.pem /etc/openldap/cacerts/

Bước 8. Enable TLS trên LDAP

Bước 8.1. Kiểm tra LDAP protocol

slapcat -b “cn=config” | egrep “olcTLSCertificateFile|olcTLSCertificateKeyFile”
olcTLSCertificateFile: “OpenLDAP Server”
olcTLSCertificateKeyFile: /etc/openldap/certs/password

Bước 8.2. Tạo file tls7.ldif có nội dung như sau

dn: cn=config
changetype: modify
replace: olcTLSCertificateFile
olcTLSCertificateFile: /etc/openldap/certs/dwtl-gamma-prod.edumall.io.crt
-
replace: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/openldap/certs/dwtl-gamma-prod.edumall.io.key

Bước 8.3. Thay đổi quyền sở hữu cho tài khoản ldap

chown -R ldap:ldap /etc/openldap/certs
chown -R ldap:ldap /etc/openldap/cacerts

Bước 8.4. Đẩy cấu hình vào LDAP server

ldapmodify -Y EXTERNAL -H ldapi:// -f tls7.ldif

Bước 8.5. Thêm thuộc tính mới olcTLSCACertificateFile bằng cách tạo file tls7_1.ldif với nội dung như sau

dn: cn=config
changetype: modify
add: olcTLSCACertificateFile
olcTLSCACertificateFile: /etc/openldap/cacerts/ca.cert.pem

Bước 8.6. Đẩy cấu hình vào LDAP server

ldapmodify -Y EXTERNAL -H ldapi:// -f tls7_1.ldif

Bước 8.7. Validate lại với slapcat

slapcat -b “cn=config” | egrep “olcTLSCertificateFile|olcTLSCertificateKeyFile|olcTLSCACertificateFile”
olcTLSCertificateFile: /etc/openldap/certs/ldap.example.com.crt
olcTLSCertificateKeyFile: /etc/openldap/certs/ldap.example.com.key
olcTLSCACertificateFile: /etc/openldap/cacerts/ca.cert.pem

Bước 8.8. Thực hiện sửa file cấu hình /etc/sysconfig/slapd thay đổi nội dung như sau

SLAPD_URLS=”ldapi:/// ldap:/// ldaps:///”

# Change the below in /etc/openldap/ldap.conf
#TLS_CACERTDIR /etc/openldap/certs
TLS_REQCERT never

Bước 8.9. Restart lại LDAP để kích hoạt TLS

systemctl restart slapd

Phần 2. Thực hiện cấu hình Impala để enable LDAP (Thực hiện trên giao diện quản trị của Clouder Manager)

Phần 2.1. Thực hiện cấu hình cho ImpalaD trên Cloudera Manager

Bước 1. Vào phần quản trị cấu hình của Impala

Bước 2. Tại ô search, thực hiện gõ ldap

Bước 3. Thay đổi các giá trị cấu hình như sau

  • Tick vào Enable LDAP Authentication và Enable LDAP TLS
  • Sửa giá trị textbox LDAP URL

ldap://dwtl-gamma-prod.edumall.io:389

Chú ý: Đây là địa chỉ cài đặt LDAP Server

  • Sửa giá trị textbox LDAP Pattern

uid=#UID,ou=People,dc=topica,dc=local

Chú ý: Nhập thông tin ou, dc tương ứng với bên LDAP Server đã cài đặt

  • Sửa giá trị textbox LDAP Server CA Certificate

/etc/openldap/cacerts/ca.cert.pem

Chú ý: Cần copy file ca.cert.pem trên LDAP Server về các server cài đặt impala daemon để impala-shell có thể xác thực được

Bước 4. Thực hiện restart cụm Impala

Phần 2.1. Thực hiện cấu hình cho Impala Client bao gồm impala-shell, hue và JDBC Client

- Với impala-shell chạy lệnh như sau

impala-shell -l -u ldapdw — auth_creds_ok_in_clear

- Với hue thực hiện sửa cấu hình trên Cloudera Manager như sau

Tìm thông tin cấu hình Hue Server Advanced Configuration Snippet (Safety Valve) for hue_safety_valve_server.ini sửa như sau

[impala]
auth_username=<LDAP username of Hue user to be authenticated>
auth_password=<LDAP password of Hue user to be authenticated>

- Với JDBC Client sửa connection string tương tự như sau

jdbc:impala://host:port/db;AuthMech=3;UID=ldap_user;PWD=ldap_password

--

--