1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"io/ioutil"
6
7
"os"
@@ -19,6 +20,7 @@ type configSSHCmd struct {
19
20
remove bool
20
21
21
22
startToken , startMessage , endToken string
23
+ privateKeyFilepath string
22
24
}
23
25
24
26
func (cmd * configSSHCmd ) Spec () cli.CommandSpec {
@@ -31,7 +33,8 @@ func (cmd *configSSHCmd) Spec() cli.CommandSpec {
31
33
32
34
func (cmd * configSSHCmd ) RegisterFlags (fl * pflag.FlagSet ) {
33
35
fl .BoolVar (& cmd .remove , "remove" , false , "remove the auto-generated Coder Enterprise ssh config" )
34
- defaultPath := filepath .Join (os .Getenv ("HOME" ), ".ssh" , "config" )
36
+ home := os .Getenv ("HOME" )
37
+ defaultPath := filepath .Join (home , ".ssh" , "config" )
35
38
fl .StringVar (& cmd .filepath , "config-path" , defaultPath , "overide the default path of your ssh config file" )
36
39
37
40
cmd .startToken = "# ------------START-CODER-ENTERPRISE-----------"
@@ -44,9 +47,13 @@ func (cmd *configSSHCmd) RegisterFlags(fl *pflag.FlagSet) {
44
47
#
45
48
# You should not hand-edit this section, unless you are deleting it.`
46
49
cmd .endToken = "# ------------END-CODER-ENTERPRISE------------"
50
+ cmd .privateKeyFilepath = filepath .Join (home , ".ssh" , "coder_enterprise" )
47
51
}
48
52
49
53
func (cmd * configSSHCmd ) Run (fl * pflag.FlagSet ) {
54
+ ctx , cancel := context .WithCancel (context .Background ())
55
+ defer cancel ()
56
+
50
57
currentConfig , err := readStr (cmd .filepath )
51
58
if err != nil {
52
59
flog .Fatal ("failed to read ssh config file %q: %v" , cmd .filepath , err )
@@ -91,30 +98,48 @@ func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
91
98
if err != nil {
92
99
flog .Fatal ("failed to write new configurations to ssh config file %q: %v" , cmd .filepath , err )
93
100
}
101
+ err = cmd .writeSSHKey (ctx , entClient )
102
+ if err != nil {
103
+ flog .Fatal ("failed to fetch and write ssh key: %v" , err )
104
+ }
105
+
94
106
fmt .Printf ("An auto-generated ssh config was written to %q\n " , cmd .filepath )
107
+ fmt .Printf ("Your private ssh key was written to %q\n " , cmd .privateKeyFilepath )
95
108
fmt .Println ("You should now be able to ssh into your environment" )
96
109
fmt .Printf ("For example, try running\n \n \t $ ssh coder:%s\n \n " , envs [0 ].Name )
97
110
}
98
111
112
+ func (cmd * configSSHCmd ) writeSSHKey (ctx context.Context , client * entclient.Client ) error {
113
+ key , err := client .SSHKey ()
114
+ if err != nil {
115
+ return err
116
+ }
117
+ err = ioutil .WriteFile (cmd .privateKeyFilepath , []byte (key .PrivateKey ), 400 )
118
+ if err != nil {
119
+ return err
120
+ }
121
+ return nil
122
+ }
123
+
99
124
func (cmd * configSSHCmd ) makeNewConfigs (userName string , envs []entclient.Environment ) string {
100
125
newConfig := fmt .Sprintf ("\n %s\n %s\n \n " , cmd .startToken , cmd .startMessage )
101
126
for _ , env := range envs {
102
- newConfig += makeConfig (userName , env .Name )
127
+ newConfig += cmd . makeConfig (userName , env .Name )
103
128
}
104
129
newConfig += fmt .Sprintf ("\n %s\n " , cmd .endToken )
105
130
106
131
return newConfig
107
132
}
108
133
109
- func makeConfig (userName , envName string ) string {
134
+ func ( cmd * configSSHCmd ) makeConfig (userName , envName string ) string {
110
135
return fmt .Sprintf (
111
136
`Host coder:%s
112
137
HostName %s
113
138
User %s-%s
114
- Port 2222
115
- KeepAlive=yes
139
+ StrictHostKeyChecking no
116
140
ConnectTimeout=0
117
- ` , envName , "MOCK-SSHPROXY-IP" , userName , envName ) // TODO: get real ssh proxy ip address
141
+ IdentityFile=%s
142
+ ` , envName , "MOCK-SSHPROXY-IP" , userName , envName , cmd .privateKeyFilepath ) // TODO: get real ssh proxy ip address
118
143
}
119
144
120
145
func writeStr (filename , data string ) error {
0 commit comments