-
Notifications
You must be signed in to change notification settings - Fork 38
feat(attestation): enable predicate chainloop.dev/attestation/v0.2 with in-toto ResourceDescriptors #107
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
Merged
Merged
feat(attestation): enable predicate chainloop.dev/attestation/v0.2 with in-toto ResourceDescriptors #107
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -17,7 +17,6 @@ package action | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
@@ -43,13 +42,12 @@ type WorkflowRunItemFull struct { | |
} | ||
|
||
type WorkflowRunAttestationItem struct { | ||
ID string `json:"id"` | ||
CreatedAt *time.Time `json:"createdAt"` | ||
Envelope *dsse.Envelope `json:"envelope"` | ||
statement *in_toto.Statement | ||
predicateV1 *chainloop.ProvenancePredicateV01 | ||
Materials []*Material `json:"materials,omitempty"` | ||
EnvVars []*EnvVar `json:"envvars,omitempty"` | ||
ID string `json:"id"` | ||
CreatedAt *time.Time `json:"createdAt"` | ||
Envelope *dsse.Envelope `json:"envelope"` | ||
statement *in_toto.Statement | ||
Materials []*Material `json:"materials,omitempty"` | ||
EnvVars []*EnvVar `json:"envvars,omitempty"` | ||
} | ||
|
||
type Material struct { | ||
|
@@ -63,10 +61,6 @@ type EnvVar struct { | |
Value string `json:"value"` | ||
} | ||
|
||
func (i *WorkflowRunAttestationItem) Predicate() *chainloop.ProvenancePredicateV01 { | ||
return i.predicateV1 | ||
} | ||
|
||
func (i *WorkflowRunAttestationItem) Statement() *in_toto.Statement { | ||
return i.statement | ||
} | ||
|
@@ -114,24 +108,9 @@ func (action *WorkflowRunDescribe) Run(runID string, verify bool, publicKey stri | |
item.Verified = true | ||
} | ||
|
||
// Decode in-toto statement | ||
statement := &in_toto.Statement{} | ||
decodedPayload, err := envelope.DecodeB64Payload() | ||
statement, err := chainloop.ExtractStatement(envelope) | ||
if err != nil { | ||
return nil, fmt.Errorf("decoding payload: %w", err) | ||
} | ||
|
||
if err := json.Unmarshal(decodedPayload, statement); err != nil { | ||
return nil, fmt.Errorf("un-marshaling predicate: %w", err) | ||
} | ||
|
||
var predicate *chainloop.ProvenancePredicateV01 | ||
if statement.PredicateType == chainloop.PredicateTypeV01 { | ||
if predicate, err = extractPredicateV1(statement); err != nil { | ||
return nil, fmt.Errorf("extracting predicate: %w", err) | ||
} | ||
} else { | ||
return nil, errors.New("predicate type not supported") | ||
return nil, fmt.Errorf("extracting statement: %w", err) | ||
} | ||
|
||
envVars := make([]*EnvVar, 0, len(attestation.GetEnvVars())) | ||
|
@@ -146,30 +125,15 @@ func (action *WorkflowRunDescribe) Run(runID string, verify bool, publicKey stri | |
|
||
item.Attestation = &WorkflowRunAttestationItem{ | ||
ID: attestation.Id, CreatedAt: toTimePtr(attestation.CreatedAt.AsTime()), | ||
Envelope: envelope, | ||
statement: statement, | ||
predicateV1: predicate, | ||
EnvVars: envVars, | ||
Materials: materials, | ||
Envelope: envelope, | ||
statement: statement, | ||
EnvVars: envVars, | ||
Materials: materials, | ||
} | ||
|
||
return item, nil | ||
} | ||
|
||
func extractPredicateV1(statement *in_toto.Statement) (*chainloop.ProvenancePredicateV01, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to renderer.chainloop |
||
jsonPredicate, err := json.Marshal(statement.Predicate) | ||
if err != nil { | ||
return nil, fmt.Errorf("un-marshaling predicate: %w", err) | ||
} | ||
|
||
predicate := &chainloop.ProvenancePredicateV01{} | ||
if err := json.Unmarshal(jsonPredicate, predicate); err != nil { | ||
return nil, fmt.Errorf("un-marshaling predicate: %w", err) | ||
} | ||
|
||
return predicate, nil | ||
} | ||
|
||
func verifyEnvelope(ctx context.Context, e *dsse.Envelope, publicKey string) error { | ||
// Currently we only support basic cosign public key check | ||
// TODO: Add more verification methods | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
// | ||
// Copyright 2023 The Chainloop Authors. | ||
// | ||
// 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 service | ||
|
||
import ( | ||
"testing" | ||
|
||
cpAPI "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" | ||
"github.com/chainloop-dev/chainloop/internal/attestation/renderer/chainloop" | ||
crv1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExtractMaterials(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input []*chainloop.NormalizedMaterial | ||
want []*cpAPI.AttestationItem_Material | ||
}{ | ||
{ | ||
name: "different material types", | ||
input: []*chainloop.NormalizedMaterial{ | ||
{ | ||
Name: "foo", | ||
Type: "STRING", | ||
Value: "bar", | ||
}, | ||
{ | ||
Name: "foo", | ||
Type: "ARTIFACT", | ||
Value: "bar", | ||
Hash: &crv1.Hash{Algorithm: "sha256", Hex: "deadbeef"}, | ||
}, | ||
}, | ||
want: []*cpAPI.AttestationItem_Material{ | ||
{ | ||
Name: "foo", | ||
Type: "STRING", | ||
Value: "bar", | ||
}, | ||
{ | ||
Name: "foo", | ||
Type: "ARTIFACT", | ||
Value: "bar@sha256:deadbeef", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := extractMaterials(tc.input) | ||
assert.Equal(t, got, tc.want) | ||
}) | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the predicate is not needed in fact since we are already providing the materials and env variables denormalized