feat: Support an environment variable to suppress Cloud SDK credentials warnings

This commit is contained in:
Cera 2019-10-09 04:00:36 +11:00 committed by Daniel Azuma
parent be7599f536
commit 9c46b3b049
2 changed files with 16 additions and 7 deletions

View File

@ -64,13 +64,13 @@ module Google
CLOUD_SDK_CLIENT_ID = "764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.app"\
"s.googleusercontent.com".freeze
CLOUD_SDK_CREDENTIALS_WARNING = "Your application has authenticated "\
"using end user credentials from Google Cloud SDK. We recommend that "\
"most server applications use service accounts instead. If your "\
"application continues to use end user credentials from Cloud SDK, "\
'you might receive a "quota exceeded" or "API not enabled" error. For'\
" more information about service accounts, see "\
"https://cloud.google.com/docs/authentication/.".freeze
CLOUD_SDK_CREDENTIALS_WARNING = "Your application has authenticated using end user "\
"credentials from Google Cloud SDK. We recommend that most server applications use "\
"service accounts instead. If your application continues to use end user credentials "\
'from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For '\
"more information about service accounts, see "\
"https://cloud.google.com/docs/authentication/. To suppress this message, set the "\
"GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS environment variable.".freeze
# make_creds proxies the construction of a credentials instance
#
@ -167,6 +167,7 @@ module Google
# Issues warning if cloud sdk client id is used
def warn_if_cloud_sdk_credentials client_id
return if ENV["GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS"]
warn CLOUD_SDK_CREDENTIALS_WARNING if client_id == CLOUD_SDK_CLIENT_ID
end

View File

@ -128,6 +128,7 @@ describe Google::Auth::Credentials, :private do
DEFAULT_PATHS = ["~/default/path/to/file.txt"].freeze
end
allow(::ENV).to receive(:[]).with("GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS") { "true" }
allow(::ENV).to receive(:[]).with("PATH_ENV_DUMMY") { "/fake/path/to/file.txt" }
allow(::File).to receive(:file?).with("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("PATH_ENV_TEST") { "/unknown/path/to/file.txt" }
@ -164,6 +165,7 @@ describe Google::Auth::Credentials, :private do
DEFAULT_PATHS = ["~/default/path/to/file.txt"].freeze
end
allow(::ENV).to receive(:[]).with("GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS") { "true" }
allow(::ENV).to receive(:[]).with("PATH_ENV_DUMMY") { "/fake/path/to/file.txt" }
allow(::File).to receive(:file?).with("/fake/path/to/file.txt") { false }
allow(::File).to receive(:file?).with(test_json_env_val) { false }
@ -198,6 +200,7 @@ describe Google::Auth::Credentials, :private do
DEFAULT_PATHS = ["~/default/path/to/file.txt"].freeze
end
allow(::ENV).to receive(:[]).with("GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS") { "true" }
allow(::ENV).to receive(:[]).with("PATH_ENV_DUMMY") { "/fake/path/to/file.txt" }
allow(::File).to receive(:file?).with("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("JSON_ENV_DUMMY") { nil }
@ -232,6 +235,7 @@ describe Google::Auth::Credentials, :private do
DEFAULT_PATHS = ["~/default/path/to/file.txt"].freeze
end
allow(::ENV).to receive(:[]).with("GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS") { "true" }
allow(::ENV).to receive(:[]).with("PATH_ENV_DUMMY") { "/fake/path/to/file.txt" }
allow(::File).to receive(:file?).with("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("JSON_ENV_DUMMY") { nil }
@ -310,6 +314,7 @@ describe Google::Auth::Credentials, :private do
self.paths = ["~/default/path/to/file.txt"]
end
allow(::ENV).to receive(:[]).with("GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS") { "true" }
allow(::ENV).to receive(:[]).with("PATH_ENV_DUMMY") { "/fake/path/to/file.txt" }
allow(::File).to receive(:file?).with("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("PATH_ENV_TEST") { "/unknown/path/to/file.txt" }
@ -345,6 +350,7 @@ describe Google::Auth::Credentials, :private do
self.paths = ["~/default/path/to/file.txt"]
end
allow(::ENV).to receive(:[]).with("GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS") { "true" }
allow(::ENV).to receive(:[]).with("PATH_ENV_DUMMY") { "/fake/path/to/file.txt" }
allow(::File).to receive(:file?).with("/fake/path/to/file.txt") { false }
allow(::File).to receive(:file?).with(test_json_env_val) { false }
@ -378,6 +384,7 @@ describe Google::Auth::Credentials, :private do
self.paths = ["~/default/path/to/file.txt"]
end
allow(::ENV).to receive(:[]).with("GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS") { "true" }
allow(::ENV).to receive(:[]).with("PATH_ENV_DUMMY") { "/fake/path/to/file.txt" }
allow(::File).to receive(:file?).with("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("JSON_ENV_DUMMY") { nil }
@ -411,6 +418,7 @@ describe Google::Auth::Credentials, :private do
self.paths = ["~/default/path/to/file.txt"]
end
allow(::ENV).to receive(:[]).with("GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS") { "true" }
allow(::ENV).to receive(:[]).with("PATH_ENV_DUMMY") { "/fake/path/to/file.txt" }
allow(::File).to receive(:file?).with("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("JSON_ENV_DUMMY") { nil }