This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
/
ssh_conf.go
124 lines (102 loc) · 2.64 KB
/
ssh_conf.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
package utils
import (
"bytes"
"os"
"path/filepath"
"regexp"
"text/template"
)
const HostConfigTemplate = `# workspace start {{.Name}}.{{.Namespace}}.workspace
Host {{.Name}}.{{.Namespace}}.workspace
HostName {{.Hostname}}
LogLevel error
Port {{.Port}}
IdentityFile "{{.PrivateKeyPath}}"
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
User workspace
# workspace end {{.Name}}.{{.Namespace}}.workspace`
const HostConfigRegex = `(?s)(# workspace start {{.Name}}.{{.Namespace}}.workspace)(.*)(# workspace end {{.Name}}.{{.Namespace}}.workspace)`
type HostConfig struct {
Name string
Namespace string
Hostname string
Port uint16
PrivateKeyPath string
template *template.Template
}
func NewHostConfig(name string, namespace string, privateKeyPath string) (string, error) {
template, err := template.New("ssh_conf").Parse(HostConfigTemplate)
if err != nil {
return "", err
}
hostConfig := &HostConfig{
Name: name,
Namespace: namespace,
template: template,
Port: 2222,
Hostname: "localhost",
PrivateKeyPath: privateKeyPath,
}
return hostConfig.serialize()
}
func (o *HostConfig) serialize() (string, error) {
var data bytes.Buffer
if err := o.template.Execute(&data, o); err != nil {
return "", err
}
return data.String(), nil
}
func buildRegex(name string, namespace string) (string, error) {
template, err := template.New("host-regex").Parse(HostConfigRegex)
if err != nil {
return "", err
}
var data bytes.Buffer
if err := template.Execute(&data, struct {
Name string
Namespace string
}{
Name: name,
Namespace: namespace,
}); err != nil {
return "", err
}
return data.String(), nil
}
func DeleteSshConfEntry(name string, namespace string) error {
sshConfPath := filepath.Join(os.Getenv("HOME"), ".ssh", "config")
file, err := os.ReadFile(sshConfPath)
if err != nil {
return err
}
expression, err := buildRegex(name, namespace)
if err != nil {
return err
}
regex, err := regexp.Compile(expression)
if err != nil {
return err
}
err = os.WriteFile(sshConfPath, []byte(regex.ReplaceAllString(string(file), "")), 0644)
if err != nil {
return err
}
return nil
}
func AppendSshConfEntry(name string, namespace string, privateKeyPath string) error {
file, err := os.OpenFile(filepath.Join(os.Getenv("HOME"), ".ssh", "config"),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer file.Close()
hostConfig, err := NewHostConfig(name, namespace, privateKeyPath)
if err != nil {
return err
}
if _, err := file.WriteString(hostConfig); err != nil {
return err
}
return nil
}