Skip to content

Commit b32c123

Browse files
committed
[local-app] auto-establish SSH connection
1 parent e2f4da3 commit b32c123

File tree

7 files changed

+633
-28
lines changed

7 files changed

+633
-28
lines changed

components/local-app/BUILD.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ packages:
66
- go.sum
77
- "**/*.go"
88
deps:
9+
- components/supervisor-api/go:lib
910
- components/gitpod-protocol/go:lib
1011
config:
1112
packaging: app

components/local-app/cmd/debug/main.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import (
2121
)
2222

2323
func main() {
24-
keyring.MockInit()
2524
app := cli.App{
2625
Name: "local-app-debug",
2726
Commands: []*cli.Command{
2827
{
2928
Name: "login",
3029
Action: func(c *cli.Context) error {
30+
keyring.MockInit()
3131
_, err := auth.Login(context.Background(), auth.LoginOpts{
3232
GitpodURL: c.String("gitpod-host"),
3333
})
@@ -49,7 +49,10 @@ func main() {
4949
{
5050
Name: "run",
5151
Action: func(c *cli.Context) error {
52-
return run(c.String("gitpod-host"))
52+
if c.Bool("mock-keyring") {
53+
keyring.MockInit()
54+
}
55+
return run(c.String("gitpod-host"), c.String("token"), c.String("ssh_config"))
5356
},
5457
Flags: []cli.Flag{
5558
&cli.StringFlag{
@@ -58,6 +61,18 @@ func main() {
5861
},
5962
Name: "gitpod-host",
6063
},
64+
&cli.StringFlag{
65+
Name: "token",
66+
EnvVars: []string{
67+
"GITPOD_TOKEN",
68+
},
69+
},
70+
&cli.PathFlag{
71+
Name: "ssh_config",
72+
},
73+
&cli.BoolFlag{
74+
Name: "mock-keyring",
75+
},
6176
},
6277
},
6378
},
@@ -68,15 +83,26 @@ func main() {
6883
}
6984
}
7085

71-
func run(host string) error {
72-
tkn, err := auth.GetToken()
86+
func run(host, token, sshConfig string) error {
87+
if token != "" {
88+
auth.SetToken(host, token)
89+
}
90+
91+
tkn, err := auth.GetToken(host)
7392
if errors.Is(err, keyring.ErrNotFound) {
7493
tkn, err = auth.Login(context.Background(), auth.LoginOpts{GitpodURL: host})
7594
}
7695
if err != nil {
7796
return err
7897
}
7998

99+
cb := bastion.CompositeCallbacks{
100+
&logCallbacks{},
101+
}
102+
if sshConfig != "" {
103+
cb = append(cb, &bastion.SSHConfigWritingCallback{Path: sshConfig})
104+
}
105+
80106
wshost := host
81107
wshost = strings.ReplaceAll(wshost, "https://", "wss://")
82108
wshost = strings.ReplaceAll(wshost, "http://", "ws://")
@@ -89,7 +115,7 @@ func run(host string) error {
89115
if err != nil {
90116
return err
91117
}
92-
b := bastion.New(client, &logCallbacks{})
118+
b := bastion.New(client, cb)
93119
return b.Run()
94120
}
95121

components/local-app/go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ go 1.16
44

55
require (
66
github.com/gitpod-io/gitpod/gitpod-protocol v0.0.0-00010101000000-000000000000
7-
github.com/jpillora/chisel v1.7.6 // indirect
7+
github.com/gitpod-io/gitpod/supervisor/api v0.0.0-00010101000000-000000000000
8+
github.com/jpillora/chisel v1.7.6
9+
github.com/kevinburke/ssh_config v1.1.0 // indirect
810
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 // indirect
911
github.com/sirupsen/logrus v1.7.0
1012
github.com/urfave/cli/v2 v2.3.0
1113
github.com/zalando/go-keyring v0.1.1
1214
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
15+
google.golang.org/grpc v1.36.0
1316
)
1417

1518
replace github.com/gitpod-io/gitpod/gitpod-protocol => ../gitpod-protocol/go // leeway
19+
20+
replace github.com/gitpod-io/gitpod/supervisor/api => ../supervisor-api/go // leeway

components/local-app/go.sum

Lines changed: 356 additions & 0 deletions
Large diffs are not rendered by default.

components/local-app/pkg/auth/auth.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ import (
2020

2121
const (
2222
keyringService = "gitpod-io"
23-
keyringUser = "token"
2423
)
2524

25+
// SetToken returns the persisted Gitpod token
26+
func SetToken(host, token string) error {
27+
return keyring.Set(keyringService, host, token)
28+
}
29+
2630
// GetToken returns the persisted Gitpod token
27-
func GetToken() (token string, err error) {
28-
return keyring.Get(keyringService, keyringUser)
31+
func GetToken(host string) (token string, err error) {
32+
return keyring.Get(keyringService, host)
2933
}
3034

3135
// LoginOpts configure the login process
@@ -106,7 +110,7 @@ func Login(ctx context.Context, opts LoginOpts) (token string, err error) {
106110
}
107111
token = string(tkn)
108112

109-
err = keyring.Set(keyringService, keyringUser, token)
113+
err = keyring.Set(keyringService, baseURL, token)
110114
if err != nil {
111115
return "", err
112116
}

0 commit comments

Comments
 (0)