Skip to content

Commit

Permalink
Merge pull request #328 from akitada/respect-insert-key
Browse files Browse the repository at this point in the history
Respect config.ssh.insert_key
  • Loading branch information
ggiamarchi committed May 22, 2017
2 parents 5a29c6a + 09e04f3 commit b424746
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ end
### SSH authentication

* `keypair_name` - The name of the key pair register in nova to associate with the VM. The public key should
be the matching pair for the private key configured with `config.ssh.private_key_path` on Vagrant.
* `public_key_path` - if `keypair_name` is not provided, the path to the public key will be used by vagrant to generate a keypair on the OpenStack cloud. The keypair will be destroyed when the VM is destroyed.
be the matching pair for the private key configured with `config.ssh.private_key_path` on Vagrant. When `config.ssh.insert_key` is `false`, this is ignored.
* `public_key_path` - if `keypair_name` is not provided, the path to the public key will be used by vagrant to generate a keypair on the OpenStack cloud. The keypair will be destroyed when the VM is destroyed. When `config.ssh.insert_key` is `false`, this is ignored.

If neither `keypair_name` nor `public_key_path` are set, vagrant will generate a new ssh key and automatically import it in OpenStack.
If neither `keypair_name` nor `public_key_path` are set, vagrant will generate a new ssh key and automatically import it in OpenStack, unless `config.ssh.insert_key` is `false`.

* `ssh_disabled` - if set to `true`, all ssh actions managed by the provider will be disabled during the `vagrant up`.
We recommend to use this option only to create private VMs that won't be accessed directly from vagrant. By contrast,
Expand Down
2 changes: 1 addition & 1 deletion source/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Metrics/CyclomaticComplexity:
Max: 15

Metrics/MethodLength:
Max: 60
Max: 65

Metrics/LineLength:
Max: 150
Expand Down
6 changes: 4 additions & 2 deletions source/lib/vagrant-openstack-provider/action/create_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def create_server(env, options)
env[:ui].info(" -- ImageRef : #{options[:image].id}")
end
env[:ui].info(" -- Boot volume : #{options[:volume_boot][:id]} (#{options[:volume_boot][:device]})") unless options[:volume_boot].nil?
env[:ui].info(" -- KeyPair : #{options[:keypair_name]}")
env[:ui].info(" -- KeyPair : #{options[:keypair_name]}") unless options[:keypair_name].nil?

unless options[:networks].empty?
formated_networks = ' -- '
Expand Down Expand Up @@ -105,7 +105,9 @@ def create_server(env, options)
unless options[:image].nil?
log << "image '#{options[:image].name}' (#{options[:image].id}) "
end
log << "and keypair '#{options[:keypair_name]}'"
unless options[:keypair_name].nil?
log << "and keypair '#{options[:keypair_name]}'"
end

@logger.info(log)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def read_ssh_info(env)
port: @resolver.resolve_ssh_port(env),
username: @resolver.resolve_ssh_username(env)
}
hash[:private_key_path] = "#{env[:machine].data_dir}/#{get_keypair_name(env)}" unless config.keypair_name || config.public_key_path
if env[:machine].config.ssh.insert_key
hash[:private_key_path] = "#{env[:machine].data_dir}/#{get_keypair_name(env)}" unless config.keypair_name || config.public_key_path
end
# Should work silently when https://github.com/mitchellh/vagrant/issues/4637 is fixed
hash[:log_level] = 'ERROR'
hash
Expand Down
2 changes: 1 addition & 1 deletion source/lib/vagrant-openstack-provider/client/nova.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def create_server(env, options)
s['imageRef'] = options[:image_ref]
end
s['flavorRef'] = options[:flavor_ref]
s['key_name'] = options[:keypair]
s['key_name'] = options[:keypair] unless options[:keypair].nil?
s['availability_zone'] = options[:availability_zone] unless options[:availability_zone].nil?
s['security_groups'] = options[:security_groups] unless options[:security_groups].nil?
s['user_data'] = Base64.encode64(options[:user_data]) unless options[:user_data].nil?
Expand Down
10 changes: 6 additions & 4 deletions source/lib/vagrant-openstack-provider/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,12 @@ def validate(machine)
validate_stack_config(errors)
validate_ssh_timeout(errors)

if machine.config.ssh.private_key_path
puts I18n.t('vagrant_openstack.config.keypair_name_required').yellow unless @keypair_name || @public_key_path
else
errors << I18n.t('vagrant_openstack.config.private_key_missing') if @keypair_name || @public_key_path
if machine.config.ssh.insert_key
if machine.config.ssh.private_key_path
puts I18n.t('vagrant_openstack.config.keypair_name_required').yellow unless @keypair_name || @public_key_path
else
errors << I18n.t('vagrant_openstack.config.private_key_missing') if @keypair_name || @public_key_path
end
end

{
Expand Down
2 changes: 2 additions & 0 deletions source/lib/vagrant-openstack-provider/config_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def resolve_floating_ip(env)

def resolve_keypair(env)
config = env[:machine].provider_config
machine_config = env[:machine].config
nova = env[:openstack_client].nova
return nil unless machine_config.ssh.insert_key
return config.keypair_name if config.keypair_name
return nova.import_keypair_from_file(env, config.public_key_path) if config.public_key_path
generate_keypair(env)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
double('ssh_config').tap do |config|
config.stub(:username) { 'sshuser' }
config.stub(:port) { nil }
config.stub(:insert_key) { true }
end
end

Expand Down Expand Up @@ -166,6 +167,16 @@
log_level: 'ERROR')
end
end

context 'with neither keypair_name nor public_key_path specified and ssh.insert_key is false' do
it 'does not return private_key_path' do
ssh_config.stub(:insert_key) { false }
config.stub(:floating_ip) { '80.80.80.80' }
config.stub(:keypair_name) { nil }
config.stub(:public_key_path) { nil }
@action.read_ssh_info(env).should eq(host: '80.80.80.80', port: 22, username: 'sshuser', log_level: 'ERROR')
end
end
end

context 'without config.floating_ip specified' do
Expand Down
11 changes: 11 additions & 0 deletions source/spec/vagrant-openstack-provider/config_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
double('ssh_config').tap do |config|
config.stub(:username) { nil }
config.stub(:port) { nil }
config.stub(:insert_key) { true }
end
end

Expand Down Expand Up @@ -405,6 +406,16 @@
@action.resolve_keypair(env).should eq('my-keypair-imported')
end
end

context 'with insert_key false' do
it 'does nothing and return nil' do
config.stub(:keypair_name) { 'my-keypair' }
ssh_config.stub(:insert_key) { false }
nova.should_not_receive(:import_keypair_from_file)
@action.should_not_receive(:generate_keypair)
@action.resolve_keypair(env).should be_nil
end
end
end

describe 'generate_keypair' do
Expand Down
11 changes: 11 additions & 0 deletions source/spec/vagrant-openstack-provider/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
machine.stub_chain(:env, :root_path).and_return '/'
ssh.stub(:private_key_path) { 'private key path' }
ssh.stub(:username) { 'ssh username' }
ssh.stub(:insert_key) { true }
config.stub(:ssh) { ssh }
machine.stub(:config) { config }
subject.username = 'foo'
Expand Down Expand Up @@ -348,6 +349,16 @@
validation_errors.first.should == error_message
end
end

context 'keypair_name or public_key_path is set and ssh.insert_key is false' do
it 'should not error' do
ssh.stub(:private_key_path) { nil }
ssh.stub(:insert_key) { false }
subject.public_key_path = 'public_key'
I18n.should_not_receive(:t)
validation_errors.should be_empty
end
end
end

context 'the password' do
Expand Down

0 comments on commit b424746

Please sign in to comment.