forked from mdespuits/capistrano-recipes
-
Notifications
You must be signed in to change notification settings - Fork 3
/
htpasswd.rb
62 lines (48 loc) · 1.61 KB
/
htpasswd.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
namespace :setup do
desc <<-DESC
Creates and installs htpasswd file for basic auth.
User data are read from :users databag.
Configuration
-------------
- :htpasswd_file - - :htpasswd_file - PATH to basic auth htpasswd file
can be used both by Apache and Nginx.
- :basic_auth_users - list of :users databag ID's (Array)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
recipe: :htpasswd
set :enable_basic_auth, true
set :basic_auth_users, [:jon, :jim]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- databag format:
- "id", "name"and either "htpasswd" or "crypted_htpasswd" are
required.
- Either plain text of crypted password can be specified
crypted_htpasswd takes precedence before plain text.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
"id" : "jon",
"name" : "jon",
"comment" : "Basic Auth user Jon Doe",
"home" : "",
"htpasswd" : "plaintext_secret"
"crypted_htpasswd" : "$apr1$Wzfn/GtZ$Dc3CxM(llz0MR5YZhG9FDQD/"
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Source #{ path_to __FILE__}
DESC
task :htpasswd, roles: :web do
unless fetch(:enable_basic_auth, false)
logger.info "Basic auth usage not configured"
else
run "cat /dev/null > #{htpasswd_file}"
basic_auth_users.each do |user|
user = get_data_bag :users, user
cmd = if user.has_key? "crypted_htpasswd"
"echo #{user['name']}:#{user['crypted_htpasswd']} >> #{htpasswd_file}"
else
"htpasswd -mb #{htpasswd_file} #{user['name']} #{user['htpasswd']}"
end
run cmd
end
end
end
end