Skip to content

Commit

Permalink
Fix gce metadata recursion issue #1796 (#1801)
Browse files Browse the repository at this point in the history
* Fix gce metadata recursion issue #1796

Only append `?recursive=true` is the generated url is a directory.

Previously the api calls would be:

	`/computeMetadata/v1/?recursive=true/`
	`/computeMetadata/v1/?recursive=true/instance/`
	`/computeMetadata/v1/?recursive=true/instance/instance/`

until #<SystemStackError: stack level too deep>

Signed-off-by: joe.nuspl <nuspl@nvwls.com>

* Avoid frozen string issue

Signed-off-by: joe.nuspl <nuspl@nvwls.com>

---------

Signed-off-by: joe.nuspl <nuspl@nvwls.com>
  • Loading branch information
nvwls authored Jul 18, 2023
1 parent 471ec49 commit 21d7863
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/ohai/mixin/gce_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module GCEMetadata

# Trailing dot to host is added to avoid DNS search path
GCE_METADATA_ADDR ||= "metadata.google.internal."
GCE_METADATA_URL ||= "/computeMetadata/v1/?recursive=true"
GCE_METADATA_URL ||= "/computeMetadata/v1"

# fetch the meta content with a timeout and the required header
def http_get(uri)
Expand All @@ -39,7 +39,9 @@ def http_get(uri)
end

def fetch_metadata(id = "")
response = http_get("#{GCE_METADATA_URL}/#{id}")
url = "#{GCE_METADATA_URL}/#{id}"
url = "#{url}?recursive=true" if url.end_with?("/")
response = http_get(url)
if response.code == "200"
json_data = parse_json(response.body)
if json_data.nil?
Expand Down

0 comments on commit 21d7863

Please sign in to comment.