ã¢ã«ãã«chefæ¥è¨ï¼æ¥ç®ãdata bagã«ã¤ã㦠/ ã¾ãã¯ã¦ã¼ã¶ç®¡çã¯ãã¯ããã¯ãªã©
2日目ã®ç¶ãã
ã³ã³ã»ããã¯こちらããåç
§ä¸ããã
ï¼æ¥ç®ã®ç®æ¨
- ã¦ã¼ã¶ç®¡ç(data bag)
- ã¦ã¼ã¶ä½æ
- bash_profile管ç
- sudo
- ããã®æ®µé㧠ec2-userã®sudo権éãå¥å¥ªããæ°ã¦ã¼ã¶ã«sudo権éãä»ä¸
- security_limit
ï¼æ¥ç®ãå§ããåã«ï¼data bag
ã¦ã¼ã¶æ
å ±ãªã©ãã¯ãã¯ããã¯ãè·¨ãã°ãã¼ãã«ãªå¤ã
cookbookã«ãã¡ãã¡æ¸ãã®ã¯å¾çã§ã¯ããã¾ããã
ããã«çã§ç½®ãã¦ããã®ãæ°ãå¼ãã¾ããã
ãããªãè¦æã«ãçãããããã«ãdata bagãã¨ããä»çµã¿ãããã¾ãã
databagãä½æãã¦ããã¨ãè¤æ°ã®ã¯ãã¯ããã¯ã«ã¾ããã£ã¦ããå
±éã®å¤æ°ãªã©ãä¿åãã¦ãããã¨ãã§ãã¾ãã
ã·ã¼ã¯ã¬ãããã¼ãä½æãã
ã¾ãã¯data bagãæå·/è¤åããããã®ã·ã¼ã¯ã¬ãããã¼ãä½æãã¾ãããã
以ä¸ã®ã³ãã³ãã§data bagç¨ã®éµãã¡ã¤ã«ãä½ã£ã¦ããã¾ãã
openssl rand -base64 512 > data_bag_key # ãunable to write 'random state'ãã¨ããã¨ã©ã¼ãåºãå ´åã¯sudoå®è¡ãã¾ããã
éµãå ±éãã£ã¬ã¯ããªã«ç½®ã
chef-repoæ¯ã«éµãä½ã£ã¦ãããã®ã§ãããæ¯åä½ãã®ãé¢åãªã®ã§
ä»åã¯å
±éã§åç
§ã§ããHOMEãã£ã¬ã¯ããªä»¥ä¸ã«ç½®ãã¦ãã¾ãã¾ãããã
mv data_bag_key ~/.chef/data_bag_key # knife.rbã«ãencrypted_data_bag_secretãã追å vi <chef-repo>/.chef/knife.rb ... encrypted_data_bag_secret "#{ENV['HOME']}/.chef/data_bag_key" ...
data bagãä½æï¼ç·¨éãã
knife soloã³ãã³ãã§data bagãæ°è¦ä½æãã¾ãã
ãã®æãEDITORç°å¢å¤æ°ããäºãè¨å®ãã¦ããå¿
è¦ãããã®ã§
æªè¨å®ã®å ´å㯠.bashrc ãªã©ã«è¿½è¨ãã¦ããã¾ãããã
vi .bashrc ... " 以ä¸ã追å export EDITOR=vim ... # databagã®ä½æ # ãdatabags/users/alpaca3.jsonããä½ããã knife solo data bag create users alpaca3 # ç·¨éããå ´åã¯edit knife solo data bag edit users alpaca3
ãã®æã®æ³¨æç¹ã¨ãã¦
databags/users/alpaca3.jsonãç´æ¥ç·¨éãããã¨ã¯ã§ãã¾ããã*1
ãã¼ã¿ãç·¨éãããå ´å㯠knife solo data bag edit ã³ãã³ãããè¡ãå¿
è¦ãããã¾ãã
ã¦ã¼ã¶ä½æ
ä»åã¯å®å
¨ãªãªã¸ãã«ã®ãuser-originãã¯ãã¯ããã¯ãä½ãã¾ãã
ãããã®åã«ã¦ã¼ã¶ã®data bagãä½ã£ã¦ããã¾ãããã
ãalpaca3ãã¨ããã¦ã¼ã¶ãä½ãã¾ãã
create databag
knife solo data bag create users alpaca3
/databags/users/alpaca3.json ã®ä¸èº«
以ä¸ã®ããã«ç·¨éãã¾ãããã
{ "id": "alpaca3", "name": "alpaca3", "password": "$1$xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "ssh_key": "ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxx" }
passwordã«ã¤ãã¦
chefã«è¨è¿°ãããã¹ã¯ã¼ãæååã¯shadow passwordã«ããå¿
è¦ãããããã§ãã
以ä¸ã®ã³ãã³ãã§æ¨æºåºåãããæååãpasswordã®æ¬ã«è¨å®ãã¾ãããã
openssl passwd -1 > input > verify input $1$xxxxxxxxxxxxxxxxxxxxxxxxxxxxx <- ããï¼
â»åèããã¥ã¡ã³ã
http://docs.opscode.com/resource_user.html#password-shadow-hash
ssh_keyã«ã¤ãã¦
ä»åã¯ec2-userã®éµãæåãã¾ãããã
ä¸åº¦ ec2-userã§ãã°ã¤ã³ãã以ä¸ã®ã³ãã³ãã§è¡¨ç¤ºãããæååããssh_keyãã«ãã¾ãã*2
cat ~/.ssh/authorized_keys
/site_cookbooks/user-origin/recipes/default.rb ãæ¸ã
ã§ã¯ãã¬ã·ããæ¸ãã¦ããã¾ãã
å°ãé·ããªããããªã®ã§å°åºãããªãã説æãã¾ãã
# æå·åããããã¼ã¿ããã°ã®æ å ±ãåå¾ user = Chef::EncryptedDataBagItem.load("users", 'alpaca3') user_name = user['name'] password = user['password'] ssh_key = user['ssh_key'] home = "/home/#{user_name}" # ãalpaca3ãã¦ã¼ã¶ã®ä½æ user user_name do password password home home shell "/bin/bash" supports :manage_home => true # ãã¼ã ãã£ã¬ã¯ããªã管çãã end # ãalpaca3ããwheelã°ã«ã¼ãã«è¿½å ãã group "wheel" do action [:modify] members [user_name] append true end
ã¾ãã¯ã¦ã¼ã¶ã®ä½æã§ãã
ï¼è¡ãã® Chef::EncryptedDataBagItem.load() ãç¨ã㦠databagã®ãã¼ã¿ããã¼ããã¦ãã¾ãã
ãã®æ
å ±ãç¨ãã¦ãalpaca3ãã¦ã¼ã¶ãä½æãã¾ãã
ãã®å¾ã«ãalpaca3ãã¦ã¼ã¶ãwheelã°ã«ã¼ãã«è¿½å ãã¦ãã¾ãã
次ã«sshç¨ã®ãã£ã¬ã¯ããªãkeyãè¨å®ãã¾ãã
ãdirectoryããªã½ã¼ã¹ã使ã£ã¦ãã£ã¬ã¯ããªãä½æããå¾ã
ãfileããªã½ã¼ã¹ã使ã£ã¦ãã¡ã¤ã«ãã¢ãããã¼ããã¦ãã¾ãã
# .sshãã£ã¬ã¯ããªãä½ãã¾ã directory "#{home}/.ssh" do owner user_name group user_name end # authorized_keysãã¡ã¤ã«ãä½ãã¾ã authorized_keys_file ="#{home}/.ssh/authorized_keys" file authorized_keys_file do owner user_name mode 0600 content "#{ssh_key} #{user_name}" # ãã¡ã¤ã«ã®ä¸èº«ãç´æ¥æå® not_if { ::File.exists?("#{authorized_keys_file}")} # æ¢ã«ãã¡ã¤ã«ãåå¨ãã¦ããããªã½ã¼ã¹ãå®è¡ããªã end
次ã«bash_profileã®ç®¡çã§ãã
ã¾ãã¯bash_profileãã©ã®ããã«ç®¡çãããã¨ãã¦ããã説æãããã¨æãã¾ãã
<home> âââ .bash_profile # .bash_profileã.bash_profile_inc以ä¸ã®ãã¡ã¤ã«ãèªãè¨å®ãå ¥ãã¦ãã âââ .bash_profile_inc # profile includeç¨ãã£ã¬ã¯ã㪠âââ base_profile.sh # å ±éprofile âââ ... # ããã«ã¦ã§ã¢æ¯ã«bash_profileãç¨æãã
ä»ã®æ§ã
ãªãªã½ã¼ã¹ã .bash_profile_inc 以ä¸ã«
åã
ã®profileãç½®ããããªéç¨ã§ãã
ãããããã¨ã«ãã£ã¦ã.bash_profileèªä½ãããããªãã¦ã
ãªã½ã¼ã¹æ¯ã®æ§ã
ãªprofileã管çãããã¨ãã¦ãã¾ãã
ç¶ãã®ã¬ã·ãã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
# 1. bash_profile includeç¨ã®ãã£ã¬ã¯ããªãä½ãã¾ã directory "#{home}/.bash_profile_inc" do owner user_name group user_name end # 2.ãbase_profile.shï¼éçãã¡ã¤ã«ï¼ããbash_profile includeç¨ã®ãã£ã¬ã¯ããªã«é ç½®ãã¾ã cookbook_file "#{home}/.bash_profile_inc/base_profile.sh" do source "base_profile.sh" mode 0644 owner user_name group user_name end # 3. bash_profileæ¬ä½ã«ã.bash_profile_incãã£ã¬ã¯ããªä»¥ä¸ã®ãã¡ã¤ã«ãèªã¿è¾¼ãããããªå¦çã追å ãã¦ãã¾ã script "include bash_profile" do environment 'HOME' => home user user_name group user_name interpreter "bash" not_if "grep -q 'bash_profile_include' #{home}/.bash_profile" flags "-e" code <<-"EOH" cat << EOF >> #{home}/.bash_profile 2>&1 # bash_profile_include for file in \\`find ~/.bash_profile_inc -type f\\`; do source \\$file done EOF EOH end
ã¾ãæ°ãããªã½ã¼ã¹ãåºã¦ããã®ã§ç°¡åã«èª¬æãã
2. ã®ãcookbook_fileãã¯filesãã£ã¬ã¯ããªä»¥ä¸ã«ç½®ãããéçãã¡ã¤ã«ããã®ã¾ã¾ãµã¼ãã«è»¢éãã¾ãã
ã¡ãªã¿ã« 2. ã§ä½¿ç¨ãã¦ããéçãã¡ã¤ã«ã®ä¸èº«ã¯
ç¾æ®µéã§ä»¥ä¸ã®ãããªå
容ã§ãã
vi files/default/base_profile.sh # ä»å¾è²ã£ã¦ããã¯ã export LANG="ja_JP.UTF-8" alias sl='ls' alias ll='ls -l'
3. ã®ãscriptãã¯ãcodeãã«æ¸ãããå
容ããµã¼ãä¸ã§å®è¡ãã¾ãã
3. ã§è¡ãªã£ã¦ããå¦çã¯æ¢åã®.bash_profileã«è¿½è¨ãè¡ãªã£ã¦ããã®ã§
éè¤ãã¦å®è¡ãããããã¾ããã
ãã®ãã not_if 㧠ãbash_profile_includeãã¨ããæååãåå¨ããªãæã ãå®è¡ããããã«ãã¦ãã¾ã*3ã
cooking
ã§ã¯nodes jsonã«è¿½å ãã¦cookãã¾ãããã
vi nodes/ec2-chef-repo.json { "run_list":[ "recipe[selinux::disabled]", "recipe[openssh]", "recipe[ntp]", "recipe[sysctl]", "recipe[user-origin]" ] } # cook knife solo cook ec2-chef-test
ãã¾ããã£ãã§ããããï¼
ä»åã¯å©ç¨ãã¾ããã§ããããDataBagã®å
容ããã®ã¾ã¾ã¦ã¼ã¶ç»é²ã«ä½¿ããããªã¬ã·ããããããã§ãã
æ°ã«ãªãæ¹ã¯è©¦ãã¦ã¿ã¦ã¯ãããã§ããããã
https://github.com/opscode-cookbooks/users
sudo
sudo管çããã¾ãã
ããã©ã«ãã¦ã¼ã¶ã§ãã ec2-user ã«ç®¡çè
権éãæãããã®ã¯ã¡ãã£ã¨å«ãªã®ã§
ec2-userã®sudo権éãå¥å¥ªãã¦alpaca3ã¦ã¼ã¶ã«sudo権éãä»ãããã¨æãã¾ãã
sudoã¯ãã¯ããã¯ã¯opscodeã®ããã¦ã³ãã¼ããã¾ãã
# Berksfileã«ä»¥ä¸ã追å ãberks install ãå¿ããã«ã cookbook 'sudo'
/site_cookbooks/sudo/attributes/default.rb ãæ¸ã
usersã¯alpaca3ã®ã¿ã«ãã¦ããã¾ãã
default['authorization']['sudo']['groups'] = [] default['authorization']['sudo']['users'] = ['alpaca3'] # å¿é ã§ããã°æåã ãec2-userãæ®ããã¾ã¾ã«ãã¦ããã¦ãè¯ãã§ã #default['authorization']['sudo']['users'] = ['ec2-user', 'alpaca3'] default['authorization']['sudo']['passwordless'] = true default['authorization']['sudo']['include_sudoers_d'] = false default['authorization']['sudo']['agent_forwarding'] = false default['authorization']['sudo']['sudoers_defaults'] = ['!lecture,tty_tickets,!fqdn']
cooking
ã§ã¯nodes jsonã«ã¬ã·ãã追å ãã¦cookãã¾ãããã
vi nodes/ec2-chef-repo.json # sudoè¿½è¨ ... # cook knife solo cook ec2-chef-test
ãã®ã¬ã·ããæåãããã¨ã¯ ec2-userã§ã®sudoãã§ããªããªãã¾ãã
ããªãã¡knife soloã®å®è¡ãã§ããªããªãã¾ãã
ssh/configã§ã¦ã¼ã¶ã®æå®ãalpaca3ãã«å¤ãã¦ããã¾ãããã
# ssh configè¨å® vi .ssh/config Host ec2-chef-test HostName ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com IdentityFile /path/to/private.pem User alpaca3 Port 22
Caution!!!!
sudoã¯ãã¯ããã¯ã¯ããã©ã«ãã®ã¾ã¾ï¼site-cookbooksãç¨æããªãã¾ã¾ï¼å®è¡ãã¦ãã¾ãã¨
誰ãsudoã§ããªããªã£ã¦ãã¾ãã¾ãï¼*4
ã注æä¸ããã
security_limits
security_limitsããªãªã¸ãã«ã§ä½æãã¦ã¿ã¾ãã
åºæ¥ä¸ãããã¡ã¤ã«ã¯ãããªæãã
security_limits âââ attributes âãããâââ default.rb âââ recipes âãããâââ default.rb âââ templates âââ default âââ limits.conf.erb
/site_cookbooks/security_limits/attributes/default.rb
# è¨å®ãããã¦ã¼ã¶é å default['security_limits']['users'] = ['alpaca3', 'nobody'] # soft / hard limitã®è¨å® default['security_limits']['soft'] = 10240 default['security_limits']['hard'] = 10240
/site_cookbooks/security_limits/recipes/default.rb
# ãã³ãã¬ã¼ãèªã¿è¾¼ã template '/etc/security/limits.conf' do source "limits.conf.erb" mode "0644" variables( :users => node['security_limits']['users'], :hard_limit => node['security_limits']['hard'], :soft_limit => node['security_limits']['soft'] ) end
/site_cookbooks/security_limits/templates/default/limits.conf.erb
# Dynamically generated file dropped off by Chef! <% @users.each do |user| %> <%= user %> hard nofile <%= @hard_limit %> <%= user %> soft nofile <%= @soft_limit %> <% end %>
cooking
ã§ã¯nodes jsonã«ã¬ã·ãã追å ãã¦cookãã¾ãããã
vi nodes/ec2-chef-repo.json # security_limitsè¿½è¨ ... # cook knife solo cook ec2-chef-test
ãã¾ãããã¨ããµã¼ãä¸ã«ä»¥ä¸ã®ãããªãã¡ã¤ã«ãä½æããã¦ããã¯ãã§ãã
$ cat /etc/security/limits.conf # Dynamically generated file dropped off by Chef! alpaca3 hard nofile 10240 alpaca3 soft nofile 10240 nobody hard nofile 10240 nobody soft nofile 10240
è£è¶³
ä»åãknife soloãå®è¡ããã¦ã¼ã¶ãchefã§è¨å®/ä½æãã¦ãã¾ããã
ããã«é¢ãã¦ã¯chef管çã ã¨å¾®å¦ããªãã¨æã£ãããã¦ãã¾ãã*5
ä»åã¯ããã¦chef管çã§è¡ãã¾ãããã
ãã®ãããã¯ã±ã¼ã¹ãã¤ã±ã¼ã¹ã§éç¨ãå¤ãã¦ãè¯ãããããã¾ãããã
次åã¢ã¸ã§ã³ã
å ±éã§ä½¿ããããªããã±ã¼ã¸ãããã«ã¦ã§ã¢ãã¤ã³ã¹ãã¼ã«ãã¦ã¿ããã¨æãã¾ãã
*1:ç´æ¥è¡¨ç¤ºãã¦ãencryptããããã¼ã¿ã表示ããã¾ã
*2:ããã¦databagã使ç¨ãã¦ãã¾ããããã°ã¤ã³ããããã ãã§ããã° /home/ec2-user/.ssh/authorized_keys ã /home/alpaca3/.ssh/authorized_keys ã«ã³ãã¼ããã ãã§ãè¯ãã§ãã
*3:ãã¼ãå¾®å¦
*4:ããã©ã«ã㯠['sudo']['users']=[] ã®ãã
*5:äºãä½ã£ã¦ãããã»ããããã®ã§ã¯ãã¨ããã話