Skip to content

Commit 36f6360

Browse files
Check if a signed URL is specified and use it download tools
1 parent 2fa738f commit 36f6360

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

tools/download.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"path/filepath"
3131
"runtime"
3232

33+
"github.com/arduino/arduino-create-agent/utilities"
3334
"github.com/arduino/arduino-create-agent/v2/pkgs"
3435
"github.com/arduino/go-paths-helper"
3536
"github.com/blang/semver"
@@ -98,17 +99,33 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
9899
return t.writeMap()
99100
}
100101
}
102+
return t.download(pack, correctTool.Name, correctTool.Version, correctSystem.URL, correctSystem.Checksum)
103+
}
104+
105+
// DowloadUsingURL will download the tool from the specified URL if it is signed and can be verified.
106+
// If no URL is specified, it will follow the standard download procedure.
107+
func (t *Tools) DownloadFromURL(pack, name, version, behaviour, URL, signature, checksum string) error {
108+
if URL != "" && signature != "" && checksum != "" {
109+
err := utilities.VerifyInput(URL, signature)
110+
if err != nil {
111+
return err
112+
}
113+
return t.download(pack, name, version, URL, checksum)
114+
}
115+
return t.Download(pack, name, version, behaviour)
116+
}
101117

118+
func (t *Tools) download(pack, name, version, url, checksum256 string) error {
102119
// Download the tool
103-
t.logger("Downloading tool " + name + " from " + correctSystem.URL)
104-
resp, err := http.Get(correctSystem.URL)
120+
t.logger("Downloading tool " + name + " from " + url)
121+
resp, err := http.Get(url)
105122
if err != nil {
106123
return err
107124
}
108125
defer resp.Body.Close()
109126

110127
// Read the body
111-
body, err = io.ReadAll(resp.Body)
128+
body, err := io.ReadAll(resp.Body)
112129
if err != nil {
113130
return err
114131
}
@@ -117,7 +134,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
117134
checksum := sha256.Sum256(body)
118135
checkSumString := "SHA-256:" + hex.EncodeToString(checksum[:sha256.Size])
119136

120-
if checkSumString != correctSystem.Checksum {
137+
if checkSumString != checksum256 {
121138
return errors.New("checksum doesn't match")
122139
}
123140

@@ -140,7 +157,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
140157
return fmt.Errorf("extracting archive: %s", err)
141158
}
142159

143-
location := t.directory.Join(pack, correctTool.Name, correctTool.Version)
160+
location := t.directory.Join(pack, name, version)
144161
err = location.RemoveAll()
145162
if err != nil {
146163
return err
@@ -172,7 +189,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
172189
t.logger("Updating map with location " + location.String())
173190

174191
t.setMapValue(name, location.String())
175-
t.setMapValue(name+"-"+correctTool.Version, location.String())
192+
t.setMapValue(name+"-"+version, location.String())
176193
return t.writeMap()
177194
}
178195

0 commit comments

Comments
 (0)