-
Notifications
You must be signed in to change notification settings - Fork 53
/
ssh.go
62 lines (51 loc) · 1.52 KB
/
ssh.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
func generateKeys(user User) error {
base := getPath(user)
output, err := exec.Command("ssh-keygen", "-f", fmt.Sprintf("%s/key", base), "-P", "").CombinedOutput()
if err != nil {
return fmt.Errorf(string(output))
}
return nil
}
func addSSHConfig(user User, hostname string) error {
configPath := filepath.Join(user.Home, ".ssh", "config")
if _, err := os.Stat(configPath); os.IsNotExist(err) {
err := ioutil.WriteFile(configPath, []byte(""), 0644)
if err != nil {
return err
}
}
config, err := ioutil.ReadFile(configPath)
if err != nil {
return err
}
hasHost := strings.Index(string(config), fmt.Sprintf("Host %s", hostname))
if hasHost != -1 {
return nil
}
keyfile := filepath.Join(getPath(user), "key")
newConfig := string(config)
newConfig += fmt.Sprintf("Host %s\n User docker\n IdentityFile %s", hostname, keyfile)
return ioutil.WriteFile(configPath, []byte(newConfig), 0644)
}
func removeSSHConfig(user User, hostname string) error {
configPath := filepath.Join(user.Home, ".ssh", "config")
config, err := ioutil.ReadFile(configPath)
if err != nil {
return err
}
keyfile := filepath.Join(getPath(user), "key")
hostConfig := fmt.Sprintf("Host %s\n User docker\n IdentityFile %s", hostname, keyfile)
hostMatcher := regexp.MustCompile(fmt.Sprintf("(?m)^%s?$", hostConfig))
newConfig := hostMatcher.ReplaceAllString(string(config), "")
return ioutil.WriteFile(configPath, []byte(newConfig), 0644)
}