Skip to content

Commit bd3ab2e

Browse files
committed
Merge pull request #33 from tejaycar/master
and here are the rest of the fixes.
2 parents cc827ec + e137013 commit bd3ab2e

File tree

18 files changed

+308
-81
lines changed

18 files changed

+308
-81
lines changed

.kitchen.yml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
---
2-
driver_plugin: vagrant
2+
driver:
3+
name: vagrant
4+
35
driver_config:
46
require_chef_omnibus: true
7+
box: opscode-ubuntu-12.04
8+
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box
9+
10+
provisioner:
11+
name: chef_zero
12+
require_chef_omnibus: true
13+
chef_omnibus_url: https://www.getchef.com/chef/install.sh
514

615
platforms:
716
- name: ubuntu-12.04
8-
driver_config:
9-
box: opscode-ubuntu-12.04
10-
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box
17+
run_list:
18+
- recipe[apt]
1119

1220
suites:
13-
- name: default
14-
run_list:
15-
- recipe[minitest-handler]
16-
- recipe[ssh_test]
21+
- name: known_hosts
22+
run_list:
23+
- recipe[ssh_test::known_hosts]
24+
attributes:
25+
ssh_test:
26+
known_hosts:
27+
test_entry: 'dummy5 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCe1poC4rUN8CoW+bWfij2KvY6wYhHdZsxbhyfwUVEclCDcjf4W9Xlso3dogVYXIlqdaeB7SqXmczvsOKl51UTE6Or7G7tiffwZoe4z4J1tRX9HCi+5ZOjUeLtvu6764mpMoTpEwFekER4osXosJw29Nu6cLeM+REpyH/RMZwpb1FUEIOptdupepWh7vlvv4fu0dw1ir0t2lHuW/QCmNh9umSsIxGSOJMnwt1ohk4LKYLykHEc3RRsWZS9rPjbNmAerrdbhF8FF5dyMi+rde5dcIApDmWz3hQwIrZdB+fF5oITVXulTsyhoERQyJf/70oWSECzTuO/jRudlx870bC7b'
28+
- name: config
29+
run_list:
30+
- recipe[ssh_test::config]

Berksfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ DEPENDENCIES
66
path: test/cookbooks/ssh_test
77

88
GRAPH
9+
apt (2.6.0)
910
ssh (0.10.0)
1011
ssh_test (0.2.0)
12+
apt (>= 0.0.0)
1113
ssh (>= 0.0.0)

Gemfile.lock

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ GEM
55
ast (2.0.0)
66
astrolabe (1.3.0)
77
parser (>= 2.2.0.pre.3, < 3.0)
8-
berkshelf (3.1.5)
8+
berkshelf (3.2.1)
99
addressable (~> 2.3.4)
1010
berkshelf-api-client (~> 1.2)
1111
buff-config (~> 1.0)
1212
buff-extensions (~> 1.0)
1313
buff-shell_out (~> 0.1)
14-
celluloid (~> 0.16.0.pre)
15-
celluloid-io (~> 0.16.0.pre)
14+
celluloid (~> 0.16.0)
15+
celluloid-io (~> 0.16.1)
16+
cleanroom (~> 1.0)
1617
faraday (~> 0.9.0)
1718
minitar (~> 0.5.4)
1819
octokit (~> 3.0)
1920
retryable (~> 1.3.3)
2021
ridley (~> 4.0)
2122
solve (~> 1.1)
22-
thor (~> 0.18)
23+
thor (~> 0.19)
2324
berkshelf-api-client (1.2.0)
2425
faraday (~> 0.9.0)
2526
buff-config (1.0.1)
@@ -54,9 +55,9 @@ GEM
5455
plist (~> 3.1.0)
5556
pry (~> 0.9)
5657
rest-client (>= 1.0.4, <= 1.6.7)
57-
chef-zero (2.2)
58+
chef-zero (2.2.1)
59+
ffi-yajl (~> 1.1)
5860
hashie (~> 2.0)
59-
json
6061
mixlib-log (~> 1.3)
6162
rack
6263
chefspec (4.1.1)
@@ -65,6 +66,7 @@ GEM
6566
rspec (~> 3.0)
6667
ci_reporter (1.9.3)
6768
builder (>= 2.1.2)
69+
cleanroom (1.0.0)
6870
coderay (1.1.0)
6971
dep-selector-libgecode (1.0.2)
7072
dep_selector (1.0.3)
@@ -78,9 +80,9 @@ GEM
7880
net-ssh
7981
ohai
8082
ffi (1.9.6)
81-
ffi-yajl (1.2.0)
83+
ffi-yajl (1.3.1)
8284
ffi (~> 1.5)
83-
libyajl2 (~> 1.0)
85+
libyajl2 (~> 1.2)
8486
foodcritic (4.0.0)
8587
erubis
8688
gherkin (~> 2.11)
@@ -103,10 +105,10 @@ GEM
103105
test-kitchen (>= 1.0.0)
104106
kitchen-vagrant (0.15.0)
105107
test-kitchen (~> 1.0)
106-
libyajl2 (1.1.0)
108+
libyajl2 (1.2.0)
107109
method_source (0.8.2)
108110
mime-types (1.25.1)
109-
mini_portile (0.6.0)
111+
mini_portile (0.6.1)
110112
minitar (0.5.4)
111113
minitest (4.7.5)
112114
minitest-chef-handler (1.0.3)
@@ -118,7 +120,7 @@ GEM
118120
mixlib-cli (1.5.0)
119121
mixlib-config (2.1.0)
120122
mixlib-log (1.6.0)
121-
mixlib-shellout (1.6.0)
123+
mixlib-shellout (1.6.1)
122124
multi_json (1.10.1)
123125
multipart-post (2.0.0)
124126
net-http-persistent (2.9.4)
@@ -133,10 +135,10 @@ GEM
133135
net-ssh (>= 2.6.5)
134136
net-ssh-gateway (>= 1.2.0)
135137
nio4r (1.0.1)
136-
nokogiri (1.6.3.1)
137-
mini_portile (= 0.6.0)
138-
octokit (3.4.0)
139-
sawyer (~> 0.5.3)
138+
nokogiri (1.6.5)
139+
mini_portile (~> 0.6.0)
140+
octokit (3.7.0)
141+
sawyer (~> 0.6.0, >= 0.5.3)
140142
ohai (7.4.0)
141143
ffi (~> 1.9)
142144
ffi-yajl (~> 1.0)
@@ -148,7 +150,7 @@ GEM
148150
mixlib-shellout (~> 1.2)
149151
systemu (~> 2.6.4)
150152
wmi-lite (~> 1.0)
151-
parser (2.2.0.pre.5)
153+
parser (2.2.0.pre.8)
152154
ast (>= 1.1, < 3.0)
153155
slop (~> 3.4, >= 3.4.5)
154156
plist (3.1.0)
@@ -160,18 +162,18 @@ GEM
160162
slop (~> 3.4)
161163
rack (1.5.2)
162164
rainbow (2.0.0)
163-
rake (10.3.2)
165+
rake (10.4.2)
164166
rest-client (1.6.7)
165167
mime-types (>= 1.16)
166168
retryable (1.3.6)
167-
ridley (4.0.0)
169+
ridley (4.1.1)
168170
addressable
169171
buff-config (~> 1.0)
170172
buff-extensions (~> 1.0)
171173
buff-ignore (~> 1.1)
172174
buff-shell_out (~> 0.1)
173-
celluloid (~> 0.16.0.pre)
174-
celluloid-io (~> 0.16.0.pre)
175+
celluloid (~> 0.16.0)
176+
celluloid-io (~> 0.16.1)
175177
erubis
176178
faraday (~> 0.9.0)
177179
hashie (>= 2.0.2, < 3.0.0)
@@ -193,16 +195,16 @@ GEM
193195
rspec-mocks (3.1.3)
194196
rspec-support (~> 3.1.0)
195197
rspec-support (3.1.2)
196-
rubocop (0.26.1)
198+
rubocop (0.28.0)
197199
astrolabe (~> 1.3)
198-
parser (>= 2.2.0.pre.4, < 3.0)
200+
parser (>= 2.2.0.pre.7, < 3.0)
199201
powerpack (~> 0.0.6)
200202
rainbow (>= 1.99.1, < 3.0)
201203
ruby-progressbar (~> 1.4)
202-
ruby-progressbar (1.6.0)
204+
ruby-progressbar (1.7.0)
203205
rufus-lru (1.0.5)
204206
safe_yaml (1.0.4)
205-
sawyer (0.5.5)
207+
sawyer (0.6.0)
206208
addressable (~> 2.3.5)
207209
faraday (~> 0.8, < 0.10)
208210
semverse (1.2.1)

ROADMAP.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ v0.10.1
1313
==========
1414
* add foodcritic support
1515

16+
v1.0
17+
=========
18+
* Switch to MWRP using poise
19+
* Rename some actions and attributes
20+
* known_hosts.key becomes known_hosts.entry
21+
THIS WILL BREAK stuff
1622

1723
Eventually (PRs are VERY welcome)
1824
===========

libraries/ssh_helpers.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ def user_dir(username)
77
pwent.dir
88
end
99

10+
def user_group(username)
11+
gid = user_gid(username)
12+
return nil unless gid
13+
14+
Etc.getgrgid(gid).name
15+
end
16+
17+
def user_gid(username)
18+
username ? pwent_for(username).gid : nil
19+
end
20+
1021
def pwent_for(uid)
1122
uid.is_a?(Fixnum) ? Etc.getpwuid(uid) : Etc.getpwnam(uid)
1223
end

providers/config.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ def whyrun_supported?
77
end
88

99
action :add do
10-
return if @new_resource.options.eql? @existing_entries[new_resource.name]
11-
@existing_entries[@new_resource.name] = @new_resource.options
10+
unless @new_resource.options.eql? @existing_entries[new_resource.name]
11+
@existing_entries[@new_resource.name] = @new_resource.options
1212

13-
converge_by "Adding {@new_resource.name} to #{@path} with #{new_resource.options.inspect}" do
14-
create_directory
15-
create_file
13+
converge_by "Adding {@new_resource.name} to #{@path} with #{new_resource.options.inspect}" do
14+
create_directory
15+
create_file
16+
end
1617
end
1718
end
1819

@@ -21,11 +22,12 @@ def whyrun_supported?
2122
end
2223

2324
action :remove do
24-
return unless @current_resource.exists?
25-
@existing_entries.delete @new_resource.name
25+
if @current_resource.exists?
26+
@existing_entries.delete @new_resource.name
2627

27-
converge_by "Remove #{@new_resource.name} from #{@path}" do
28-
create_file
28+
converge_by "Remove #{@new_resource.name} from #{@path}" do
29+
create_file
30+
end
2931
end
3032
end
3133

@@ -48,7 +50,7 @@ def create_file
4850

4951
def load_current_resource
5052
@user = new_resource.user || 'root'
51-
@group = new_resource.group || pwent_for(@user).gid
53+
@group = new_resource.group || user_group(@user)
5254
@path = new_resource.path || default_or_user_path(new_resource.user)
5355
@existing_entries = parse_file @path
5456

providers/known_hosts.rb

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,91 @@
11
require 'shellwords'
2+
require 'mixlib/shellout'
3+
24
include Chef::SSH::Helpers
35

6+
use_inline_resources
7+
8+
def whyrun_supported?
9+
true
10+
end
11+
412
action :add do
5-
ssh_user = new_resource.user || 'root'
6-
known_hosts_path = default_or_user_path(node['ssh']['known_hosts_path'], ssh_user)
7-
host, port = new_resource.host.split(':')
8-
# set the port to the default (22) if it wasn't already set
9-
port = new_resource.port unless port
10-
11-
key = new_resource.key
12-
if key.nil?
13-
results = `ssh-keyscan #{new_resource.hashed ? '-H ' : ''} -p #{port.to_i} #{Shellwords.escape(host)}`
14-
Chef::Application.fatal! results.strip if key =~ /getaddrinfo/
15-
key = results.strip
16-
end
13+
unless @current_resource.exists?
14+
directory ::File.basename(new_resource.path) do
15+
action :create
16+
owner new_resource.user if new_resource.user
17+
group new_resource.group if new_resource.group
18+
mode new_resource.user ? 00700 : 00755
19+
end
1720

18-
execute "add known_host entry for #{host}" do
19-
not_if "ssh-keygen -H -F #{Shellwords.escape(host)} -f #{known_hosts_path} | grep 'Host #{host} found'"
20-
command "echo '#{key}' >> #{known_hosts_path}"
21-
user ssh_user
22-
end
21+
file new_resource.path do
22+
action :create
23+
mode new_resource.user ? 00600 : 00644
24+
owner new_resource.user if new_resource.user
25+
group new_resource.group if new_resource.group
26+
end
2327

24-
log "entry_for_#{host}_exists" do
25-
message "An entry for #{host} already exists in #{known_hosts_path}."
26-
level :debug
27-
only_if "ssh-keygen -H -F #{Shellwords.escape(host)} -f #{known_hosts_path} | grep 'Host #{host} found'"
28+
execute "add known_host entry for #{new_resource.host}" do
29+
command "echo '#{new_resource.key}' >> #{new_resource.path}"
30+
user new_resource.user if new_resource.user
31+
umask new_resource.user ? 0077 : 0022
32+
end
2833
end
2934
end
3035

3136
action :remove do
32-
ssh_user = new_resource.user || 'root'
33-
known_hosts_path = default_or_user_path(node['ssh']['known_hosts_path'], ssh_user)
34-
execute "remove known_host entry for #{new_resource.host}" do
35-
command "ssh-keygen -R #{Shellwords.escape(new_resource.host)}"
36-
user ssh_user
37-
umask '0600'
37+
if @current_resource.exists?
38+
execute "remove known_host entry for #{new_resource.host}" do
39+
command "ssh-keygen -R #{Shellwords.escape(new_resource.host)} -f #{new_resource.path}"
40+
user new_resource.user if new_resource.user
41+
umask new_resource.user ? 0077 : 0022
42+
end
3843
end
3944
end
45+
46+
def initialize(new_resource, run_context)
47+
super(new_resource, run_context)
48+
49+
new_resource.path default_or_user_path(new_resource.user) unless new_resource.path
50+
if new_resource.host.match(/:/)
51+
host, port = new_resource.host.split(':')
52+
new_resource.host host
53+
new_resource.port port unless new_resource.port
54+
end
55+
56+
new_resource.port 22 unless new_resource.port
57+
58+
new_resource.group user_group(new_resource.user) unless new_resource.group
59+
60+
load_current_resource
61+
load_key_if_needed
62+
end
63+
64+
def load_key_if_needed
65+
return if @current_resource.exists?
66+
return if new_resource.key
67+
return if new_resource.action.is_a?(Array) ? new_resource.action.include?(:remove) : new_resource.action == :remove
68+
69+
keyscan = Mixlib::ShellOut.new(
70+
"ssh-keyscan #{new_resource.hashed ? '-H ' : ''} -p #{new_resource.port.to_i} #{Shellwords.escape(new_resource.host)}"
71+
)
72+
keyscan.run_command
73+
keyscan.error! # this will raise an error if the command failed for any reason.
74+
new_resource.key keyscan.stdout.strip
75+
end
76+
77+
def load_current_resource
78+
search = Mixlib::ShellOut.new(
79+
"ssh-keygen -H -F #{Shellwords.escape(new_resource.host)} "\
80+
"-f #{new_resource.path} | grep 'Host #{new_resource.host} found'"
81+
)
82+
search.run_command
83+
@current_resource = Chef::Resource::SshKnownHosts.new(@new_resource.name)
84+
@current_resource.exists = search.status.success?
85+
end
86+
87+
protected
88+
89+
def default_or_user_path(username = nil)
90+
username ? "#{user_dir(username)}/.ssh/known_hosts" : node['ssh']['known_hosts_path']
91+
end

0 commit comments

Comments
 (0)