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

View File

@ -128,6 +128,7 @@ describe Google::Auth::Credentials, :private do
DEFAULT_PATHS = ["~/default/path/to/file.txt"].freeze DEFAULT_PATHS = ["~/default/path/to/file.txt"].freeze
end 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(::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("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("PATH_ENV_TEST") { "/unknown/path/to/file.txt" } 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 DEFAULT_PATHS = ["~/default/path/to/file.txt"].freeze
end 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(::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("/fake/path/to/file.txt") { false }
allow(::File).to receive(:file?).with(test_json_env_val) { 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 DEFAULT_PATHS = ["~/default/path/to/file.txt"].freeze
end 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(::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("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("JSON_ENV_DUMMY") { nil } 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 DEFAULT_PATHS = ["~/default/path/to/file.txt"].freeze
end 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(::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("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("JSON_ENV_DUMMY") { nil } 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"] self.paths = ["~/default/path/to/file.txt"]
end 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(::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("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("PATH_ENV_TEST") { "/unknown/path/to/file.txt" } 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"] self.paths = ["~/default/path/to/file.txt"]
end 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(::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("/fake/path/to/file.txt") { false }
allow(::File).to receive(:file?).with(test_json_env_val) { 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"] self.paths = ["~/default/path/to/file.txt"]
end 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(::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("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("JSON_ENV_DUMMY") { nil } 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"] self.paths = ["~/default/path/to/file.txt"]
end 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(::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("/fake/path/to/file.txt") { false }
allow(::ENV).to receive(:[]).with("JSON_ENV_DUMMY") { nil } allow(::ENV).to receive(:[]).with("JSON_ENV_DUMMY") { nil }