-
Notifications
You must be signed in to change notification settings - Fork 194
feat: check product name file on linux for gce #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -100,6 +100,11 @@ class GCECredentials extends CredentialsLoader implements | |||||
| */ | ||||||
| const FLAVOR_HEADER = 'Metadata-Flavor'; | ||||||
|
|
||||||
| /** | ||||||
| * The Linux file which contains the product name. | ||||||
| */ | ||||||
| private const GKE_PRODUCT_NAME_FILE = '/sys/class/dmi/id/product_name'; | ||||||
|
|
||||||
| /** | ||||||
| * Note: the explicit `timeout` and `tries` below is a workaround. The underlying | ||||||
| * issue is that resolving an unknown host on some networks will take | ||||||
|
|
@@ -340,6 +345,22 @@ public static function onGce(callable $httpHandler = null) | |||||
| } catch (ConnectException $e) { | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (PHP_OS === 'Windows') { | ||||||
| // @TODO: implement GCE residency detection on Windows | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| // Detect GCE residency on Linux | ||||||
| return self::detectResidencyLinux(self::GKE_PRODUCT_NAME_FILE); | ||||||
| } | ||||||
|
|
||||||
| private static function detectResidencyLinux(string $productNameFile): bool | ||||||
| { | ||||||
| if (file_exists($productNameFile)) { | ||||||
| $productName = trim((string) file_get_contents($productNameFile)); | ||||||
| return 0 === strpos($productName, 'Google'); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe lowercase is better?
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The is determined by the Operating system, and the string is uppercase (literally, Why do you think lowercase is better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My only question is: Should we normalize the contents (e.g. force lowercase) in order to avoid any sort of subtle issues with string matching/discrepancies in this file's contents?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Product Name is either To my knowledge, other language implementations have not included such a normalization |
||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.