From d03e5478b032f13dde5910d0f05adcd08be8bdb6 Mon Sep 17 00:00:00 2001 From: Jason Del Ponte Date: Wed, 10 Apr 2019 11:59:44 -0700 Subject: [PATCH] aws/ec2metadata: Fix EC2 Metadata client panic with debug logging (#290) Fixes a panic that could occur witihin the EC2 Metadata client when both AWS_EC2_METADATA_DISABLED env var is set and log level is LogDebugWithHTTPBody. The SDK's client response body debug functionality would panic because the Request.HTTPResponse value was not specified. Updates the client debug loggers to check if the response is set before attempting to log. V2 SDK port of: aws/aws-sdk-go#2461 --- aws/client_logger.go | 6 ++++++ aws/ec2metadata/service.go | 3 +++ aws/ec2metadata/service_test.go | 5 ++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/aws/client_logger.go b/aws/client_logger.go index f3e2e86a016..5f1c90d5bcc 100644 --- a/aws/client_logger.go +++ b/aws/client_logger.go @@ -71,6 +71,12 @@ const logRespErrMsg = `DEBUG ERROR: Response %s/%s: func logResponse(r *Request) { lw := &logWriter{r.Config.Logger, bytes.NewBuffer(nil)} + if r.HTTPResponse.Body == nil { + lw.Logger.Log(fmt.Sprintf(logRespErrMsg, + r.Metadata.ServiceName, r.Operation.Name, "request's HTTPResponse is nil")) + return + } + r.HTTPResponse.Body = &teeReaderCloser{ Reader: io.TeeReader(r.HTTPResponse.Body, lw), Source: r.HTTPResponse.Body, diff --git a/aws/ec2metadata/service.go b/aws/ec2metadata/service.go index 5c0a7e69cde..fb9aba2e338 100644 --- a/aws/ec2metadata/service.go +++ b/aws/ec2metadata/service.go @@ -58,6 +58,9 @@ func New(config aws.Config) *EC2Metadata { svc.Handlers.Send.SwapNamed(aws.NamedHandler{ Name: defaults.SendHandler.Name, Fn: func(r *aws.Request) { + r.HTTPResponse = &http.Response{ + Header: http.Header{}, + } r.Error = awserr.New( aws.ErrCodeRequestCanceled, "EC2 IMDS access disabled via "+disableServiceEnvVar+" env var", diff --git a/aws/ec2metadata/service_test.go b/aws/ec2metadata/service_test.go index d5615309e3b..8cc05945923 100644 --- a/aws/ec2metadata/service_test.go +++ b/aws/ec2metadata/service_test.go @@ -18,7 +18,10 @@ func TestClientDisableIMDS(t *testing.T) { os.Setenv("AWS_EC2_METADATA_DISABLED", "true") - svc := ec2metadata.New(unit.Config()) + cfg := unit.Config() + cfg.LogLevel = aws.LogDebugWithHTTPBody + + svc := ec2metadata.New(cfg) resp, err := svc.Region() if err == nil { t.Fatalf("expect error, got none")