/
portforward.go
136 lines (109 loc) · 3.15 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package ssm
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/AndreZiviani/aws-fuzzy/internal/ssm_plugin"
"github.com/AndreZiviani/aws-fuzzy/internal/sso"
"github.com/AndreZiviani/aws-fuzzy/internal/tracing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
awsssm "github.com/aws/aws-sdk-go-v2/service/ssm"
opentracing "github.com/opentracing/opentracing-go"
)
func NewPortForward(profile, region, ports string) *PortForward {
pf := PortForward{
Profile: profile,
Region: region,
Ports: ports,
}
return &pf
}
func (p *PortForward) DoPortForward(ctx context.Context, id, local, host, remote string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "ssmportforward")
defer span.Finish()
login := sso.Login{Profile: p.Profile}
creds, err := login.GetCredentials(ctx)
if err != nil {
return err
}
cfg, err := sso.NewAwsConfig(ctx, creds, config.WithRegion(p.Region))
if err != nil {
return err
}
docName := "AWS-StartPortForwardingSessionToRemoteHost" // https://us-east-1.console.aws.amazon.com/systems-manager/documents/AWS-StartPortForwardingSession/description?region=us-east-1
input := &awsssm.StartSessionInput{
DocumentName: &docName,
Parameters: map[string][]string{
"portNumber": []string{remote},
"localPortNumber": []string{local},
"host": []string{host},
},
Target: &id,
}
inputJson, err := json.Marshal(input)
ssmclient := awsssm.NewFromConfig(cfg)
session, err := ssmclient.StartSession(ctx, input)
sessionJson, _ := json.Marshal(session)
if err != nil {
return err
}
/*
// we cant incluse the plugin directly here because it is mostly unmaintained
// maybe this issue will be fixed and we could try to embed the plugin here
// https://github.com/aws/session-manager-plugin/issues/1
input = []string{
"ignored",
string(sessionJson),
p.Region,
"StartSession",
p.Profile,
fmt.Sprintf("{\"Target\":\"%s\"}",
id,
fmt.Sprintf("https://ssm.%s.amazonaws.com", p.Region,
}
session.ValidadeInputAndStartSession(input, os.Stdout)
*/
// for now we have to use the embeded the binary
ssm_plugin.RunPlugin(
string(sessionJson),
p.Region,
"StartSession",
p.Profile,
string(inputJson),
)
_, err = ssmclient.TerminateSession(ctx, &awsssm.TerminateSessionInput{
SessionId: session.SessionId,
})
return err
}
func (p *PortForward) Execute(ctx context.Context) error {
closer, err := tracing.InitTracing()
if err != nil {
fmt.Printf("failed to initialize tracing, %s\n", err)
}
defer closer.Close()
tracer := opentracing.GlobalTracer()
span, ctx := opentracing.StartSpanFromContextWithTracer(ctx, tracer, "ssm")
login := sso.Login{Profile: p.Profile}
creds, err := login.GetCredentials(ctx)
if err != nil {
return err
}
cfg, err := sso.NewAwsConfig(ctx, creds, config.WithRegion(p.Region))
if err != nil {
return err
}
instances, err := GetInstances(ctx, cfg)
if err != nil {
return err
}
span.Finish()
instance, err := tui(instances)
if err != nil {
return err
}
ports := strings.Split(p.Ports, ":")
return p.DoPortForward(ctx, aws.ToString(instance.InstanceId), ports[0], ports[1], ports[2])
}