Skip to content

Commit

Permalink
Use htauth lib to manage htpasswd files.
Browse files Browse the repository at this point in the history
Waiting for copiousfreetime/htauth#1 to be merged
  • Loading branch information
guilhem committed Mar 7, 2014
1 parent 3a411cf commit 4d5caed
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ platforms:

suites:
- name: default
run_list: ["recipe[htpasswd]"]
run_list:
- "recipe[htpasswd]"
- "recipe[htpasswd_test]"
attributes: {}
4 changes: 4 additions & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
site :opscode

metadata

group :integration do
cookbook 'htpasswd_test', :path => './test/cookbooks/htpasswd_test'
end
19 changes: 16 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
source 'https://rubygems.org'

gem 'berkshelf'
gem 'test-kitchen', :group => :integration
gem 'kitchen-vagrant', :group => :integration
group :test, :development, :integration do
gem 'rake'
gem 'chef', '>= 10.12.0'
end

group :test, :integration do
gem 'chefspec', '~> 3.0'
gem 'foodcritic', '~> 3.0'
gem 'rubocop', '~> 0.16'
end

group :integration do
gem 'berkshelf', '~> 2.0'
gem 'test-kitchen', '~> 1.1'
gem 'kitchen-vagrant', '~> 0.14'
end
84 changes: 66 additions & 18 deletions providers/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,94 @@
# limitations under the License.
#

def initialize(*args)
super
@action = :add
end
use_inline_resources if defined?(use_inline_resources)

include Chef::DSL::IncludeRecipe

action :add do
if ::File.exists?(new_resource.file)
add
if user_set?
Chef::Log.info "#{ @new_resource } already exists - nothing to do."
else
create
converge_by("Create #{ @new_resource }") do
if ::File.exists?(new_resource.file)
add
else
create
end
end
end
end

action :overwrite do
create
converge_by("Create #{ @new_resource }") do
create
end
end

action :delete do
delete
if user_exists?
converge_by("Delete #{ @new_resource }") do
delete
end
else
Chef::Log.info "#{ @current_resource } doesn't exist - can't delete."
end
end

def load_current_resource
include_recipe "htpasswd"

require 'htauth'
end

private

def user_entry
HTAuth::PasswdFile.new(new_resource.file).fetch(new_resource.user)
rescue
nil
end

def user_exists?
!user_entry.nil?
end

def user_set?
user_entry.authenticated?(new_resource.password) unless user_entry.nil?
end

# cmd = "htpasswd -v #{file} #{user} #{password}"
# return Mixlib::ShellOut.new(cmd).run_command.exitstatus == 0
# end

def create
execute "create htpasswd" do
command "htpasswd -c -b #{new_resource.file} #{new_resource.user} #{new_resource.password}"
ruby_block "Creating htpasswd file #{ new_resource.file }" do
block do
pf = HTAuth::PasswdFile.new(new_resource.file, HTAuth::File::CREATE)
pf.add(new_resource.user, new_resource.password)
pf.save!
end
end
new_resource.updated_by_last_action(true)
end

def add
execute "add to htpasswd" do
command "htpasswd -b #{new_resource.file} #{new_resource.user} #{new_resource.password}"
ruby_block "Adding #{new_resource.user} to htpasswd file #{ new_resource.file }" do
block do
pf = HTAuth::PasswdFile.new(new_resource.file)
pf.add_or_update(new_resource.user, new_resource.password)
pf.save!
end
only_if { ::File.exists?(new_resource.file) }
end
new_resource.updated_by_last_action(true)
end

def delete
execute "delete from htpasswd" do
command "htpasswd -D #{new_resource.file} #{new_resource.user}"
ruby_block "Delete #{new_resource.user} to htpasswd file #{ new_resource.file }" do
block do
pf = HTAuth::PasswdFile.new(new_resource.file)
pf.delete(new_resource.user)
pf.save!
end
only_if { ::File.exists?(new_resource.file) }
end
new_resource.updated_by_last_action(true)
end
2 changes: 2 additions & 0 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# limitations under the License.
#

chef_gem "htauth"

unless which("htpasswd")
include_recipe "htpasswd::build-in"
end
6 changes: 2 additions & 4 deletions resources/default.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
actions :add, :overwrite, :delete
default_action :add

attribute :file, :kind_of => String, :name_attribute => true
attribute :user, :kind_of => String, :required => true
attribute :password, :kind_of => String, :required => true

def initialize(*args)
super
@action = :add
end
attr_accessor :exists
2 changes: 2 additions & 0 deletions test/cookbooks/htpasswd_test/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name 'htpasswd_test'
version '0.1.0'
22 changes: 22 additions & 0 deletions test/cookbooks/htpasswd_test/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
file = '/tmp/htpass_test'

htpasswd file do
user 'foo'
password 'bar'
end

htpasswd file do
user 'john'
password 'do'
action :overwrite
end

htpasswd file do
user 'admin'
password 'admin'
end

htpasswd file do
user 'admin'
action :delete
end
5 changes: 0 additions & 5 deletions test/support/Gemfile

This file was deleted.

0 comments on commit 4d5caed

Please sign in to comment.