|
1 | 1 | package pkgs
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
| 6 | + "crypto/sha256" |
| 7 | + "encoding/hex" |
| 8 | + "errors" |
5 | 9 | "fmt"
|
| 10 | + "io" |
6 | 11 | "io/ioutil"
|
7 | 12 | "net/http"
|
| 13 | + "os" |
8 | 14 | "os/user"
|
9 | 15 | "path/filepath"
|
10 | 16 | "runtime"
|
@@ -133,25 +139,36 @@ func (c *Tools) install(ctx context.Context, packager string, tool Tool) error {
|
133 | 139 | }
|
134 | 140 | defer res.Body.Close()
|
135 | 141 |
|
136 |
| - err = extract.Archive(ctx, res.Body, c.Folder, rename(packager, tool.Name, tool.Version)) |
| 142 | + // Use a teereader to only read once |
| 143 | + var buffer bytes.Buffer |
| 144 | + reader := io.TeeReader(res.Body, &buffer) |
| 145 | + |
| 146 | + basepath := filepath.Join(packager, tool.Name, tool.Version) |
| 147 | + err = extract.Archive(ctx, reader, c.Folder, rename(basepath)) |
137 | 148 | if err != nil {
|
138 | 149 | return err
|
139 | 150 | }
|
140 | 151 |
|
| 152 | + checksum := sha256.Sum256(buffer.Bytes()) |
| 153 | + checkSumString := "SHA-256:" + hex.EncodeToString(checksum[:sha256.Size]) |
| 154 | + |
| 155 | + if checkSumString != tool.Systems[i].Checksum { |
| 156 | + os.RemoveAll(basepath) |
| 157 | + return errors.New("checksum doesn't match") |
| 158 | + } |
| 159 | + |
141 | 160 | return nil
|
142 | 161 | }
|
143 | 162 |
|
144 | 163 | func (c *Tools) Remove(ctx context.Context, payload *tools.ToolPayload) error {
|
145 | 164 | return nil
|
146 | 165 | }
|
147 | 166 |
|
148 |
| -func rename(packager, name, version string) extract.Renamer { |
149 |
| - base := filepath.Join(packager, name, version) |
| 167 | +func rename(base string) extract.Renamer { |
150 | 168 | return func(path string) string {
|
151 | 169 | parts := strings.Split(path, string(filepath.Separator))
|
152 | 170 | path = strings.Join(parts[1:], string(filepath.Separator))
|
153 | 171 | path = filepath.Join(base, path)
|
154 |
| - fmt.Println("path", path) |
155 | 172 | return path
|
156 | 173 | }
|
157 | 174 | }
|
|
0 commit comments