-
Notifications
You must be signed in to change notification settings - Fork 1
/
getters.go
90 lines (72 loc) · 2.22 KB
/
getters.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
package befehl
import (
"encoding/json"
"os"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
func (instance *Instance) getDefaultSshUser() string {
if instance.runtimeConfig.User != "" {
return instance.runtimeConfig.User
}
return "root"
}
func (instance *Instance) getSshKnowHostsPath() string {
path := os.Getenv("HOME") + "/.ssh/known_hosts"
config := instance.options.SshHostKeyConfig
if config.Enabled && config.KnownHostsPath != "" {
path = config.KnownHostsPath
}
return path
}
func (instance *Instance) getSshHostKeyCallback() (hostKeyCallback ssh.HostKeyCallback, err error) {
hostKeyCallback = ssh.InsecureIgnoreHostKey()
if instance.options.SshHostKeyConfig.Enabled {
hostKeyCallback, err = knownhosts.New(instance.getSshKnowHostsPath())
}
return
}
func (instance *Instance) getSshClientConfig(getSshUser func() string) (*ssh.ClientConfig, error) {
if hostKeyCallback, err := instance.getSshHostKeyCallback(); err == nil {
return &ssh.ClientConfig{
User: getSshUser(),
Auth: []ssh.AuthMethod{
ssh.PublicKeys(instance.sshKey),
},
Timeout: time.Duration(10) * time.Second,
HostKeyCallback: hostKeyCallback,
}, nil
} else {
return nil, err
}
}
func (instance *Instance) getDefaultSshClientConfig() (*ssh.ClientConfig, error) {
return instance.getSshClientConfig(func() string { return instance.getDefaultSshUser() })
}
func (instance *Instance) getSshUserClientConfig(sshUser string) (*ssh.ClientConfig, error) {
return instance.getSshClientConfig(func() string { return sshUser })
}
func (instance *Instance) getLogDir() string {
if instance.options.LogDir != "" {
return instance.options.LogDir
}
return os.Getenv("HOME") + "/befehl/logs"
}
func (instance *Instance) getLogFilePath(host string) string {
return instance.getLogDir() + "/" + host
}
func (instance *Instance) getPrivKeyFile() string {
if instance.options.PrivateKeyFile != "" {
return instance.options.PrivateKeyFile
}
return os.Getenv("HOME") + "/.ssh/id_rsa"
}
func GetRuntimeConfig(pathToRuntimeConfig string) (config RuntimeConfig, err error) {
configBytes, err := os.ReadFile(pathToRuntimeConfig)
if err != nil {
return
}
err = json.Unmarshal(configBytes, &config)
return
}