2020-03-10 16:39:19 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-04-29 14:58:36 -04:00
|
|
|
"log"
|
2020-03-12 22:26:38 -04:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2020-03-10 16:39:19 -04:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2020-03-12 22:26:38 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2020-03-10 16:39:19 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ssm"
|
2020-03-30 07:47:31 -04:00
|
|
|
"github.com/hashicorp/packer/common/net"
|
2020-03-12 22:26:38 -04:00
|
|
|
"github.com/hashicorp/packer/common/retry"
|
2020-03-10 16:39:19 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepCreateSSMTunnel struct {
|
2020-04-29 14:58:36 -04:00
|
|
|
AWSSession *session.Session
|
|
|
|
Region string
|
|
|
|
LocalPortNumber int
|
|
|
|
RemotePortNumber int
|
|
|
|
SSMAgentEnabled bool
|
|
|
|
instanceId string
|
|
|
|
session *ssm.StartSessionOutput
|
2020-03-10 16:39:19 -04:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:58:36 -04:00
|
|
|
// Run executes the Packer build step that creates a session tunnel.
|
2020-03-10 16:39:19 -04:00
|
|
|
func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-04-01 17:33:44 -04:00
|
|
|
if !s.SSMAgentEnabled {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:39:19 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-04-29 14:58:36 -04:00
|
|
|
if err := s.ConfigureLocalHostPort(ctx); err != nil {
|
2020-03-30 07:47:31 -04:00
|
|
|
err := fmt.Errorf("error finding an available port to initiate a session tunnel: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-03-12 22:26:38 -04:00
|
|
|
instance, ok := state.Get("instance").(*ec2.Instance)
|
|
|
|
if !ok {
|
2020-04-29 14:58:36 -04:00
|
|
|
err := fmt.Errorf("error encountered in obtaining target instance id for session tunnel")
|
2020-03-12 22:26:38 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2020-04-01 17:33:44 -04:00
|
|
|
s.instanceId = aws.StringValue(instance.InstanceId)
|
2020-03-30 07:47:31 -04:00
|
|
|
|
2020-04-29 14:58:36 -04:00
|
|
|
log.Printf("Starting PortForwarding session to instance %q on local port %q to remote port %q", s.instanceId, s.LocalPortNumber, s.RemotePortNumber)
|
|
|
|
input := s.BuildTunnelInputForInstance(s.instanceId)
|
|
|
|
ssmconn := ssm.New(s.AWSSession)
|
2020-03-12 22:26:38 -04:00
|
|
|
var output *ssm.StartSessionOutput
|
2020-04-29 14:58:36 -04:00
|
|
|
err := retry.Config{
|
2020-03-12 22:26:38 -04:00
|
|
|
ShouldRetry: func(err error) bool { return isAWSErr(err, "TargetNotConnected", "") },
|
2020-04-01 17:33:44 -04:00
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 60 * time.Second, Multiplier: 2}).Linear,
|
2020-04-29 14:58:36 -04:00
|
|
|
}.Run(ctx, func(ctx context.Context) (err error) {
|
2020-03-12 22:26:38 -04:00
|
|
|
output, err = ssmconn.StartSessionWithContext(ctx, &input)
|
|
|
|
return err
|
|
|
|
})
|
2020-03-10 16:39:19 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2020-04-01 17:33:44 -04:00
|
|
|
err = fmt.Errorf("error encountered in starting session for instance %q: %s", s.instanceId, err)
|
2020-03-10 16:39:19 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2020-03-12 22:26:38 -04:00
|
|
|
|
2020-04-29 14:58:36 -04:00
|
|
|
driver := SSMDriver{
|
|
|
|
Region: s.Region,
|
|
|
|
Session: output,
|
|
|
|
SessionParams: input,
|
|
|
|
SessionEndpoint: ssmconn.Endpoint,
|
2020-03-12 22:26:38 -04:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:58:36 -04:00
|
|
|
if err := driver.StartSession(ctx); err != nil {
|
|
|
|
err = fmt.Errorf("error encountered in establishing a tunnel %s", err)
|
2020-03-12 22:26:38 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:58:36 -04:00
|
|
|
ui.Message(fmt.Sprintf("PortForwarding session tunnel to instance %q established!", s.instanceId))
|
|
|
|
state.Put("sessionPort", s.LocalPortNumber)
|
2020-03-30 07:47:31 -04:00
|
|
|
|
2020-03-10 16:39:19 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:58:36 -04:00
|
|
|
// Cleanup terminates an active session on AWS, which in turn terminates the associated tunnel process running on the local machine.
|
2020-03-10 16:39:19 -04:00
|
|
|
func (s *StepCreateSSMTunnel) Cleanup(state multistep.StateBag) {
|
2020-04-29 14:58:36 -04:00
|
|
|
if s.session == nil {
|
2020-03-10 16:39:19 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ssmconn := ssm.New(s.AWSSession)
|
2020-04-29 14:58:36 -04:00
|
|
|
_, err := ssmconn.TerminateSession(&ssm.TerminateSessionInput{SessionId: s.session.SessionId})
|
2020-03-10 16:39:19 -04:00
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("Error terminating SSM Session %q. Please terminate the session manually: %s",
|
2020-04-29 14:58:36 -04:00
|
|
|
aws.StringValue(s.session.SessionId), err)
|
2020-03-10 16:39:19 -04:00
|
|
|
ui.Error(msg)
|
|
|
|
}
|
2020-03-12 22:26:38 -04:00
|
|
|
|
2020-03-10 16:39:19 -04:00
|
|
|
}
|
2020-04-29 14:58:36 -04:00
|
|
|
|
|
|
|
// ConfigureLocalHostPort finds an available port on the localhost that can be used for the remote tunnel.
|
|
|
|
// Defaults to using s.LocalPortNumber if it is set.
|
|
|
|
func (s *StepCreateSSMTunnel) ConfigureLocalHostPort(ctx context.Context) error {
|
|
|
|
if s.LocalPortNumber != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Find an available TCP port for our HTTP server
|
|
|
|
l, err := net.ListenRangeConfig{
|
|
|
|
Min: 8000,
|
|
|
|
Max: 9000,
|
|
|
|
Addr: "0.0.0.0",
|
|
|
|
Network: "tcp",
|
|
|
|
}.Listen(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.LocalPortNumber = l.Port
|
|
|
|
// Stop listening on selected port so that the AWS session-manager-plugin can use it.
|
|
|
|
// The port is closed right before we start the session to avoid two Packer builds from getting the same port - fingers-crossed
|
|
|
|
l.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateSSMTunnel) BuildTunnelInputForInstance(instance string) ssm.StartSessionInput {
|
|
|
|
dst, src := strconv.Itoa(s.RemotePortNumber), strconv.Itoa(s.LocalPortNumber)
|
|
|
|
params := map[string][]*string{
|
|
|
|
"portNumber": []*string{aws.String(dst)},
|
|
|
|
"localPortNumber": []*string{aws.String(src)},
|
|
|
|
}
|
|
|
|
|
|
|
|
input := ssm.StartSessionInput{
|
|
|
|
DocumentName: aws.String("AWS-StartPortForwardingSession"),
|
|
|
|
Parameters: params,
|
|
|
|
Target: aws.String(instance),
|
|
|
|
}
|
|
|
|
|
|
|
|
return input
|
|
|
|
}
|