From 6f5f6bf999b86d7e731892b4d44b2ab8b355fa09 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Mon, 11 May 2020 11:11:55 -0400 Subject: [PATCH] builder/amazon Fix invalid pointer issue for non SSMAgengtEnabled builds Tests before change ``` panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1392ca2] goroutine 299 [running]: github.com/hashicorp/packer/builder/amazon/common.(*StepCreateSSMTunnel).Cleanup(0xc0003dc460, 0x4d1a4c0, 0xc0006e9800) /home/wilken/Development/packer/builder/amazon/common/step_create_ssm_tunnel.go:95 +0xf2 github.com/hashicorp/packer/helper/multistep.(*BasicRunner).Run(0xc0006e98f0, 0x4d408c0, 0xc00065fcc0, 0x4d1a4c0, 0xc0006e9800) /home/wilken/Development/packer/helper/multistep/basic_runner.go:79 +0x2c6 github.com/hashicorp/packer/builder/amazon/ebs.(*Builder).Run(0xc000726800, 0x4d408c0, 0xc00065fcc0, 0x4d5e300, 0xc0006e8d80, 0x4cc7220, 0xc000434120, 0x0, 0x0, 0x0, ...) /home/wilken/Development/packer/builder/amazon/ebs/builder.go:330 +0x17e2 github.com/hashicorp/packer/packer.(*CoreBuild).Run(0xc000720500, 0x4d408c0, 0xc00065fcc0, 0x4d5e180, 0xc0006fe510, 0x0, 0x0, 0x0, 0x0, 0x0) /home/wilken/Development/packer/packer/build.go:287 +0x7ef github.com/hashicorp/packer/command.(*BuildCommand).RunContext.func1(0xc0004d14d0, 0xc0003dc3c0, 0xc000441500, 0xa, 0x4d5e1e0, 0xc000720500, 0x4d408c0, 0xc00065fcc0, 0x4d5e180, 0xc0006fe510, ...) /home/wilken/Development/packer/command/build.go:290 +0x189 created by github.com/hashicorp/packer/command.(*BuildCommand).RunContext /home/wilken/Development/packer/command/build.go:284 +0xd5a FAIL github.com/hashicorp/packer/provisioner/shell 188.335s FAIL ``` Test After change ``` --- PASS: TestShellProvisioner (212.39s) --- PASS: TestShellProvisioner/testing_amazon-ebs_builder_against_shell_provisioner (212.39s) PASS ``` --- builder/amazon/common/step_create_ssm_tunnel.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/builder/amazon/common/step_create_ssm_tunnel.go b/builder/amazon/common/step_create_ssm_tunnel.go index e62697c7c..8b9a2fefe 100644 --- a/builder/amazon/common/step_create_ssm_tunnel.go +++ b/builder/amazon/common/step_create_ssm_tunnel.go @@ -89,8 +89,18 @@ func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag) // Cleanup terminates an active session on AWS, which in turn terminates the associated tunnel process running on the local machine. func (s *StepCreateSSMTunnel) Cleanup(state multistep.StateBag) { + if !s.SSMAgentEnabled { + return + } + ui := state.Get("ui").(packer.Ui) + if s.session == nil || s.session.SessionId == nil { + msg := fmt.Sprintf("Unable to find a valid session to instance %q; skipping the termination step", s.instanceId) + ui.Error(msg) + return + } + ssmconn := ssm.New(s.AWSSession) _, err := ssmconn.TerminateSession(&ssm.TerminateSessionInput{SessionId: s.session.SessionId}) if err != nil {