2023-01-05 03:49:44 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
2023-03-29 08:13:26 -04:00
|
|
|
|
2023-01-05 03:49:44 -05:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/printer"
|
|
|
|
"go/token"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/sigstore"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versionsapi"
|
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// this tool is used to generate hardcoded measurements for the enterprise build.
|
|
|
|
// Measurements are embedded in the constellation cli.
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
defaultConf := config.Default()
|
|
|
|
log.Printf("Generating measurements for %s\n", defaultConf.Image)
|
|
|
|
|
|
|
|
const filePath = "./measurements_enterprise.go"
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
file, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
rekor, err := sigstore.NewRekor()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var returnStmtCtr int
|
|
|
|
|
|
|
|
newFile := astutil.Apply(file, func(cursor *astutil.Cursor) bool {
|
|
|
|
n := cursor.Node()
|
|
|
|
|
|
|
|
// find all switch cases for the CSPs of the form:
|
|
|
|
// switch provider {
|
|
|
|
// case cloudprovider.XYZ:
|
|
|
|
// return M{...}
|
|
|
|
|
|
|
|
if clause, ok := n.(*ast.CaseClause); ok && len(clause.List) > 0 && len(clause.Body) > 0 {
|
|
|
|
sel, ok := clause.List[0].(*ast.SelectorExpr)
|
|
|
|
if !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
returnStmt, ok := clause.Body[0].(*ast.ReturnStmt)
|
|
|
|
if !ok || len(returnStmt.Results) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := cloudprovider.FromString(sel.Sel.Name)
|
|
|
|
if provider == cloudprovider.Unknown {
|
|
|
|
log.Fatalf("unknown provider %s", sel.Sel.Name)
|
|
|
|
}
|
|
|
|
log.Println("Found", provider)
|
|
|
|
returnStmtCtr++
|
|
|
|
// retrieve and validate measurements for the given CSP and image
|
|
|
|
measuremnts := mustGetMeasurements(ctx, rekor, []byte(constants.CosignPublicKey), http.DefaultClient, provider, defaultConf.Image)
|
|
|
|
// replace the return statement with a composite literal containing the validated measurements
|
2023-03-23 12:44:30 -04:00
|
|
|
returnStmt.Results[0] = measurementsCompositeLiteral(measuremnts)
|
2023-01-05 03:49:44 -05:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
if returnStmtCtr == 0 {
|
|
|
|
log.Fatal("no measurements updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
printConfig := printer.Config{Mode: printer.UseSpaces | printer.TabIndent, Tabwidth: 8}
|
|
|
|
|
|
|
|
if err = printConfig.Fprint(&buf, fset, newFile); err != nil {
|
|
|
|
log.Fatalf("error formatting file %s: %s", filePath, err)
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(filePath, buf.Bytes(), 0o644); err != nil {
|
|
|
|
log.Fatalf("error writing file %s: %s", filePath, err)
|
|
|
|
}
|
|
|
|
log.Println("Successfully generated hashes.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// mustGetMeasurements fetches the measurements for the given image and CSP and verifies them.
|
|
|
|
func mustGetMeasurements(ctx context.Context, verifier rekorVerifier, cosignPublicKey []byte, client *http.Client, provider cloudprovider.Provider, image string) measurements.M {
|
|
|
|
measurementsURL, err := measurementURL(provider, image, "measurements.json")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
signatureURL, err := measurementURL(provider, image, "measurements.json.sig")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Fetching measurements from", measurementsURL, "and signature from", signatureURL)
|
|
|
|
var fetchedMeasurements measurements.M
|
|
|
|
hash, err := fetchedMeasurements.FetchAndVerify(
|
|
|
|
ctx, client,
|
|
|
|
measurementsURL,
|
|
|
|
signatureURL,
|
|
|
|
cosignPublicKey,
|
|
|
|
measurements.WithMetadata{
|
|
|
|
CSP: provider,
|
|
|
|
Image: image,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := verifyWithRekor(ctx, verifier, hash); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return fetchedMeasurements
|
|
|
|
}
|
|
|
|
|
|
|
|
// measurementURL returns the URL for the measurements file for the given image and CSP.
|
|
|
|
func measurementURL(provider cloudprovider.Provider, image, file string) (*url.URL, error) {
|
|
|
|
version, err := versionsapi.NewVersionFromShortPath(image, versionsapi.VersionKindImage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing image name: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.Parse(
|
2023-02-03 05:05:42 -05:00
|
|
|
version.ArtifactsURL() + path.Join("/image", "csp", strings.ToLower(provider.String()), file),
|
2023-01-05 03:49:44 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyWithRekor verifies that the given hash is present in rekor and is valid.
|
|
|
|
func verifyWithRekor(ctx context.Context, verifier rekorVerifier, hash string) error {
|
|
|
|
uuids, err := verifier.SearchByHash(ctx, hash)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("searching Rekor for hash: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(uuids) == 0 {
|
|
|
|
return fmt.Errorf("no matching entries in Rekor")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect the first entry in Rekor to be our original entry.
|
|
|
|
// SHA256 should ensure there is no entry with the same hash.
|
|
|
|
// Any subsequent hashes are treated as potential attacks and are ignored.
|
|
|
|
// Attacks on Rekor will be monitored from other backend services.
|
|
|
|
artifactUUID := uuids[0]
|
|
|
|
|
|
|
|
return verifier.VerifyEntry(
|
|
|
|
ctx, artifactUUID,
|
|
|
|
base64.StdEncoding.EncodeToString([]byte(constants.CosignPublicKey)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// byteArrayCompositeLit returns a *ast.CompositeLit representing a byte array literal.
|
|
|
|
// The returned literal is of the form:
|
2023-03-08 08:13:57 -05:00
|
|
|
// []byte{ 0x01, 0x02, 0x03, ... }.
|
|
|
|
func byteArrayCompositeLit(hex []byte) *ast.CompositeLit {
|
2023-01-05 03:49:44 -05:00
|
|
|
var elts []ast.Expr
|
|
|
|
// create list of byte literals
|
2023-03-23 12:44:30 -04:00
|
|
|
for _, b := range hex {
|
2023-01-05 03:49:44 -05:00
|
|
|
elts = append(elts, &ast.BasicLit{
|
2023-03-23 12:44:30 -04:00
|
|
|
Kind: token.INT,
|
|
|
|
Value: fmt.Sprintf("0x%02x", b),
|
2023-01-05 03:49:44 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
// create the composite literal
|
2023-03-08 08:13:57 -05:00
|
|
|
// containing the byte literals as part of an []byte slice
|
2023-01-05 03:49:44 -05:00
|
|
|
return &ast.CompositeLit{
|
|
|
|
Type: &ast.ArrayType{
|
2023-03-23 12:44:30 -04:00
|
|
|
Elt: ast.NewIdent("byte"),
|
2023-01-05 03:49:44 -05:00
|
|
|
},
|
2023-03-23 12:44:30 -04:00
|
|
|
Elts: elts,
|
2023-01-05 03:49:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// measurementsEntryKeyValueExpr returns a *ast.KeyValueExpr representing a measurements.Measurement entry.
|
|
|
|
// The returned expression is of the form:
|
|
|
|
//
|
|
|
|
// 0: {
|
2023-03-08 08:13:57 -05:00
|
|
|
// Expected: []byte{ 0x01, 0x02, 0x03, ... },
|
2023-01-05 03:49:44 -05:00
|
|
|
// WarnOnly: false,
|
|
|
|
// },
|
2023-03-23 12:44:30 -04:00
|
|
|
func measurementsEntryKeyValueExpr(pcr uint32, measuremnt measurements.Measurement) *ast.KeyValueExpr {
|
2023-01-05 03:49:44 -05:00
|
|
|
key := fmt.Sprintf("%d", pcr)
|
2023-03-23 12:44:30 -04:00
|
|
|
|
|
|
|
var validationOptString string
|
2023-03-22 06:47:39 -04:00
|
|
|
if measuremnt.ValidationOpt {
|
2023-03-23 12:44:30 -04:00
|
|
|
validationOptString = "WarnOnly"
|
2023-01-05 03:49:44 -05:00
|
|
|
} else {
|
2023-03-23 12:44:30 -04:00
|
|
|
validationOptString = "Enforce"
|
2023-01-05 03:49:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ast.KeyValueExpr{
|
|
|
|
Key: &ast.BasicLit{
|
2023-03-23 12:44:30 -04:00
|
|
|
Kind: token.INT,
|
|
|
|
Value: key,
|
2023-01-05 03:49:44 -05:00
|
|
|
},
|
|
|
|
Value: &ast.CompositeLit{
|
|
|
|
Elts: []ast.Expr{
|
|
|
|
&ast.KeyValueExpr{
|
2023-03-23 12:44:30 -04:00
|
|
|
Key: &ast.Ident{Name: "Expected"},
|
|
|
|
Value: byteArrayCompositeLit(measuremnt.Expected),
|
2023-01-05 03:49:44 -05:00
|
|
|
},
|
|
|
|
&ast.KeyValueExpr{
|
2023-03-23 12:44:30 -04:00
|
|
|
Key: &ast.Ident{Name: "ValidationOpt"},
|
|
|
|
Value: &ast.Ident{Name: validationOptString},
|
2023-01-05 03:49:44 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-03-23 12:44:30 -04:00
|
|
|
}
|
2023-01-05 03:49:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// measurementsCompositeLiteral returns a *ast.CompositeLit representing a measurements.M literal.
|
|
|
|
// The returned literal is of the form:
|
|
|
|
//
|
|
|
|
// M{
|
|
|
|
// 0: {
|
2023-03-08 08:13:57 -05:00
|
|
|
// Expected: []byte{ 0x01, 0x02, 0x03, ... },
|
2023-01-05 03:49:44 -05:00
|
|
|
// WarnOnly: false,
|
|
|
|
// },
|
|
|
|
// 1: {
|
2023-03-08 08:13:57 -05:00
|
|
|
// Expected: []byte{ 0x01, 0x02, 0x03, ... },
|
2023-01-05 03:49:44 -05:00
|
|
|
// WarnOnly: false,
|
|
|
|
// },
|
|
|
|
// ...
|
|
|
|
// }
|
2023-03-23 12:44:30 -04:00
|
|
|
func measurementsCompositeLiteral(measurements measurements.M) *ast.CompositeLit {
|
2023-01-05 03:49:44 -05:00
|
|
|
var elts []ast.Expr
|
|
|
|
pcrs := make([]uint32, 0, len(measurements))
|
|
|
|
for pcr := range measurements {
|
|
|
|
pcrs = append(pcrs, pcr)
|
|
|
|
}
|
|
|
|
sort.Slice(pcrs, func(i, j int) bool { return pcrs[i] < pcrs[j] })
|
|
|
|
for _, pcr := range pcrs {
|
2023-03-23 12:44:30 -04:00
|
|
|
kvExpr := measurementsEntryKeyValueExpr(pcr, measurements[pcr])
|
2023-01-05 03:49:44 -05:00
|
|
|
elts = append(elts, kvExpr)
|
|
|
|
}
|
|
|
|
return &ast.CompositeLit{
|
|
|
|
Type: &ast.Ident{
|
2023-03-23 12:44:30 -04:00
|
|
|
Name: "M",
|
2023-01-05 03:49:44 -05:00
|
|
|
},
|
2023-03-23 12:44:30 -04:00
|
|
|
Elts: elts,
|
2023-01-05 03:49:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type rekorVerifier interface {
|
|
|
|
SearchByHash(context.Context, string) ([]string, error)
|
|
|
|
VerifyEntry(context.Context, string, string) error
|
|
|
|
}
|