Unescape private_key loaded from ENV (#132)

This commit is contained in:
Daniel Azuma 2018-06-05 09:56:48 -07:00 committed by GitHub
parent 18611aae6e
commit 9232aa3176
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -58,7 +58,7 @@ module Google
if json_key_io if json_key_io
private_key, client_email = read_json_key(json_key_io) private_key, client_email = read_json_key(json_key_io)
else else
private_key = ENV[CredentialsLoader::PRIVATE_KEY_VAR] private_key = unescape ENV[CredentialsLoader::PRIVATE_KEY_VAR]
client_email = ENV[CredentialsLoader::CLIENT_EMAIL_VAR] client_email = ENV[CredentialsLoader::CLIENT_EMAIL_VAR]
end end
@ -78,6 +78,15 @@ module Google
[json_key['private_key'], json_key['client_email']] [json_key['private_key'], json_key['client_email']]
end end
# Handles certain escape sequences that sometimes appear in input.
# Specifically, interprets the "\n" sequence for newline, and removes
# enclosing quotes.
def self.unescape(str)
str = str.gsub '\n', "\n"
str = str[1..-2] if str.start_with?('"') && str.end_with?('"')
str
end
def initialize(options = {}) def initialize(options = {})
super(options) super(options)
end end

View File

@ -211,6 +211,13 @@ describe Google::Auth::ServiceAccountCredentials do
ENV[CLIENT_EMAIL_VAR] = cred_json[:client_email] ENV[CLIENT_EMAIL_VAR] = cred_json[:client_email]
expect(@clz.from_env(@scope)).to_not be_nil expect(@clz.from_env(@scope)).to_not be_nil
end end
it 'succeeds when GOOGLE_PRIVATE_KEY is escaped' do
escaped_key = cred_json[:private_key].gsub "\n", '\n'
ENV[PRIVATE_KEY_VAR] = "\"#{escaped_key}\""
ENV[CLIENT_EMAIL_VAR] = cred_json[:client_email]
expect(@clz.from_env(@scope)).to_not be_nil
end
end end
describe '#from_well_known_path' do describe '#from_well_known_path' do