Skip to content

Commit

Permalink
saving progress of refactors
Browse files Browse the repository at this point in the history
Signed-off-by: chaosinthecrd <[email protected]>
  • Loading branch information
ChaosInTheCRD committed Jan 15, 2024
1 parent a54b4c0 commit 01ed308
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 259 deletions.
3 changes: 0 additions & 3 deletions attestation/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ func NewCollection(name string, attestors []CompletedAttestor) Collection {
Attestations: make([]CollectionAttestation, 0),
}

//move start/stop time to collection
//todo: this is a bit of a hack, but it's the easiest way to get the start/stop time

for _, completed := range attestors {
collection.Attestations = append(collection.Attestations, NewCollectionAttestation(completed))
}
Expand Down
76 changes: 19 additions & 57 deletions attestation/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,75 +117,34 @@ func NewContext(attestors []Attestor, opts ...AttestationContextOption) (*Attest
}

func (ctx *AttestationContext) RunAttestors() error {
preAttestors := []Attestor{}
materialAttestors := []Attestor{}
exeucteAttestors := []Attestor{}
productAttestors := []Attestor{}
postAttestors := []Attestor{}

attestors := make(map[RunType][]Attestor)
for _, attestor := range ctx.attestors {
switch attestor.RunType() {
case PreMaterialRunType:
preAttestors = append(preAttestors, attestor)

case MaterialRunType:
materialAttestors = append(materialAttestors, attestor)

case ExecuteRunType:
exeucteAttestors = append(exeucteAttestors, attestor)

case ProductRunType:
productAttestors = append(productAttestors, attestor)

case PostProductRunType:
postAttestors = append(postAttestors, attestor)

default:
if attestor.RunType() == "" {
return ErrInvalidOption{
Option: "attestor.RunType",
Option: "RunType",
Reason: fmt.Sprintf("unknown run type %v", attestor.RunType()),
}
}
attestors[attestor.RunType()] = append(attestors[attestor.RunType()], attestor)
}

for _, attestor := range preAttestors {
if err := ctx.runAttestor(attestor); err != nil {
return err
}
}

for _, attestor := range materialAttestors {
if err := ctx.runAttestor(attestor); err != nil {
return err
}
}

for _, attestor := range exeucteAttestors {
if err := ctx.runAttestor(attestor); err != nil {
return err
}
}

for _, attestor := range productAttestors {
if err := ctx.runAttestor(attestor); err != nil {
return err
}
}

for _, attestor := range postAttestors {
if err := ctx.runAttestor(attestor); err != nil {
return err
for _, atts := range attestors {
for _, att := range atts {
log.Infof("Starting %v attestor...", att.Name())
if err := ctx.runAttestor(att); err != nil {
log.Errorf("Error running %v attestor: %w", att.Name(), err)
return err
}
}
}

return nil
}

func (ctx *AttestationContext) runAttestor(attestor Attestor) error {
log.Infof("Starting %v attestor...", attestor.Name())
startTime := time.Now()
// NOTE: Not sure if this is the right place to check for an error running the attestor - might be better to let the caller handle it
if err := attestor.Attest(ctx); err != nil {
log.Errorf("Error running %v attestor: %w", attestor.Name(), err)
ctx.completedAttestors = append(ctx.completedAttestors, CompletedAttestor{
Attestor: attestor,
StartTime: startTime,
Expand All @@ -205,24 +164,26 @@ func (ctx *AttestationContext) runAttestor(attestor Attestor) error {
ctx.addMaterials(materialer)
}

if producter, ok := attestor.(Producer); ok {
ctx.addProducts(producter)
if producer, ok := attestor.(Producer); ok {
ctx.addProducts(producer)
}

return nil
}

func (ctx *AttestationContext) CompletedAttestors() []CompletedAttestor {
// NOTE: Not sure if fashioning a copy of the slice is necessary here
attestors := make([]CompletedAttestor, len(ctx.completedAttestors))
copy(attestors, ctx.completedAttestors)
return attestors
return ctx.completedAttestors
}

func (ctx *AttestationContext) WorkingDir() string {
return ctx.workingDir
}

func (ctx *AttestationContext) Hashes() []crypto.Hash {
// NOTE: Not sure if fashioning a copy of the slice is necessary here
hashes := make([]crypto.Hash, len(ctx.hashes))
copy(hashes, ctx.hashes)
return hashes
Expand All @@ -242,12 +203,13 @@ func (ctx *AttestationContext) Materials() map[string]cryptoutil.DigestSet {
}

func (ctx *AttestationContext) Products() map[string]Product {
// NOTE: We're making a copy here and not using it
prodCopy := make(map[string]Product)
for k, v := range ctx.products {
prodCopy[k] = v
}

return ctx.products
return prodCopy
}

func (ctx *AttestationContext) addMaterials(materialer Materialer) {
Expand Down
1 change: 1 addition & 0 deletions attestation/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Subjecter interface {
Subjects() map[string]cryptoutil.DigestSet
}

// NOTE: not sure on the name of this interface, however I can't think of an alternative for now
// Materialer allows attestors to communicate about materials that were observed
// while the attestor executed. For example the material attestor records the hashes
// of all files before a command is run.
Expand Down
6 changes: 6 additions & 0 deletions dsse/dsse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package dsse

import (
"encoding/json"
"fmt"
)

Expand Down Expand Up @@ -70,6 +71,11 @@ type SignatureTimestamp struct {
Data []byte `json:"data"`
}

func (e *Envelope) Write(b []byte) (int, error) {
json.Unmarshal(b, e)
return len(b), nil
}

// preauthEncode wraps the data to be signed or verified and it's type in the DSSE protocol's
// pre-authentication encoding as detailed at https://github.com/secure-systems-lab/dsse/blob/master/protocol.md
// PAE(type, body) = "DSSEv1" + SP + LEN(type) + SP + type + SP + LEN(body) + SP + body
Expand Down
19 changes: 11 additions & 8 deletions dsse/dsse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/in-toto/go-witness/cryptoutil"
"github.com/in-toto/go-witness/timestamp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -244,30 +245,28 @@ func TestTimestamp(t *testing.T) {
{t: time.Now().Add(128 * time.Hour)},
}

allTimestampers := make([]Timestamper, 0)
allTimestampVerifiers := make([]TimestampVerifier, 0)
allTimestampers := make([]timestamp.Timestamper, 0)
for _, expected := range expectedTimestampers {
allTimestampers = append(allTimestampers, expected)
allTimestampVerifiers = append(allTimestampVerifiers, expected)
}

for _, unexpected := range unexpectedTimestampers {
allTimestampers = append(allTimestampers, unexpected)
allTimestampVerifiers = append(allTimestampVerifiers, unexpected)
}

env, err := Sign("dummydata", bytes.NewReader([]byte("this is some dummy data")), SignWithSigners(s), SignWithTimestampers(allTimestampers...))
require.NoError(t, err)

approvedVerifiers, err := env.Verify(VerifyWithVerifiers(v), VerifyWithRoots(root), VerifyWithIntermediates(intermediate), VerifyWithTimestampVerifiers(allTimestampVerifiers...))
approvedVerifiers, err := env.Verify(VerifyWithVerifiers(v), VerifyWithRoots(root), VerifyWithIntermediates(intermediate), VerifyWithTimestampAuthorities(allTimestampers...))
require.NoError(t, err)
assert.Len(t, approvedVerifiers, 1)
assert.Len(t, approvedVerifiers[0].PassedTimestampVerifiers, len(expectedTimestampers))
assert.ElementsMatch(t, approvedVerifiers[0].PassedTimestampVerifiers, expectedTimestampers)
assert.Len(t, approvedVerifiers[0].TimestampAuthority, len(expectedTimestampers))
assert.ElementsMatch(t, approvedVerifiers[0].TimestampAuthority, expectedTimestampers)
}

type dummyTimestamper struct {
t time.Time
t time.Time
url string
}

func (dt dummyTimestamper) Timestamp(context.Context, io.Reader) ([]byte, error) {
Expand All @@ -286,3 +285,7 @@ func (dt dummyTimestamper) Verify(ctx context.Context, ts io.Reader, sig io.Read

return dt.t, nil
}

func (dt dummyTimestamper) Url(ctx context.Context) string {
return dt.url
}
9 changes: 3 additions & 6 deletions dsse/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ import (
"io"

"github.com/in-toto/go-witness/cryptoutil"
"github.com/in-toto/go-witness/timestamp"
)

type Timestamper interface {
Timestamp(context.Context, io.Reader) ([]byte, error)
}

type signOptions struct {
signers []cryptoutil.Signer
timestampers []Timestamper
timestampers []timestamp.Timestamper
}

type SignOption func(*signOptions)
Expand All @@ -41,7 +38,7 @@ func SignWithSigners(signers ...cryptoutil.Signer) SignOption {
}
}

func SignWithTimestampers(timestampers ...Timestamper) SignOption {
func SignWithTimestampers(timestampers ...timestamp.Timestamper) SignOption {
return func(so *signOptions) {
so.timestampers = timestampers
}
Expand Down
Loading

0 comments on commit 01ed308

Please sign in to comment.