Add support for a system default credentials file.

The path is:
  - windows: %ProgramData%\Google\Auth\application_default_credentials.json
  - other: /etc/google/auth/application_default_credentials.json
This commit is contained in:
Todd Derr committed 2015-06-29 16:35:45 -04:00
1 parent dc12124faf
commit 86de5fe13b
6 files changed
+108 -2

No files matched your search

@@ -32,6 +32,7 @@ $LOAD_PATH.unshift(spec_dir)
$LOAD_PATH.uniq!
require 'faraday'
require 'fakefs/safe'
require 'googleauth'
require 'spec_helper'
@@ -140,6 +141,19 @@ describe '#get_application_default' do
stubs.verify_stubbed_calls
end
it 'succeeds with system default file' do
ENV.delete(@var_name) unless ENV[@var_name].nil?
FakeFS do
Dir.mktmpdir do |dir|
key_path = File.join('/etc/google/auth/', CREDENTIALS_FILE_NAME)
FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text)
expect(Google::Auth.get_application_default(@scope)).to_not be_nil
File.delete(key_path)
end
end
end
it 'succeeds if environment vars are valid' do
ENV.delete(@var_name) unless ENV[@var_name].nil? # no env var
ENV[PRIVATE_KEY_VAR] = cred_json[:private_key]
+27
View File
@@ -32,6 +32,7 @@ $LOAD_PATH.unshift(spec_dir)
$LOAD_PATH.uniq!
require 'apply_auth_examples'
require 'fakefs/safe'
require 'fileutils'
require 'googleauth/service_account'
require 'jwt'
@@ -231,6 +232,32 @@ describe Google::Auth::ServiceAccountCredentials do
end
end
end
describe '#from_system_default_path' do
before(:example) do
@scope = 'https://www.googleapis.com/auth/userinfo.profile'
@path = File.join('/etc/google/auth/', CREDENTIALS_FILE_NAME)
@clz = ServiceAccountCredentials
end
it 'is nil if no file exists' do
FakeFS do
expect(ServiceAccountCredentials.from_system_default_path(@scope)).
to be_nil
end
end
it 'successfully loads the file when it is present' do
FakeFS do
Dir.mktmpdir do |dir|
FileUtils.mkdir_p(File.dirname(@path))
File.write(@path, cred_json_text)
expect(@clz.from_system_default_path(@scope)).to_not be_nil
File.delete(@path)
end
end
end
end
end
describe Google::Auth::ServiceAccountJwtHeaderCredentials do
+41
View File
@@ -32,6 +32,7 @@ $LOAD_PATH.unshift(spec_dir)
$LOAD_PATH.uniq!
require 'apply_auth_examples'
require 'fakefs/safe'
require 'fileutils'
require 'googleauth/user_refresh'
require 'jwt'
@@ -185,4 +186,44 @@ describe Google::Auth::UserRefreshCredentials do
end
end
end
describe '#from_system_default_path' do
before(:example) do
@scope = 'https://www.googleapis.com/auth/userinfo.profile'
@path = File.join('/etc/google/auth/', CREDENTIALS_FILE_NAME)
@clz = UserRefreshCredentials
end
it 'is nil if no file exists' do
FakeFS do
expect(UserRefreshCredentials.from_system_default_path(@scope)).
to be_nil
end
end
it 'fails if the file is invalid' do
needed = %w(client_id client_secret refresh_token)
needed.each do |missing|
FakeFS do
Dir.mktmpdir do |dir|
FileUtils.mkdir_p(File.dirname(@path))
File.write(@path, cred_json_text(missing))
expect { @clz.from_system_default_path(@scope) }.to raise_error
File.delete(@path)
end
end
end
end
it 'successfully loads the file when it is present' do
FakeFS do
Dir.mktmpdir do |dir|
FileUtils.mkdir_p(File.dirname(@path))
File.write(@path, cred_json_text)
expect(@clz.from_system_default_path(@scope)).to_not be_nil
File.delete(@path)
end
end
end
end
end