Program LOG

勉強した内容をまとめ中。。。間違ってたら教えて。。。

Chefでオレオレ証明書を作成して簡易的にhttpsのアクセスを有効にする

フロントサイド用の環境構築がひと段落したので、
今度は、サーバーサイドの環境構築をもっと作りこんでいきたいと思います。
本日の題材も開発環境だとよくあると思いますが、HTTPSのアクセスを簡易的に行うようにする為、
SSL自己証明書(オレオレ証明書と言ったりしますが・・・w)を作成する事があると思います。
まぁ、認証が正常にされないので、ブラウザからアラートがおきますが・・・w
それを今回はChefで作成できるようにしちゃいたいと思います。

コマンド1発!オレオレ証明書

色んなサイトを見ましたが、実は一発でコマンドを実行すると、
オレオレ証明書を作成する事が出来る事が判明!
そのコマンドをレシピに記載しておき、今後も使えるようにしていきたいと思います。

まず、今回は開発用で、本番では使用しないので、別のレシピを作成したいと思います。
ssl.rbというファイルをrecipesに作成して記述していきます。

package "mod_ssl" do
    action :install
end

strength = 2048
serial = 0
days = 365
subject = "/C=JP/CN=localhost"
crt_file = "/etc/pki/tls/certs/localhost.crt"
key_file = "/etc/pki/tls/private/localhost.key"
crt_and_key_file = "/etc/pki/tls/private/localhost.crt_and_key"

bash "create_self_signed_cerficiate" do
  code <<-EOH
    openssl req -new -newkey rsa:#{strength} -sha1 -x509 -nodes \
      -set_serial #{serial} \
      -days #{days} \
      -subj "#{subject}" \
      -out "#{crt_file}" \
      -keyout "#{key_file}" &&
    cat "#{crt_file}" "#{key_file}" >> "#{crt_and_key_file}" &&
    chmod 400 "#{key_file}" "#{crt_and_key_file}"
  EOH
  creates crt_and_key_file
  notifies :reload, 'service[httpd]'
end

実はたったこれだけ・・・・w
これでいつものようにknife soloを実行します。

$ knife solo cook develop
Running Chef on develop...
Checking Chef version...
DL is deprecated, please use Fiddle
・・・中略・・・
  * package[mod_ssl] action install
    - install version 2.2.15-30.el6.centos of package mod_ssl

Recipe: develop::sslbash[create_self_signed_cerficiate] action run
    - execute "bash"  "/tmp/chef-script20140706-5421-1ly8kml"

これだけでは、httpsにはアクセス出来ないので、アクセスできるように、
VurtualHostの設定を行いたいと思います。
今までのchefを正常に実行していれば、以下のように設定されています。

$ vagrant ssh
[vagrant@localhost ~]$ sudo vim /etc/httpd/conf.d/vhost.conf
NameVirtualHost *:80
<VirtualHost *:80>
  ServerName develop.jp
  DocumentRoot /var/www/html/
  ErrorLog /var/log/php_error.log
  TransferLog /var/log/php_access.log
  <Directory /var/www/html/>
    AllowOverride all
    Order allow,deny
    allow from all
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName php.develop.jp
  DocumentRoot /usr/share/phpMyAdmin/
  ErrorLog /var/log/php_error.log
  TransferLog /var/log/php_access.log
  <Directory /usr/share/phpMyAdmin/>
    AllowOverride all
    Order allow,deny
    allow from all
  </Directory>
</VirtualHost>

そこに以下を追記します。

NameVirtualHost *:443
<VirtualHost *:443>
    ServerName develop.jp
    DocumentRoot /var/www/html/
    ErrorLog logs/ssl_error_test_log
    CustomLog logs/ssl_access_test_log combined
    LogLevel warn
    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
    <Directory /var/www/html/>
        AllowOverRide ALL
    </Directory>
</VirtualHost>

追記したら、httpdを再起動します。

[vagrant@localhost ~]$ sudo service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

これで「https://develop.jp/」に接続できるはずです。
Chromeアクセスした時に「このサイトのセキュリティ・・・」って出てくれば正常に完了しています。
今回は、オレオレ証明書をchefで一発作成って感じで行いました。
この一発コマンドはchef以外でも使えるので重宝しそうです!

次回は、phpmyadminのセットアップなど、もっと開発環境の充実をはかりたいと思います。

使用環境