-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathutils.go
More file actions
539 lines (471 loc) · 14.9 KB
/
Copy pathutils.go
File metadata and controls
539 lines (471 loc) · 14.9 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// SPDX-FileCopyrightText: 2026 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0
package utils
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
"syscall"
"unicode"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/types"
"golang.org/x/term"
)
const promptEnd = ": "
var prodVersionArchRegex = regexp.MustCompile(`suse\/(?:multi-linux-)?manager\/.*:`)
var imageValid = regexp.MustCompile("^((?:[^:/]+(?::[0-9]+)?/)?[^:]+)(?::([^:]+))?$")
// Taken from https://github.com/go-playground/validator/blob/2e1df48/regexes.go#L58
var fqdnValid = regexp.MustCompile(
`^([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})(\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})*?` +
`(\.[a-zA-Z]{1}[a-zA-Z0-9]{0,62})\.?$`,
)
// InspectResult holds the results of the inspection scripts.
type InspectResult struct {
InspectData `mapstructure:",squash"`
Timezone string
HasHubXmlrpcAPI bool `mapstructure:"has_hubxmlrpc"`
Debug bool `mapstructure:"debug"`
}
func checkValueSize(value string, minValue int, maxValue int) bool {
if minValue == 0 && maxValue == 0 {
return true
}
if len(value) < minValue {
fmt.Printf(NL("Has to be more than %d character long", "Has to be more than %d characters long", minValue), minValue)
return false
}
if len(value) > maxValue {
fmt.Printf(NL("Has to be less than %d character long", "Has to be less than %d characters long", maxValue), maxValue)
return false
}
return true
}
// CheckValidPassword performs check to a given password.
func CheckValidPassword(value *string, prompt string, minValue int, maxValue int) string {
fmt.Print(prompt + promptEnd)
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
log.Error().Err(err).Msg(L("Failed to read password"))
return ""
}
tmpValue := strings.TrimSpace(string(bytePassword))
if tmpValue == "" {
fmt.Println("A value is required")
return ""
}
r := regexp.MustCompile(`[\t ]`)
invalidChars := r.MatchString(tmpValue)
if invalidChars {
fmt.Println(L("Cannot contain spaces or tabs"))
return ""
}
if !checkValueSize(tmpValue, minValue, maxValue) {
fmt.Println()
return ""
}
fmt.Println()
*value = tmpValue
return *value
}
// AskPasswordIfMissing asks for password if missing.
// Don't perform any check if min and max are set to 0.
func AskPasswordIfMissing(value *string, prompt string, minValue int, maxValue int) {
if *value == "" && !term.IsTerminal(int(os.Stdin.Fd())) {
log.Warn().Msg(L("not an interactive device, not asking for missing value"))
return
}
for *value == "" {
firstRound := CheckValidPassword(value, prompt, minValue, maxValue)
if firstRound == "" {
continue
}
secondRound := CheckValidPassword(value, L("Confirm the password"), minValue, maxValue)
if secondRound != firstRound {
fmt.Println(L("Two different passwords have been provided"))
*value = ""
} else {
*value = secondRound
}
}
}
// AskPasswordIfMissingOnce asks for password if missing only once
// Don't perform any check if min and max are set to 0.
func AskPasswordIfMissingOnce(value *string, prompt string, minValue int, maxValue int) {
if *value == "" && !term.IsTerminal(int(os.Stdin.Fd())) {
log.Warn().Msg(L("not an interactive device, not asking for missing value"))
return
}
for *value == "" {
*value = CheckValidPassword(value, prompt, minValue, maxValue)
}
}
// AskIfMissing asks for a value if missing.
// Don't perform any check if minValue and maxValue are set to 0.
func AskIfMissing(value *string, prompt string, minValue int, maxValue int, checker func(string) bool) {
if *value == "" && !term.IsTerminal(int(os.Stdin.Fd())) {
log.Warn().Msg(L("not an interactive device, not asking for missing value"))
return
}
reader := bufio.NewReader(os.Stdin)
for *value == "" {
fmt.Print(prompt + promptEnd)
newValue, err := reader.ReadString('\n')
if err != nil {
log.Fatal().Err(err).Msg(L("failed to read input"))
}
tmpValue := strings.TrimSpace(newValue)
if checkValueSize(tmpValue, minValue, maxValue) && (checker == nil || checker(tmpValue)) {
*value = tmpValue
}
fmt.Println()
if *value == "" {
fmt.Print(L("A value is required"))
}
}
}
// YesNo asks a question in CLI.
func YesNo(question string) (bool, error) {
reader := bufio.NewReader(os.Stdin)
for {
// Translators: This is appended to the end of a question. Choices are short for `yes` and `no`.
fmt.Printf(L("%s [y/N]"), question)
response, err := reader.ReadString('\n')
if err != nil {
return false, err
}
response = strings.ToLower(strings.TrimSpace(response))
// Empty responses imply `no`.
if len(response) == 0 {
return false, nil
}
if strings.HasPrefix(L("yes"), response) {
return true, nil
}
if strings.HasPrefix(L("no"), response) {
return false, nil
}
}
}
// SplitRegistryHostAndPath splits a registry string into domain and path.
func SplitRegistryHostAndPath(registry string) (domain string, path string) {
separator := "://"
index := strings.Index(registry, separator)
if index != -1 {
registry = registry[index+len(separator):]
}
idx := strings.Index(registry, "/")
if idx == -1 {
return registry, ""
}
return registry[:idx], registry[idx+1:]
}
// ComputeImage assembles the container image from its name and tag.
func ComputeImage(
globalRegistry string,
globalTag string,
imageFlags types.ImageFlags,
) (string, error) {
// Compute the registry
registry := globalRegistry
if imageFlags.Registry.Host != "" {
registry = imageFlags.Registry.Host
}
name := imageFlags.Name
if !StartWithFQDN(name) && !strings.HasPrefix(name, "localhost/") {
name = path.Join(registry, imageFlags.Name)
}
// Compute the tag
tag := globalTag
if imageFlags.Tag != "" {
tag = imageFlags.Tag
}
submatches := imageValid.FindStringSubmatch(name)
if submatches == nil {
return "", fmt.Errorf(L("invalid image name: %s"), name)
}
if submatches[2] == `` {
if len(tag) <= 0 {
return name, fmt.Errorf(L("tag missing on %s"), name)
}
// No tag provided in the URL name, append the one passed
imageName := fmt.Sprintf("%s:%s", name, tag)
imageName = strings.ToLower(imageName) // podman does not accept repo in upper case
log.Info().Msgf(L("Computed image name is %s"), imageName)
return imageName, nil
}
imageName := strings.ToLower(name) // podman does not accept repo in upper case
log.Info().Msgf(L("Computed image name is %s"), imageName)
return imageName, nil
}
// The fullImage must contain the pattern `suse/manager/...` or `suse/multi-linux-manager/...`
// If registry has a path, then, the fullImage must start with that path.
func ComputePTF(registry string, user string, ptfID string, fullImage string, suffix string) (string, error) {
submatches := prodVersionArchRegex.FindStringSubmatch(fullImage)
if submatches == nil || len(submatches) != 1 {
return "", fmt.Errorf(L("invalid image name: %s"), fullImage)
}
imagePath := submatches[0]
tag := fmt.Sprintf("latest-%s-%s", suffix, ptfID)
registryHost, registryPath := SplitRegistryHostAndPath(registry)
// registry.suse.de is an internal registry and ptf containers here
// are shipped in a slightly different path
if registryHost == "registry.suse.de" {
sep := "containerfile/"
idx := strings.Index(registry, sep)
if idx > 1 {
imagePath = registry[idx+len(sep):]
}
return fmt.Sprintf("%s/ptf/%s/containers/a/%s%s", registryHost, ptfID, imagePath, tag), nil
}
if registryPath != "" && !strings.HasPrefix(imagePath, registryPath) {
return "", fmt.Errorf(L("image path '%[1]s' does not start with registry path '%[2]s'"), imagePath, registryPath)
}
return fmt.Sprintf("%s/a/%s/%s/%s%s", registryHost, strings.ToLower(user), ptfID, imagePath, tag), nil
}
// GetLocalTimezone returns the timezone set on the current machine.
func GetLocalTimezone() string {
out, err := RunCmdOutput(zerolog.DebugLevel, "timedatectl", "show", "--value", "-p", "Timezone")
if err != nil {
log.Fatal().Err(err).Msgf(L("Failed to run %s"), "timedatectl show --value -p Timezone")
}
return strings.TrimSpace(string(out))
}
// GetRandomBase64 generates random base64-encoded data.
func GetRandomBase64(size int) string {
data := make([]byte, size)
if _, err := rand.Read(data); err != nil {
log.Fatal().Err(err).Msg(L("Failed to read random data"))
}
return base64.StdEncoding.EncodeToString(data)
}
// ContainsUpperCase check if string contains an uppercase character.
func ContainsUpperCase(str string) bool {
for _, char := range str {
if unicode.IsUpper(char) {
return true
}
}
return false
}
// GetURLBody provide the body content of an GET HTTP request.
func GetURLBody(URL string) ([]byte, error) {
// Download the key from the URL
log.Debug().Msgf("Downloading %s", URL)
resp, err := http.Get(URL)
if err != nil {
return nil, Errorf(err, L("error downloading from %s"), URL)
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(L("bad status: %s"), resp.Status)
}
var buf bytes.Buffer
if _, err = io.Copy(&buf, resp.Body); err != nil {
return nil, err
}
// Extract the byte slice from the buffer
data := buf.Bytes()
return data, nil
}
// DownloadFile downloads from a remote path to a local file.
func DownloadFile(filepath string, URL string) (err error) {
data, err := GetURLBody(URL)
if err != nil {
return err
}
// Writer the body to file
log.Debug().Msgf("Saving %s to %s", URL, filepath)
return os.WriteFile(filepath, data, 0644)
}
// maxInts compensates the absence of max on Debian 12's go version.
func maxInts(a int, b int) int {
if a < b {
return b
}
return a
}
// CompareVersion compare the server image version and the server deployed version.
func CompareVersion(imageVersion string, deployedVersion string) int {
image := versionAsSlice(imageVersion)
deployed := versionAsSlice(deployedVersion)
maxLen := maxInts(len(image), len(deployed))
return getPaddedVersion(image, maxLen) - getPaddedVersion(deployed, maxLen)
}
func versionAsSlice(version string) []string {
re := regexp.MustCompile(`[^0-9]`)
parts := strings.Split(version, ".")
result := make([]string, len(parts))
for i, part := range parts {
result[i] = re.ReplaceAllString(part, "")
}
return result
}
func getPaddedVersion(version []string, size int) int {
padded := version
if len(version) != size {
padded = make([]string, size)
copy(padded, version)
for i, part := range padded {
if part == "" {
padded[i] = "0"
}
}
}
result, _ := strconv.Atoi(strings.Join(padded, ""))
return result
}
// Errorf helps providing consistent errors.
//
// Instead of fmt.Printf(L("the message for %s: %s"), value, err) use:
//
// Errorf(err, L("the message for %s"), value)
func Errorf(err error, message string, args ...any) error {
formattedMessage := fmt.Sprintf(message, args...)
return Error(err, formattedMessage)
}
// Error helps providing consistent errors.
//
// Instead of fmt.Printf(L("the message: %s"), err) use:
//
// Error(err, L("the message"))
func Error(err error, message string) error {
// l10n-ignore
return fmt.Errorf("%s: %w", message, err)
}
// JoinErrors aggregate multiple multiple errors into one.
//
// Replacement for errors.Join which is not available in go 1.19.
func JoinErrors(errs ...error) error {
var messages []string
for _, err := range errs {
if err != nil {
messages = append(messages, err.Error())
}
}
if len(messages) == 0 {
return nil
}
return errors.New(strings.Join(messages, "; "))
}
// GetFqdn returns and checks the FQDN of the host system.
func GetFqdn(args []string) (string, error) {
var fqdn string
if len(args) == 1 {
fqdn = args[0]
} else {
out, err := RunCmdOutput(zerolog.DebugLevel, "hostname", "-f")
if err != nil {
return "", Error(err, L("failed to compute server FQDN"))
}
fqdn = strings.TrimSpace(string(out))
}
if err := IsValidFQDN(fqdn); err != nil {
return "", err
}
return fqdn, nil
}
// IsValidFQDN returns an error if the argument is not a valid FQDN.
func IsValidFQDN(fqdn string) error {
if !IsWellFormedFQDN(fqdn) {
return fmt.Errorf(L("%s is not a valid FQDN"), fqdn)
}
_, err := net.LookupHost(fqdn)
if err != nil {
return Errorf(err, L("cannot resolve %s"), fqdn)
}
return nil
}
// IsWellFormedFQDN returns an false if the argument is not a well formed FQDN.
func IsWellFormedFQDN(fqdn string) bool {
return fqdnValid.MatchString(fqdn)
}
// StartWithFQDN returns true if the argument start with a well formed FQDN.
func StartWithFQDN(url string) bool {
fqdn, _ := SplitRegistryHostAndPath(url)
return IsWellFormedFQDN(fqdn)
}
// CommandExists checks if cmd exists in $PATH.
func CommandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
// SaveBinaryData saves binary data to a file.
func SaveBinaryData(filename string, data []int8) error {
// Need to convert the array of signed ints to unsigned/byte
byteArray := make([]byte, len(data))
for i, v := range data {
byteArray[i] = byte(v)
}
file, err := os.Create(filename)
if err != nil {
return Errorf(err, L("error creating file %s"), filename)
}
defer file.Close()
_, err = file.Write(byteArray)
if err != nil {
return Errorf(err, L("error writing file %s"), filename)
}
return nil
}
// CreateChecksum creates sha256 checksum of provided file.
// Uses system `sha256sum` binary to avoid pulling crypto dependencies.
func CreateChecksum(file string) error {
outputFile := file + ".sha256sum"
output, err := NewRunner("sha256sum", file).Exec()
if err != nil {
return Errorf(err, L("Failed to calculate checksum of the file %s"), file)
}
// We want only checksum, drop the filepath
output = bytes.Split(output, []byte(" "))[0]
if err := os.WriteFile(outputFile, output, 0622); err != nil {
return Errorf(err, L("Failed to write checksum of the file %[1]s to the %[2]s"), file, outputFile)
}
return nil
}
// ValidateChecksum checks integrity of the file by checking against stored checksum
// Uses system `sha256sum` binary to avoid pulling crypt dependencies.
func ValidateChecksum(file string) error {
checksum, err := NewRunner("sha256sum", file).Exec()
if err != nil {
return Errorf(err, L("Failed to calculate checksum of the file %s"), file)
}
// We want only checksum, drop the filepath
checksum = bytes.Split(checksum, []byte(" "))[0]
output, err := os.ReadFile(file + ".sha256sum")
if err != nil {
return Errorf(err, L("Failed to read checksum of the file %[1]s"), file)
}
// Split by space to work with older backups
if !bytes.Equal(checksum, bytes.Split(output, []byte(" "))[0]) {
return fmt.Errorf(L("Checksum of %s does not match"), file)
}
return nil
}
// RandomHexString returns a cryptographically secure random hex string.
func RandomHexString(nBytes int) (string, error) {
if nBytes <= 0 {
return "", errors.New(L("nBytes must be > 0"))
}
b := make([]byte, nBytes)
if _, err := rand.Read(b); err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}