forked from in-toto/go-witness
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Nick Kane <nkane@testifysec.com>
- Loading branch information
1 parent
38dc13c
commit 644571c
Showing
7 changed files
with
269 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright 2024 The Witness Contributors | ||
// | ||
// 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 vex | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/in-toto/go-witness/attestation" | ||
"github.com/in-toto/go-witness/cryptoutil" | ||
"github.com/in-toto/go-witness/log" | ||
"github.com/invopop/jsonschema" | ||
vex "github.com/openvex/go-vex/pkg/vex" | ||
) | ||
|
||
const ( | ||
Name = "vex" | ||
Type = "https://openvex.dev/ns" | ||
RunType = attestation.PostProductRunType | ||
) | ||
|
||
// This is a hacky way to create a compile time error in case the attestor | ||
// doesn't implement the expected interfaces. | ||
var ( | ||
_ attestation.Attestor = &Attestor{} | ||
) | ||
|
||
func init() { | ||
attestation.RegisterAttestation(Name, Type, RunType, func() attestation.Attestor { | ||
return New() | ||
}) | ||
} | ||
|
||
type Attestor struct { | ||
VEXDocument vex.VEX `json:"vexDocument"` | ||
ReportFile string `json:"reportFileName,omitempty"` | ||
ReportDigestSet cryptoutil.DigestSet `json:"reportDigestSet,omitempty"` | ||
} | ||
|
||
func New() *Attestor { | ||
return &Attestor{} | ||
} | ||
|
||
func (a *Attestor) Name() string { | ||
return Name | ||
} | ||
|
||
func (a *Attestor) Type() string { | ||
return Type | ||
} | ||
|
||
func (a *Attestor) RunType() attestation.RunType { | ||
return RunType | ||
} | ||
|
||
func (a *Attestor) Schema() *jsonschema.Schema { | ||
return jsonschema.Reflect(&a) | ||
} | ||
|
||
func (a *Attestor) Attest(ctx *attestation.AttestationContext) error { | ||
if err := a.getCandidate(ctx); err != nil { | ||
log.Debugf("(attestation/vex) error getting candidate: %w", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *Attestor) getCandidate(ctx *attestation.AttestationContext) error { | ||
products := ctx.Products() | ||
|
||
if len(products) == 0 { | ||
return fmt.Errorf("no products to attest") | ||
} | ||
|
||
for path, product := range products { | ||
newDigestSet, err := cryptoutil.CalculateDigestSetFromFile(path, ctx.Hashes()) | ||
if newDigestSet == nil || err != nil { | ||
return fmt.Errorf("error calculating digest set from file: %s", path) | ||
} | ||
|
||
if !newDigestSet.Equal(product.Digest) { | ||
return fmt.Errorf("integrity error: product digest set does not match candidate digest set") | ||
} | ||
|
||
f, err := os.Open(path) | ||
if err != nil { | ||
return fmt.Errorf("error opening file: %s", path) | ||
} | ||
|
||
reportBytes, err := io.ReadAll(f) | ||
if err != nil { | ||
return fmt.Errorf("error reading file: %s", path) | ||
} | ||
|
||
// Check to see if we can unmarshal into VEX type | ||
if err := json.Unmarshal(reportBytes, &a.VEXDocument); err != nil { | ||
log.Debugf("(attestation/vex) error unmarshaling VEX document: %w", err) | ||
continue | ||
} | ||
|
||
a.ReportFile = path | ||
a.ReportDigestSet = product.Digest | ||
|
||
return nil | ||
} | ||
return fmt.Errorf("no VEX file found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2024 The Witness Contributors | ||
// | ||
// 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 vex | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/in-toto/go-witness/attestation" | ||
vex "github.com/openvex/go-vex/pkg/vex" | ||
) | ||
|
||
// NOTE(nick): examples https://github.com/openvex/vexctl/tree/main/examples/openvex | ||
|
||
const vexDocumentExpected = `{ | ||
"vexDocument": { | ||
"@context": "https://openvex.dev/ns/v0.2.0", | ||
"@id": "https://openvex.dev/docs/public/vex-0f3be8817faafa24e4bfb3d17eaf619efb1fe54923b9c42c57b156a936b91431", | ||
"author": "John Doe", | ||
"role": "Senior Trusted Vex Issuer", | ||
"timestamp": "1970-01-01T00:00:00Z", | ||
"version": 1, | ||
"statements": [ | ||
{ | ||
"vulnerability": { | ||
"name": "CVE-1234-5678" | ||
}, | ||
"products": [ | ||
{ | ||
"@id": "pkg:apk/wolfi/bash@1.0.0" | ||
} | ||
], | ||
"status": "fixed" | ||
} | ||
] | ||
} | ||
}` | ||
|
||
func TestAttest(t *testing.T) { | ||
vexAttestor := New() | ||
vexAttestor.VEXDocument.Context = "https://openvex.dev/ns/v0.2.0" | ||
vexAttestor.VEXDocument.ID = "https://openvex.dev/docs/public/vex-0f3be8817faafa24e4bfb3d17eaf619efb1fe54923b9c42c57b156a936b91431" | ||
vexAttestor.VEXDocument.Author = "John Doe" | ||
vexAttestor.VEXDocument.AuthorRole = "Senior Trusted Vex Issuer" | ||
vexAttestor.VEXDocument.Version = 1 | ||
time := time.Date(1970, 1, 1, 0, 0, 0, 0, time.Now().UTC().Location()) | ||
vexAttestor.VEXDocument.Timestamp = &time | ||
vexAttestor.VEXDocument.Statements = []vex.Statement{ | ||
{ | ||
Vulnerability: vex.Vulnerability{ | ||
Name: "CVE-1234-5678", | ||
}, | ||
Products: []vex.Product{ | ||
{ | ||
Component: vex.Component{ | ||
ID: "pkg:apk/wolfi/bash@1.0.0", | ||
}, | ||
}, | ||
}, | ||
Status: vex.StatusFixed, | ||
}, | ||
} | ||
|
||
attestorCollection := []attestation.Attestor{vexAttestor} | ||
ctx, err := attestation.NewContext("test", append(attestorCollection, vexAttestor)) | ||
if err != nil { | ||
t.Errorf("error creating attestation context: %s", err) | ||
} | ||
err = ctx.RunAttestors() | ||
if err != nil { | ||
t.Errorf("error attesting: %s", err.Error()) | ||
} | ||
|
||
vexDocJSON, err := json.MarshalIndent(vexAttestor, "", " ") | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err) | ||
} | ||
|
||
expectedJSON := []byte(vexDocumentExpected) | ||
|
||
if !bytes.Equal(vexDocJSON, expectedJSON) { | ||
t.Errorf("expected \n%s\n, got \n%s\n", expectedJSON, vexDocJSON) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters