Skip to content

Commit 66d1197

Browse files
committed
fix authorized_keys comment bug
1 parent 5ef8855 commit 66d1197

File tree

6 files changed

+45
-26
lines changed

6 files changed

+45
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG for ssh
22

3+
## 0.10.20
4+
* allow full line comments on authorized_keys
5+
36
## 0.10.18
47
* Cleanup tests to get them running again
58
* updates for rubocop errors

metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
license 'Apache 2.0'
55
description 'LWRPs for managing SSH known_hosts and config files'
66
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
7-
version '0.10.18'
7+
version '0.10.20'
88
issues_url 'https://github.com/markolson/chef-ssh/issues'
99
source_url 'https://github.com/markolson/chef-ssh'
1010

providers/authorized_keys.rb

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,21 @@ def update_file
5151
end
5252

5353
def format_lines
54-
@lines.collect do |line|
55-
joined = if line[:options].nil?
56-
''
57-
else
58-
line[:options].collect do |key, value|
59-
value.nil? || value.empty? ? key.to_s : "#{key}=\"#{value}\""
60-
end.join(',')
61-
end
62-
joined << ' ' unless joined.empty?
63-
joined << line[:type] << ' ' << line[:key]
64-
line[:comment] && (joined << ' ' << line[:comment])
65-
joined
66-
end.join("\n") + "\n"
54+
@lines.collect { |line| line.is_a?(String) ? line : construct_line(line) }.join("\n") + "\n"
55+
end
56+
57+
def construct_line(line)
58+
joined = if line[:options].nil?
59+
''
60+
else
61+
line[:options].collect do |key, value|
62+
value.nil? || value.empty? ? key.to_s : "#{key}=\"#{value}\""
63+
end.join(',')
64+
end
65+
joined << ' ' unless joined.empty?
66+
joined << line[:type] << ' ' << line[:key]
67+
line[:comment] && (joined << ' ' << line[:comment])
68+
joined
6769
end
6870

6971
def initialize(new_resource, run_context)
@@ -77,7 +79,7 @@ def initialize(new_resource, run_context)
7779
def load_current_resource
7880
@lines = ::File.exist?(@path) ? parse(::IO.readlines(@path)) : []
7981

80-
current_line = @lines.find { |line| line[:key] == @new_resource.key }
82+
current_line = @lines.find { |line| line.is_a?(Hash) && line[:key] == @new_resource.key }
8183
@current_resource = Chef::Resource::SshKnownHosts.new(@new_resource.name)
8284
@current_resource.exists = current_line
8385
end
@@ -86,15 +88,19 @@ def load_current_resource
8688

8789
def parse(current)
8890
current.reduce([]) do |memo, row|
89-
line = {}
90-
# split on whitespace that is not inside of quotes
91-
fields = row.split(/(?!\B"[^"]*)\s(?![^"]*"\B)/)
92-
line[:options] = parse_options(fields.shift) unless types.include? fields[0]
93-
validate_type(fields[0], @path)
94-
line[:type] = fields[0]
95-
line[:key] = fields[1]
96-
line[:comment] = fields[2..-1].join(' ') if fields[2]
97-
memo << line
91+
if /^#/ =~ row || row.strip.empty?
92+
memo << row
93+
else
94+
line = {}
95+
# split on whitespace that is not inside of quotes
96+
fields = row.split(/(?!\B"[^"]*)\s(?![^"]*"\B)/)
97+
line[:options] = parse_options(fields.shift) unless types.include? fields[0]
98+
validate_type(fields[0], @path)
99+
line[:type] = fields[0]
100+
line[:key] = fields[1]
101+
line[:comment] = fields[2..-1].join(' ') if fields[2]
102+
memo << line
103+
end
98104
end
99105
end
100106

test/cookbooks/ssh_test/recipes/authorized_keys.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
action [:create, :manage]
66
end
77

8+
directory '/home/test-user/.ssh' do
9+
mode 0o0700
10+
end
11+
12+
file '/home/test-user/.ssh/authorized_keys' do
13+
content "# this is a comment\n" + 'ssh-rsa iWasHereAAAAB2EAAAADAQABAAACAQCeCRfSzGWGNsisAZpuFIS0GmHJfgms3g8okwL9h9AvoQPwgyhyri/Wlcz3eyZMvuR4/vwh9FgWpRwLxot7QSGry58GYR9tHkDT9o3m0Hlx28E+K2gbNK5SyFROx5lSfOZkCSyPjBEBmTAadpVYZBJj789oeAT3dDvsxMAqokCIjV5Ey9xBIWKapbsDiTdOHmtDhlrFZfBc75I6tTnW9WGVG6gCQtzyC/tJ2DmWJhtEz9UjxhAOUzazHM2CJ2IlF3SHm+nz7xjTWmGVRzpiellmN+2Stmianea9ga8L//9v06gDKqp2lNSsi2SJujAsEiKAGtQu6Aa4hdxRFt87m6WSN9lusAazZvnX5s93lAmUAG+wWPnAsujkRSDwv2Ju+GdQFW3ncML7aXFOhIMViG6B98X2h9f3W6XdwQseh10QfvFZ3fAmcAvWvlEM0pGXdfKeFY0LfD7UFxTvzEfqPKnbV6SKlAIMAQ3CX+Q1sZ4nfqopZVJwHDHSL/KQeVKePdyFbZcFVE4L/zruS/fLDqiDMq9yZqMu3WkP5bp4crzguaVwHmrTG4k1XOH5jkMrUj7javMLQHWu56bj0heynhXw7gzXnC/DSgY58/1BPEy7ejsGr0RX2LBRulh84UkV0cjLs8MZyBrhS4dYwyBmtcYlh+OVVVwFimg4ayR7UlkVMw==' # rubocop:disable all'
14+
end
15+
816
ssh_authorized_keys 'first' do
917
user 'test-user'
1018
type 'ssh-rsa'

test/cookbooks/ssh_test/recipes/known_hosts.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
include_recipe 'ssh'
44

55
file '/etc/ssh/ssh_known_hosts' do
6-
content "#{::TestData.dummy1_key}\n#{::TestData.dummy2_key}\n"\
6+
content "# this is a comment\n#{::TestData.dummy1_key}\n#{::TestData.dummy2_key}\n"\
77
"#{::TestData.dummy6_key}\n"
88
action :create
99
end
1010

1111
file '/home/vagrant/.ssh/known_hosts' do
12-
content "#{::TestData.dummy3_key}\n#{::TestData.dummy4_key}\n"
12+
content "#{::TestData.dummy3_key}\n# this is a comment\n#{::TestData.dummy4_key}\n"
1313
action :create
1414
end
1515

test/integration/known_hosts/serverspec/known_hosts_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
its(:content) { should include(TestData.github_key) }
88
its(:content) { should include(TestData.dummy1_key) }
99
its(:content) { should_not match(/dummy2/) }
10+
its(:content) { is_expected.to contain('# this is a comment') }
1011
end
1112

1213
describe file('/home/vagrant/.ssh/known_hosts') do
1314
it { should be_file }
1415
it { should be_readable }
1516
its(:content) { should include(TestData.dummy3_key) }
1617
its(:content) { should_not match(/dummy4/) }
18+
its(:content) { is_expected.to contain('# this is a comment') }
1719
end
1820

1921
describe command('ssh-keygen -F github.com -f /home/vagrant/.ssh/known_hosts') do

0 commit comments

Comments
 (0)