-
Notifications
You must be signed in to change notification settings - Fork 311
/
config.go
194 lines (163 loc) · 4.56 KB
/
config.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
package ssh
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"github.com/loft-sh/log"
"github.com/loft-sh/log/scanner"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
)
var configLock sync.Mutex
var (
MarkerStartPrefix = "# DevPod Start "
MarkerEndPrefix = "# DevPod End "
)
func ConfigureSSHConfig(configPath, context, workspace, user string, log log.Logger) error {
return configureSSHConfigSameFile(configPath, context, workspace, user, "", log)
}
func configureSSHConfigSameFile(configPath, context, workspace, user, command string, log log.Logger) error {
configLock.Lock()
defer configLock.Unlock()
sshConfigPath := configPath
if sshConfigPath == "" {
var err error
sshConfigPath, err = getSSHConfig()
if err != nil {
return err
}
}
newFile, err := addHost(sshConfigPath, workspace+"."+"devpod", user, context, workspace, command)
if err != nil {
return errors.Wrap(err, "parse ssh config")
}
return writeSSHConfig(sshConfigPath, newFile, log)
}
type DevPodSSHEntry struct {
Host string
User string
Workspace string
}
func addHost(path, host, user, context, workspace, command string) (string, error) {
newConfig, err := removeFromConfig(path, host)
if err != nil {
return "", err
}
newLines := []string{newConfig}
// get path to executable
execPath, err := os.Executable()
if err != nil {
return "", err
}
// add new section
startMarker := MarkerStartPrefix + host
endMarker := MarkerEndPrefix + host
newLines = append(newLines, startMarker)
newLines = append(newLines, "Host "+host)
newLines = append(newLines, " ForwardAgent yes")
newLines = append(newLines, " LogLevel error")
newLines = append(newLines, " StrictHostKeyChecking no")
newLines = append(newLines, " UserKnownHostsFile /dev/null")
if command != "" {
newLines = append(newLines, fmt.Sprintf(" ProxyCommand %s", command))
} else {
newLines = append(newLines, fmt.Sprintf(" ProxyCommand %s ssh --stdio --context %s --user %s %s", execPath, context, user, workspace))
}
newLines = append(newLines, " User "+user)
newLines = append(newLines, endMarker)
return strings.Join(newLines, "\n"), nil
}
func GetUser(workspace string) (string, error) {
sshConfigPath, err := getSSHConfig()
if err != nil {
return "", err
}
user := "root"
_, err = transformHostSection(sshConfigPath, workspace+"."+"devpod", func(line string) string {
splitted := strings.Split(strings.ToLower(strings.TrimSpace(line)), " ")
if len(splitted) == 2 && splitted[0] == "user" {
user = strings.Trim(splitted[1], "\"")
}
return line
})
if err != nil {
return "", err
}
return user, nil
}
func RemoveFromConfig(workspace string, log log.Logger) error {
configLock.Lock()
defer configLock.Unlock()
sshConfigPath, err := getSSHConfig()
if err != nil {
return err
}
newFile, err := removeFromConfig(sshConfigPath, workspace+"."+"devpod")
if err != nil {
return errors.Wrap(err, "parse ssh config")
}
return writeSSHConfig(sshConfigPath, newFile, log)
}
func writeSSHConfig(path, content string, log log.Logger) error {
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
log.Debugf("error creating ssh directory: %v", err)
}
err = os.WriteFile(path, []byte(content), 0600)
if err != nil {
return errors.Wrap(err, "write ssh config")
}
return nil
}
func getSSHConfig() (string, error) {
homeDir, err := homedir.Dir()
if err != nil {
return "", errors.Wrap(err, "get home dir")
}
return filepath.Join(homeDir, ".ssh", "config"), nil
}
func removeFromConfig(path, host string) (string, error) {
return transformHostSection(path, host, func(line string) string {
return ""
})
}
func transformHostSection(path, host string, transform func(line string) string) (string, error) {
var reader io.Reader
f, err := os.Open(path)
if err != nil {
if !os.IsNotExist(err) {
return "", err
}
reader = strings.NewReader("")
} else {
reader = f
defer f.Close()
}
configScanner := scanner.NewScanner(reader)
newLines := []string{}
inSection := false
startMarker := MarkerStartPrefix + host
endMarker := MarkerEndPrefix + host
for configScanner.Scan() {
text := configScanner.Text()
if strings.HasPrefix(text, startMarker) {
inSection = true
} else if strings.HasPrefix(text, endMarker) {
inSection = false
} else if !inSection {
newLines = append(newLines, text)
} else if inSection {
text = transform(text)
if text != "" {
newLines = append(newLines, text)
}
}
}
if configScanner.Err() != nil {
return "", errors.Wrap(err, "parse ssh config")
}
return strings.Join(newLines, "\n"), nil
}