fbpx

oc-idを使ってみる #getchef

この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。

Chef Server 12から、Chef社のOAuth2サービスであるid.chef.ioで使われているoc-idが同梱されています。oc-idは、プライベートのSupermarketChef Analyticsのバージョン1.1以降を利用する際に必要となります。

本稿ではまずプライベートのoc-idサービスを設定し、次にプライベートSupermarketとChef Analyticsに連携させてみます。

前提条件

ここでは以下のホストを使用します。すべて Ubuntu 14.04 LTS です。

  • chef-server.example.jp (192.168.122.101) : Chef Server 12.0.7
  • ws.example.jp (192.168.122.200) : Workstation (Chef Client 12.2.1)
  • supermarket.example.jp (192.168.122.122): Supermarketデプロイ先
  • chef-analytics.example.jp (192.168.122.121) : Chef Analyticsインストール先

なお、お互いに名前解決ができている必要があります。OAuth2では連携のためにホスト名が重要となるからです。

プライベートSupermarket

[和訳] プライベートSupermarketサーバを立ててみように従います。

Chef Serverでoc-id設定を行う

Chef Server 12.0.7をスタンドアローンでインストールし、設定を完了しているものとします。ここでは既に利用しているChef Serverに、oc-id設定を追加します。

Chef ServerにSSHでログインし、/etc/opscode/chef-server.rbに次を追加します。ここではSupermarketのみの設定です。リダイレクトURIはSupermarketのホスト名を指定します。


ubuntu@chef-server:~$ sudo vi /etc/opscode/chef-server.rb
oc_id['applications'] = {
'supermarket' => {
'redirect_uri' => 'https://supermarket.example.jp/auth/chef_oauth2/callback'
}
}

chef-server-ctl reconfigureを実行し、設定を反映します。


ubuntu@chef-server:~$ sudo chef-server-ctl reconfigure
:
:
:
Recipe: private-chef::oc_id
:
:
:
Running handlers:
Running handlers complete
Chef Client finished, 35/390 resources updated in 22.738009376 seconds
opscode Reconfigured!
ubuntu@chef-server:~$

/etc/opscode/oc-id-applications/supermarket.jsonファイルが生成されるので、中の値をどこかにメモしておきます。これらの値は後で使います。


ubuntu@chef-server:~$ sudo cat /etc/opscode/oc-id-applications/supermarket.json
{
"name": "supermarket",
"uid": "b2b762d6a5d6fa5b4376f65f95ad516d5c2c15312f0554894705023db72ab1a5",
"secret": "93fb8c71b38debef397f4141bcf03657f3e600cac1117362fd3005497f6f881b",
"redirect_uri": "https://supermarket.example.jp/auth/chef_oauth2/callback"
}
ubuntu@chef-server:~$

WorkstationからSupermarketをデプロイする

Chef Serverからログアウトし、Workstationで作業を行います。まず、chef-repoにSupermarketのomnibusインストールを行うためのCookbookを配置します。


ubuntu@ws:~$ cd chef-repo/cookbooks/
ubuntu@ws:~/chef-repo/cookbooks$ git clone https://github.com/irvingpop/supermarket-omnibus-cookbook.git supermarket-omnibus-cookbook
Cloning into 'supermarket-omnibus-cookbook'...
remote: Counting objects: 77, done.
remote: Total 77 (delta 0), reused 0 (delta 0), pack-reused 77
Unpacking objects: 100% (77/77), done.
Checking connectivity... done.
ubuntu@ws:~/chef-repo/cookbooks$

依存関係にあるchef-server-ingredient Cookbookとpackagecloud Cookbookを取得します。


ubuntu@ws:~/chef-repo$ knife cookbook site install chef-server-ingredient
Installing chef-server-ingredient to /home/ubuntu/chef-repo/cookbooks
Checking out the master branch.
Creating pristine copy branch chef-vendor-chef-server-ingredient
Downloading chef-server-ingredient from the cookbooks site at version 0.3.2 to /home/ubuntu/chef-repo/cookbooks/chef-server-ingredient.tar.gz
:
:
:
Cookbook chef-server-ingredient version 0.3.2 successfully installed
Installing packagecloud to /home/ubuntu/chef-repo/cookbooks
Checking out the master branch.
Creating pristine copy branch chef-vendor-packagecloud
Downloading packagecloud from the cookbooks site at version 0.0.17 to /home/ubuntu/chef-repo/cookbooks/packagecloud.tar.gz
:
:
:
Cookbook packagecloud version 0.0.17 successfully installed
ubuntu@ws:~/chef-repo$

取得した 3つのCookbookをChef Serverにアップロードします。


ubuntu@ws:~/chef-repo$ knife upload cookbooks/supermarket-omnibus-cookbook/ cookbooks/chef-server-ingredient/ cookbooks/packagecloud/
Created cookbooks/supermarket-omnibus-cookbook
Created cookbooks/chef-server-ingredient
Created cookbooks/packagecloud
ubuntu@ws:~/chef-repo$

SupermarketをデプロイするためのRoleを作成します。ここで先程メモしておいた値を使います。uidの値をchef_oauth2_app_idに、secretの値をchef_oauth2_secretに設定します。Chef社ブログではchef_server_urlのみを設定していますが、chef_oauth2_urlも追加します。きちんと連携が行われない場合があります。参考: "Sign in with your chef account" redirects to https://id.opscode.com/... instead of local Chef Server


ubuntu@ws:~/chef-repo$ vi roles/supermarket.json
{
"name": "supermarket",
"description": "Private Supermarket role",
"run_list": [ "recipe[supermarket-omnibus-cookbook::default]" ],
"default_attributes": {
"supermarket_omnibus": {
"chef_server_url": "https://chef-server.example.jp",
"chef_oauth2_url": "https://chef-server.example.jp",
"chef_oauth2_app_id": "b2b762d6a5d6fa5b4376f65f95ad516d5c2c15312f0554894705023db72ab1a5",
"chef_oauth2_secret": "93fb8c71b38debef397f4141bcf03657f3e600cac1117362fd3005497f6f881b",
"chef_oauth2_verify_ssl": false
}
}
}
ubuntu@ws:~/chef-repo$

RoleをChef Serverにアップロードします。


ubuntu@ws:~/chef-repo$ knife upload roles/supermarket.json
Created roles/supermarket.json
ubuntu@ws:~/chef-repo$

knife bootstrapコマンドで、Supermarketをデプロイします。


ubuntu@ws:~/chef-repo$ knife bootstrap -N supermarket.example.jp supermarket.example.jp -r 'role[supermarket]' -x ubuntu -P ubuntu --sudo
Creating new client for supermarket.example.jp
Creating new node for supermarket.example.jp
Connecting to supermarket.example.jp
supermarket.example.jp
supermarket.example.jp Starting first Chef Client run...
supermarket.example.jp Starting Chef Client, version 12.3.0
supermarket.example.jp resolving cookbooks for run list: ["supermarket-omnibus-cookbook::default"]
supermarket.example.jp Synchronizing Cookbooks:
supermarket.example.jp - supermarket-omnibus-cookbook
supermarket.example.jp - chef-server-ingredient
supermarket.example.jp - packagecloud
supermarket.example.jp Compiling Cookbooks...
supermarket.example.jp Converging 3 resources
supermarket.example.jp Recipe: supermarket-omnibus-cookbook::default
:
:
:
supermarket.example.jp * execute[supermarket-reconfigure] action run
supermarket.example.jp - execute supermarket-ctl reconfigure
supermarket.example.jp
supermarket.example.jp
supermarket.example.jp Running handlers:
supermarket.example.jp Running handlers complete
supermarket.example.jp Chef Client finished, 10/11 resources updated in 198.307162596 seconds
ubuntu@ws:~/chef-repo$

デプロイ完了しました。

プライベートSupermarketにサインイン

ブラウザでSupermarketのURIを開きます。「Sign Up For a Chef Account」でサインアップします。

010-supermarket-top

と、oc-idによってChef ServerとSupermarketが連携しているため、サインアップ先はChef Serverとなっています。Chef ServerのアカウントでSupermarketが利用できる状態です。ここでChef Serverでアカウントを取得したことにして、次に進みましょう。

なお、ここでChef Serverのサインアップ画面にならない場合、Supermarketのデプロイがうまくいっていない可能性があります。参考: "Sign in with your chef account" redirects to https://id.opscode.com/... instead of local Chef Server

020-chef-server-signup

Supermarketのトップページに戻り、「Sign In With Your Chef Account」でサインインします。

030-supermarket-signin

Chef ServerのアカウントにSupermarketを紐付けてよいか確認画面が表示されるので、「Yes」で承認します。

040-oc-id-auth-required

Supermarketにサインインできました。

050-supermarket-logged-in

以降はChef ServerのアカウントでSupermarketを利用できます。

Supermarketからサインアウトすると、Chef ServerとのアカウントとSupermarketが紐付いたまま、という警告が出ます。一旦連携を解除するには、赤枠の中のリンクをたどります。

060-supermarket-logged-out

Revoke」を選択すると、Supermarketとの連携が解除されます。

070-oc-id-revoking

プライベートSupermarketの利用の詳細については別稿に譲ります。

Chef Analytics

Standalone (version 1.1) | Install Chef Analyticsに従ってスタンドアローンでインストールします。

Chef Analyticsのインストール

chef-analytics.example.jpにログインし、パッケージをインストールします。


ubuntu@chef-analytics:~$ sudo dpkg -i opscode-analytics_1.1.2-1_amd64.deb
以前に未選択のパッケージ opscode-analytics を選択しています。
(データベースを読み込んでいます ... 現在 56008 個のファイルとディレクトリがインストールされています。)
Preparing to unpack opscode-analytics_1.1.2-1_amd64.deb ...
Unpacking opscode-analytics (1.1.2-1) ...
opscode-analytics (1.1.2-1) を設定しています ...
ubuntu@chef-analytics:~$

Chef Serverでoc-id設定を行う

/etc/opscode/chef-server.rbにChef Analytics設定を追加します。さらに、RabbitMQの設定も追加します。Chef AnalyticsはChef Serverと同じRabbitMQに接続するためです。この設定を間違えると後でエラーとなります。


ubuntu@chef-server:~$ sudo vi /etc/opscode/chef-server.rb
oc_id['applications'] = {
'supermarket' => {
'redirect_uri' => 'https://supermarket.example.jp/auth/chef_oauth2/callback'
},
'analytics' => {
'redirect_uri' => 'https://chef-analytics.example.jp/'
}
}
rabbitmq['vip'] = '192.168.122.101'
rabbitmq['node_ip_address'] = '0.0.0.0'

Chef Serverを再設定します。


ubuntu@chef-server:~$ sudo chef-server-ctl reconfigure
:
:
:
Recipe: private-chef::oc_id
:
:
:
Running handlers:
Running handlers complete
Chef Client finished, 36/393 resources updated in 22.955613935 seconds
opscode Reconfigured!
ubuntu@chef-server:~$

Chef Serverを再起動します。


ubuntu@chef-server:~$ sudo chef-server-ctl restart
ok: run: bookshelf: (pid 19693) 1s
ok: run: nginx: (pid 19735) 0s
ok: run: oc_bifrost: (pid 19750) 0s
ok: run: oc_id: (pid 19790) 0s
ok: run: opscode-chef-mover: (pid 19793) 1s
ok: run: opscode-erchef: (pid 19821) 0s
ok: run: opscode-expander: (pid 19901) 1s
ok: run: opscode-expander-reindexer: (pid 19959) 1s
ok: run: opscode-solr4: (pid 19993) 0s
ok: run: postgresql: (pid 20022) 0s
ok: run: rabbitmq: (pid 20024) 0s
ok: run: redis_lb: (pid 20068) 0s
ubuntu@chef-server:~$

マネジメントコンソールを再設定します。


ubuntu@chef-server:~$ sudo opscode-manage-ctl reconfigure
:
:
:
Running handlers:
Running handlers complete
Chef Client finished, 10/71 resources updated in 16.58458282 seconds
opscode-manage Reconfigured!
ubuntu@chef-server:~$

Chef Analyticsの設定を行う

再びchef-analytics.example.jpにログインして設定を続けます。

Chef Serverの/etc/opscode-analyticsディレクトリをまるごとコピーします。


ubuntu@chef-server:~$ ls -la /etc/opscode-analytics
合計 16
drwxrwxr-x 2 opscode opscode 4096 5月 1 17:45 .
drwxr-xr-x 93 root root 4096 5月 1 12:20 ..
-rw------- 1 root root 655 5月 1 17:43 actions-source.json
-rw------- 1 opscode root 1679 4月 9 10:42 webui_priv.pem
ubuntu@chef-server:~$


ubuntu@chef-analytics:~$ ls -al /etc/opscode-analytics/
合計 16
drwxrwxr-x 2 root root 4096 5月 1 17:38 .
drwxr-xr-x 90 root root 4096 5月 1 17:34 ..
-rw-r--r-- 1 root root 656 5月 1 17:48 actions-source.json
-rw-r--r-- 1 root root 1679 5月 1 17:37 webui_priv.pem
ubuntu@chef-analytics:~$

所有者などが合いませんが、後で修正されるのでそのままにしておきます。

設定ファイルを作成します。


ubuntu@chef-analytics:~$ sudo vi /etc/opscode-analytics/opscode-analytics.rb
analytics_fqdn "chef-analytics.example.jp"
topology "standalone"
ubuntu@chef-analytics:~$

設定が正しいか検証します。


ubuntu@chef-analytics:~$ sudo opscode-analytics-ctl preflight-check


[SUCCESS] Preflight check successful!
ubuntu@chef-analytics:~$

もしここでRabbitMQに接続できない場合は次のようなエラーになります。設定を確認してみてください。


ubuntu@chef-analytics:~$ sudo opscode-analytics-ctl preflight-check


[ERROR] Can't connect to RabbitMQ on Enterprise Chef server at 192.168.122.101:5672
[FAILURE] Preflight check failed. Please check the installation instructions at http://docs.opscode.com/install_analytics.html
ubuntu@chef-analytics:~$

Chef Analyticsを再設定します。


ubuntu@chef-analytics:~$ sudo opscode-analytics-ctl reconfigure
:
:
:
Running handlers:
Running handlers complete
Chef Client finished, 280/295 resources updated in 96.093788452 seconds
opscode-analytics Reconfigured!
ubuntu@chef-analytics:~$

Chef Analyticsをテストします。


ubuntu@chef-analytics:~$ sudo opscode-analytics-ctl test
Running with options:

{:config_file=>"/opt/opscode-analytics/embedded/service/analytics-test/config.rb", :smoke_tests_only=>true}

Running tests from the following directory:
/opt/opscode-analytics/embedded/service/analytics-test/spec/integration

Randomized with seed 20487

basic server state check
is running

Finished in 0.01898 seconds
1 example, 0 failures

Randomized with seed 20487


ubuntu@chef-analytics:~$

1つしかテストされていませんが、問題ないようです。

Chef Analyticsのサインイン

ブラウザでChef AnalyticsのURIを開きます。「Start Analytics」でサインインします。

100-analytics-top

Chef ServerのアカウントにChef Analyticsを紐付けてよいか確認画面が表示されるので、「Yes」で承認します。

110-oc-id-auth-required

Chef Analyticsにサインインできました。

120-analytics-signin

Chef Analyticsの利用の詳細については別稿に譲ります。

まとめ

Chef社がOAuth2サービスとして利用しているoc-idについて、プライベートSupermarketとChef Analyticsで実際に試してみました。Chef Serverでアカウント登録するとoc-id連携したサービスに使い回せるので、関連するサービスのアカウント一括管理に非常に便利と言えます。是非試してみてください。

Author

Chef・Docker・Mirantis製品などの技術要素に加えて、会議の進め方・文章の書き方などの業務改善にも取り組んでいます。「Chef活用ガイド」共著のほか、Debian Official Developerもやっています。

Daisuke Higuchiの記事一覧

新規CTA