-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
portforward.go
108 lines (98 loc) · 2.85 KB
/
portforward.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package ecsta
import (
"context"
"fmt"
"io"
"strconv"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/aws/aws-sdk-go-v2/service/ssm"
)
type PortforwardOption struct {
ID string `help:"task ID"`
Container string `help:"container name"`
LocalPort int `help:"local port"`
RemotePort int `help:"remote port"`
RemoteHost string `help:"remote host"`
L string `name:"L" help:"short expression of local-port:remote-host:remote-port" short:"L"`
Family *string `help:"task definition family name"`
Service *string `help:"ECS service name"`
stdout io.Writer
stderr io.Writer
}
func (opt *PortforwardOption) ParseL() error {
if opt.L == "" {
return nil
}
parts := strings.SplitN(opt.L, ":", 3)
if len(parts) != 3 {
return fmt.Errorf("invalid format: %s", opt.L)
}
if parts[0] != "" {
localPort, err := strconv.Atoi(parts[0])
if err != nil {
return fmt.Errorf("invalid local port: %s", parts[0])
}
opt.LocalPort = localPort
} else {
opt.LocalPort = 0 // use ephemeral port
}
remotePort, err := strconv.Atoi(parts[2])
if err != nil {
return fmt.Errorf("invalid remote port: %s", parts[2])
}
opt.RemoteHost = parts[1]
opt.RemotePort = remotePort
return nil
}
func (app *Ecsta) RunPortforward(ctx context.Context, opt *PortforwardOption) error {
if err := opt.ParseL(); err != nil {
return err
}
if opt.RemotePort == 0 {
return fmt.Errorf("remote-port must be specified")
}
if err := app.SetCluster(ctx); err != nil {
return err
}
task, err := app.findTask(ctx, &optionFindTask{
id: opt.ID, family: opt.Family, service: opt.Service,
selectFunc: selectFuncExcludeStopped,
})
if err != nil {
return fmt.Errorf("failed to select tasks: %w", err)
}
name, err := app.findContainerName(ctx, task, opt.Container)
if err != nil {
return fmt.Errorf("failed to select containers: %w", err)
}
opt.Container = name
target, err := ssmRequestTarget(task, opt.Container)
if err != nil {
return fmt.Errorf("failed to build ssm request parameters: %w", err)
}
in := &ssm.StartSessionInput{
Target: aws.String(target),
DocumentName: aws.String("AWS-StartPortForwardingSession"),
Parameters: map[string][]string{
"portNumber": {strconv.Itoa(opt.RemotePort)},
"localPortNumber": {strconv.Itoa(opt.LocalPort)},
},
Reason: aws.String("port forwarding"),
}
if opt.RemoteHost != "" {
in.Parameters["host"] = []string{opt.RemoteHost}
in.DocumentName = aws.String("AWS-StartPortForwardingSessionToRemoteHost")
}
res, err := app.ssm.StartSession(ctx, in)
if err != nil {
return fmt.Errorf("failed to start session: %w", err)
}
sess := &types.Session{
SessionId: res.SessionId,
StreamUrl: res.StreamUrl,
TokenValue: res.TokenValue,
}
return app.runSessionManagerPlugin(ctx, task, sess, target, opt.stdout, opt.stderr)
}