Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make startedOn and finishedOn optional fields for hasSLSA #877

Merged
merged 4 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions cmd/guacgql/cmd/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ func ingestScorecards(ctx context.Context, client graphql.Client) {

func ingestSLSA(ctx context.Context, client graphql.Client) {
logger := logging.FromContext(ctx)

startTime := time.Now()
finishTime := time.Now().Add(10 * time.Second)
predicate := []model.SLSAPredicateInputSpec{
{
Key: "buildDefinition.externalParameters.repository",
Expand Down Expand Up @@ -124,8 +125,8 @@ func ingestSLSA(ctx context.Context, client graphql.Client) {
BuildType: "Test:SLSA",
SlsaPredicate: predicate,
SlsaVersion: "v1",
StartedOn: time.Now(),
FinishedOn: time.Now().Add(10 * time.Second),
StartedOn: &startTime,
FinishedOn: &finishTime,
Origin: "Demo ingestion",
Collector: "Demo ingestion",
}
Expand Down Expand Up @@ -155,8 +156,6 @@ func ingestSLSA(ctx context.Context, client graphql.Client) {
BuildType: "Test:SLSA2",
SlsaPredicate: predicate,
SlsaVersion: "v1",
StartedOn: time.Now(),
FinishedOn: time.Now().Add(10 * time.Second),
Origin: "Demo ingestion",
Collector: "Demo ingestion",
}
Expand Down
4 changes: 2 additions & 2 deletions internal/testing/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ var (
HasSlsa: &model.SLSAInputSpec{
BuildType: "https://github.com/Attestations/GitHubActionsWorkflow@v1",
SlsaVersion: "https://slsa.dev/provenance/v0.2",
StartedOn: slsaStartTime,
StartedOn: &slsaStartTime,
SlsaPredicate: []model.SLSAPredicateInputSpec{
{Key: "slsa.metadata.completeness.environment", Value: "true"},
{Key: "slsa.metadata.buildStartedOn", Value: "2020-08-19T08:38:00Z"},
Expand Down Expand Up @@ -381,7 +381,7 @@ var (
HasSlsa: &model.SLSAInputSpec{
BuildType: "https://slsa-framework.github.io/github-actions-buildtypes/workflow/v1",
SlsaVersion: "https://slsa.dev/provenance/v1",
StartedOn: slsa1time,
StartedOn: &slsa1time,
SlsaPredicate: []model.SLSAPredicateInputSpec{
{Key: "slsa.buildDefinition.buildType", Value: "https://slsa-framework.github.io/github-actions-buildtypes/workflow/v1"},
{Key: "slsa.buildDefinition.externalParameters.inputs.build_id", Value: "1.23456768e+08"},
Expand Down
11 changes: 11 additions & 0 deletions pkg/assembler/backends/inmem/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"sync"
"sync/atomic"
"time"

"github.com/guacsec/guac/pkg/assembler/backends"
"github.com/guacsec/guac/pkg/assembler/graphql/model"
Expand Down Expand Up @@ -181,6 +182,16 @@ func nilToEmpty(input *string) string {
return *input
}

func timePtrEqual(a, b *time.Time) bool {
if a == nil && b == nil {
return true
}
if a != nil && b != nil {
return a.Equal(*b)
}
return false
}

func toLower(filter *string) *string {
if filter != nil {
lower := strings.ToLower(*filter)
Expand Down
12 changes: 6 additions & 6 deletions pkg/assembler/backends/inmem/hasSLSA.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type hasSLSAStruct struct {
buildType string
predicates []*model.SLSAPredicate
version string
start time.Time
finish time.Time
start *time.Time
finish *time.Time
origin string
collector string
}
Expand Down Expand Up @@ -227,8 +227,8 @@ func (c *demoClient) ingestSLSA(ctx context.Context,
sl.buildType == slsa.BuildType &&
cmp.Equal(sl.predicates, preds) &&
sl.version == slsa.SlsaVersion &&
sl.start == slsa.StartedOn &&
sl.finish == slsa.FinishedOn &&
timePtrEqual(sl.start, slsa.StartedOn) &&
timePtrEqual(sl.finish, slsa.FinishedOn) &&
sl.origin == slsa.Origin &&
sl.collector == slsa.Collector {
return c.convSLSA(sl)
Expand Down Expand Up @@ -324,8 +324,8 @@ func (c *demoClient) addSLSAIfMatch(out []*model.HasSlsa,
noMatch(filter.SlsaVersion, link.version) ||
noMatch(filter.Origin, link.origin) ||
noMatch(filter.Collector, link.collector) ||
(filter.StartedOn != nil && !filter.StartedOn.Equal(link.start)) ||
(filter.FinishedOn != nil && !filter.FinishedOn.Equal(link.finish)) ||
(filter.StartedOn != nil && (link.start == nil || !filter.StartedOn.Equal(*link.start))) ||
(filter.FinishedOn != nil && (link.finish == nil || !filter.FinishedOn.Equal(*link.finish))) ||
(filter.BuiltBy != nil && filter.BuiltBy.ID != nil && *filter.BuiltBy.ID != nodeID(bb.id)) ||
(filter.BuiltBy != nil && filter.BuiltBy.URI != nil && *filter.BuiltBy.URI != bb.uri) ||
!matchSLSAPreds(link.predicates, filter.Predicate) ||
Expand Down
7 changes: 4 additions & 3 deletions pkg/assembler/backends/inmem/hasSLSA_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var b2 = &model.BuilderInputSpec{

func TestHasSLSA(t *testing.T) {
testTime := time.Unix(1e9+5, 0)
testTime2 := time.Unix(1e9, 0)
type call struct {
Sub *model.ArtifactInputSpec
BF []*model.ArtifactInputSpec
Expand Down Expand Up @@ -203,15 +204,15 @@ func TestHasSLSA(t *testing.T) {
BF: []*model.ArtifactInputSpec{a2},
BB: b1,
SLSA: &model.SLSAInputSpec{
StartedOn: time.Unix(1e9, 0),
StartedOn: &testTime2,
},
},
{
Sub: a1,
BF: []*model.ArtifactInputSpec{a2},
BB: b1,
SLSA: &model.SLSAInputSpec{
StartedOn: testTime,
StartedOn: &testTime,
},
},
},
Expand All @@ -224,7 +225,7 @@ func TestHasSLSA(t *testing.T) {
Slsa: &model.Slsa{
BuiltBy: b1out,
BuiltFrom: []*model.Artifact{a2out},
StartedOn: testTime,
StartedOn: &testTime,
},
},
},
Expand Down
16 changes: 8 additions & 8 deletions pkg/assembler/clients/generated/operations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 6 additions & 18 deletions pkg/assembler/graphql/generated/hasSLSA.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/assembler/graphql/generated/root_.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/assembler/graphql/model/nodes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/assembler/graphql/schema/hasSLSA.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ type SLSA {
"Version of the SLSA predicate"
slsaVersion: String!
"Timestamp (RFC3339Nano format) of build start time"
startedOn: Time!
startedOn: Time
"Timestamp (RFC3339Nano format) of build end time"
finishedOn: Time!
finishedOn: Time
"Document from which this attestation is generated from"
origin: String!
"GUAC collector for the document"
Expand Down Expand Up @@ -115,8 +115,8 @@ input SLSAInputSpec {
buildType: String!
slsaPredicate: [SLSAPredicateInputSpec!]!
slsaVersion: String!
startedOn: Time!
finishedOn: Time!
startedOn: Time
finishedOn: Time
origin: String!
collector: String!
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/ingestor/parser/slsa/parser_slsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,30 +197,30 @@ func getSlsaEntity(name string, digests scommon.DigestSet) (*slsaEntity, error)
func fillSLSA01(inp *model.SLSAInputSpec, stmt *in_toto.ProvenanceStatementSLSA01) {
inp.BuildType = stmt.Predicate.Recipe.Type
if stmt.Predicate.Metadata.BuildStartedOn != nil {
inp.StartedOn = *stmt.Predicate.Metadata.BuildStartedOn
inp.StartedOn = stmt.Predicate.Metadata.BuildStartedOn
}
if stmt.Predicate.Metadata.BuildFinishedOn != nil {
inp.FinishedOn = *stmt.Predicate.Metadata.BuildStartedOn
inp.FinishedOn = stmt.Predicate.Metadata.BuildStartedOn
}
}

func fillSLSA02(inp *model.SLSAInputSpec, stmt *in_toto.ProvenanceStatementSLSA02) {
inp.BuildType = stmt.Predicate.BuildType
if stmt.Predicate.Metadata.BuildStartedOn != nil {
inp.StartedOn = *stmt.Predicate.Metadata.BuildStartedOn
inp.StartedOn = stmt.Predicate.Metadata.BuildStartedOn
}
if stmt.Predicate.Metadata.BuildFinishedOn != nil {
inp.FinishedOn = *stmt.Predicate.Metadata.BuildStartedOn
inp.FinishedOn = stmt.Predicate.Metadata.BuildStartedOn
}
}

func fillSLSA1(inp *model.SLSAInputSpec, stmt *in_toto.ProvenanceStatementSLSA1) {
inp.BuildType = stmt.Predicate.BuildDefinition.BuildType
if stmt.Predicate.RunDetails.BuildMetadata.StartedOn != nil {
inp.StartedOn = *stmt.Predicate.RunDetails.BuildMetadata.StartedOn
inp.StartedOn = stmt.Predicate.RunDetails.BuildMetadata.StartedOn
}
if stmt.Predicate.RunDetails.BuildMetadata.FinishedOn != nil {
inp.FinishedOn = *stmt.Predicate.RunDetails.BuildMetadata.FinishedOn
inp.FinishedOn = stmt.Predicate.RunDetails.BuildMetadata.FinishedOn
}
}

Expand Down