Treat empty credentials environment variables as unset. (#205)

This commit is contained in:
igorpeshansky 2019-03-27 13:55:08 -04:00 committed by Daniel Azuma
parent 43ebe19be9
commit 23f9b5071f
4 changed files with 21 additions and 4 deletions

View File

@ -8,4 +8,4 @@ AllCops:
Metrics/ClassLength: Metrics/ClassLength:
Max: 110 Max: 110
Metrics/ModuleLength: Metrics/ModuleLength:
Max: 105 Max: 110

View File

@ -95,7 +95,7 @@ module Google
# * `:connection_builder` A `Proc` that returns a connection. # * `:connection_builder` A `Proc` that returns a connection.
def from_env scope = nil, options = {} def from_env scope = nil, options = {}
options = interpret_options scope, options options = interpret_options scope, options
if ENV.key? ENV_VAR if ENV.key?(ENV_VAR) && !ENV[ENV_VAR].empty?
path = ENV[ENV_VAR] path = ENV[ENV_VAR]
raise "file #{path} does not exist" unless File.exist? path raise "file #{path} does not exist" unless File.exist? path
File.open path do |f| File.open path do |f|
@ -192,11 +192,13 @@ module Google
end end
def service_account_env_vars? def service_account_env_vars?
([PRIVATE_KEY_VAR, CLIENT_EMAIL_VAR] - ENV.keys).empty? ([PRIVATE_KEY_VAR, CLIENT_EMAIL_VAR] - ENV.keys).empty? &&
!ENV.to_h.fetch_values(PRIVATE_KEY_VAR, CLIENT_EMAIL_VAR).join(" ").empty?
end end
def authorized_user_env_vars? def authorized_user_env_vars?
([CLIENT_ID_VAR, CLIENT_SECRET_VAR, REFRESH_TOKEN_VAR] - ENV.keys).empty? ([CLIENT_ID_VAR, CLIENT_SECRET_VAR, REFRESH_TOKEN_VAR] - ENV.keys).empty? &&
!ENV.to_h.fetch_values(CLIENT_ID_VAR, CLIENT_SECRET_VAR, REFRESH_TOKEN_VAR).join(" ").empty?
end end
end end
end end

View File

@ -187,6 +187,11 @@ describe Google::Auth::ServiceAccountCredentials do
expect(ServiceAccountCredentials.from_env(@scope)).to be_nil expect(ServiceAccountCredentials.from_env(@scope)).to be_nil
end end
it "returns nil if the GOOGLE_APPLICATION_CREDENTIALS is empty" do
ENV[@var_name] = ""
expect(ServiceAccountCredentials.from_env(@scope)).to be_nil
end
it "fails if the GOOGLE_APPLICATION_CREDENTIALS path does not exist" do it "fails if the GOOGLE_APPLICATION_CREDENTIALS path does not exist" do
ENV.delete @var_name unless ENV[@var_name].nil? ENV.delete @var_name unless ENV[@var_name].nil?
expect(ServiceAccountCredentials.from_env(@scope)).to be_nil expect(ServiceAccountCredentials.from_env(@scope)).to be_nil
@ -390,6 +395,11 @@ describe Google::Auth::ServiceAccountJwtHeaderCredentials do
expect(clz.from_env).to be_nil expect(clz.from_env).to be_nil
end end
it "returns nil if the GOOGLE_APPLICATION_CREDENTIALS is empty" do
ENV[@var_name] = ""
expect(clz.from_env).to be_nil
end
it "fails if the GOOGLE_APPLICATION_CREDENTIALS path does not exist" do it "fails if the GOOGLE_APPLICATION_CREDENTIALS path does not exist" do
ENV.delete @var_name unless ENV[@var_name].nil? ENV.delete @var_name unless ENV[@var_name].nil?
expect(clz.from_env).to be_nil expect(clz.from_env).to be_nil

View File

@ -106,6 +106,11 @@ describe Google::Auth::UserRefreshCredentials do
expect(UserRefreshCredentials.from_env(@scope)).to be_nil expect(UserRefreshCredentials.from_env(@scope)).to be_nil
end end
it "returns nil if the GOOGLE_APPLICATION_CREDENTIALS is empty" do
ENV[@var_name] = ""
expect(UserRefreshCredentials.from_env(@scope)).to be_nil
end
it "fails if the GOOGLE_APPLICATION_CREDENTIALS path does not exist" do it "fails if the GOOGLE_APPLICATION_CREDENTIALS path does not exist" do
ENV.delete @var_name unless ENV[@var_name].nil? ENV.delete @var_name unless ENV[@var_name].nil?
expect(UserRefreshCredentials.from_env(@scope)).to be_nil expect(UserRefreshCredentials.from_env(@scope)).to be_nil