Sanitize hostnames for keypairs

Fixes #76
This commit is contained in:
R. Tyler Croy 2013-06-30 20:44:07 -07:00
parent 613cae868f
commit 302e0ea555
3 changed files with 23 additions and 8 deletions

View File

@ -15,7 +15,8 @@ module Blimpy
end
def self.key_name
"Blimpy-#{ENV['USER']}-#{Socket.gethostname}"
safe_hostname = Socket.gethostname.gsub('.', '-')
"Blimpy-#{ENV['USER']}-#{safe_hostname}"
end
end
end

View File

@ -1,3 +1,3 @@
module Blimpy
VERSION = "0.6.7"
VERSION = "0.7.0"
end

View File

@ -2,26 +2,40 @@ require 'spec_helper'
require 'blimpy/keys'
describe Blimpy::Keys do
subject(:keys) { described_class }
describe '#public_key' do
context 'with no SSH keys' do
it 'should raise a SSHKeyNotFoundError' do
File.stub(:exists?).and_return(false)
expect {
subject.public_key
keys.public_key
}.to raise_error(Blimpy::SSHKeyNotFoundError)
end
end
end
describe '#key_name' do
let(:username) { 'rspecguy' }
before :each do
ENV['USER'] = 'tester'
ENV['USER'] = username
Socket.should_receive(:gethostname).and_return(hostname)
end
it do
hostname = 'rspec'
Socket.should_receive(:gethostname).and_return(hostname)
subject.key_name.should == "Blimpy-tester-#{hostname}"
context 'with a simple hostname' do
let(:hostname) { 'rspec' }
it 'should create the right key name' do
expect(keys.key_name).to eql("Blimpy-#{username}-#{hostname}")
end
end
context 'with a FQDN' do
let(:hostname) { 'rspec.github.io' }
it 'should create the right key name' do
expect(keys.key_name).to eql("Blimpy-#{username}-rspec-github-io")
end
end
end
end