-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
213 lines (190 loc) · 4.25 KB
/
Copy pathmain.go
File metadata and controls
213 lines (190 loc) · 4.25 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"fmt"
"os"
"os/exec"
osuser "os/user"
"path/filepath"
"strings"
)
const (
// ssh options which takes no value
optsNoValue = "1246AaCfGgKkMNnqsTtVvXxYy"
// ssh options which takes value
optsWithValue = "BbcDEeFIiJLlmOoPpRSWw"
)
// awscli command available
var awsAvailable bool
func init() {
_, err := exec.LookPath("aws")
awsAvailable = err == nil
// _, err = exec.LookPath("ssh")
// _, err2 := exec.LookPath("ssh-keygen")
// sshAvailable = err == nil && err2 == nil
}
func hostIsEc2Instance(host string) bool {
// `^i-([\u\l\d]{8}|[\u\l\d]{17})^`
if !strings.HasPrefix(host, "i-") {
return false
}
hostr := []rune(host)
hostlen := len(hostr)
if hostlen != 10 && hostlen != 19 {
return false
}
for i := 2; i < hostlen; i++ {
c := hostr[i]
if (c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9') {
return false
}
}
return true
}
// values for ec2 instance connect
type awsargs struct {
user string
instId string
// identity file is specified only if the user explicitly specifies it
ident string
}
// parse arguments to get values for ec2 instance connect
// Returns:
// 1. arguments after parsing
// 2. values for ec2 instance connect
func argparse() ([]string, *awsargs) {
args := os.Args[1:]
// if aws command is not available, checking process is not needed anymore
if !awsAvailable {
return args, nil
}
user, host, ident := "", "", ""
acnt := len(args)
i := 0
argloop:
for i < acnt {
arg := args[i]
if arg[0] == '-' {
if len(arg) == 1 {
// arg is just '-' (invalid)
return args, nil
}
argr := []rune(arg[1:])
arglen := len(argr)
charloop:
for ci, c := range argr {
if strings.ContainsRune(optsNoValue, c) {
continue charloop
} else if strings.ContainsRune(optsWithValue, c) {
if ci == arglen-1 {
i++ // skip the next arg as its value
// `-l value`
if c == 'l' {
user = args[i]
} else if c == 'i' {
ident = args[i]
}
} else {
// `-lvalue`
if c == 'l' {
user = string(argr[ci+1:])
} else if c == 'i' {
ident = string(argr[ci+1:])
}
}
i++ // when continue argloop, i should be increased
continue argloop
} else {
// if the option is not in the list, it should be invalid or unknown option
return args, nil
}
}
} else {
// user@host or host
atat := strings.IndexRune(arg, '@')
if atat == -1 {
u, err := osuser.Current()
if err != nil {
return args, nil
}
user = u.Username
host = arg
} else {
host = arg[atat+1:]
if user == "" {
user = arg[:atat]
}
}
// host is ec2 instance id pattern or not
if hostIsEc2Instance(host) {
return args, &awsargs{user, host, ident}
} else {
return args, nil
}
}
i++
}
return args, nil
}
// launch ssh command
func ssh(args []string) int {
ssh := exec.Command("ssh", args...)
ssh.Stdout = os.Stdout
ssh.Stderr = os.Stderr
ssh.Stdin = os.Stdin
ssh.Run()
return ssh.ProcessState.ExitCode()
}
func createSubmitIdent(arg *awsargs, tmpDir string) (string, error) {
priv := filepath.Join(tmpDir, "id_eish")
pub := filepath.Join(tmpDir, "id_eish.pub")
exec.Command("ssh-keygen", "-t", "rsa", "-N", "", "-f", priv).Run()
privAbs, err := filepath.Abs(priv)
if err != nil {
return "", err
}
pubAbs, err := filepath.Abs(pub)
if err != nil {
return "", err
}
pubUrl := "file://" + pubAbs
exec.Command(
"aws",
"ec2-instance-connect",
"send-ssh-public-key",
"--instance-id",
arg.instId,
"--instance-os-user",
arg.user,
"--ssh-public-key",
pubUrl,
).Run()
return privAbs, nil
}
func main() {
args, aws := argparse()
if aws == nil {
// fallback to original ssh command
os.Exit(ssh(args))
}
tmpDir, err := os.MkdirTemp("", "eish-tmp-")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(ssh(args))
}
defer os.RemoveAll(tmpDir)
customOpts := []string{
"-o",
"ProxyCommand=aws ec2-instance-connect open-tunnel --instance-id %h",
"-o",
"IdentityAgent=none",
}
if aws.ident == "" {
privKey, err := createSubmitIdent(aws, tmpDir)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(ssh(args))
}
customOpts = append(customOpts, "-i", privKey)
}
os.Exit(ssh(append(customOpts, args...)))
}