Unescape private_key loaded from ENV (#132)

This commit is contained in:
Daniel Azuma authored and GitHub committed 2018-06-05 09:56:48 -07:00
1 parent 18611aae6e
commit 9232aa3176
2 files changed
+17 -1

No files matched your search

+10 -1
View File
@@ -58,7 +58,7 @@ module Google
if json_key_io
private_key, client_email = read_json_key(json_key_io)
else
private_key = ENV[CredentialsLoader::PRIVATE_KEY_VAR]
private_key = unescape ENV[CredentialsLoader::PRIVATE_KEY_VAR]
client_email = ENV[CredentialsLoader::CLIENT_EMAIL_VAR]
end
@@ -78,6 +78,15 @@ module Google
[json_key['private_key'], json_key['client_email']]
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 = {})
super(options)
end
+7
View File
@@ -211,6 +211,13 @@ describe Google::Auth::ServiceAccountCredentials do
ENV[CLIENT_EMAIL_VAR] = cred_json[:client_email]
expect(@clz.from_env(@scope)).to_not be_nil
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
describe '#from_well_known_path' do