Skip to content

feat(auth): make isOnComputeEngine more resilient - #343

Open
brianquinlan wants to merge 14 commits into
googleapis:mainfrom
brianquinlan:fix_on_compute_engine
Open

feat(auth): make isOnComputeEngine more resilient#343
brianquinlan wants to merge 14 commits into
googleapis:mainfrom
brianquinlan:fix_on_compute_engine

Conversation

@brianquinlan

Copy link
Copy Markdown
Contributor

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances Google Compute Engine (GCE) detection by adding static detection via the Linux DMI BIOS product name, introducing retry logic, and falling back to a direct IP ping (169.254.169.254) if the metadata host ping fails. However, two issues were identified in the feedback: first, the retry loop incorrectly returns false immediately on an invalid response (such as a non-200 status or missing header), which prevents subsequent retries and fallbacks; second, increasing the default timeout to 3 seconds with 3 retries and IP fallback can cause up to a 12-second delay during startup in non-GCE environments, so a shorter timeout of 1 second is recommended.

Comment thread pkgs/google_cloud_auth/lib/src/compute_engine_credentials.dart Outdated
Comment thread pkgs/google_cloud_auth/lib/src/compute_engine_credentials.dart Outdated
@brianquinlan

Copy link
Copy Markdown
Contributor Author

/gbcrun

@brianquinlan

Copy link
Copy Markdown
Contributor Author

/gcbrun

@brianquinlan

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Google Compute Engine (GCE) detection logic by introducing internalIsOnComputeEngine. This new implementation adds retry logic for transient HTTP errors, a request timeout, and a fallback to static GCE detection on Linux via the DMI BIOS product name. It also updates the test suite to thoroughly cover these scenarios. The review feedback points out a potential unhandled asynchronous exception in Dart when a request times out and subsequently fails without an active listener. It suggests attaching .catchError immediately upon request creation to prevent this, which also allows for simplifying the client cleanup logic in the finally block.

Comment thread pkgs/google_cloud_auth/lib/src/compute_engine_credentials.dart Outdated
Comment thread pkgs/google_cloud_auth/lib/src/compute_engine_credentials.dart
@brianquinlan

Copy link
Copy Markdown
Contributor Author

/gbcrun

1 similar comment
@brianquinlan

Copy link
Copy Markdown
Contributor Author

/gbcrun

@brianquinlan

Copy link
Copy Markdown
Contributor Author

/gcbrun

Comment on lines +45 to +47
if (!isLinux) {
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional] I like to omit braces for guard clauses that fit on one line.

Suggested change
if (!isLinux) {
return false;
}
if (!isLinux) return false;

}
try {
final content = await File(path).readAsString();
return content.trim().startsWith('Google');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the standard pattern for other SDKs too?

@internal
Future<bool> internalIsOnComputeEngine({
http.Client? client,
String? Function(String name)? getEnvironmentVariable,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] How about readEnvironment?

// which the `http.Client` API contract doesn't allow. Therefore, only
// tests are allowed to provide `client`.
if (closeClient) {
unawaited(Future.wait(inFlightRequests).whenComplete(httpClient.close));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit]

Suggested change
unawaited(Future.wait(inFlightRequests).whenComplete(httpClient.close));
unawaited(inFlightRequests.wait.whenComplete(httpClient.close));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants