This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
rust.go
115 lines (96 loc) · 3.1 KB
/
rust.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
package dependency
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"time"
"github.com/Masterminds/semver"
)
type Rust struct {
checksummer Checksummer
fileSystem FileSystem
githubClient GithubClient
webClient WebClient
}
func (r Rust) GetAllVersionRefs() ([]string, error) {
tags, err := r.githubClient.GetTags("rust-lang", "rust")
if err != nil {
return nil, fmt.Errorf("could not get tags: %w", err)
}
var versions []string
for _, tag := range tags {
if strings.HasPrefix(tag, "release-") {
continue
}
version, err := semver.NewVersion(tag)
if err != nil {
return nil, fmt.Errorf("failed to parse version %s: %w", tag, err)
}
if version.Prerelease() != "" || version.Major() == 0 {
continue
}
versions = append(versions, tag)
}
return versions, nil
}
func (r Rust) GetDependencyVersion(version string) (DepVersion, error) {
releaseDate, err := r.GetReleaseDate(version)
if err != nil {
return DepVersion{}, err
}
dependencyURL := r.dependencyURL(version)
sha, err := r.getDependencySHA(dependencyURL, version)
if err != nil {
return DepVersion{}, fmt.Errorf("could not get rust sha: %w", err)
}
return DepVersion{
Version: version,
URI: dependencyURL,
SHA256: sha,
ReleaseDate: releaseDate,
DeprecationDate: nil,
CPE: fmt.Sprintf("cpe:2.3:a:rust-lang:rust:%s:*:*:*:*:*:*:*", version),
}, nil
}
func (r Rust) GetReleaseDate(version string) (*time.Time, error) {
tagCommit, err := r.githubClient.GetTagCommit("rust-lang", "rust", version)
if err != nil {
return nil, fmt.Errorf("could not get release date: %w", err)
}
return &tagCommit.Date, nil
}
func (r Rust) getDependencySHA(dependencyURL, version string) (string, error) {
rustGPGKey, err := r.webClient.Get("https://static.rust-lang.org/rust-key.gpg.ascii")
if err != nil {
return "", fmt.Errorf("could not get rust GPG key: %w", err)
}
dependencySignature, err := r.webClient.Get(r.dependencySignatureURL(version))
if err != nil {
return "", fmt.Errorf("could not get dependency signature: %w", err)
}
dependencyOutputDir, err := ioutil.TempDir("", "rust")
if err != nil {
return "", fmt.Errorf("failed to create temp directory: %w", err)
}
dependencyOutputPath := filepath.Join(dependencyOutputDir, filepath.Base(dependencyURL))
err = r.webClient.Download(dependencyURL, dependencyOutputPath)
if err != nil {
return "", fmt.Errorf("could not download dependency: %w", err)
}
err = r.checksummer.VerifyASC(string(dependencySignature), dependencyOutputPath, string(rustGPGKey))
if err != nil {
return "", fmt.Errorf("dependency signature verification failed: %w", err)
}
dependencySHA, err := r.checksummer.GetSHA256(dependencyOutputPath)
if err != nil {
return "", fmt.Errorf("could not get SHA256: %w", err)
}
return dependencySHA, nil
}
func (r Rust) dependencySignatureURL(version string) string {
return fmt.Sprintf("https://static.rust-lang.org/dist/rustc-%s-src.tar.gz.asc", version)
}
func (r Rust) dependencyURL(version string) string {
return fmt.Sprintf("https://static.rust-lang.org/dist/rustc-%s-src.tar.gz", version)
}