/
tools.go
419 lines (365 loc) · 9.67 KB
/
tools.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
Copyright © 2022 - 2024 SUSE LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tools
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
scp "github.com/bramvdbogaerde/go-scp"
"github.com/pkg/errors"
ssh "golang.org/x/crypto/ssh"
)
/**
* Get File through HTTP
* @param url URL where to download the file
* @param fileName of the file to create
* @param skipVerify Skip TLS check if needed
* @returns Nothing or an error
*/
func GetFileFromURL(url string, fileName string, skipVerify bool) error {
if !skipVerify {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
data, err := http.Get(url)
if err != nil {
return err
}
defer data.Body.Close()
// Create file
file, err := os.Create(fileName)
if err != nil {
return err
}
// Save data in file
_, err = io.Copy(file, data.Body)
return err
}
/**
* Get a list of files
* @param dir Directory where to search
* @param pattern Search pattern
* @returns List of files or an error
*/
func GetFilesList(dir string, pattern string) ([]string, error) {
files, err := filepath.Glob(dir + "/" + pattern)
if err != nil {
return nil, err
}
if files != nil {
return files, nil
}
return nil, err
}
/**
* Simple sed command
* @param oldValue Value or simple regex to modify
* @param newValue New value to set
* @param file File to modify
* @returns Nothing or an error
*/
// Sed code partially from https://forum.golangbridge.org/t/using-sed-in-golang/23526/16
func Sed(oldValue, newValue, file string) error {
fileData, err := os.ReadFile(file)
if err != nil {
return err
}
// Get file permissions
info, err := os.Stat(file)
if err != nil {
return err
}
mode := info.Mode()
// Regex is in the old value var
regex := regexp.MustCompile(oldValue)
fileString := string(fileData)
fileString = regex.ReplaceAllString(fileString, newValue)
fileData = []byte(fileString)
err = os.WriteFile(file, fileData, mode)
return err
}
type Client struct {
Host string
Username string
Password string
}
/**
* Define SSH client
* @remarks This function is only used internally, not exported
* @returns SSH Client configuration
*/
// NOTE: clientConfig does not have unit test as it is
// used only in connectToHost
func (c *Client) clientConfig() *ssh.ClientConfig {
sshConfig := &ssh.ClientConfig{
User: c.Username,
Auth: []ssh.AuthMethod{ssh.Password(c.Password)},
Timeout: 30 * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
return sshConfig
}
/**
* Connect on the client through SSH
* @remarks This function is only used internally, not exported
* @param Client is the receiver where to execute the command
* @param ssh.Client Client definition to connect to
* @returns Pointer to the SSH Client or an error
*/
// NOTE: connectToHost does not have unit test as it is
// used in RunSSH which is already tested
func (c *Client) connectToHost() (*ssh.Client, error) {
// Define ssh connection
sshConfig := c.clientConfig()
// Connect to client
sshClient, err := ssh.Dial("tcp", c.Host, sshConfig)
if err != nil {
return nil, err
}
return sshClient, nil
}
/**
* Get file through SSH (a SCP in fact!)
* @param localFile Local file to create
* @param remoteFile Remote file to copy from
* @param perm Permissions to set on the local file
* @returns Nothing or an error
*/
func (c *Client) GetFile(localFile, remoteFile string, perm fs.FileMode) error {
// Define ssh connection
sshConfig := c.clientConfig()
// Create a local file to write to.
f, err := os.OpenFile(localFile, os.O_RDWR|os.O_CREATE, perm)
if err != nil {
// Failed to open
return err
}
defer f.Close()
// Connect to client
scpClient := scp.NewClient(c.Host, sshConfig)
defer scpClient.Close()
if err := scpClient.Connect(); err != nil {
// Failed to connect
return err
}
if err := scpClient.CopyFromRemote(context.Background(), f, remoteFile); err != nil {
// Failed to copy
return err
}
return nil
}
/**
* Run a command on client through SSH
* @param Client is the receiver where to execute the command
* @param cmd Command to execute
* @returns Result of the command or an error
*/
func (c *Client) RunSSH(cmd string) (string, error) {
sshClient, err := c.connectToHost()
if err != nil {
// Failed to connect
return "", err
}
defer sshClient.Close()
// Open a client session
session, err := sshClient.NewSession()
if err != nil {
// Failed to create session
return "", err
}
defer session.Close()
// Execute the command on the opened session
var stdout bytes.Buffer
var stderr bytes.Buffer
session.Stdout = &stdout
session.Stderr = &stderr
if err := session.Run(cmd); err != nil {
// Failed to execute the command
return stdout.String(), errors.Wrapf(err, stderr.String())
}
return stdout.String(), nil
}
/**
* Send file through SSH (a SCP in fact!)
* @param src Source file
* @param dst Destination file on the client
* @param perm Permissions to set on the file
* @returns Nothing or an error
*/
func (c *Client) SendFile(src, dst, perm string) error {
// Define ssh connection
sshConfig := c.clientConfig()
// Connect to client
scpClient := scp.NewClient(c.Host, sshConfig)
defer scpClient.Close()
if err := scpClient.Connect(); err != nil {
// Failed to connect
return err
}
f, err := os.Open(src)
if err != nil {
// Failed to open
return err
}
defer f.Close()
if err := scpClient.CopyFromFile(context.Background(), *f, dst, perm); err != nil {
// Failed to copy
return err
}
return nil
}
/**
* Share files through HTTP (simple way, no security at all!)
* @remarks A HTTP server is up and running
* @param directory The directory where is files are
* @param listenAddr Port where to listen to
* @returns Nothing
*/
// TODO: Use Server function from http helpers instead
func HTTPShare(directory, listenAddr string) {
fs := http.FileServer(http.Dir(directory))
go func() {
if err := http.ListenAndServe(listenAddr, fs); err != nil {
fmt.Printf("Server failed: %s\n", err)
}
}()
}
/**
* Configure a timeout based on TIMEOUT_SCALE env variable
* @remarks Multiply a duration with TIMEOUT_SCALE value
* @param timeout Initial timeout value
* @returns Modified timeout value
*/
func SetTimeout(timeout time.Duration) time.Duration {
s, set := os.LookupEnv("TIMEOUT_SCALE")
// Only if TIMEOUT_SCALE is set
if set {
scale, err := strconv.Atoi(s)
if err != nil {
return timeout
}
// Return the scaled timeout
return timeout * time.Duration(scale)
}
// Don't return error, in the worst case return the initial value
// Otherwise an additional step will be needed for some commands (like Eventually)
return timeout
}
/**
* Add data to file
* @remarks Added data is optional
* @param srcFile Source file
* @param dstFile Destination file
* @param data Data to add at the end of destination file
* @returns Nothing or an error
*/
func AddDataToFile(srcfile, dstfile string, data []byte) error {
// Open source file
f, err := os.Open(srcfile)
if err != nil {
return err
}
defer f.Close()
// Open/create destination file
d, err := os.OpenFile(dstfile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer d.Close()
// Copy source to dest
if _, err = io.Copy(d, f); err != nil {
return err
}
// Add data to dest
if _, err = d.Write([]byte(data)); err != nil {
return err
}
return nil
}
/**
* Write data to file
* @remarks This function simply writes data to a file
* @param dstFile Destination file
* @param data Data to write
* @returns Nothing or an error
*/
func WriteFile(dstfile string, data []byte) error {
// Open/create destination file
d, err := os.OpenFile(dstfile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer d.Close()
// Add data to dest
if _, err = d.Write([]byte(data)); err != nil {
return err
}
return nil
}
/**
* Copy file
* @remarks This function simply copies a file to another
* @param srcFile Source file
* @param dstFile Destination file
* @returns Nothing or an error
*/
// NOTE: CopyFile does not have unit test as it uses
// AddDataToFile which is already tested
func CopyFile(srcFile, dstFile string) error {
// Add data to file without adding data(!) is in fact a copy
return (AddDataToFile(srcFile, dstFile, []byte("")))
}
/**
* Trim a string
* @remarks Remove all from s string after c char
* @param s String to trim
* @param c Character used as a separator
* @returns Trimmed string
*/
func TrimStringFromChar(s, c string) string {
if idx := strings.Index(s, c); idx != -1 {
return s[:idx]
}
return s
}
/**
* Create a temporary file
* @remarks Create temporary file with a name based on baseName
* @param baseName String to trim
* @returns Created filename or an error
*/
func CreateTemp(baseName string) (string, error) {
t, err := os.CreateTemp("", baseName)
if err != nil {
return "", err
}
return t.Name(), nil
}
/**
* Check if the string is a valid IPv4
* @param value Value to check
* @returns True if value is an IPv4, otherwise False
*/
func IsIPv4(value string) bool {
const regex = `^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$`
return regexp.MustCompile(regex).MatchString(value)
}