Centos7 搭建LDAP並啓用TLS加密

簡介

  • LDAP(輕量級目錄訪問協議,Lightweight Directory Access Protocol)是爲了實現目錄服務的信息服務。
  • 目錄服務是一種特殊的數據庫系統,其專門針對讀取,瀏覽和搜索操做進行了特定的優化。在網絡中應用了LDAP後,用戶只須要使用一個帳號和密碼就能夠輕鬆訪問網絡中的全部服務,實現用戶身份的統一認證。
  • 簡單來講:拿LDAP來統一管理一些帳號,例如: Gitlab,JenKins,Samba,SVN,Zabbix等。

關於SSL/TLS

  • LDAP over SSL
# LDAP over SSL 也就是 ldaps
# ldap默認不加密狀況下是走的389端口
# 當使用ldaps的時候走的就是636端口了
# 能夠簡單理解成http和https的關係
# 固然ldaps已經淘汰了,否則也不會有LDAP over TLS出來
  • LDAP over TLS
# TLS能夠簡單理解爲ldaps的升級
# 它默認走389端口,可是會通信的時候加密
# 客戶端鏈接LDAP時,須要指明通信類型爲TLS,因此他能夠跟不加密的模式同樣,任意端口都行

對比一下鏈接方式:
ldaps: ldapsearch -H ldaps://127.0.0.1
TLS:   ldapsearch -ZZ -H ldap://127.0.0.1

環境

CentOS Linux release 7.5.1804
Kernel 4.20.0-1.el7.elrepo.x86_64

docker-ce 18.09
docker-compose 1.23.1

安裝docker-compose

yum install -y python-pip
pip install docker-compose
docker-compose -v

準備證書

  • 安裝cfssl
wget -O /bin/cfssl https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget -O /bin/cfssljson https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget -O /bin/cfssl-certinfo  https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
for cfssl in `ls /bin/cfssl*`;do chmod +x $cfssl;done;
  • 配置證書信息
cd $HOME && mkdir ssl && cd ssl

# ca配置文件
cat > ca-config.json << EOF
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "ldap": {
        "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ],
        "expiry": "87600h"
      }
    }
  }
}
EOF
# 自簽名ca的證書申請
cat > ldap-ca-csr.json << EOF
{
  "CN": "ldap",
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "Shenzhen",
      "L": "Shenzhen",
      "O": "ldap",
      "OU": "LDAP Security"
    }
  ]
}
EOF

# ldap證書申請資料
# 下面hosts字段裏就是使用這張證書的主機
# 特別注意必定要加上宿主機的IP地址,反正是本身頒發的證書,怎麼加都行!!!
# 加上本機迴環地址,加上ldap容器名,我這裏容器名待會設置成openldap
# 若是你要放到公網去的話,那一能夠加上FQDN地址

cat > ldap-csr.json << EOF
{
    "CN": "ldap",
    "hosts": [
      "127.0.0.1",
      "192.168.1.1",
      "openldap",
      "ldap.lotbrick.com",
      "lotbrick.com"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "ST": "Shenzhen",
            "L": "Shenzhen",
            "O": "ldap",
            "OU": "LDAP Security"
        }
    ]
}
EOF
  • 給證書籤名
# CA自簽名
cfssl gencert -initca ldap-ca-csr.json | cfssljson -bare ca

# LDAP證書籤名,ldap須要的文件爲:ca證書,ldap證書,ldap私鑰
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=ldap ldap-csr.json | cfssljson -bare ldap

# 查看生成的證書
# 其中  ldap-key.pem  ldap.pem ca.pem 是咱們須要的
[root@master ssl]#ls
ca-config.json  ca.csr  ca-key.pem  ca.pem  ldap-ca-csr.json  ldap.csr  ldap-csr.json  ldap-key.pem  ldap.pem

開始安裝ldap

  • 克隆倉庫,獲取docker-compose.yaml文件
cd $HOME
git clone https://github.com/JyBigBoss/docker-compose.git
cd docker-compose/LDAP/
  • 複製證書
mkdir ssl/
cp $HOME/ssl/{ldap-key.pem,ldap.pem,ca.pem} ssl/
  • 修改docker-compose.yaml文件
vi docker-compose.yaml

# 修改下面的幾項
# 鏡像使用的是osixia/openldap
# 詳細的配置解釋:https://github.com/osixia/docker-openldap

LDAP_ORGANISATION: "lotbrick.com"
LDAP_DOMAIN: "lotbrick.com"
LDAP_ADMIN_PASSWORD: "admin"
LDAP_CONFIG_PASSWORD: "admin"
LDAP_TLS: "true"
LDAP_TLS_CRT_FILENAME: "ldap.pem"
LDAP_TLS_KEY_FILENAME: "ldap-key.pem"
LDAP_TLS_CA_CRT_FILENAME: "ca.pem"
LDAP_TLS_ENFORCE: "true"
LDAP_TLS_VERIFY_CLIENT: "try"
domainname: "lotbrick.com"
hostname: "lotbrick.com"

# 特別注意LDAP_TLS_VERIFY_CLIENT
# 不要設置成demand,這個選項能夠理解成雙向認證,也就是客戶端鏈接ldap時也許要提供證書,也就是客戶端也須要有本身的證書
# 設置成try就行,客戶端不提供證書也能鏈接,反正鏈接已經加密了。
# 官方文檔:http://www.openldap.org/doc/admin24/tls.html

Centos7 搭建LDAP並啓用TLS加密

  • 啓動ldap
#第一次啓動會比較慢,淡定點
docker-compose pull
docker-compose up -d 
ls
docker ps -a 

# 啓動以後會生成幾個文件夾
# ldapconf保存的是ldap的配置文件
# ldapdata保存的是ldap的數據
# lam保存的是lam管理工具的配置

[root@master LDAP]#docker-compose up -d
Creating network "ldap_default" with the default driver
Creating openldap ... done
Creating ldap-account-manager ... done
[root@master LDAP]#ls
docker-compose.yaml  lam  ldapconf  ldapdata  ssl

[root@master LDAP]#docker ps -a
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS                                        NAMES
9b4ebdad17eb        jinyunboss/ldap-account-manager:6.6   "docker-php-entrypoi…"   2 minutes ago       Up 2 minutes        0.0.0.0:8080->80/tcp                         ldap-account-manager
a7ff3bd5dced        osixia/openldap:1.2.2                 "/container/tool/run"    2 minutes ago       Up 2 minutes        0.0.0.0:389->389/tcp, 0.0.0.0:636->636/tcp   openldap

打開瀏覽器,配置LDAP Account Manager

  • LDAP Account Manager容器監聽在8080端口
  • 打開http://192.168.1.1:8080
# 配置一下lam管理頁面
# lam管理界面默認密碼是: lam
# lam能夠管理多個ldap服務器,因此能夠擁有多個profile,每一個profile對應一臺服務器

# 簡單添加個用戶,而後用另外一臺linux機器測試ldap鏈接

Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密

測試LDAP

  • 安裝ldap客戶端
yum install -y openldap-clients  nss-pam-ldapd
  • 配置配置客戶端
# 配置系統使用ldap認證
authconfig-tui

# 將自簽名的ca證書給客戶端
cd /etc/openldap/cacerts/

# 修改/etc/nslcd.conf,添加管理員憑據
echo "binddn cn=admin,dc=lotbrick,dc=com" >> /etc/nslcd.conf
echo "bindpw admin"  >> /etc/nslcd.conf

cat /etc/nslcd.conf

# 重啓nslcd服務

systemctl restart nslcd

Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密

  • 測試
# 執行命令看看能不能讀取到LDAP用戶
# 能鏈接上ldap的話,執行以後會出現ldap用戶

getent passwd

id bigboss

# 切換成bigboss用戶試試

su - bigboss

Centos7 搭建LDAP並啓用TLS加密
Centos7 搭建LDAP並啓用TLS加密

Centos7 搭建LDAP並啓用TLS加密

相關文章
相關標籤/搜索