0
0

More than 5 years have passed since last update.

itamae|Centos7でldapサーバーを構築するレシピ

Last updated at Posted at 2016-08-25

概要

CentOS7のサーバにldapサーバーを構築するレシピ

構築ネットワーク

▪️ 192.168.56.101
centos7-admin(管理サーバー)
ldapサーバーとitamae実行サーバーを兼ねる構築済みが前提(本稿では取り扱わない)

▪️ 192.168.56.102
centos7-web(webサーバー)
ldap認証のCentos7でwebサーバーを構築するレシピで構築したwebサーバー

▪️ 192.168.56.103
centos7-db(dbサーバー)
DBサーバー(本稿では取り扱わない)

▪️ 192.168.56.104
centos7-adm2(管理サーバーのバックアップサーバー)
本稿で構築するバックアップ用ldapサーバー

▪️ 192.168.56.201 centos7−ldap(ldap用VIP)
管理サーバーの冗長運用時のVIP

前準備

webサーバーで構築した前準備と同じ

  • 構築するサーバーにCentOS7をMinimalのisoからインストールする
  • (構築するwebサーバーで)インストール後に192.168.56.104のローカルIPを振る
  • (構築するwebサーバーで)root以外のマスターアカウントを作成し、そのアカウントで公開鍵認証ができるようにauthorized_keysに公開鍵を追加する
  • (構築するwebサーバーで)マスターアカウントでパスワードなしsudoができるように設定する
  • (構築するwebサーバーで)yum upgrade を実行してOSを最新状態にする

本編

(※ パスワードやドメインなどは目的に応じて変更すること)
構築の概要としては

  • openldap-serversとopenldap-develをyumでインストール
  • セットアップ
    • rsyslogの設定
    • schamaを追加するので必要なファイルのコピー(schema.OpenLDAP/openssh-lpk-openldap.schema)
    • DB用の設定ファイル DB_CONFIGのコピー
    • ldaps起動用のcrtファイル/keyファイルの準備(crt/keyは通常のssl証明書で良い、自己署名証明書を作成する場合には下記で作成する)
      • openssl genrsa 4096 >server.key
      • openssl req -new -key server.key > server.csr
      • openssl x509 -days 3650 -req -signkey server.key < server.csr > server.crt
    • OLCで設定するので必要なldifファイルの準備
      • add_rootpw.ldif(システムパスワード設定用)
      • change-domain.ldif(ドメイン/ドメインルートDN/ドメインルートパスワード設定用)
      • set_tls.ldif(ldaps設定用)
    • 読み込むschemaのldifファイルの準備
      • schema.confという名で読み込みschema設定ファイルを作る
      • "slaptest -f schema_convert.conf -F 出力先 "でldifファイルを作成
      • 作成されたベースldifファイルを目的に合わせて編集する
    • OLCなので、slapd(ldap−server)を起動してコマンドでldifファイルを適用する
      • add_rootpw.ldif (ldapadd -Y EXTERNAL -H ldapi:// -f add_rootpw.ldif)
      • change-domain.ldif (ldapmodify -x -D cn=config -f change-domain.ldif -w abcde12345)
      • set_tls.ldif (ldapmodify -x -D cn=config -f set_tls.ldif -w abcde12345)
      • corba.ldif (ldapadd -x -D "cn=config" -f "cn={0}corba.ldif" -w abcde12345)
      • cosine.ldif (ldapadd -x -D "cn=config" -f "cn={2}cosine.ldif" -w abcde12345)
      • duaconf.ldif (ldapadd -x -D "cn=config" -f "cn={3}duaconf.ldif" -w abcde12345)
      • dyngroup.ldif (ldapadd -x -D "cn=config" -f "cn={4}dyngroup.ldif" -w abcde12345)
      • inetorgperson.ldif (ldapadd -x -D "cn=config" -f "cn={5}inetorgperson.ldif" -w abcde12345)
      • java.ldif (ldapadd -x -D "cn=config" -f "cn={6}java.ldif" -w abcde12345)
      • misc.ldif (ldapadd -x -D "cn=config" -f "cn={7}misc.ldif" -w abcde12345)
      • nis.ldif (ldapadd -x -D "cn=config" -f "cn={8}nis.ldif" -w abcde12345)
      • openldap.ldif (ldapadd -x -D "cn=config" -f "cn={9}openldap.ldif" -w abcde12345)
      • ppolicy.ldif (ldapadd -x -D "cn=config" -f "cn={10}ppolicy.ldif" -w abcde12345)
      • collective.ldif (ldapadd -x -D "cn=config" -f "cn={11}collective.ldif" -w abcde12345
      • sudo.ldif (ldapadd -x -D "cn=config" -f "cn={12}sudo.ldif" -w abcde12345
      • openssh-lpk-openldap.ldif (ldapadd -x -D "cn=config" -f "cn={13}openssh-lpk-openldap.ldif" -w abcde12345
  • セットアップが終わったら、/etc/sysconfig/slapdを編集し、ldapsでの起動設定し、slapdを再起動させる

であり、この一連の構築作業について、必要の各ファイル、設定編集済みのldifファイル等を準備し、itamaeのレシピ ldap_server.rb に起こし、そのレシピをitamaeで実行する

ldap_server.rb
# package install
[
    "openldap-devel",
    "openldap-servers",
].each {| pkg |
    package pkg do
        action :install
    end
}

# copy files
template "/etc/rsyslog.conf" do
    action :create
    owner  "root"
    group  "root"
    mode   "644"
    source "files/ldap-server/rsyslog.conf"
end

service "rsyslog" do
   action :restart
end

execute "sudo.schama" do
    action  :run
    command "find /usr/share/doc/ -type f -name schema.OpenLDAP -print0 | xargs -0 -I% cp %  /etc/openldap/schema/sudo.schema"
end

execute "openssh-lpk-openldap" do
    action  :run
    command "find /usr/share/doc/ -type f -name openssh-lpk-openldap.schema -print0 | xargs -0 -I% cp %  /etc/openldap/schema/openssh-lpk-openldap.schema"
end

execute "DB_CONFIG" do
    action  :run
    command "cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG"
end

[
    "/etc/openldap/certs",
    "/etc/openldap/ldif_files",
    "/etc/openldap/ldif_files/ldif_conv",
    "/etc/openldap/ldif_files/ldif_conv/cn=config",
    "/etc/openldap/ldif_files/ldif_conv/cn=config/cn=schema",
].each {| dir |
    directory dir do
        action :create
        owner  "root"
        group  "root"
    end
}

[
    "server.crt",
    "server.key",
].each {| tmp |
    template "/etc/openldap/certs/#{tmp}" do
        action :create
        owner  "root"
        group  "root"
        mode   "644"
        source "files/ldap-server/#{tmp}"
    end 
}

[
    "add_rootpw.ldif",
    "change-domain.ldif",
    "set_tls.ldif",
].each {| tmp |
    template "/etc/openldap/ldif_files/#{tmp}" do
        action :create
        owner  "root"
        group  "root"
        mode   "644"
        source "files/ldap-server/#{tmp}"
    end
}

template "/etc/openldap/ldif_files/ldif_conv/cn=config/cn=schema.ldif" do
    action  :create
    owner   "root"
    group   "root"
    mode    "644"
    source  "files/ldap-server/ldif_conv/cn=config/cn=schema.ldif"
end

[
    "cn={0}corba.ldif",
    "cn={1}core.ldif",
    "cn={2}cosine.ldif",
    "cn={3}duaconf.ldif",
    "cn={4}dyngroup.ldif",
    "cn={5}inetorgperson.ldif",
    "cn={6}java.ldif",
    "cn={7}misc.ldif",
    "cn={8}nis.ldif",
    "cn={9}openldap.ldif",
    "cn={10}ppolicy.ldif",
    "cn={11}collective.ldif",
    "cn={12}sudo.ldif",
    "cn={13}openssh-lpk-openldap.ldif",
].each {| tmp |
    template "/etc/openldap/ldif_files/ldif_conv/cn=config/cn=schema/#{tmp}" do
        action  :create
        owner   "root"
        group   "root"
        mode    "644"
        source  "files/ldap-server/ldif_conv/cn=config/cn=schema/#{tmp}"
    end
}

# setup ldap-server
service "slapd" do
    action :start
end

execute "rootpw" do
    action  :run
    command "/bin/ldapadd -Y EXTERNAL -H ldapi:// -f /etc/openldap/ldif_files/add_rootpw.ldif"
end

[
    "change-domain.ldif",
    "set_tls.ldif",
].each {| file |
    execute "#{file}" do
        action  :run
        command "/bin/ldapmodify -x -H ldapi:// -D cn=config -w 12345abcde -f /etc/openldap/ldif_files/#{file}"
    end
}

[
    "cn={0}corba.ldif",
#    "cn={1}core.ldif",
    "cn={2}cosine.ldif",
    "cn={3}duaconf.ldif",
    "cn={4}dyngroup.ldif",
    "cn={5}inetorgperson.ldif",
    "cn={6}java.ldif",
    "cn={7}misc.ldif",
    "cn={8}nis.ldif",
    "cn={9}openldap.ldif",
    "cn={10}ppolicy.ldif",
    "cn={11}collective.ldif",
    "cn={12}sudo.ldif",
    "cn={13}openssh-lpk-openldap.ldif",
].each {| file |
    execute "#{file}" do
        action  :run
        command "/bin/ldapadd -x -H ldapi:// -D cn=config -w 12345abcde -f /etc/openldap/ldif_files/ldif_conv/cn=config/cn=schema/#{file}"
    end
}

# restart ldap-server 
template "/etc/sysconfig/slapd" do
    action :create
    owner  "root"
    group  "root"
    mode   "644"
    source "files/ldap-server/slapd"
end

service "slapd" do
   action [:restart, :enable]
end

(管理サーバーでitamaeを実行する)
itamae ssh -u xxx -h 192.168.56.104 -i id_rsa_xxx ldap_server.rb

本稿使用した、レシピとその他必要なファイル類はgithubに上げてある

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0