Skip to content

Commit

Permalink
Merge pull request #87 from hfinucane/gh-62
Browse files Browse the repository at this point in the history
Support arbitrarily named private keys
  • Loading branch information
tyler-ball committed Nov 18, 2015
2 parents c6a10da + d4e9868 commit 3e2f428
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/cheffish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def self.get_private_key_with_path(name, config = profiled_config)
next unless File.exist?(private_key_path)
Dir.entries(private_key_path).sort.each do |key|
ext = File.extname(key)
if ext == '' || ext == '.pem'
if key == name || ext == '' || ext == '.pem'
key_name = key[0..-(ext.length+1)]
if key_name == name
if key_name == name || key == name
Chef::Log.info("Reading key #{name} from file #{private_key_path}/#{key}")
return [ IO.read("#{private_key_path}/#{key}"), "#{private_key_path}/#{key}" ]
end
Expand Down
13 changes: 13 additions & 0 deletions spec/unit/get_private_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ def setup_key
key_file
end

def setup_arbitrarily_named_key
key_file = File.expand_path("ned_stark.xxx", directory_that_exists)
File.open(key_file, "w+") do |f|
f.write private_key_contents
end
key_file
end

def setup_pem_key
key_file = File.expand_path("ned_stark.pem", directory_that_exists)
File.open(key_file, "w+") do |f|
Expand Down Expand Up @@ -57,6 +65,11 @@ def setup_garbage_key
expect(Cheffish.get_private_key("ned_stark", config)).to eq(private_key_pem_contents)
end

it "returns the contents of arbitrarily named keys" do
setup_arbitrarily_named_key
expect(Cheffish.get_private_key("ned_stark.xxx", config)).to eq(private_key_contents)
end

# we arbitrarily prefer "ned_stark" over "ned_stark.pem" for deterministic behavior
it "returns the contents of the key that does not have an extension if both exist" do
setup_key
Expand Down

0 comments on commit 3e2f428

Please sign in to comment.