Skip to content

Commit 97adc5f

Browse files
colek42mikhailswift
authored andcommitted
feat/sarif attestor
1 parent bf5c3a7 commit 97adc5f

3 files changed

Lines changed: 131 additions & 1 deletion

File tree

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/go-git/go-git/v5 v5.4.2
88
github.com/go-openapi/runtime v0.21.0
99
github.com/open-policy-agent/opa v0.37.0
10+
github.com/owenrumney/go-sarif v1.1.1
1011
github.com/sigstore/rekor v0.4.0
1112
github.com/sirupsen/logrus v1.8.1
1213
github.com/spf13/cobra v1.3.0
@@ -19,7 +20,10 @@ require (
1920
gopkg.in/square/go-jose.v2 v2.6.0
2021
)
2122

22-
require github.com/jmespath/go-jmespath v0.4.0 // indirect
23+
require (
24+
github.com/jmespath/go-jmespath v0.4.0 // indirect
25+
github.com/zclconf/go-cty v1.10.0 // indirect
26+
)
2327

2428
require (
2529
github.com/Microsoft/go-winio v0.5.1 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ github.com/apex/log v1.1.4/go.mod h1:AlpoD9aScyQfJDVHmLMEcx4oU6LqzkWp4Mg9GdAcEvQ
199199
github.com/apex/logs v0.0.4/go.mod h1:XzxuLZ5myVHDy9SAmYpamKKRNApGj54PfYLcFrXqDwo=
200200
github.com/aphistic/golf v0.0.0-20180712155816-02c07f170c5a/go.mod h1:3NqKYiepwy8kCu4PNA+aP7WUV72eXWJeP9/r3/K9aLE=
201201
github.com/aphistic/sweet v0.2.0/go.mod h1:fWDlIh/isSE9n6EPsRmC0det+whmX6dJid3stzu0Xys=
202+
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
202203
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
203204
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
204205
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
@@ -1269,6 +1270,8 @@ github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJ
12691270
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
12701271
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
12711272
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
1273+
github.com/owenrumney/go-sarif v1.1.1 h1:QNObu6YX1igyFKhdzd7vgzmw7XsWN3/6NMGuDzBgXmE=
1274+
github.com/owenrumney/go-sarif v1.1.1/go.mod h1:dNDiPlF04ESR/6fHlPyq7gHKmrM0sHUvAGjsoh8ZH0U=
12721275
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
12731276
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
12741277
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
@@ -1528,6 +1531,8 @@ github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852/go.mod h1:tw
15281531
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI=
15291532
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
15301533
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
1534+
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
1535+
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
15311536
github.com/willf/bitset v1.1.11-0.20200630133818-d5bec3311243/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
15321537
github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI=
15331538
github.com/xanzy/go-gitlab v0.31.0/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug=
@@ -1567,6 +1572,8 @@ github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPS
15671572
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=
15681573
github.com/zalando/go-keyring v0.1.0/go.mod h1:RaxNwUITJaHVdQ0VC7pELPZ3tOWn13nr0gZMZEhpVU0=
15691574
github.com/zalando/go-keyring v0.1.1/go.mod h1:OIC+OZ28XbmwFxU/Rp9V7eKzZjamBJwRzC8UFJH9+L8=
1575+
github.com/zclconf/go-cty v1.10.0 h1:mp9ZXQeIcN8kAwuqorjH+Q+njbJKjLrvB2yIh4q7U+0=
1576+
github.com/zclconf/go-cty v1.10.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
15701577
github.com/zeebo/errs v1.2.2 h1:5NFypMTuSdoySVTqlNs1dEoU21QVamMQJxW/Fii5O7g=
15711578
github.com/zeebo/errs v1.2.2/go.mod h1:sgbWHsvVuTPHcqJJGQ1WhI5KbWlHYz+2+2C/LSEtCw4=
15721579
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=

pkg/attestation/sarif/sarif.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)