This repository has been archived by the owner on Sep 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
403 lines (329 loc) · 8.18 KB
/
main.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Command cloudshell gives access to Google Cloud Shell from the terminal.
package main // import "go.astrophena.name/cloudshell"
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"time"
"github.com/urfave/cli/v2"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
cloudshell "google.golang.org/api/cloudshell/v1"
"google.golang.org/api/option"
)
const envName = "users/me/environments/default"
var version = "devel"
func main() {
if version == "devel" {
bi, ok := debug.ReadBuildInfo()
if ok {
version = strings.TrimPrefix(bi.Main.Version, "v")
}
}
log.SetFlags(0)
app := &cli.App{
Name: "cloudshell",
Usage: "Connect to Google Cloud Shell from the terminal.",
EnableBashCompletion: true,
Version: version,
HideHelpCommand: true,
Commands: []*cli.Command{
{
Name: "connect",
Aliases: []string{"c"},
Usage: "Establish an interactive SSH session with Cloud Shell",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
Aliases: []string{"k"},
Usage: "Path to the SSH key that should be used for authentication.",
},
&cli.StringFlag{
Name: "fwd",
Aliases: []string{"f"},
Usage: "Forward local port to remote port: [local]:[remote]",
},
},
Action: cmdConnect,
},
{
Name: "info",
Aliases: []string{"i"},
Usage: "Print information about the environment",
Action: cmdInfo,
},
{
Name: "key",
Aliases: []string{"k"},
Usage: "Manage public keys associated with the Cloud Shell",
HideHelpCommand: true,
Subcommands: []*cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "List public keys associated with the Cloud Shell",
Action: cmdKeyList,
},
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a public SSH key to the Cloud Shell",
ArgsUsage: "[public key, e.g. $(cat ~/.ssh/id_rsa.pub)]",
Action: cmdKeyAdd,
},
{
Name: "remove",
Aliases: []string{"r"},
Usage: "Remove a public SSH key from the Cloud Shell",
ArgsUsage: "[key]",
Action: cmdKeyRemove,
},
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func cmdInfo(c *cli.Context) error {
s, err := service()
if err != nil {
return err
}
e, err := s.Users.Environments.Get(envName).Do()
if err != nil {
return err
}
state := strings.ToLower(e.State)
state = strings.Title(state) + "."
fmt.Println(state)
fmt.Printf("Docker Image: %s\n", e.DockerImage)
if e.SshHost != "" && e.SshPort != 0 && e.SshUsername != "" {
fmt.Println("SSH connection details:")
fmt.Printf(" Host: %s\n", e.SshHost)
fmt.Printf(" Port: %d\n", e.SshPort)
fmt.Printf(" Username: %s\n", e.SshUsername)
} else {
fmt.Println("SSH is unavaliable.")
}
return nil
}
func cmdConnect(c *cli.Context) error {
s, err := service()
if err != nil {
return err
}
e, err := s.Users.Environments.Get(envName).Do()
if err != nil {
return err
}
if e.State == "STARTING" || e.State == "SUSPENDED" {
if err := start(e, s); err != nil {
return err
}
}
e, err = s.Users.Environments.Get(envName).Do()
if err != nil {
return err
}
return ssh(e, s, c.String("key"), c.String("fwd"))
}
func cmdKeyList(c *cli.Context) error {
s, err := service()
if err != nil {
return err
}
e, err := s.Users.Environments.Get(envName).Do()
if err != nil {
return err
}
for _, k := range e.PublicKeys {
log.Println(k)
}
return nil
}
func cmdKeyAdd(c *cli.Context) error {
key := c.Args().Get(0)
if key == "" {
return errors.New("key is required")
}
s, err := service()
if err != nil {
return err
}
_, err = s.Users.Environments.AddPublicKey(envName, &cloudshell.AddPublicKeyRequest{Key: key}).Do()
if err != nil {
return err
}
return nil
}
func cmdKeyRemove(c *cli.Context) error {
key := c.Args().Get(0)
if key == "" {
return errors.New("key is required")
}
s, err := service()
if err != nil {
return err
}
if _, err := s.Users.Environments.RemovePublicKey(envName, &cloudshell.RemovePublicKeyRequest{Key: key}).Do(); err != nil {
return err
}
return nil
}
func start(e *cloudshell.Environment, s *cloudshell.Service) error {
if _, err := s.Users.Environments.Start(e.Name, &cloudshell.StartEnvironmentRequest{}).Do(); err != nil {
return err
}
log.Println("Environment is starting...")
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
loop:
for {
e, err := s.Users.Environments.Get(e.Name).Do()
if err != nil {
return err
}
if e.State == "RUNNING" {
break loop
}
select {
case <-ticker.C:
continue
case <-interrupt:
log.Println("exiting")
os.Exit(0)
}
}
return nil
}
func ssh(e *cloudshell.Environment, s *cloudshell.Service, key, fwd string) error {
if e.SshHost == "" || e.SshPort == 0 || e.SshUsername == "" {
return errors.New("ssh is unavaliable")
}
host := e.SshUsername + "@" + e.SshHost
port := strconv.FormatInt(e.SshPort, 10)
path, err := exec.LookPath("ssh")
if err != nil {
return err
}
cmd := exec.Command(path, host, "-p", port, "-o", "StrictHostKeyChecking=no")
if key != "" {
cmd.Args = append(cmd.Args, "-i", key)
}
if fwd != "" {
cmd.Args = append(cmd.Args, "-L", fwd)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func service() (*cloudshell.Service, error) {
c, err := client()
if err != nil {
return nil, err
}
return cloudshell.NewService(context.Background(), option.WithHTTPClient(c))
}
func client() (*http.Client, error) {
dir, err := configDir()
if err != nil {
return nil, err
}
path, err := clientSecretsFile(dir)
if err != nil {
return nil, err
}
b, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("unable to read client secrets file: %w", err)
}
cfg, err := google.ConfigFromJSON(b, cloudshell.CloudPlatformScope+" email")
if err != nil {
return nil, fmt.Errorf("unable to parse client secrets file: %w", err)
}
cf := credsFile(dir)
tok, err := tokenFromFile(cf)
if err != nil {
tok, err = token(cfg)
if err != nil {
return nil, err
}
if err := saveToken(cf, tok); err != nil {
return nil, err
}
}
return cfg.Client(context.Background(), tok), nil
}
func token(config *oauth2.Config) (*oauth2.Token, error) {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the "+
"authorization code: %v\n", authURL)
var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
return nil, fmt.Errorf("unable to read authorization code: %w", err)
}
tok, err := config.Exchange(context.Background(), authCode)
if err != nil {
return nil, fmt.Errorf("unable to retrieve token: %w", err)
}
return tok, nil
}
func tokenFromFile(path string) (*oauth2.Token, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
var tok oauth2.Token
return &tok, json.NewDecoder(f).Decode(&tok)
}
func saveToken(path string, token *oauth2.Token) error {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer f.Close()
return json.NewEncoder(f).Encode(token)
}
func configDir() (string, error) {
ucd, err := os.UserConfigDir()
if err != nil {
return "", err
}
dir := filepath.Join(ucd, "cloudshell")
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0700); err != nil {
return "", err
}
}
return dir, nil
}
func clientSecretsFile(dir string) (string, error) {
path := filepath.Join(dir, "client_secrets.json")
if _, err := os.Stat(path); os.IsNotExist(err) {
return "", fmt.Errorf(
"client_secrets.json is missing in %v.\nSee https://github.com/astrophena/cloudshell#setup for setup instructions.",
dir,
)
}
return path, nil
}
func credsFile(dir string) string { return filepath.Join(dir, "creds.json") }