Skip to content

Commit

Permalink
Merge pull request #184 from pearkes/add_ability_to_use_env_creds
Browse files Browse the repository at this point in the history
Adds ability to use environment variable for key
  • Loading branch information
petems committed Nov 2, 2015
2 parents 28e184e + 46e4ee0 commit 2045bd6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/tugboat/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 @@ -86,6 +86,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

0 comments on commit 2045bd6

Please sign in to comment.