-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds code to generate a VSA (#1141)
* This adds code to generate a VSA. The VSA is added as an output option to the validate image command. https://issues.redhat.com/browse/EC-189
- Loading branch information
1 parent
5eee667
commit 4b52ce6
Showing
13 changed files
with
371 additions
and
0 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,61 @@ | ||
// Copyright The Enterprise Contract 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. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package applicationsnapshot | ||
|
||
import ( | ||
"github.com/in-toto/in-toto-golang/in_toto" | ||
) | ||
|
||
const ( | ||
// Make it visible elsewhere | ||
PredicateVSAProvenance = "https://enterprisecontract.dev/verification_summary/v1" | ||
StatmentVSA = "https://in-toto.io/Statement/v1" | ||
) | ||
|
||
type ProvenanceStatementVSA struct { | ||
in_toto.StatementHeader | ||
Predicate Report `json:"predicate"` | ||
} | ||
|
||
func NewVSA(report Report) (ProvenanceStatementVSA, error) { | ||
subjects, err := getSubjects(report) | ||
if err != nil { | ||
return ProvenanceStatementVSA{}, err | ||
} | ||
|
||
return ProvenanceStatementVSA{ | ||
StatementHeader: in_toto.StatementHeader{ | ||
Type: StatmentVSA, | ||
PredicateType: PredicateVSAProvenance, | ||
Subject: subjects, | ||
}, | ||
Predicate: report, | ||
}, nil | ||
} | ||
|
||
func getSubjects(report Report) ([]in_toto.Subject, error) { | ||
statements, err := report.attestations() | ||
if err != nil { | ||
return []in_toto.Subject{}, err | ||
} | ||
|
||
var subjects []in_toto.Subject | ||
for _, stmt := range statements { | ||
subjects = append(subjects, stmt.Subject...) | ||
} | ||
return subjects, nil | ||
} |
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,148 @@ | ||
// Copyright The Enterprise Contract 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. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package applicationsnapshot | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
ecc "github.com/enterprise-contract/enterprise-contract-controller/api/v1alpha1" | ||
"github.com/in-toto/in-toto-golang/in_toto" | ||
app "github.com/redhat-appstudio/application-api/api/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/enterprise-contract/ec-cli/internal/attestation" | ||
"github.com/enterprise-contract/ec-cli/internal/evaluator" | ||
"github.com/enterprise-contract/ec-cli/internal/policy" | ||
"github.com/enterprise-contract/ec-cli/internal/signature" | ||
"github.com/enterprise-contract/ec-cli/internal/utils" | ||
) | ||
|
||
type provenance struct { | ||
statement in_toto.Statement | ||
data []byte | ||
} | ||
|
||
func (p provenance) Type() string { | ||
return in_toto.StatementInTotoV01 | ||
} | ||
|
||
func (p provenance) PredicateType() string { | ||
return p.statement.StatementHeader.PredicateType | ||
} | ||
|
||
func (p provenance) Statement() []byte { | ||
return p.data | ||
} | ||
|
||
func (p provenance) Signatures() []signature.EntitySignature { | ||
return []signature.EntitySignature{} | ||
} | ||
|
||
func (p provenance) Subject() []in_toto.Subject { | ||
return p.statement.Subject | ||
} | ||
|
||
func TestNewVSA(t *testing.T) { | ||
components := []Component{ | ||
{ | ||
SnapshotComponent: app.SnapshotComponent{Name: "component1"}, | ||
Violations: []evaluator.Result{ | ||
{ | ||
Message: "violation1", | ||
}, | ||
}, | ||
Attestations: []attestation.Attestation{ | ||
provenance{ | ||
statement: in_toto.Statement{}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
utils.SetTestRekorPublicKey(t) | ||
Check failure on line 79 in internal/applicationsnapshot/vsa_test.go GitHub Actions / Test
|
||
pkey := utils.TestPublicKey | ||
Check failure on line 80 in internal/applicationsnapshot/vsa_test.go GitHub Actions / Test
|
||
testPolicy, err := policy.NewPolicy(context.Background(), policy.Options{ | ||
PublicKey: pkey, | ||
EffectiveTime: policy.Now, | ||
PolicyRef: toJson(&ecc.EnterpriseContractPolicySpec{PublicKey: pkey}), | ||
}) | ||
assert.NoError(t, err) | ||
|
||
report, err := NewReport("snappy", components, testPolicy, "data here", nil) | ||
assert.NoError(t, err) | ||
|
||
expected := ProvenanceStatementVSA{ | ||
StatementHeader: in_toto.StatementHeader{ | ||
Type: "https://in-toto.io/Statement/v1", | ||
PredicateType: "https://enterprisecontract.dev/verification_summary/v1", | ||
Subject: nil, | ||
}, | ||
Predicate: report, | ||
} | ||
vsa, err := NewVSA(report) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, vsa) | ||
} | ||
|
||
func TestSubjects(t *testing.T) { | ||
expected := []in_toto.Subject{ | ||
{ | ||
Name: "my-subject", | ||
Digest: nil, | ||
}, | ||
} | ||
statement := in_toto.Statement{ | ||
StatementHeader: in_toto.StatementHeader{ | ||
Subject: expected, | ||
}, | ||
} | ||
data, err := json.Marshal(statement) | ||
assert.NoError(t, err) | ||
|
||
components := []Component{ | ||
{ | ||
SnapshotComponent: app.SnapshotComponent{Name: "component1"}, | ||
Violations: []evaluator.Result{ | ||
{ | ||
Message: "violation1", | ||
}, | ||
}, | ||
Attestations: []attestation.Attestation{ | ||
provenance{ | ||
statement: statement, | ||
data: data, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
report := Report{Components: components} | ||
subjects, err := getSubjects(report) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, subjects) | ||
} | ||
|
||
func toJson(policy any) string { | ||
newInline, err := json.Marshal(policy) | ||
if err != nil { | ||
panic(fmt.Errorf("invalid JSON: %w", err)) | ||
} | ||
return string(newInline) | ||
} |
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
Oops, something went wrong.