-
Notifications
You must be signed in to change notification settings - Fork 161
/
alt-http.go
86 lines (69 loc) · 1.97 KB
/
alt-http.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
package sources
import (
"crypto/sha256"
"errors"
"fmt"
"net/url"
"path/filepath"
"strings"
"github.com/lxc/distrobuilder/shared"
)
type altLinux struct {
common
}
func (s *altLinux) Run() error {
arch := s.definition.Image.ArchitectureMapped
if arch == "armhf" {
arch = "armh"
}
baseURL := fmt.Sprintf(
"%s/%s/cloud/%s/",
s.definition.Source.URL,
s.definition.Image.Release,
arch,
)
fname := fmt.Sprintf("alt-%s-rootfs-systemd-%s.tar.xz", strings.ToLower(s.definition.Image.Release), arch)
url, err := url.Parse(baseURL)
if err != nil {
return fmt.Errorf("Failed to parse URL %q: %w", baseURL, err)
}
checksumFile := ""
if !s.definition.Source.SkipVerification {
if len(s.definition.Source.Keys) != 0 {
checksumFile = baseURL + "SHA256SUMS"
fpath, err := s.DownloadHash(s.definition.Image, checksumFile+".gpg", "", nil)
if err != nil {
return fmt.Errorf("Failed to download %q: %w", checksumFile+".gpg", err)
}
_, err = s.DownloadHash(s.definition.Image, checksumFile, "", nil)
if err != nil {
return fmt.Errorf("Failed to download %q: %w", checksumFile, err)
}
valid, err := s.VerifyFile(
filepath.Join(fpath, "SHA256SUMS"),
filepath.Join(fpath, "SHA256SUMS.gpg"))
if err != nil {
return fmt.Errorf("Failed to verify file: %w", err)
}
if !valid {
return fmt.Errorf("Invalid signature for %q", "SHA256SUMS")
}
} else {
// Force gpg checks when using http
if url.Scheme != "https" {
return errors.New("GPG keys are required if downloading from HTTP")
}
}
}
fpath, err := s.DownloadHash(s.definition.Image, baseURL+fname, checksumFile, sha256.New())
if err != nil {
return fmt.Errorf("Failed to download %q: %w", baseURL+fname, err)
}
s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image")
// Unpack
err = shared.Unpack(filepath.Join(fpath, fname), s.rootfsDir)
if err != nil {
return fmt.Errorf("Failed to unpack %q: %w", fname, err)
}
return nil
}