-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The idea is a TaskRun produces an unsigned SBOM and pushes it to the OCI registry. The reference to the SBOM is then added to the result IMAGE_SBOM_URL. When Chains sees this result, it downloads the SBOM from the OCI registry, signs it, then adds to the image attestations with an SBOM-specific predicate. IMAGE_SBOM_FORMAT is used to specify the predicate to list in the attestation. This is also possible when using the IMAGES result, and the corresponding SBOMS result. Both must have the exact number of items. When using the SBOMS result, SBOMS_FORMAT is also required to specify the SBOM format for all the items in SBOMS. Signed-off-by: Luiz Carvalho <[email protected]>
- Loading branch information
Showing
7 changed files
with
444 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
Copyright 2023 The Tekton 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 sbom | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
intoto "github.com/in-toto/in-toto-golang/in_toto" | ||
slsa "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v0.2" | ||
"github.com/tektoncd/chains/pkg/chains/objects" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const maxSBOMBytes = 5 * 1024 * 1024 // 5 Megabytes | ||
|
||
func GenerateAttestation(builderID string, sbom *objects.SBOMObject, logger *zap.SugaredLogger) (interface{}, error) { | ||
subject := []intoto.Subject{ | ||
{Name: sbom.GetImageURL(), Digest: toDigestSet(sbom.GetImageDigest())}, | ||
} | ||
|
||
data, err := getData(sbom.GetSBOMURL()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
att := intoto.Statement{ | ||
StatementHeader: intoto.StatementHeader{ | ||
Type: intoto.StatementInTotoV01, | ||
PredicateType: sbom.GetSBOMFormat(), | ||
Subject: subject, | ||
}, | ||
Predicate: SBOMPredicate{ | ||
Data: data, | ||
}, | ||
} | ||
return att, nil | ||
} | ||
|
||
type SBOMPredicate struct { | ||
Data string `json:"Data"` | ||
} | ||
|
||
func getData(uri string) (string, error) { | ||
// TODO: remote options so it works against a private repository? | ||
ref, err := name.ParseReference(uri) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
image, err := remote.Image(ref) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
layers, err := image.Layers() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if len(layers) != 1 { | ||
return "", fmt.Errorf("expected exactly 1 layer in sbom image %s, found %d", uri, len(layers)) | ||
} | ||
|
||
layer, err := layers[0].Uncompressed() | ||
if err != nil { | ||
return "", err | ||
} | ||
defer layer.Close() | ||
|
||
var data bytes.Buffer | ||
if _, err := io.Copy(&data, io.LimitReader(layer, maxSBOMBytes)); err != nil { | ||
return "", err | ||
} | ||
|
||
return data.String(), nil | ||
} | ||
|
||
func toDigestSet(digest string) slsa.DigestSet { | ||
algo, value, found := strings.Cut(digest, ":") | ||
if !found { | ||
value = algo | ||
algo = "sha256" | ||
} | ||
return slsa.DigestSet{algo: value} | ||
} |
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
Oops, something went wrong.