Add support for loading either credentials type as determined by the loaded content
This commit is contained in:
1 parent
3bd8751519
commit
4d4bcd46e8
3 files changed
+134
-77
No files matched your search
@@ -49,86 +49,108 @@ describe '#get_application_default' do
|
||||
ENV['HOME'] = @home unless @home == ENV['HOME']
|
||||
end
|
||||
|
||||
def cred_json_text
|
||||
cred_json = {
|
||||
private_key_id: 'a_private_key_id',
|
||||
private_key: @key.to_pem,
|
||||
client_email: 'app@developer.gserviceaccount.com',
|
||||
client_id: 'app.apps.googleusercontent.com',
|
||||
type: 'service_account'
|
||||
}
|
||||
MultiJson.dump(cred_json)
|
||||
end
|
||||
shared_examples 'it loads them correctly' do
|
||||
it 'fails if the GOOGLE_APPLICATION_CREDENTIALS path does not exist' do
|
||||
Dir.mktmpdir do |dir|
|
||||
key_path = File.join(dir, 'does-not-exist')
|
||||
ENV[@var_name] = key_path
|
||||
expect { Google::Auth.get_application_default(@scope) }.to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
it 'fails if the GOOGLE_APPLICATION_CREDENTIALS path does not exist' do
|
||||
Dir.mktmpdir do |dir|
|
||||
key_path = File.join(dir, 'does-not-exist')
|
||||
ENV[@var_name] = key_path
|
||||
expect { Google::Auth.get_application_default(@scope) }.to raise_error
|
||||
it 'succeeds if the GOOGLE_APPLICATION_CREDENTIALS file is valid' do
|
||||
Dir.mktmpdir do |dir|
|
||||
key_path = File.join(dir, 'my_cert_file')
|
||||
FileUtils.mkdir_p(File.dirname(key_path))
|
||||
File.write(key_path, cred_json_text)
|
||||
ENV[@var_name] = key_path
|
||||
expect(Google::Auth.get_application_default(@scope)).to_not be_nil
|
||||
end
|
||||
end
|
||||
|
||||
it 'succeeds with default file without GOOGLE_APPLICATION_CREDENTIALS' do
|
||||
ENV.delete(@var_name) unless ENV[@var_name].nil?
|
||||
Dir.mktmpdir do |dir|
|
||||
key_path = File.join(dir, '.config',
|
||||
CredentialsLoader::WELL_KNOWN_PATH)
|
||||
FileUtils.mkdir_p(File.dirname(key_path))
|
||||
File.write(key_path, cred_json_text)
|
||||
ENV['HOME'] = dir
|
||||
expect(Google::Auth.get_application_default(@scope)).to_not be_nil
|
||||
end
|
||||
end
|
||||
|
||||
it 'fails without default file or env if not on compute engine' do
|
||||
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
||||
stub.get('/') do |_env|
|
||||
[404,
|
||||
{ 'Metadata-Flavor' => 'Google' },
|
||||
'']
|
||||
end
|
||||
end # GCE not detected
|
||||
Dir.mktmpdir do |dir|
|
||||
ENV.delete(@var_name) unless ENV[@var_name].nil? # no env var
|
||||
ENV['HOME'] = dir # no config present in this tmp dir
|
||||
c = Faraday.new do |b|
|
||||
b.adapter(:test, stubs)
|
||||
end
|
||||
blk = proc do
|
||||
Google::Auth.get_application_default(@scope, connection: c)
|
||||
end
|
||||
expect(&blk).to raise_error
|
||||
end
|
||||
stubs.verify_stubbed_calls
|
||||
end
|
||||
|
||||
it 'succeeds without default file or env if on compute engine' do
|
||||
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
||||
stub.get('/') do |_env|
|
||||
[200,
|
||||
{ 'Metadata-Flavor' => 'Google' },
|
||||
'']
|
||||
end
|
||||
end # GCE detected
|
||||
Dir.mktmpdir do |dir|
|
||||
ENV.delete(@var_name) unless ENV[@var_name].nil? # no env var
|
||||
ENV['HOME'] = dir # no config present in this tmp dir
|
||||
c = Faraday.new do |b|
|
||||
b.adapter(:test, stubs)
|
||||
end
|
||||
creds = Google::Auth.get_application_default(
|
||||
@scope,
|
||||
connection: c)
|
||||
expect(creds).to_not be_nil
|
||||
end
|
||||
stubs.verify_stubbed_calls
|
||||
end
|
||||
end
|
||||
|
||||
it 'succeeds if the GOOGLE_APPLICATION_CREDENTIALS file is valid' do
|
||||
Dir.mktmpdir do |dir|
|
||||
key_path = File.join(dir, 'my_cert_file')
|
||||
FileUtils.mkdir_p(File.dirname(key_path))
|
||||
File.write(key_path, cred_json_text)
|
||||
ENV[@var_name] = key_path
|
||||
expect(Google::Auth.get_application_default(@scope)).to_not be_nil
|
||||
describe 'when credential type is service account' do
|
||||
def cred_json_text
|
||||
cred_json = {
|
||||
private_key_id: 'a_private_key_id',
|
||||
private_key: @key.to_pem,
|
||||
client_email: 'app@developer.gserviceaccount.com',
|
||||
client_id: 'app.apps.googleusercontent.com',
|
||||
type: 'service_account'
|
||||
}
|
||||
MultiJson.dump(cred_json)
|
||||
end
|
||||
|
||||
it_behaves_like 'it loads them correctly'
|
||||
end
|
||||
|
||||
it 'succeeds with default file if GOOGLE_APPLICATION_CREDENTIALS is unset' do
|
||||
ENV.delete(@var_name) unless ENV[@var_name].nil?
|
||||
Dir.mktmpdir do |dir|
|
||||
key_path = File.join(dir, '.config',
|
||||
CredentialsLoader::WELL_KNOWN_PATH)
|
||||
FileUtils.mkdir_p(File.dirname(key_path))
|
||||
File.write(key_path, cred_json_text)
|
||||
ENV['HOME'] = dir
|
||||
expect(Google::Auth.get_application_default(@scope)).to_not be_nil
|
||||
describe 'when credential type is authorized_user' do
|
||||
def cred_json_text
|
||||
cred_json = {
|
||||
client_secret: 'privatekey',
|
||||
refresh_token: 'refreshtoken',
|
||||
client_id: 'app.apps.googleusercontent.com',
|
||||
type: 'authorized_user'
|
||||
}
|
||||
MultiJson.dump(cred_json)
|
||||
end
|
||||
end
|
||||
|
||||
it 'fails without default file or env if not on compute engine' do
|
||||
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
||||
stub.get('/') do |_env|
|
||||
[404,
|
||||
{ 'Metadata-Flavor' => 'Google' },
|
||||
'']
|
||||
end
|
||||
end # GCE not detected
|
||||
Dir.mktmpdir do |dir|
|
||||
ENV.delete(@var_name) unless ENV[@var_name].nil? # no env var
|
||||
ENV['HOME'] = dir # no config present in this tmp dir
|
||||
c = Faraday.new do |b|
|
||||
b.adapter(:test, stubs)
|
||||
end
|
||||
blk = proc do
|
||||
Google::Auth.get_application_default(@scope, connection: c)
|
||||
end
|
||||
expect(&blk).to raise_error
|
||||
end
|
||||
stubs.verify_stubbed_calls
|
||||
end
|
||||
|
||||
it 'succeeds without default file or env if on compute engine' do
|
||||
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
||||
stub.get('/') do |_env|
|
||||
[200,
|
||||
{ 'Metadata-Flavor' => 'Google' },
|
||||
'']
|
||||
end
|
||||
end # GCE detected
|
||||
Dir.mktmpdir do |dir|
|
||||
ENV.delete(@var_name) unless ENV[@var_name].nil? # no env var
|
||||
ENV['HOME'] = dir # no config present in this tmp dir
|
||||
c = Faraday.new do |b|
|
||||
b.adapter(:test, stubs)
|
||||
end
|
||||
expect(Google::Auth.get_application_default(@scope,
|
||||
connection: c)).to_not be_nil
|
||||
end
|
||||
stubs.verify_stubbed_calls
|
||||
it_behaves_like 'it loads them correctly'
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user