From 3aa840d0d83ec327298be03e28fa5e4c7bd24a92 Mon Sep 17 00:00:00 2001 From: Garry Shutler Date: Mon, 29 Mar 2021 11:54:38 +0100 Subject: [PATCH] Check metadata response code Avoids an error in some regions where no response body is provided when IMDS access is disabled. --- pkg/aws/util.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/aws/util.go b/pkg/aws/util.go index 7bd8a1b..5dc2af8 100644 --- a/pkg/aws/util.go +++ b/pkg/aws/util.go @@ -18,8 +18,13 @@ func GetLocalRegion() string { klog.Errorf("unable to get current region information, %v", err) return "" } - defer resp.Body.Close() + + if resp.StatusCode != 200 { + klog.Errorf("unable to get current region information, status code %v", resp.StatusCode) + return "" + } + body, err := ioutil.ReadAll(resp.Body) if err != nil { klog.Errorf("cannot read response from instance metadata, %v", err)