forked from chainloop-dev/chainloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(verification): attestation verify command (chainloop-dev#1814)
Signed-off-by: Jose I. Paris <[email protected]>
- Loading branch information
Showing
4 changed files
with
135 additions
and
25 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,56 @@ | ||
// | ||
// Copyright 2025 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 cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/chainloop-dev/chainloop/app/cli/internal/action" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newAttestationVerifyCmd() *cobra.Command { | ||
var fileOrURL string | ||
cmd := &cobra.Command{ | ||
Use: "verify file-or-url", | ||
Short: "verify an attestation", | ||
Long: "Verify an attestation by validating its validation material against the configured trusted root", | ||
DisableFlagsInUseLine: true, | ||
Example: ` # verify local attestation | ||
chainloop attestation verify --bundle attestation.json | ||
# verify an attestation stored in an https endpoint | ||
chainloop attestation verify -b https://myrepository/attestation.json`, | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
res, err := action.NewAttestationVerifyAction(actionOpts).Run(cmd.Context(), fileOrURL) | ||
if err != nil { | ||
return fmt.Errorf("verifying attestation: %w", err) | ||
} | ||
if res { | ||
actionOpts.Logger.Info().Msg("attestation verified successfully") | ||
} else { | ||
actionOpts.Logger.Warn().Msg("attestation couldn't be verified") | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&fileOrURL, "bundle", "b", "", "bundle path or URL") | ||
cobra.CheckErr(cmd.MarkFlagRequired("bundle")) | ||
|
||
return cmd | ||
} |
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,73 @@ | ||
// | ||
// Copyright 2025 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 action | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" | ||
"github.com/chainloop-dev/chainloop/pkg/attestation/verifier" | ||
"github.com/sigstore/cosign/v2/pkg/blob" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type AttestationVerifyAction struct { | ||
cfg *ActionsOpts | ||
} | ||
|
||
func NewAttestationVerifyAction(cfg *ActionsOpts) *AttestationVerifyAction { | ||
return &AttestationVerifyAction{cfg} | ||
} | ||
|
||
func (action *AttestationVerifyAction) Run(ctx context.Context, fileOrURL string) (bool, error) { | ||
content, err := blob.LoadFileOrURL(fileOrURL) | ||
if err != nil { | ||
return false, fmt.Errorf("loading attestation: %w", err) | ||
} | ||
|
||
return verifyBundle(ctx, content, action.cfg) | ||
} | ||
|
||
func verifyBundle(ctx context.Context, content []byte, opts *ActionsOpts) (bool, error) { | ||
sc := pb.NewSigningServiceClient(opts.CPConnection) | ||
trResp, err := sc.GetTrustedRoot(ctx, &pb.GetTrustedRootRequest{}) | ||
if err != nil { | ||
// if trusted root is not implemented, skip verification | ||
if status.Code(err) != codes.Unimplemented { | ||
return false, fmt.Errorf("failed getting trusted root: %w", err) | ||
} | ||
} | ||
|
||
if trResp != nil { | ||
tr, err := trustedRootPbToVerifier(trResp) | ||
if err != nil { | ||
return false, fmt.Errorf("getting roots: %w", err) | ||
} | ||
if err = verifier.VerifyBundle(ctx, content, tr); err != nil { | ||
if !errors.Is(err, verifier.ErrMissingVerificationMaterial) { | ||
opts.Logger.Debug().Err(err).Msg("bundle verification failed") | ||
return false, errors.New("bundle verification failed") | ||
} | ||
} else { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, 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