Last active
December 22, 2015 13:49
-
-
Save sawanoboly/6481669 to your computer and use it in GitHub Desktop.
実践LWRP、HTTP認証用ファイル(htpasswd,htdigest)をChefのリソースとして管理する part.2 of 3 ref: http://qiita.com/sawanoboly/items/79c7cdf782d64980677d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webrick' | |
def whyrun_supported? | |
true | |
end | |
action :create do | |
if @current_resource.crypted_passwd | |
Chef::Log.warn "====== Found http_auth user #{@new_resource.user}" | |
salt = @current_resource.crypted_passwd[0..1] | |
if @current_resource.crypted_passwd == @new_resource.password.crypt(salt) | |
Chef::Log.warn "====== http_auth user #{@new_resource.user} was not modified. (up to date)" | |
else | |
converge_by("====== Update http_auth user #{@new_resource.user}") do | |
Chef::Log.warn "====== Update http_auth user #{@new_resource.user}" | |
update_user! | |
end | |
end | |
else | |
converge_by("====== Create http_auth user #{@new_resource.user}") do | |
Chef::Log.warn "====== Create http_auth user #{@new_resource.user}" | |
update_user! | |
end | |
end | |
end | |
action :delete do | |
unless @current_resource.crypted_passwd | |
Chef::Log.warn "====== http_auth user #{@new_resource.user} was not found. Nothing to do. (up to date)" | |
else | |
converge_by("====== Delete http_auth user #{@new_resource.user}") do | |
Chef::Log.warn "====== Delete http_auth user #{@new_resource.user}" | |
delete_user! | |
end | |
end | |
end | |
def update_user! | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.path) | |
htpasswd.set_passwd nil, @new_resource.user, @new_resource.password | |
htpasswd.flush | |
end | |
def delete_user! | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.path) | |
htpasswd.delete_passwd nil, @new_resource.user | |
htpasswd.flush | |
end | |
def load_current_resource | |
@current_resource = Chef::Resource::HttpsvAuthBasic.new(@new_resource.name) | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.path) | |
@current_resource.crypted_passwd = htpasswd.get_passwd nil, @new_resource.user, true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
actions :create | |
default_action :create | |
attribute :user, :kind_of => String, :required => true | |
attribute :password, :kind_of => String | |
attribute :path, :kind_of => String, :required => true | |
attr_accessor :crypted_passwd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webrick' | |
def whyrun_supported? | |
true | |
end | |
action :create do | |
if @current_resource.crypted_passwd | |
Chef::Log.warn "====== Found http_auth user #{@new_resource.user}" | |
salt = @current_resource.crypted_passwd[0..1] | |
if @current_resource.crypted_passwd == @new_resource.password.crypt(salt) | |
Chef::Log.warn "====== http_auth user #{@new_resource.user} was not modified. (up to date)" | |
else | |
converge_by("====== Update http_auth user #{@new_resource.user}") do | |
Chef::Log.warn "====== Update http_auth user #{@new_resource.user}" | |
flush_userdb! | |
end | |
end | |
else | |
converge_by("====== Create http_auth user #{@new_resource.user}") do | |
Chef::Log.warn "====== Create http_auth user #{@new_resource.user}" | |
flush_userdb! | |
end | |
end | |
end | |
def flush_userdb! | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.path) | |
htpasswd.set_passwd nil, @new_resource.user, @new_resource.password | |
htpasswd.flush | |
end | |
def load_current_resource | |
@current_resource = Chef::Resource::HttpsvAuthBasic.new(@new_resource.name) | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.path) | |
@current_resource.crypted_passwd = htpasswd.get_passwd nil, @new_resource.user, true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webrick' | |
def whyrun_supported? | |
true | |
end | |
action :create do | |
converge_by("====== Create http_auth user #{@new_resource.user}") do | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.path) | |
htpasswd.set_passwd nil, @new_resource.user, @new_resource.password | |
htpasswd.flush | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -- snip -- | |
def update_user! | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.path) | |
htpasswd.set_passwd nil, @new_resource.user, @new_resource.password | |
htpasswd.flush | |
fix_permission! | |
end | |
def delete_user! | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.path) | |
htpasswd.delete_passwd nil, @new_resource.user | |
htpasswd.flush | |
fix_permission! | |
end | |
def fix_permission! | |
FileUtils.chmod(@new_resource.filemode.to_i, @new_resource.path) | |
end | |
# -- snip -- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ chef-solo -c solo.rb -o 'httpsv::sample06' | |
Starting Chef Client, version 11.6.0 | |
[2013-09-07T23:46:16+09:00] WARN: Run List override has been provided. | |
[2013-09-07T23:46:16+09:00] WARN: Original Run List: [] | |
[2013-09-07T23:46:16+09:00] WARN: Overridden Run List: [recipe[httpsv::sample06]] | |
Compiling Cookbooks... | |
Converging 2 resources | |
Recipe: httpsv::sample06 | |
* httpsv_auth_basic[var/www/site1:hoge1] action delete[2013-09-07T23:46:17+09:00] WARN: ====== http_auth user hoge1 was not found. Nothing to do. (up to date) | |
(up to date) | |
* httpsv_auth_basic[var/www/site1:hoge2] action delete[2013-09-07T23:46:17+09:00] WARN: ====== http_auth user hoge2 was not found. Nothing to do. (up to date) | |
(up to date) | |
Chef Client finished, 0 resources updated |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base_dir = ENV['PWD'] | |
httpsv_auth_basic 'var/www/site1' do | |
user 'hoge1' | |
path File.join(base_dir, self.name, '.htpasswd') | |
name [self.name, self.user].join(':') | |
password 'password1' | |
end | |
httpsv_auth_basic 'var/www/site1' do | |
user 'hoge2' | |
path File.join(base_dir, self.name, '.htpasswd') | |
name [self.name, self.user].join(':') | |
password rand.to_s | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base_dir = ENV['PWD'] | |
httpsv_auth_basic 'var/www/site1' do | |
user 'hoge1' | |
path File.join(base_dir, self.name, '.htpasswd') | |
name [self.name, self.user].join(':') | |
password 'password1' | |
filemode 0640 | |
end | |
httpsv_auth_basic 'var/www/site1' do | |
user 'hoge2' | |
path File.join(base_dir, self.name, '.htpasswd') | |
name [self.name, self.user].join(':') | |
password rand.to_s | |
filemode 0640 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment