This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommon.go
472 lines (396 loc) · 15.6 KB
/
common.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
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
// Copyright 2022 The Project Oak Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package common provides utility functions for building and verifying released binaries.
package common
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
toml "github.com/pelletier/go-toml"
"github.com/project-oak/transparent-release/slsa"
)
const (
// InTotoStatementV01 is the statement type for the generalized link format
// containing statements. This is constant for all predicate types.
InTotoStatementV01 = "https://in-toto.io/Statement/v0.1"
// SLSAPredicateV02 is the predicate type for the SLSA v0.2 Provenance predicate type.
SLSAPredicateV02 = "https://slsa.dev/provenance/v0.2"
// AmberBuildTypeV1 is the SLSA BuildType for Amber builds.
AmberBuildTypeV1 = "https://github.com/project-oak/transparent-release/schema/amber-slsa-buildtype/v1/provenance.json"
)
// BuildConfig is a struct wrapping arguments for building a binary from source.
type BuildConfig struct {
// URL of a public Git repository. Required for generating the provenance file.
Repo string `toml:"repo"`
// The GitHub commit hash to build the binary from. Required for checking
// that the binary is being release from the correct source.
// TODO(razieh): It might be better to instead use Git tree hash.
CommitHash string `toml:"commit_hash"`
// URI identifying the Docker image to use for building the binary.
BuilderImage string `toml:"builder_image"`
// Command to pass to the `docker run` command. The command is taken as an
// array instead of a single string to avoid unnecessary parsing. See
// https://docs.docker.com/engine/reference/builder/#cmd and
// https://man7.org/linux/man-pages/man3/exec.3.html for more details.
Command []string `toml:"command"`
// The path, relative to the root of the git repository, where the binary
// built by the `docker run` command is expected to be found.
OutputPath string `toml:"output_path"`
// Expected SHA256 hash of the output binary. Could be empty.
ExpectedBinarySha256Hash string `toml:"expected_binary_sha256_hash"`
}
// RepoCheckoutInfo contains info about the location of a locally checked out
// repository.
type RepoCheckoutInfo struct {
// Path to the root of the repo
RepoRoot string
// Path to a file containing the logs
Logs string
}
// LoadBuildConfigFromFile loads build configuration from a toml file in the given path and returns an instance of BuildConfig.
func LoadBuildConfigFromFile(path string) (*BuildConfig, error) {
tomlTree, err := toml.LoadFile(path)
if err != nil {
return nil, fmt.Errorf("couldn't load toml file: %v", err)
}
config := BuildConfig{}
if err := tomlTree.Unmarshal(&config); err != nil {
return nil, fmt.Errorf("couldn't ubmarshal toml file: %v", err)
}
return &config, nil
}
// LoadBuildConfigFromProvenance loads build configuration from a SLSA Provenance object.
func LoadBuildConfigFromProvenance(provenance *slsa.Provenance) (*BuildConfig, error) {
if len(provenance.Subject) != 1 {
return nil, fmt.Errorf("the provenance must have exactly one Subject, got %d", len(provenance.Subject))
}
expectedBinarySha256Hash := provenance.Subject[0].Digest["sha256"]
if len(provenance.Subject) != 1 {
return nil, fmt.Errorf("the provenance's subject digest must specify a sha256 hash, got %d", expectedBinarySha256Hash)
}
if len(provenance.Predicate.Materials) != 2 {
return nil, fmt.Errorf("the provenance must have exactly two Materials, got %d", len(provenance.Predicate.Materials))
}
builderImage := provenance.Predicate.Materials[0].URI
if builderImage == "" {
return nil, fmt.Errorf("the provenance's first material must specify a URI, got %d", builderImage)
}
repo := provenance.Predicate.Materials[1].URI
if repo == "" {
return nil, fmt.Errorf("the provenance's second material must specify a URI, got %d", repo)
}
commitHash := provenance.Predicate.Materials[1].Digest["sha1"]
if commitHash == "" {
return nil, fmt.Errorf("the provenance's second material must have an sha1 hash, got %d", commitHash)
}
command := provenance.Predicate.BuildConfig.Command
if command[0] == "" {
return nil, fmt.Errorf("the provenance's buildConfig must specify a command, got %d", command)
}
outputPath := provenance.Predicate.BuildConfig.OutputPath
if outputPath == "" {
return nil, fmt.Errorf("the provenance's second material must have an sha1 hash, got %d", outputPath)
}
config := BuildConfig{
Repo: provenance.Predicate.Materials[1].URI,
CommitHash: commitHash,
BuilderImage: builderImage,
Command: command,
OutputPath: outputPath,
ExpectedBinarySha256Hash: expectedBinarySha256Hash,
}
return &config, nil
}
// Build executes a command for building a binary. The command is created from
// the arguments in this object.
func (b *BuildConfig) Build() error {
// TODO(razieh): Add a validate method, and/or a new type for a ValidatedBuildConfig.
// Check that the OutputPath is empty, so that we don't accidentally release
// the wrong binary in case the build silently fails for whatever reason.
if _, err := os.Stat(b.OutputPath); !os.IsNotExist(err) {
return fmt.Errorf("the specified output path (%s) is not empty", b.OutputPath)
}
// TODO(razieh): The build must be hermetic. Consider disabling the network
// after fetching all sources to ensure that the build can be completed with
// all the provided sources, without fetching any other files from external
// sources.
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("couldn't get the current working directory: %v", err)
}
defaultDockerRunFlags := []string{
// TODO(razieh): Check that b.DockerRunFlags does not set similar flags.
// Mount the current working directory to workspace.
fmt.Sprintf("--volume=%s:/workspace", cwd),
"--workdir=/workspace",
// Remove the container file system after the container exits.
"--rm",
// Get a pseudo-tty to the docker container.
// TODO(razieh): We probably don't need it for presubmit.
"--tty"}
var args []string
args = append(args, "run")
args = append(args, defaultDockerRunFlags...)
args = append(args, b.BuilderImage)
args = append(args, b.Command...)
cmd := exec.Command("docker", args...)
stderr, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("couldn't get a pipe to stderr: %v", err)
}
log.Printf("Running command: %q.", cmd.String())
if err := cmd.Start(); err != nil {
return fmt.Errorf("couldn't start the command: %v", err)
}
tmpfileName, err := saveToTempFile(stderr)
if err != nil {
return fmt.Errorf("couldn't save error logs to file: %v", err)
}
if err := cmd.Wait(); err != nil {
return fmt.Errorf("failed to complete the command: %v, see %s for error logs",
err, tmpfileName)
}
// Verify that a file is built in the output path; return an error otherwise.
if _, err := os.Stat(b.OutputPath); err != nil {
return fmt.Errorf("missing expected output file in %s, see %s for error logs",
b.OutputPath, tmpfileName)
}
return nil
}
// VerifyCommit checks that code is running in a Git repository at the Git
// commit hash equal to `CommitHash` in this BuildConfig.
func (b *BuildConfig) VerifyCommit() error {
cmd := exec.Command("git", "rev-parse", "--verify", "HEAD")
lastCommitIDBytes, err := cmd.Output()
if err != nil {
return fmt.Errorf("couldn't get the last Git commit hash: %v", err)
}
lastCommitID := strings.TrimSpace(string(lastCommitIDBytes))
if lastCommitID != b.CommitHash {
return fmt.Errorf("the last commit hash (%q) does not match the given commit hash (%q)", lastCommitID, b.CommitHash)
}
return nil
}
// ComputeBinarySha256Hash computes the SHA256 hash of the file in the
// `OutputPath` of this BuildConfig.
func (b *BuildConfig) ComputeBinarySha256Hash() (string, error) {
binarySha256Hash, err := computeSha256Hash(b.OutputPath)
if err != nil {
return "", fmt.Errorf("couldn't compute SHA256 hash of %q: %v", b.OutputPath, err)
}
return binarySha256Hash, nil
}
// VerifyBinarySha256Hash computes the SHA256 hash of the binary built by this
// BuildConfig, and checks that this hash is equal to `ExpectedSha256Hash` in
// this BuildConfig. Returns an error if the hashes are not equal.
func (b *BuildConfig) VerifyBinarySha256Hash() error {
binarySha256Hash, err := b.ComputeBinarySha256Hash()
if err != nil {
return err
}
if b.ExpectedBinarySha256Hash == "" || b.ExpectedBinarySha256Hash != binarySha256Hash {
return fmt.Errorf("the hash of the generated binary does not match the expected SHA256 hash; got %s, want %v",
binarySha256Hash, b.ExpectedBinarySha256Hash)
}
return nil
}
// GenerateProvenanceStatement generates a provenance statement from this config. If
// `ExpectedBinarySha256Hash` is non-empty, the provenance statement is generated only if the
// SHA256 hash of the generated binary is equal to `ExpectedBinarySha256Hash`.
func (b *BuildConfig) GenerateProvenanceStatement() (*slsa.Provenance, error) {
binarySha256Hash, err := b.ComputeBinarySha256Hash()
if err != nil {
return nil, err
}
log.Printf("The hash of the binary is: %s", binarySha256Hash)
if b.ExpectedBinarySha256Hash != "" && b.ExpectedBinarySha256Hash != binarySha256Hash {
return nil, fmt.Errorf("the hash of the output binary does not match the expected binary hash; got %s, want %v",
binarySha256Hash, b.ExpectedBinarySha256Hash)
}
subject := slsa.Subject{
// TODO(#57): Get the name as an input in the TOML file.
Name: fmt.Sprintf("%s-%s", filepath.Base(b.OutputPath), b.CommitHash),
Digest: map[string]string{"sha256": string(binarySha256Hash)},
}
alg, digest, err := parseBuilderImageURI(b.BuilderImage)
if err != nil {
return nil, fmt.Errorf("malformed builder image URI: %v", err)
}
predicate := slsa.Predicate{
BuildType: AmberBuildTypeV1,
BuildConfig: slsa.BuildConfig{
Command: b.Command,
OutputPath: b.OutputPath,
},
Materials: []slsa.Material{
// Builder image
slsa.Material{
URI: b.BuilderImage,
Digest: map[string]string{alg: digest},
},
// Source code
slsa.Material{
URI: b.Repo,
Digest: map[string]string{"sha1": b.CommitHash},
},
},
}
return &slsa.Provenance{
Type: InTotoStatementV01,
Subject: []slsa.Subject{subject},
PredicateType: SLSAPredicateV02,
Predicate: predicate,
}, nil
}
func parseBuilderImageURI(imageURI string) (string, string, error) {
// We expect the URI of the builder image to be of the form NAME@DIGEST
URIParts := strings.Split(imageURI, "@")
if len(URIParts) != 2 {
return "", "", fmt.Errorf("the builder image URI (%q) does not have the required NAME@DIGEST format", imageURI)
}
// We expect the DIGEST to be of the form ALG:VALUE
digestParts := strings.Split(URIParts[1], ":")
if len(digestParts) != 2 {
return "", "", fmt.Errorf("the builder image digest (%q) does not have the required ALG:VALUE format", URIParts[1])
}
return digestParts[0], digestParts[1], nil
}
// saveToTempFile creates a tempfile in `/tmp` and writes the content of the
// given reader to that file.
func saveToTempFile(reader io.Reader) (string, error) {
bytes, err := io.ReadAll(reader)
if err != nil {
return "", err
}
tmpfile, err := ioutil.TempFile("", "log-*.txt")
if err != nil {
return "", fmt.Errorf("couldn't create tempfile: %v", err)
}
if _, err := tmpfile.Write(bytes); err != nil {
tmpfile.Close()
return "", fmt.Errorf("couldn't write bytes to tempfile: %v", err)
}
return tmpfile.Name(), nil
}
// FetchSourcesFromRepo fetches a repo from the given URL into a temporary directory,
// and checks out the specified commit. An instance of RepoCheckoutInfo
// containing the absolute path to the root of the repo is returned.
func FetchSourcesFromRepo(repoURL, commitHash string) (*RepoCheckoutInfo, error) {
// create a temp folder in the current directory for fetching the repo.
targetDir, err := ioutil.TempDir("", "release-*")
if err != nil {
return nil, fmt.Errorf("couldn't create temp directory: %v", err)
}
log.Printf("checking out the repo in %s.", targetDir)
if _, err := os.Stat(targetDir); !os.IsNotExist(err) {
// If target dir already exists remove it and its content.
if err := os.RemoveAll(targetDir); err != nil {
return nil, fmt.Errorf("couldn't remove pre-existing files in %s: %v", targetDir, err)
}
}
// Make targetDir and its parents, and cd to it.
if err := os.MkdirAll(targetDir, 0755); err != nil {
return nil, fmt.Errorf("couldn't create directories at %s: %v", targetDir, err)
}
if err := os.Chdir(targetDir); err != nil {
return nil, fmt.Errorf("couldn't change directory to %s: %v", targetDir, err)
}
// Clone the repo.
tmpfileName, err := cloneGitRepo(repoURL)
if err != nil {
return nil, fmt.Errorf("couldn't clone the Git repo: %v", err)
}
log.Printf("'git clone' completed. See %s for any error logs.", tmpfileName)
// Change directory to the root of the cloned repo.
repoName := path.Base(repoURL)
if err := os.Chdir(repoName); err != nil {
return nil, fmt.Errorf("couldn't change directory to %s: %v", repoName, err)
}
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("couldn't get current working directory: %v", err)
}
// Checkout the commit.
tmpfileName, err = checkoutGitCommit(commitHash)
if err != nil {
return nil, fmt.Errorf("couldn't checkout the Git commit %q: %v", commitHash, err)
}
info := RepoCheckoutInfo{
RepoRoot: cwd,
Logs: tmpfileName,
}
return &info, nil
}
func toStringSlice(slice []interface{}) []string {
ss := make([]string, 0, len(slice))
for _, s := range slice {
ss = append(ss, s.(string))
}
return ss
}
func computeSha256Hash(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("couldn't read file %q: %v", path, err)
}
sum256 := sha256.Sum256(data)
return hex.EncodeToString(sum256[:]), nil
}
func cloneGitRepo(repo string) (string, error) {
cmd := exec.Command("git", "clone", repo)
stderr, err := cmd.StderrPipe()
if err != nil {
return "", fmt.Errorf("couldn't get a pipe to stderr: %v", err)
}
log.Printf("Cloning the repo from %s...", repo)
if err := cmd.Start(); err != nil {
return "", fmt.Errorf("couldn't start the 'git clone' command: %v", err)
}
tmpfileName, err := saveToTempFile(stderr)
if err != nil {
return "", fmt.Errorf("couldn't save error logs to file: %v", err)
}
if err := cmd.Wait(); err != nil {
return "", fmt.Errorf("failed to complete the command: %v, see %s for error logs",
err, tmpfileName)
}
return tmpfileName, nil
}
func checkoutGitCommit(commitHash string) (string, error) {
cmd := exec.Command("git", "checkout", commitHash)
stderr, err := cmd.StderrPipe()
if err != nil {
return "", fmt.Errorf("couldn't get a pipe to stderr: %v", err)
}
if err := cmd.Start(); err != nil {
return "", fmt.Errorf("couldn't start the 'git checkout' command: %v", err)
}
tmpfileName, err := saveToTempFile(stderr)
if err != nil {
return "", fmt.Errorf("couldn't save error logs to file: %v", err)
}
if err := cmd.Wait(); err != nil {
return "", fmt.Errorf("failed to complete the command: %v, see %s for error logs",
err, tmpfileName)
}
return tmpfileName, nil
}