Skip to content

Commit 3628d6a

Browse files
authored
cleanup: simplify GoogleDefaultCredentials() implementation (#9600)
The helper functions had parameters that were effectively constant. And we already have an `google::cloud::Options` parameter that we could use for any configuration.
1 parent c2056c2 commit 3628d6a

File tree

1 file changed

+9
-31
lines changed

1 file changed

+9
-31
lines changed

google/cloud/internal/oauth2_google_credentials.cc

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ namespace {
4242
// credential file is found, this function returns nullptr to indicate a service
4343
// account file wasn't found.
4444
StatusOr<std::unique_ptr<Credentials>> LoadCredsFromPath(
45-
std::string const& path, bool non_service_account_ok,
46-
absl::optional<std::set<std::string>> service_account_scopes,
47-
absl::optional<std::string> service_account_subject,
48-
Options const& options) {
45+
std::string const& path, Options const& options) {
4946
std::ifstream ifs(path);
5047
if (!ifs.is_open()) {
5148
// We use kUnknown here because we don't know if the file does not exist, or
@@ -61,26 +58,16 @@ StatusOr<std::unique_ptr<Credentials>> LoadCredsFromPath(
6158
std::string cred_type = cred_json.value("type", "no type given");
6259
// If non_service_account_ok==false and the cred_type is authorized_user,
6360
// we'll return "Unsupported credential type (authorized_user)".
64-
if (cred_type == "authorized_user" && non_service_account_ok) {
65-
if (service_account_scopes || service_account_subject) {
66-
// No ptr indicates that the file we found was not a service account file.
67-
return StatusOr<std::unique_ptr<Credentials>>(nullptr);
68-
}
61+
if (cred_type == "authorized_user") {
6962
auto info = ParseAuthorizedUserCredentials(contents, path);
70-
if (!info) {
71-
return info.status();
72-
}
63+
if (!info) return std::move(info).status();
7364
std::unique_ptr<Credentials> ptr =
7465
absl::make_unique<AuthorizedUserCredentials>(*info);
7566
return StatusOr<std::unique_ptr<Credentials>>(std::move(ptr));
7667
}
7768
if (cred_type == "service_account") {
7869
auto info = ParseServiceAccountCredentials(contents, path);
79-
if (!info) {
80-
return info.status();
81-
}
82-
info->subject = std::move(service_account_subject);
83-
info->scopes = std::move(service_account_scopes);
70+
if (!info) return std::move(info).status();
8471
std::unique_ptr<Credentials> ptr =
8572
absl::make_unique<ServiceAccountCredentials>(*info, options);
8673
return StatusOr<std::unique_ptr<Credentials>>(std::move(ptr));
@@ -104,9 +91,6 @@ StatusOr<std::unique_ptr<Credentials>> LoadCredsFromPath(
10491
// file is found, this function returns nullptr to indicate a service account
10592
// file wasn't found.
10693
StatusOr<std::unique_ptr<Credentials>> MaybeLoadCredsFromAdcPaths(
107-
bool non_service_account_ok,
108-
absl::optional<std::set<std::string>> service_account_scopes,
109-
absl::optional<std::string> service_account_subject,
11094
Options const& options = {}) {
11195
// 1) Check if the GOOGLE_APPLICATION_CREDENTIALS environment variable is set.
11296
auto path = GoogleAdcFilePathFromEnvVarOrEmpty();
@@ -128,9 +112,7 @@ StatusOr<std::unique_ptr<Credentials>> MaybeLoadCredsFromAdcPaths(
128112

129113
// If the path was specified, try to load that file; explicitly fail if it
130114
// doesn't exist or can't be read and parsed.
131-
return LoadCredsFromPath(path, non_service_account_ok,
132-
std::move(service_account_scopes),
133-
std::move(service_account_subject), options);
115+
return LoadCredsFromPath(path, options);
134116
}
135117

136118
} // namespace
@@ -139,17 +121,13 @@ StatusOr<std::shared_ptr<Credentials>> GoogleDefaultCredentials(
139121
Options const& options) {
140122
// 1 and 2) Check if the GOOGLE_APPLICATION_CREDENTIALS environment variable
141123
// is set or if the gcloud ADC file exists.
142-
auto creds = MaybeLoadCredsFromAdcPaths(true, {}, {}, options);
143-
if (!creds) {
144-
return StatusOr<std::shared_ptr<Credentials>>(creds.status());
145-
}
146-
if (*creds) {
147-
return StatusOr<std::shared_ptr<Credentials>>(std::move(*creds));
148-
}
124+
auto creds = MaybeLoadCredsFromAdcPaths(options);
125+
if (!creds) return std::move(creds).status();
126+
if (*creds) return std::shared_ptr<Credentials>(*std::move(creds));
149127

150128
// 3) Check for implicit environment-based credentials (GCE, GAE Flexible,
151129
// Cloud Run or GKE Environment).
152-
return StatusOr<std::shared_ptr<Credentials>>(
130+
return std::shared_ptr<Credentials>(
153131
std::make_shared<ComputeEngineCredentials>());
154132
}
155133

0 commit comments

Comments
 (0)