LoginSignup
1
3

More than 1 year has passed since last update.

PostgreSQL の認証を LDAP で行う

Last updated at Posted at 2019-10-22

初めに

2019-10-03にPostgreSQL12 がリリースされました

https://www.postgresql.org/about/news/1976/
https://www.postgresql.org/about/press/presskit12/ja/#original_release

PostgreSQLにはLDAPでユーザー認証を行う機能があるようですが、あまり情報がないようでしたので、簡単に纏めました。
https://www.postgresql.jp/document/11/html/auth-ldap.html

LDAPはユーザの名前とパスワードの組み合わせの検証のみに使用されます。 そのため、LDAPを使用して認証を行うようにする前に、ユーザはデータベースに存在しなければなりません。

環境

  • Ubuntu 18.04 LTS
  • PostgreSQL 12 (--with-ldapでビルドしたもの)
  • OpenLDAP 2.4.x

PostgreSQL 12の構築と設定

必要なパッケージの導入

sudo apt update

sudo apt install gcc
sudo apt install make

sudo apt install libreadline-dev
sudo apt-get install zlib1g-dev

sudo apt install ldap-utils
sudo apt install slapd
sudo apt install libldap-dev

PostgreSQL 12をビルド

tar xvf ./postgresql-12.0.tar.gz
cd ./postgresql-12.0
./configure --with-ldap
make
sudo make install

OSユーザー(postgres)の作成

sudo adduser postgres

PostgreSQLのデータディレクトリの作成

sudo mkdir /usr/local/pgsql/data
sudo chown postgres:postgres /usr/local/pgsql/data
sudo chmod 700 /usr/local/pgsql/data

データーディレクトリの初期化

su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data 

PostgreSQLを起動し接続

su - postgres
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start  
/usr/local/pgsql/bin/psql  

DBのユーザー(dbadmin)を作成

postgres=# CREATE ROLE dbadmin WITH SUPERUSER LOGIN;

postgres=# \du
Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
dbadmin   | Superuser                                                  | {}
postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# \q

DBの認証方法の設定

:/usr/local/pgsql/data/pg_hba.conf
:
#local   all             all                                    trust
local   all             all                                     ldap ldapserver=localhot ldapprefix="cn=" ldapsuffix="dc=example,dc=com" 
# IPv4 local connections:
host    all             all             127.0.0.1/32            ldap ldapserver=localhot ldapprefix="cn=" ldapsuffix="dc=example,dc=com" 
:

設定が済んだらPostgreSQLを再起動

OpenLDAPの設定

LDAPのユーザーエントリー(dbadmin)

OpenLDAPに以下のエントリーを追加、データベースに追加したユーザー名(dbadmin)と同じ名前のユーザーを作成する。

dbadmin.ldif
objectclass: top
objectclass: person
objectclass: organizationalperson
objectclass: inetorgperson
dn: cn=dbadmin,dc=example,dc=com
cn: dbadmin
sn: PostgresDB管理者
mail: dbadmin@example.com
userPassword: secret123     (ここはslappasswdコマンドを使ってハッシュ化すること)

認証

/usr/local/pgsql/bin/psql -U dbadmin -d <データーベース名>
Password for user admin: 

最後に

いろいろ試しているのですが、うまく行ったり行かなかったりで、なかなかの曲者です。

1
3
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
1
3