Cloudera Manager配置LDAP

本文主要记录 cdh hadoop 集群集成 ldap 的过程,这里 ldap 安装的是 OpenLDAP 。LDAP 用来做账号管理,Kerberos作为认证。授权一般由Sentry来决定的。

集群包括7个节点,每个节点的ip、主机名和部署的组件分配如下:

192.168.0.200        master     Kerberos KDC 、OpenLDAP
192.168.0.201        slave1     kerberos client、ldap client
192.168.0.202        slave2     kerberos client、ldap client
192.168.0.203        slave3     kerberos client、ldap client
192.168.0.204        slave4     kerberos client、ldap client
192.168.0.205        slave5     kerberos client、ldap client
192.168.0.206        slave6     kerberos client、ldap client
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注意:hostname 请使用小写,要不然在集成 kerberos 时会出现一些错误。

环境说明:

  • 操作系统:Centos7.3 64位
  • OpenLDAP 版本:2.4.40
  • Kerberos 版本:1.14.1
  • CDH 版本:cdh-5.11.1

1.安装服务端

1.1 安装

同安装 kerberos 一样,这里使用 master 作为服务端安装 openldap。

$ yum install db4 db4-utils db4-devel cyrus-sasl* krb5-server-ldap -y
$ yum install openldap openldap-servers openldap-clients openldap-devel compat-openldap -y
  • 1
  • 2

查看安装的版本:

$ rpm -qa openldap
openldap-2.4.40-13.el7.x86_64

$ rpm -qa krb5-server-ldap
krb5-server-ldap-1.14.1-27.el7_3.x86_64
  • 1
  • 2
  • 3
  • 4
  • 5

1.2 OpenSSL

如果,你不配置ssl,这部分内容可以略过,实际安装过程中,我也没有详细去操作这部分内容。

OpenLDAP 默认使用 Mozilla NSS,安装后已经生成了一份证书,可使用 certutil -d /etc/openldap/certs/ -L -n 'OpenLDAP Server' 命令查看。使用如下命令生成RFC格式CA证书并分发到客户机待用。

$ certutil -d /etc/openldap/certs/ -L -a -n 'OpenLDAP Server' -f /etc/openldap/certs/password > /etc/openldap/ldapCA.rfc

# 拷贝到其他节点
$ scp /etc/openldap/ldapCA.rfc cdh2:/tmp
$ scp /etc/openldap/ldapCA.rfc cdh3:/tmp
  • 1
  • 2
  • 3
  • 4
  • 5

附,生成自签名证书的命令供参考:

$ certutil -d /etc/openldap/certs -S -n 'test cert' -x -t 'u,u,u' -s 'C=XX, ST=Default Province, L=Default City, O=Default Company Ltd, OU=Default Unit, CN=cdh1' -k rsa -v 120 -f /etc/openldap/certs/password
  • 1
  • 2

修改 /etc/sysconfig/ldap,开启 ldaps:

# Run slapd with -h "... ldaps:/// ..."
#   yes/no, default: no
SLAPD_LDAPS=yes
  • 1
  • 2
  • 3

1.3 LDAP 服务端配置

更新配置库:

rm -rf /var/lib/ldap/*
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
chown -R ldap.ldap /var/lib/ldap
  • 1
  • 2
  • 3

在2.4以前的版本中,OpenLDAP 使用 slapd.conf 配置文件来进行服务器的配置,而2.4开始则使用 slapd.d 目录保存细分后的各种配置,这一点需要注意,其数据存储位置即目录 /etc/openldap/slapd.d 。尽管该系统的数据文件是透明格式的,还是建议使用 ldapadd, ldapdelete, ldapmodify 等命令来修改而不是直接编辑。

默认配置文件保存在 /etc/openldap/slapd.d,将其备份:

cp -rf /etc/openldap/slapd.d /etc/openldap/slapd.d.bak
  • 1

添加一些基本配置,并引入 kerberos 和 openldap 的 schema:

$ cp /usr/share/doc/krb5-server-ldap-1.14.1/kerberos.schema /etc/openldap/schema/

$ touch /etc/openldap/slapd.conf

$ echo "include /etc/openldap/schema/corba.schema
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/cosine.schema
include /etc/openldap/schema/duaconf.schema
include /etc/openldap/schema/dyngroup.schema
include /etc/openldap/schema/inetorgperson.schema
include /etc/openldap/schema/java.schema
include /etc/openldap/schema/misc.schema
include /etc/openldap/schema/nis.schema
include /etc/openldap/schema/openldap.schema
include /etc/openldap/schema/ppolicy.schema
include /etc/openldap/schema/collective.schema
include /etc/openldap/schema/kerberos.schema" > /etc/openldap/slapd.conf
$ echo -e "pidfile /var/run/openldap/slapd.pid\nargsfile /var/run/openldap/slapd.args" >> /etc/openldap/slapd.conf

#更新slapd.d
$ slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d

$ chown -R ldap:ldap /etc/openldap/slapd.d && chmod -R 700 /etc/openldap/slapd.d
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

1.4 启动服务

启动 LDAP 服务:

# 启动服务命令
systemctl start slapd
# 加入开机启动项
systemctl enable slapd
  • 1
  • 2
  • 3
  • 4

查看状态,验证服务端口:

$ ps aux | grep slapd | grep -v grep
ldap       1290  0.0  0.0 714732  6312 ?        Ssl  20:53   0:00 /usr/sbin/slapd -u ldap -h ldapi:/// ldap:///

$ netstat -tunlp  | grep :389
tcp        0      0 0.0.0.0:389             0.0.0.0:*               LISTEN      1290/slapd          
tcp6       0      0 :::389                  :::*                    LISTEN      1290/slapd
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果启动失败,则运行下面命令来启动 slapd 服务并查看日志:

$ slapd -h ldap://127.0.0.1 -d 481$ systemctl status slapd
  • 1
  • 2
  • 3

待查明原因之后,停止该进程使用正常方式启动 slapd 服务

启动 systemctl start slapd 遇到的问题:

问题1:

# systemctl start slapd
Aug 30 10:05:39 master slapd[49700]: config error processing cn={1}core,cn=schema,cn=config: olcAttributeTypes: Duplicate attributeType: "2.5.4.2"
  • 1
  • 2

解决方法:

# rm -f  /etc/openldap/slapd.d/cn=config/cn=schema/cn={1}core.ldif
  • 1

问题2:

# systemctl status slapd
Aug 31 22:40:17 master slapd[48126]: sql_select option missing
Aug 31 22:40:17 master slapd[48126]: auxpropfunc error no mechanism available
  • 1
  • 2
  • 3

解决方法:

# rpm -e cyrus-sasl-sql
  • 1

问题3:

# systemctl status slapd
Aug 31 22:38:52 master slapd[47714]: auxpropfunc error invalid parameter supplied
Aug 31 22:38:52 master slapd[47714]: _sasl_plugin_load failed on sasl_auxprop_plug_init for plugin: ldapdb
Aug 31 22:38:52 master slapd[47714]: ldapdb_canonuser_plug_init() failed in sasl_canonuser_add_plugin(): inval...pplied
Aug 31 22:38:52 master slapd[47714]: _sasl_plugin_load failed on sasl_canonuser_init for plugin: ldapdb
  • 1
  • 2
  • 3
  • 4
  • 5

解决方法:

# rpm -e cyrus-sasl-ldap
  • 1

问题4:

# slapd -h ldap://127.0.0.1 -d 481
59a820cb daemon_init: listen on ldap://127.0.0.1
59a820cb daemon_init: 1 listeners to open...
ldap_url_parse_ext(ldap://127.0.0.1)
59a820cb daemon: bind(7) failed errno=98 (Address already in use)
59a820cb slap_open_listener: failed on ldap://127.0.0.1
59a820cb slapd stopped.
59a820cb connections_destroy: nothing to destroy.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

解决方法 
1. netstat -ant (查看发现 389 端口被占用了) 
2. netstat -anp | grep :389 (查看占用389端口的 进程id 30019) 
3. kill 30019 (干掉30019这个进程) 
4. 重启openldap成功

1.5 LDAP 和 Kerberos

在Kerberos安全机制里,一个principal就是realm里的一个对象,一个principal总是和一个密钥(secret key)成对出现的。

这个principal的对应物可以是service,可以是host,也可以是user,对于Kerberos来说,都没有区别。

Kdc(Key distribute center)知道所有principal的secret key,但每个principal对应的对象只知道自己的那个secret key。这也是 “共享密钥” 的由来。

为了使 Kerberos 能够绑定到 OpenLDAP 服务器,请创建一个管理员用户和一个 principal,并生成 keytab 文件,设置该文件的权限为 LDAP 服务运行用户可读( LDAP 服务运行用户一般为 ldap):

$ kadmin.local -q "addprinc ldapadmin@EXAMPLE.COM"
$ kadmin.local -q "addprinc -randkey ldap/master@EXAMPLE.COM"
$ kadmin.local -q "ktadd -k /etc/openldap/ldap.keytab ldap/master@EXAMPLE.COM"

$ chown ldap:ldap /etc/openldap/ldap.keytab && chmod 640 /etc/openldap/ldap.keytab
  • 1
  • 2
  • 3
  • 4
  • 5

ktadd 后面的-k 指定把 key 存放在一个本地文件中。

使用 ldapadmin 用户测试:

kinit ldapadmin
  • 1

系统会提示输入密码,如果一切正常,那么会安静的返回。实际上,你已经通过了kerberos的身份验证,且获得了一个Service TGT(Ticket-Granting Ticket). Service TGT的意义是, 在一段时间内,你都可以用此TGT去请求某些service,比如ldap service,而不需要再次通过kerberos的认证。

确保 LDAP 启动时使用上一步中创建的keytab文件,在 /etc/sysconfig/ldap 增加 KRB5_KTNAME 配置:

export KRB5_KTNAME=/etc/openldap/ldap.keytab
  • 1

然后,重启 slapd 服务。

1.6 创建数据库

初始化 LDAP 数据库,进入到/etc/openldap/slapd.d目录,查看cn=config/olcDatabase={2}hdb.ldif可以看到一些默认的配置,例如:

olcRootDN: cn=Manager,dc=my-domain,dc=com 
olcRootPW: secret 
olcSuffix: dc=my-domain,dc=com
  • 1
  • 2
  • 3

接下来更新这三个配置,建立 modify.ldif 文件,内容如下:

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

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: uid=ldapadmin,ou=people,dc=example,dc=com

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: root 

dn: cn=config
changetype: modify
add: olcAuthzRegexp
olcAuthzRegexp: uid=([^,]*),cn=GSSAPI,cn=auth uid=$1,ou=people,dc=example,dc=com

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcAccess
olcAccess: {0}to dn.base="" by * read
olcAccess: {1}to * by dn="uid=ldapadmin,ou=people,dc=example,dc=com" write by * read
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

说明: 
- 上面的密码使用的是明文密码 root ,你也可以使用 slappasswd -s root 生成的字符串作为密码。 
例如:

# slappasswd -s root
{SSHA}vGPWR24Vt8XyNbwfh6Hq2BoPBkML0I1j
  • 1
  • 2
  • 上面的权限中指明了只有用uid=ldapadmin,ou=people,dc=example,dc=com有写权限。

使用下面命令导入更新配置:

# ldapmodify -Y EXTERNAL -H ldapi:/// -f modify.ldif
  • 1

这时候数据库没有数据,需要添加数据,你可以手动编写 ldif 文件来导入一些用户和组,或者使用 migrationtools 工具来生成 ldif 模板。创建 setup.ldif 文件如下:

dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectclass: organization
o: example com
dc: example

dn: ou=people,dc=example,dc=com
objectclass: organizationalUnit
ou: people
description: Users

dn: ou=group,dc=example,dc=com
objectClass: organizationalUnit
ou: group

dn: uid=ldapadmin,ou=people,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
cn: LDAP admin account
uid: ldapadmin
sn: ldapadmin
uidNumber: 1003 # 这里的 uid 需要和 Unix 账户的 uid 配置,可以通过 id ldapadmin 命令进行查看
gidNumber: 1003 # 这里的 uid 需要和 Unix 账户的 uid 配置,可以通过 id ldapadmin 命令进行查看
homeDirectory: /home/ldap
loginShell: /bin/bash
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

注意:ldif文件配置末尾是不允许有空格的

使用下面命令导入数据,密码是前面设置的 root 。

$ ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f setup.ldif
  • 1

参数说明:

  • -w 指定密码
  • -x 是使用一个匿名的绑定

1.7 LDAP 的使用

导入linux系统用户

接下来你可以从 /etc/passwd, /etc/shadow, /etc/groups 中生成 ldif 更新 ldap 数据库,这需要用到 migrationtools 工具。

安装:

yum install migrationtools -y
  • 1

利用迁移工具生成模板,先修改默认的配置:

$ vim /usr/share/migrationtools/migrate_common.ph

#line 71 defalut DNS domain
$DEFAULT_MAIL_DOMAIN = "example.com";
#line 74 defalut base
$DEFAULT_BASE = "dc=example,dc=com";
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

生成模板文件:

# /usr/share/migrationtools/migrate_base.pl > base.ldif
  • 1
dn: dc=bocloud,dc=com
dc: bocloud
objectClass: top
objectClass: domain

dn: ou=Hosts,dc=bocloud,dc=com
ou: Hosts
objectClass: top
objectClass: organizationalUnit

dn: ou=Rpc,dc=bocloud,dc=com
ou: Rpc
objectClass: top
objectClass: organizationalUnit

dn: ou=Services,dc=bocloud,dc=com
ou: Services
objectClass: top
objectClass: organizationalUnit

dn: nisMapName=netgroup.byuser,dc=bocloud,dc=com
nismapname: netgroup.byuser
objectClass: top
objectClass: nisMap

dn: ou=Mounts,dc=bocloud,dc=com ou: Mounts objectClass: top objectClass: organizationalUnit dn: ou=Networks,dc=bocloud,dc=com ou: Networks objectClass: top objectClass: organizationalUnit dn: ou=People,dc=bocloud,dc=com ou: People objectClass: top objectClass: organizationalUnit dn: ou=Group,dc=bocloud,dc=com ou: Group objectClass: top objectClass: organizationalUnit dn: ou=Netgroup,dc=bocloud,dc=com ou: Netgroup objectClass: top objectClass: organizationalUnit dn: ou=Protocols,dc=bocloud,dc=com ou: Protocols objectClass: top objectClass: organizationalUnit dn: ou=Aliases,dc=bocloud,dc=com ou: Aliases objectClass: top objectClass: organizationalUnit dn: nisMapName=netgroup.byhost,dc=bocloud,dc=com nismapname: netgroup.byhost objectClass: top objectClass: nisMap
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

其中,dn: dc=bocloud,dc=comdn: ou=People,dc=bocloud,dc=comdn: ou=People,dc=bocloud,dc=com在上面的setup.ldif中已经导入了,删除就行。

然后,可以修改该文件,然后执行导入命令:

$ ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f base.ldif
  • 1

导入时可能会提示有些Entry已经导入了,因此可以删除base.ldif文件中重复的Entry,出现重复的原因是,在上面配置数据库时已经在setup.ldif文件中导入了一些Entry导致冲突。

将当前节点上的用户导入到 ldap 中,可以有选择的导入指定的用户:

//先添加用户
# useradd test
//修改密码
# passwd test

//查找系统上的test用户
# grep -E "test" /etc/passwd > test.txt

//生成test用户模板文件test.ldif
# /usr/share/migrationtools/migrate_passwd.pl test.txt test.ldif

//添加test用户到LDAP
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f test.ldif

//修改LDAP上test用户的密码
ldappasswd -x -D 'uid=ldapadmin,ou=people,dc=example,dc=com' -w root "uid=test,ou=people,dc=example,dc=com" -S
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

注意:Linux系统用户test要和LDAP中test用户密码相同,否则,在Sentry中无法进行授权(未验证)

添加CDH集群中服务用户到LDAP中

导入用户(hdfs\hive\hbase\impala\hue\admin):

//导入HDFS
# grep -E "hdfs" /etc/passwd > hdfs.txt
# /usr/share/migrationtools/migrate_passwd.pl hdfs.txt hdfs.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f hdfs.ldif

# passwd hdfs
# ldappasswd -x -D 'uid=ldapadmin,ou=people,dc=example,dc=com' -w root "uid=hdfs,ou=people,dc=example,dc=com" -S

//导入Hive
# grep -E "hive" /etc/passwd > hive.txt
# /usr/share/migrationtools/migrate_passwd.pl hive.txt hive.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f hive.ldif

# passwd hive
# ldappasswd -x -D 'uid=ldapadmin,ou=people,dc=example,dc=com' -w root "uid=hive,ou=people,dc=example,dc=com" -S

// 导入Hbase
# grep -E "hbase" /etc/passwd > hbase.txt
# /usr/share/migrationtools/migrate_passwd.pl hbase.txt hbase.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f hbase.ldif

# passwd hbase
# ldappasswd -x -D 'uid=ldapadmin,ou=people,dc=example,dc=com' -w root "uid=hbase,ou=people,dc=example,dc=com" -S

//导入impala
# grep -E "impala" /etc/passwd > impala.txt
# /usr/share/migrationtools/migrate_passwd.pl impala.txt impala.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f impala.ldif

# passwd impala
# ldappasswd -x -D 'uid=ldapadmin,ou=people,dc=example,dc=com' -w root "uid=impala,ou=people,dc=example,dc=com" -S

//导入Hue
# grep -E "hue" /etc/passwd > hue.txt
# /usr/share/migrationtools/migrate_passwd.pl hue.txt hue.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f hue.ldif

# passwd hue
# ldappasswd -x -D 'uid=ldapadmin,ou=people,dc=example,dc=com' -w root "uid=hue,ou=people,dc=example,dc=com" -S

//导入admin
# grep -E "admin" /etc/passwd > admin.txt
# /usr/share/migrationtools/migrate_passwd.pl admin.txt admin.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f admin.ldif

# passwd admin
# ldappasswd -x -D 'uid=ldapadmin,ou=people,dc=example,dc=com' -w root "uid=admin,ou=people,dc=example,dc=com" -S

//导入root
# grep -E "root" /etc/passwd > root.txt
# /usr/share/migrationtools/migrate_passwd.pl root.txt root.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f root.ldif

# ldappasswd -x -D 'uid=ldapadmin,ou=people,dc=example,dc=com' -w root "uid=root,ou=people,dc=example,dc=com" -S
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

导入组(hdfs\hive\hbase\impala\hue\admin):

//导入组hdfs
# grep -E "hdfs" /etc/group > hdfs.txt
# /usr/share/migrationtools/migrate_group.pl hdfs.txt hdfs.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f hdfs.ldif 

//导入组hive
# grep -E "hive" /etc/group > hive.txt
# /usr/share/migrationtools/migrate_group.pl hive.txt hive.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f hive.ldif 

//导入组 hbase
# grep -E "hbase" /etc/group > hbase.txt
# /usr/share/migrationtools/migrate_group.pl hbase.txt hbase.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f hbase.ldif 

//导入组impala
# grep -E "impala" /etc/group > impala.txt
# /usr/share/migrationtools/migrate_group.pl impala.txt impala.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f impala.ldif

//导入组hue
# grep -E "hue" /etc/group > hue.txt
# /usr/share/migrationtools/migrate_group.pl hue.txt hue.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f hue.ldif

//导入组admin
# grep -E "admin" /etc/group > admin.txt
# /usr/share/migrationtools/migrate_group.pl admin.txt admin.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f admin.ldif

//导入组root
# grep -E "root" /etc/group > root.txt
# /usr/share/migrationtools/migrate_group.pl root.txt root.ldif
# ldapadd -x -D "uid=ldapadmin,ou=people,dc=example,dc=com" -w root -f root.ldif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

查询

查询新添加的 hdfs 用户:

# ldapsearch -LLL -x -D 'uid=ldapadmin,ou=people,dc=example,dc=com' -w root -b 'dc=example,dc=com' 'uid=hdfs'
dn: uid=hdfs,ou=people,dc=example,dc=com
uid: hdfs
cn: Hadoop HDFS
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowLastChange: 17407
loginShell: /bin/bash
uidNumber: 986
gidNumber: 980
homeDirectory: /var/lib/hadoop-hdfs
gecos: Hadoop HDFS
userPassword:: e1NTSEF9RnJXdEFOb0NsUDV2ZlZGZGJ3d2tHbnFkc0tiSU9vMjA=
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

可以看到,通过指定 ‘uid=hdfs’,我们只查询这个用户的数据,这个查询条件叫做filter。有关 filter 的使用可以查看 ldapsearch 的 manpage。

修改

用户添加好以后,需要给其设定初始密码,运行命令如下:

$ ldappasswd -x -D 'uid=ldapadmin,ou=people,dc=example,dc=com' -w root "uid=test,ou=people,dc=example,dc=com" -S
  • 1

删除

删除用户或组条目:

$ ldapdelete -x -w root -D 'uid=ldapadmin,ou=people,dc=example,dc=com' "uid=test,ou=people,dc=example,dc=com"
$ ldapdelete -x -w root -D 'uid=ldapadmin,ou=people,dc=example,dc=com' "cn=test,ou=group,dc=example,dc=com"
  • 1
  • 2

当然,以上都是通过命令行实现的LDAP操作,但是也可以通过LDAP的GUI来操作。

LDAP GUI工具 —> phpldapamin

上面添加的用户在phpldapadmin中显示如下图所示:

这里写图片描述

但是通过上面命令添加的用户不在所在组里面,需要手动添加到组中: 
例如,将hue用户添加到hue组

这里写图片描述

2.配置 Hive 集成 LDAP

这里写图片描述

完成上面的配置后重新启动Hive,配置就生效了,如果通过beeline来链接hive,就需要提供用户名和密码了。例如:

beeline -u "jdbc:hive2://master:10000" -n hive -p hive
  • 1

注意:Cloudera文档中描述,hive的LDAP是Kerberos的替代,不能同时启用,如果同时启用将会出现以下异常:

  • Hue界面中无法连接Hive,错误提示:Bad status: 3 (Unsupported mechanism type GSSAPI)
  • beeline中使用Kerberos认证出现同上的错误

Hive同时开启Kerberos和LDAP,登录Hue时,出现如下图所示错误:

这里写图片描述

有以上情况时,将LDAP的配置移除即可解决。

Kerberos和LDAP的区别:

  • 如果使用 Kerberos 身份验证,Thrift 客户端和 HiveServer2 以及 HiveServer2 和安全 HDFS 之间都支持身份验证
  • 如果使用 LDAP 身份验证,仅在 Thrift 客户端和 HiveServer2 之间支持身份验证

3.配置 Impala 集成 LDAP

通过 Cloudera Manager 进行启动。Impala 中可以同时使用 Kerberos + LDAP 的认证方式,所以在已经启用 Kerberos 的情况下启用 LDAP 可以正常工作。在 Impala 配置页中,进行如下属性的修改,并重启 Impala 服务:

LDAP和Impala的集成与和Hive的集成非常的类似,除了Hive中提到的3个配置项之外,还需要多配一个配置项(如下图),以告诉Impala密码可以通过明文来传播(如果你的环境中没有配置TSL并且又没有设置这个配置,impala将无法正常启动)

但是,我在CDH5.11.1上不添加这个额外配置也可以使用

这里写图片描述

这里写图片描述

–ldap_passwords_in_clear_ok

通过 migrationtools 工具把系统中的 impala 账户信息导入到 OpenLDAP(Cloudera Manager 在创建 Impala 服务的过程中会自动在 Unix 中创建 Impala帐户)。(上面已经导入impala信息,如果没有导入可以安装上面步骤导入impala信息)

使用 Impala-shell 测试 LDAP 账号,首先需要得到 Kerberos 授权,假设你已经在 Kerberos 中创建了 impala/impala_admin 这个 principal。

# kinit impala/impala_admin
# impala-shell -l -u impala --auth_creds_ok_in_clear -i {IMPALAD_HOSTNAME} // IMPALAD_HOSTNAME 为你的随意一台 ImpalaD 服务器
  • 1
  • 2

这种模式链接会报:LDAP authentication is enabled, but the connection to Impala is not secured by TLS. 说明连接没有加密,后续需要进行 TLS 加密部署,这里不复述。

使用 beeline 测试 LDAP 账号,其中IMPALA_PASSWORD为之前修改的LDAP中 impala 用户密码:

beeline -u "jdbc:hive2://${HIVESERVER2_HOSTNAME}:10000/default;principal=hive/${HIVESERVER2_HOSTNAME}@BOCLOUD.COM;" -n impala -p impala;
  • 1

4. 配置 Hue 集成 LDAP

HUE 可以通过 LDAP 来进行用户管理,原理如下图所示:

这里写图片描述

在和 LDAP 打通之前,我们需要保证 HUE 的超级管理员必须可以访问 Enable LDAP 之后的 HUE,我们需要做如下准备工作: 
- 保证所有节点都有 admin 账户,即 Unix 账户体系中存在 admin 账户; 
- 保证 LDAP 中也有 admin 相关DN;(可以参考前面的 migrationtools 方式导入 admin 账户) 
- 在 HUE 中新建 admin 组,修改 HUE 的 admin 账户为 admin 组; 
- 在 kerberos 中生成 admin/admin 这个 principal;

直接绑定

# 直接绑定认证:

# 身份验证后端(Authentication Backend):backend = desktop.auth.backend.LdapBackend
# 登录时创建 LDAP 用户:create_users_on_login = true 
# 使用搜索绑定身份验证(Use Search Bind Authentication):search_bind_authentication = false 
# LDAP URL:ldap_url = ldap://master
# LDAP 用户名模式:ldap_username_pattern = uid=<username>,ou=people,dc=domain,dc=com 
# LDAP 搜索基础(LDAP Search Base):base_dn = dc=domain,dc=com 
# LDAP 用户名属性(LDAP Username Attribute):user_name_attr = uid 
# LDAP 绑定用户可分辨名称(LDAP Bind User Distinguished Name):bind_dn = uid=ldapadmin,ou=people,dc=example,dc=com
# LDAP 绑定密码(LDAP Bind Password):bind_password = ${BIND_PASSWORD}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

直接在Hue的界面配置中搜索上面的属性,配置完成之后重启Hue服务即可完成,之后可以通过管理员账号在Hue的用户管理中导入/同步LDAP账号和组。

小结

基本的配置完成,我们使用默认的直接绑定方式(search_bind_authentication = false,我们禁用了搜索绑定),可以粗糙的通过 sync / add 方式从 LDAP 中添加组,然后添加用户至 HUE,且必须保证用户名和组名一致。这是因为我们定义的 LDAP 账户是没有组的概念的,没有用户和组的映射关系。但是,真实的企业应用过程中,一个组下面往往包含多个用户,而我们的配置只是根据 uid 去匹配 ou=people 节点下的用户,目前的配置满足不了这样的情况,对权限的控制比较困难。同时,我们也无法一次性导入一个组下面的所有用户。那么怎么解决这些问题呢?在阐述如何整合 Sentry 之后,接下来的一篇中笔者会进行高级配置相关的阐述。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值