2015年6月26日金曜日

Let's Chatを入れてみた

slackを使っていのだけど、やはりプロジェクトによっては社外のサービスを使うのはなぁ?という懸念が上がるときがある。抜け道を考えるのが面倒なので、社内にChatサーバーを立ててることにした(BYOSというの?)。折角なのでSlack風Chatの「Let's Chat」を使ってみようと思ったので、備忘録。


0.環境準備

使った環境は以下の通り。

  ・vagrant
  ・CentOS 6.5
  ・Let's Chat 0.3.12

1.インストール

Let's Chatの導入要件となっている、以下のツール群を導入する。
  ・nodejs v0.12.4
  ・mongodb 3.0
  ・Python 2.7

1.1 nodejsを入れる
 ここを参照しつつ入れる!

1.2 mongodbを入れる

$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.0.3.tgz
$ tar xvfz mongodb-linux-x86_64-rhel62-3.0.3.tgz
$ cd mongodb-linux-x86_64-rhel62-3.0.3 mongo

1.3 python2.7を入れる

$ wget https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tgz
$ tar xvfz Python-2.7.6.tgz
$ cd Python-2.7.6
$ ./configure --enable-shared --with-threads
$ make
$ sudo make install

$ python -V

python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

なんだかエラーがでてる。

$ ldd /usr/local/bin/python
        linux-vdso.so.1 =>  (0x00007fffc0ad3000)
        libpython2.7.so.1.0 => not found
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f112bed4000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f112bcd0000)
        libutil.so.1 => /lib64/libutil.so.1 (0x00007f112bacd000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f112b848000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f112b4b4000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f112c0fb000)

同じですが、libpython2.7.so.1.0がないと言われている。ただ実際には、、

$ sudo find / -name 'libpython2.7.so.1.0'
/home/vagrant/Python-2.7.6/libpython2.7.so.1.0
/usr/local/lib/libpython2.7.so.1.0

といストールされている。のでリンクで対応する。

$ sudo ln /usr/local/lib/libpython2.7.so.1.0 /lib64/libpython2.7.so.1.0
$ python -V

Python 2.7.6

無事。インストールができたみたいです。

1.4 Let's Chatを入れる

最後にLet's Chatを入れる。

$ git clone https://github.com/sdelements/lets-chat.git
$ cd lets-chat/
$ npm install

gyp: Call to 'which icu-config > /dev/null || echo n' returned exit status 0. while trying to load binding.gyp


何やらエラーがでて失敗した。ライブラリを追加で入れる。

$ sudo yum install libicu-devel.x86_64

で、再チャレンジです

$ npm install

node-xmpp-core@1.0.0-alpha14 node_modules/node-xmpp-core
├- tls-connect@0.2.2
├- debug@2.2.0 (ms@0.7.1)
├- reconnect-core@0.0.1 (backoff@2.3.0)
├- node-stringprep@0.7.0 (bindings@1.2.1, debug@2.0.0, nan@1.8.4)
└- mqq ltx@0.9.0 (sax@0.6.1, node-expat@2.3.8)

をーインストール完了。

2. Let's Chatの設定

$ cp settings.yml.sample settings.yml
$ vi settings.yml
================
# Let's Chat Settings
#
# See defaults.yml for all available options
#

env: production # development / production

http:
  enable: true
  host: '0.0.0.0'
  port: 80

https:
  enable: false
  port: 5001
  key: key.pem
  cert: certificate.pem

files:
  enable: true
  provider: local
  local:
    dir: uploads

xmpp:
  enable: false
  port: 5222
  domain: example.com

database:
  uri: mongodb://localhost/letschat

secrets:
  cookie: secretsauce

auth:
  providers: [local]
  local:
    enableRegistration: true
================


$ npm start

 [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version

あがるっちゃ、あがるんだけど、エラーがでている。いろいろググってみたけど、

npm cache clean; rm -rf node_modules ;npm install

これで再インストールすればいけるみたい。
ということでやってみたら・・・行けた!

3.検索をカスタマイズ
chatの履歴から全文検索ができるようになっているのですが、、mongodbの全文検索機能を使っており、mongodbのそれは日本語には対応していない。なので、全くといっていいほど使えない。それだったらせめてmessagesの検索だけでも意味があるかなと思い、少しソースを修正。

lets-chat/app/core/messages.js

    if (options.query) {
        find = find.find({$text: {$search: options.query}});
    }


    if (options.query) {
        var r = new RegExp(options.query, "i")
        find = find.find({"text": {$regex:r}});
    }

に変更してみた。これだけでも結構便利!

4.ldap認証

デフォルトでは、local認証となっていて、さらに自由にアカウント登録ができてしまう。今回インターネット経由でアクセスをさせるので、この状態はよろしくない。
とうことで、openldapを立てて、アカウント管理!
※openldapの導入はこちら

settings.yml
===
auth:
  providers: [ldap]
  ldap:
    connect_settings:
      url: ldap://192.168.x.x:389/
    bind_options:
      bindDN: uid=bind,ou=Account,dc=example,dc=com
      bindCredentials: password
    search:
      base: "ou=Account,dc=example,dc=com"
      opts:
        scope: one
        filter: (uid={{username}})
        #filter: (sAMAccountName={{username}})
    field_mappings:
      uid: uid
      firstName: givenName
      lastName: sn
      displayName: givenName

      email: mail
===
いや~バカになるくらいで簡単にできた・・。すごい!

2015年6月6日土曜日

サーバー証明書のCSR+オレオレ証明書の作り方

サーバー証明書の発行手順

1.秘密キーの生成
openssl genrsa -rand randfile 2048 > xxx.key
2.証明書(CSR)作成
openssl req -new -sha256 -key xxx.key -out xxx.csr
3.証明書(CSR)の確認
openssl req -noout -text -in xxx.csr
4.正式証明書を発行
 正式な商用サーバー証明書発行してもらう。

5.自己証明書を発行
$ openssl x509 -days 3650 -req -signkey xxx.key < xxx.csr > xxx.crt
5.自己証明書の確認
$ openssl x509 -text < xxx.crt

OpenLdapを入れてみた

自PCにChatを導入してみようと思い(BYOS:Bring your own serverというらしい)、Slack風chatのLet's Chatを入れてみようとした。
デフォルトだとアカウントが自由に登録できちゃう。今回は特定ユーザだけどインターネット環境で利用することを考えると、さすがによろしくないなぁということで、ldap認証に変えようとopenldapの構築に思い立った・・

0.環境準備
 
 CentOS : 6.5
 openldap 2.4.

1.インストール

openldapのサーバー「openldap-servers」と、管理用コマンド「ldapadd」「ldapsearch」が含まれているクライアントパッケージ「openldap-clients」を入れる。
$ sudo yum install -y openldap-servers openldap-clients

 初期の設定を削除する
$ sudo rm -rf /etc/openldap/slapd.d/*
$ sudo rm -rf /var/lib/ldap/*

 設定ファイルをコピーする
$ sudo cp -a /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
$ sudo chown ldap. /var/lib/ldap/DB_CONFIG
$ sudo cp -a /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf

rootユーザ(管理者ユーザー)のパスワードを設定する
$ slappasswd -s [パスワード]

{SSHA}bl2YB5/i9fZ64tMNV/7dr9li4a3YsNc0 ・・・(*1)

設定ファイルの修正をする(/etc/openldap/slapd.conf)
# スキーマファイル設定
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

# 接続プロトコル
allow bind_v2

# 管理ファイル
pidfile     /var/run/openldap/slapd.pid
argsfile    /var/run/openldap/slapd.args

# userPasswordに関するアクセス権
access to attrs=userPassword
    by self write
    by dn="cn=Manager,dc=example,dc=com" write
    by anonymous auth
    by * none

# その他の属性に対するアクセス権
access to *
    by self write
    by dn="cn=Manager,dc=example,dc=com" write
    by * read

# monitorデータベースに対するアクセス権
database monitor
access to *
    by dn.exact="cn=Manager,dc=example,dc=com" read
    by * none

# データベース設定
database    bdb
suffix      "dc=example,dc=com"
checkpoint  1024 15
rootdn      "cn=Manager,dc=example,dc=com"
rootpw      {SSHA}bl2YB5/i9fZ64tMNV/7dr9li4a3YsNc0 ・・・(*1)
directory   /var/lib/ldap

# indexの設定
index objectClass                       eq,pres
index ou,cn,mail,surname,givenname      eq,pres,sub
index uid,memberUid                     eq,pres,sub
設定ファイルのチェック
$ sudo su -
# sudo -u ldap slaptest -u -v -f /etc/openldap/slapd.conf

config file testing succeeded

※設定ファイルを、設定ディレクトリ(/etc/openldap/slapd.d)に配置する場合
# sudo -u ldap slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d

config file testing succeeded

この設定をすると、起動時に設定ディレクトリが優先され、設定ファイルは無視される。
設定ファイルが無くなっても大丈夫になるが、簡単に設定変更ができなくなる。
どっちがいいんでしょう・・
ちなみに設定ディレクトリを作成しないと、デフォルトの起動スクリプトで起動すると、
ls: cannot access /etc/openldap/slapd.d//cn=config/olcDatabase*.ldif: そのようなファイルやディレクトリはありません
というエラーがでる。無視できそうだけど・・わかっていないので、設定ディレクトリを使って進める。
$ sudo service slapd start
$ chkconfig slapd on
あとはログの取得設定をしておく。rsyslogの設定ファイルに以下を追加する(/etc/rsyslog.conf)
local4.*     /var/log/ldap.log
でもって再起動。
$ sudo service rsyslog restart

2.データの登録・更新・削除

基本的にデータの登録・更新は、LDIFファイルを作成し、それをコマンドを使って反映というステップで行う。Apache Directory Studioを使うと楽にできたが、 とりあえず理解を深めるために、、地道に。 まずは、ベース ldapadd -x -D "cn=Manager,dc=example,dc=com" -W -f test.ldif.txt

登録データをコマンドで確認してみる。
ldapsearch -x -b dc=example,dc=com

削除は
ldapdelete -D "cn=Manager,dc=example,dc=com" -W "削除するDN"
ldapdelete -D "cn=Manager,dc=example,dc=com" -W "uid=bind,ou=Account,dc=example,dc=com"

修正は
ldapmodify -x -W -D cn=config -f loglevel.ldif

slappasswd -h '{CRYPT}'

3.その他

・登録ユーザのパスワード変更

 $ ldappasswd -x -D "uid=<自分のアカウント>,ou=Account,dc=example,dc=com" -S -w <自分のパスワード> "uid=<自分のアカウント>,ou=Account,dc=example,dc=com"

・デバッグログ

openldapサーバーのログレベルの変更
 vi /etc/openldap/slapd.conf
  loglevel ACL

j設定ディレクトリを作成していると、上記の設定ファイルでログレベルを設定しても無視されて
しまう。ので動的にloglevelを変更する必要がある。ステップは、

 ・動的に設定変更をする際には、設定変更用のパスワードを設定する。
  パスワードを生成する。

   $ slappasswd -s [パスワード]

 ・パスワードを設定変ファイルに書き込む。
  vi /etc/openldap/slapd.d/cn=config/olcDatabase={0}config.ldif
  ==
# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.
# CRC32 b65956a0
dn: olcDatabase={0}config
objectClass: olcDatabaseConfig
olcDatabase: {0}config
olcAccess: {0}to *  by * none
olcAddContentAcl: TRUE
olcLastMod: TRUE
olcMaxDerefDepth: 15
olcReadOnly: FALSE
olcRootDN: cn=config
olcRootPW: {SSHA}bl2YB5/i9fZ64tMNV/7dr9li4a3YsNc0 (*)
olcSyncUseSubentry: FALSE
olcMonitoring: FALSE
structuralObjectClass: olcDatabaseConfig
entryUUID: ac9a77d2-a08f-1034-959f-516e801fdd3a
creatorsName: cn=config
createTimestamp: 20150606120240Z
entryCSN: 20150606120240.046232Z#000000#000#000000
modifiersName: cn=config
modifyTimestamp: 20150606120240Z
  ==
  最初の2行を削除する
  (*)の行を追加する。パスワードは上記で生成したものを指定する。
  

・ログ変更用のファイルを作成する
  vi /etc/openldap/ldif/loglevel.ldif
  ===
  dn: cn=config
  changetype: modify
  add: olcLogLevel
  olcLogLevel: filter config ACL stats
  ===
  2回目以降に更新する場合は、
  add: olcLogLevel
  を
  replace: olcLogLevel
  に変更する。


 ・ldapmodifyコマンドで変更する
 $ ldapmodify -x -W -D cn=config -f loglevel.ldif

 Enter LDAP Password:
 modifying entry "cn=config"

 ※パスワードは上記で生成したものを!