|
| 1 | +// Copyright 2022 The Witness Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package sarif |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "io/ioutil" |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/owenrumney/go-sarif/sarif" |
| 25 | + "github.com/testifysec/witness/pkg/attestation" |
| 26 | + "github.com/testifysec/witness/pkg/cryptoutil" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + Name = "sarif" |
| 31 | + Type = "https://witness.testifysec.com/attestations/sarif/v0.1" |
| 32 | + RunType = attestation.PostRunType |
| 33 | +) |
| 34 | + |
| 35 | +var mimeTypes = []string{"text/plain", "application/json"} |
| 36 | + |
| 37 | +func init() { |
| 38 | + attestation.RegisterAttestation(Name, Type, RunType, func() attestation.Attestor { |
| 39 | + return New() |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +type Attestor struct { |
| 44 | + sarif.Report `json:"report"` |
| 45 | + ReportFile string `json:"reportFileName"` |
| 46 | + ReportDigestSet cryptoutil.DigestSet `json:"reportDigestSet"` |
| 47 | +} |
| 48 | + |
| 49 | +func New() *Attestor { |
| 50 | + return &Attestor{} |
| 51 | +} |
| 52 | + |
| 53 | +func (a *Attestor) Name() string { |
| 54 | + return Name |
| 55 | +} |
| 56 | + |
| 57 | +func (a *Attestor) Type() string { |
| 58 | + return Type |
| 59 | +} |
| 60 | + |
| 61 | +func (a *Attestor) RunType() attestation.RunType { |
| 62 | + return RunType |
| 63 | +} |
| 64 | + |
| 65 | +func (a *Attestor) Attest(ctx *attestation.AttestationContext) error { |
| 66 | + if err := a.getCanidate(ctx); err != nil { |
| 67 | + fmt.Printf("error getting canidate: %s\n", err) |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func (a *Attestor) getCanidate(ctx *attestation.AttestationContext) error { |
| 75 | + products := ctx.Products() |
| 76 | + |
| 77 | + if len(products) == 0 { |
| 78 | + return fmt.Errorf("no products to attest") |
| 79 | + } |
| 80 | + |
| 81 | + for path, product := range products { |
| 82 | + for _, mimeType := range mimeTypes { |
| 83 | + if !strings.Contains(mimeType, product.MimeType) { |
| 84 | + continue |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + newDigestSet, err := cryptoutil.CalculateDigestSetFromFile(path, ctx.Hashes()) |
| 89 | + if newDigestSet == nil || err != nil { |
| 90 | + return fmt.Errorf("error calculating digest set from file: %s", path) |
| 91 | + } |
| 92 | + |
| 93 | + if !newDigestSet.Equal(product.Digest) { |
| 94 | + return fmt.Errorf("integrity error: product digest set does not match canidate digest set") |
| 95 | + } |
| 96 | + |
| 97 | + f, err := os.Open(path) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("error opening file: %s", path) |
| 100 | + } |
| 101 | + |
| 102 | + reportBytes, err := ioutil.ReadAll(f) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("error reading file: %s", path) |
| 105 | + } |
| 106 | + |
| 107 | + //check to see if we can unmarshal into sarif type |
| 108 | + if err := json.Unmarshal(reportBytes, &a.Report); err != nil { |
| 109 | + fmt.Printf("error unmarshaling report: %s\n", err) |
| 110 | + continue |
| 111 | + } |
| 112 | + |
| 113 | + a.ReportFile = path |
| 114 | + a.ReportDigestSet = product.Digest |
| 115 | + |
| 116 | + return nil |
| 117 | + } |
| 118 | + return fmt.Errorf("no sarif file found") |
| 119 | +} |
0 commit comments