Skip to content

Commit

Permalink
aws/ec2metadata: Fix EC2 Metadata client panic with debug logging (#290)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jasdel authored Apr 10, 2019
1 parent d16cd8b commit d03e547
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions aws/client_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions aws/ec2metadata/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion aws/ec2metadata/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit d03e547

Please sign in to comment.