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

NVSHAS-6217: image signature verification #58

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 10 additions & 0 deletions cvetools/cvesearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ func (cv *CveTools) ScanImage(ctx context.Context, req *share.ScanImageRequest,
return result, nil
}

signatureData, err := rc.GetSignatureDataForImage(ctx, req.Repository, info.Digest)
if err != nil {
return result, fmt.Errorf("error getting signature data for image: %s", err.Error())
}

result.Verifiers, err = verifyImageSignatures(info.Digest, *req.SigstoreConfig, signatureData)
if err != nil {
return result, fmt.Errorf("error verifying signatures for image: %s", err.Error())
}

layers = info.Layers
for _, lf := range layerFiles {
result.Size += lf.Size
Expand Down
65 changes: 65 additions & 0 deletions cvetools/sigstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cvetools

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"

"github.com/neuvector/neuvector/share"
"github.com/neuvector/neuvector/share/scan"
)

type sigstoreInterfaceConfig struct {
ImageDigest string `json:"ImageDigest"`
RootOfTrust share.SigstoreRootOfTrust `json:"RootOfTrust"`
Verifiers []*share.SigstoreVerifier `json:"Verifiers"`
SignatureData scan.SignatureData `json:"SignatureData"`
}

func verifyImageSignatures(imgDigest string, conf share.SigstoreConfig, sigData scan.SignatureData) (verifiers []string, err error) {
inputJSON, err := json.Marshal(sigstoreInterfaceConfig{
ImageDigest: imgDigest,
RootOfTrust: *conf.RootOfTrust,
Verifiers: conf.Verifiers,
SignatureData: sigData,
})
if err != nil {
return verifiers, fmt.Errorf("could not create input json from arguments: %s", err.Error())
}
inputPath := fmt.Sprintf("/tmp/neuvector/sigstore_interface_input_%s.json", imgDigest)
confFile, err := os.Create(inputPath)
if err != nil {
return verifiers, fmt.Errorf("could not create interface input file at %s: %s", inputPath, err.Error())
}
_, err = confFile.Write(inputJSON)
if err != nil {
return verifiers, fmt.Errorf("could not write data to input file at %s: %s", inputPath, err.Error())
}
binaryOutput, err := executeVerificationBinary(inputPath)
if err != nil {
return verifiers, fmt.Errorf("error when executing verification binary: %s", err.Error())
}
verifiers = parseVerifiersFromBinaryOutput(binaryOutput)
return verifiers, nil
}

func parseVerifiersFromBinaryOutput(output string) []string {
outputLines := strings.Split(output, "\n")
lastLine := outputLines[len(outputLines)-2]
satisfiedVerifiers := strings.Split(lastLine[1:len(lastLine)-1], ", ")
return satisfiedVerifiers
}

func executeVerificationBinary(inputPath string) (output string, err error) {
inputFlag := fmt.Sprintf("--config-file=%s", inputPath)
cmd := exec.Command("/usr/bin/sigstore-interface", inputFlag)
var out strings.Builder
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return "", fmt.Errorf("error executing verification binary: %s", err.Error())
}
return out.String(), nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ replace (
k8s.io/mount-utils => k8s.io/mount-utils v0.20.14
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.22.5
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.20.15
github.com/neuvector/neuvector => github.com/alopez-suse/neuvector v0.0.0-20230430125100-acd531428c50
)

require (
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0=
github.com/alopez-suse/neuvector v0.0.0-20230417202658-90f1aec7cf68 h1:LGccTosZLvWMNlkPJAmYV9HRvaTO7VSHb55edVYYaME=
github.com/alopez-suse/neuvector v0.0.0-20230417202658-90f1aec7cf68/go.mod h1:6MKujtS3y/BQlnSXSelaUID2x3e1cmHq/31HoOQl9TM=
github.com/alopez-suse/neuvector v0.0.0-20230418021533-82c2cdf627c5 h1:NWMHo4OzhVmtSph/BsPzI6STfAQWQu+OQWyQB5NyAjw=
github.com/alopez-suse/neuvector v0.0.0-20230418021533-82c2cdf627c5/go.mod h1:6MKujtS3y/BQlnSXSelaUID2x3e1cmHq/31HoOQl9TM=
github.com/alopez-suse/neuvector v0.0.0-20230418031654-20d24ff2bf2c h1:HytODpr6djdetAtD09xr34lJGlZbOpsoHtwb0DZnDdc=
github.com/alopez-suse/neuvector v0.0.0-20230418031654-20d24ff2bf2c/go.mod h1:6MKujtS3y/BQlnSXSelaUID2x3e1cmHq/31HoOQl9TM=
github.com/alopez-suse/neuvector v0.0.0-20230430125100-acd531428c50 h1:38PM9KnWP/lK1KTPrj9AROSQfA+qzpCSl3UsvIr3GaM=
github.com/alopez-suse/neuvector v0.0.0-20230430125100-acd531428c50/go.mod h1:6MKujtS3y/BQlnSXSelaUID2x3e1cmHq/31HoOQl9TM=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
Expand Down
42 changes: 35 additions & 7 deletions vendor/github.com/neuvector/neuvector/controller/api/apis.go

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

40 changes: 36 additions & 4 deletions vendor/github.com/neuvector/neuvector/controller/api/apis.yaml

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

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

Loading