Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to use environment variable for key #184

Merged
merged 1 commit into from
Nov 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/tugboat/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def load_config_file
end

def access_token
@data['authentication']['access_token']
env_access_token || @data['authentication']['access_token']
end

def ssh_key_path
Expand Down Expand Up @@ -81,6 +81,10 @@ def default_backups_enabled
@data['defaults'].nil? ? DEFAULT_BACKUPS_ENABLED : @data['defaults']['backups_enabled']
end

def env_access_token
ENV['DO_API_TOKEN'] unless ENV['DO_API_TOKEN'].to_s.empty?
end

# Re-runs initialize
def reset!
self.send(:initialize)
Expand Down
2 changes: 2 additions & 0 deletions spec/cli/add_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
before :each do
allow(ENV).to receive(:[]).with('HOME').and_return(fake_home)
allow(ENV).to receive(:[]).with('DEBUG').and_return(nil)
allow(ENV).to receive(:[]).with('DO_API_TOKEN').and_return(nil)
allow(ENV).to receive(:[]).with('http_proxy').and_return(nil)

FileUtils.mkdir_p "#{fake_home}/.ssh"
File.open("#{fake_home}/.ssh/id_rsa.pub", 'w') {|f| f.write("ssh-dss A456= user@host") }
end
Expand Down
36 changes: 36 additions & 0 deletions spec/cli/env_variable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

describe Tugboat::CLI do
include_context "spec"

describe "DO_API_TOKEN=foobar" do
it "verifies with the ENV variable DO_API_TOKEN" do
stub_request(:get, "https://api.digitalocean.com/v2/droplets?per_page=200").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer env_variable', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplets'), :headers => {})

allow(ENV).to receive(:[]).with('HOME').and_return('/tmp/fake_home')
allow(ENV).to receive(:[]).with('DO_API_TOKEN').and_return('env_variable')
allow(ENV).to receive(:[]).with('http_proxy').and_return(nil)

@cli.verify
expect($stdout.string).to eq "Authentication with DigitalOcean was successful.\n"
expect(a_request(:get, "https://api.digitalocean.com/v2/droplets?per_page=200")).to have_been_made
end

it "does not use ENV variable DO_API_TOKEN if empty" do
stub_request(:get, "https://api.digitalocean.com/v2/droplets?per_page=200").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplets'), :headers => {})

allow(ENV).to receive(:[]).with('HOME').and_return('/tmp/fake_home')
allow(ENV).to receive(:[]).with('DO_API_TOKEN').and_return('')
allow(ENV).to receive(:[]).with('http_proxy').and_return(nil)

@cli.verify
expect($stdout.string).to eq "Authentication with DigitalOcean was successful.\n"
expect(a_request(:get, "https://api.digitalocean.com/v2/droplets?per_page=200")).to have_been_made
end
end
end