This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ssh_config.go
207 lines (173 loc) · 5.15 KB
/
ssh_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
195
196
197
198
199
200
201
202
203
204
205
206
207
package config
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path"
"strings"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/pathutil"
)
const (
sshConfigFileName = "bitrise.machine.ssh.json"
sshPrivateKeyFileName = "ssh_rsa"
sshPublicKeyFileName = "ssh_rsa.pub"
)
// SSHConfigModel ...
type SSHConfigModel struct {
IP string `json:"ip"`
Port string `json:"port"`
IdentityPath string `json:"identity_path"`
Loginname string `json:"loginname"`
}
func fullSSHConfigFilePath(dirPath string) string {
return path.Join(dirPath, sshConfigFileName)
}
func fullSSHPrivateKeyFilePath(dirPath string) string {
return path.Join(dirPath, sshPrivateKeyFileName)
}
func fullSSHPublicKeyFilePath(dirPath string) string {
return path.Join(dirPath, sshPublicKeyFileName)
}
func (model *SSHConfigModel) normalizeAndValidate() error {
if model.IP == "" {
return fmt.Errorf("Invalid, empty IP")
}
if model.Port == "" {
return fmt.Errorf("Invalid, empty Port")
}
if model.IdentityPath == "" {
return fmt.Errorf("Invalid, empty IdentityPath")
}
if model.Loginname == "" {
return fmt.Errorf("Invalid, empty Loginname")
}
return nil
}
func readSSHConfigFromBytes(configBytes []byte) (SSHConfigModel, error) {
model := SSHConfigModel{}
if err := json.Unmarshal(configBytes, &model); err != nil {
return model, err
}
if err := model.normalizeAndValidate(); err != nil {
return model, err
}
return model, nil
}
func deleteFileIfExists(pth string) error {
isExists, err := pathutil.IsPathExists(pth)
if err != nil {
return err
}
if !isExists {
return nil
}
return os.Remove(pth)
}
// DeleteSSHFilesFromDir ...
func DeleteSSHFilesFromDir(workdirPth string) (e error) {
if err := deleteFileIfExists(fullSSHConfigFilePath(workdirPth)); err != nil {
e = err
}
if err := deleteFileIfExists(fullSSHPrivateKeyFilePath(workdirPth)); err != nil {
e = err
}
if err := deleteFileIfExists(fullSSHPublicKeyFilePath(workdirPth)); err != nil {
e = err
}
return
}
// ReadSSHConfigFileFromDir ...
func ReadSSHConfigFileFromDir(workdirPth string) (SSHConfigModel, error) {
configBytes, err := fileutil.ReadBytesFromFile(fullSSHConfigFilePath(workdirPth))
if err != nil {
return SSHConfigModel{}, err
}
return readSSHConfigFromBytes(configBytes)
}
func (model SSHConfigModel) serializeIntoJSONBytes() ([]byte, error) {
return json.MarshalIndent(model, "", " ")
}
// WriteIntoFileInDir ...
func (model SSHConfigModel) WriteIntoFileInDir(workdirPth string) error {
if err := model.normalizeAndValidate(); err != nil {
return err
}
configBytes, err := model.serializeIntoJSONBytes()
if err != nil {
return err
}
if err := fileutil.WriteBytesToFile(fullSSHConfigFilePath(workdirPth), configBytes); err != nil {
return err
}
return nil
}
// CreateSSHConfigFromVagrantSSHConfigLog ...
func CreateSSHConfigFromVagrantSSHConfigLog(vagrantSSHConfigLog string) (SSHConfigModel, error) {
configModel := SSHConfigModel{}
// process it line by line
scanner := bufio.NewScanner(strings.NewReader(vagrantSSHConfigLog))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
lineSplits := strings.Split(line, " ")
if len(lineSplits) >= 2 {
if lineSplits[0] == "HostName" {
configModel.IP = lineSplits[1]
} else if lineSplits[0] == "Port" {
configModel.Port = lineSplits[1]
} else if lineSplits[0] == "IdentityFile" {
configModel.IdentityPath = strings.Join(lineSplits[1:], " ")
// starting with vagrant 1.8.x the IdentityFile path is now
// wrapped in quotation ("..."), we should remove those
configModel.IdentityPath = strings.Trim(configModel.IdentityPath, `"`)
} else if lineSplits[0] == "User" {
configModel.Loginname = lineSplits[1]
}
}
}
if err := scanner.Err(); err != nil {
return configModel, err
}
if err := configModel.normalizeAndValidate(); err != nil {
return configModel, err
}
return configModel, nil
}
// SSHCommandArgs ...
func (model SSHConfigModel) SSHCommandArgs() []string {
sshArgs := []string{model.IP, "-p", model.Port,
"-oUserKnownHostsFile=/dev/null", "-oStrictHostKeyChecking=no",
"-oPasswordAuthentication=no", "-oIdentitiesOnly=yes", "-oLogLevel=FATAL",
"-l", model.Loginname, "-i", model.IdentityPath,
}
return sshArgs
}
// WriteSSHKeypairToFiles ...
func WriteSSHKeypairToFiles(workdirPth string, privBytes, pubBytes []byte) (privFilePath, pubFilePath string, e error) {
privFilePath = fullSSHPrivateKeyFilePath(workdirPth)
pubFilePath = fullSSHPublicKeyFilePath(workdirPth)
if err := fileutil.WriteBytesToFileWithPermission(privFilePath, privBytes, 0600); err != nil {
e = err
return
}
if err := fileutil.WriteBytesToFileWithPermission(pubFilePath, pubBytes, 0600); err != nil {
e = err
return
}
return
}
// IsSSHKeypairFileExistInDirectory ...
func IsSSHKeypairFileExistInDirectory(workdirPth string) bool {
privFilePath := fullSSHPrivateKeyFilePath(workdirPth)
pubFilePath := fullSSHPublicKeyFilePath(workdirPth)
exists, err := pathutil.IsPathExists(privFilePath)
if !exists || err != nil {
return false
}
exists, err = pathutil.IsPathExists(pubFilePath)
if !exists || err != nil {
return false
}
return true
}