Skip to content
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
34 changes: 24 additions & 10 deletions app/cli/cmd/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ func newArtifactCmd() *cobra.Command {
return cmd
}

func wrappedArtifactConn(cpConn *grpc.ClientConn, role pb.CASCredentialsServiceGetRequest_Role, digest string) (*grpc.ClientConn, error) {
// Retrieve temporary credentials for the operation
client := pb.NewCASCredentialsServiceClient(cpConn)
resp, err := client.Get(context.Background(), &pb.CASCredentialsServiceGetRequest{
Role: role,
Digest: digest,
// an upload or a download is for a single digest, so the token can only access that digest
ScopeToDigest: digest != "",
})
// wrappedArtifactConn connects to the CAS API with casToken or, when casToken is empty,
// with a temporary credential from the control plane
func wrappedArtifactConn(cpConn *grpc.ClientConn, role pb.CASCredentialsServiceGetRequest_Role, digest, casToken string) (*grpc.ClientConn, error) {
var err error
if casToken == "" {
casToken, err = casCredentialFromControlPlane(cpConn, role, digest)
}
if err != nil {
return nil, err
}
Expand All @@ -70,5 +68,21 @@ func wrappedArtifactConn(cpConn *grpc.ClientConn, role pb.CASCredentialsServiceG
}
}

return grpcconn.New(viper.GetString(confOptions.CASAPI.viperKey), resp.Result.Token, opts...)
return grpcconn.New(viper.GetString(confOptions.CASAPI.viperKey), casToken, opts...)
}

// casCredentialFromControlPlane retrieves temporary credentials for the operation
func casCredentialFromControlPlane(cpConn *grpc.ClientConn, role pb.CASCredentialsServiceGetRequest_Role, digest string) (string, error) {
client := pb.NewCASCredentialsServiceClient(cpConn)
resp, err := client.Get(context.Background(), &pb.CASCredentialsServiceGetRequest{
Role: role,
Digest: digest,
// an upload or a download is for a single digest, so the token can only access that digest
ScopeToDigest: digest != "",
})
if err != nil {
return "", err
}

return resp.Result.Token, nil
}
66 changes: 54 additions & 12 deletions app/cli/cmd/artifact_download.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2024 The Chainloop Authors.
// Copyright 2024-2026 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.
Expand All @@ -17,46 +17,67 @@ package cmd

import (
"errors"
"fmt"
"os"
"slices"
"strings"

"github.com/chainloop-dev/chainloop/app/cli/pkg/action"
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

//nolint:gosec
const artifactCASTokenEnvVarName = "CHAINLOOP_ARTIFACT_CAS_TOKEN"

func newArtifactDownloadCmd() *cobra.Command {
var digest, downloadPath, outputFile string
var digest, downloadPath, outputFile, casTokenFlag string
var artifactCASConn *grpc.ClientConn
var actionOpts *action.ActionsOpts

cmd := &cobra.Command{
Use: "download",
Short: "download an artifact",
PreRunE: func(cmd *cobra.Command, args []string) error {
var err error

// Root does not build the control plane connection for this command: PreRunE builds it
// only when no CAS credential is set. Telemetry reports the command in both cases.
Annotations: map[string]string{skipActionOptsInit: trueString, reportTelemetryAnyway: trueString},
PreRunE: func(cmd *cobra.Command, _ []string) error {
if err := validateFlags(downloadPath, outputFile); err != nil {
return err
}

// Retrieve temporary credentials for uploading
artifactCASConn, err = wrappedArtifactConn(ActionOpts.CPConnection,
pb.CASCredentialsServiceGetRequest_ROLE_DOWNLOADER, digest)
casToken, err := artifactCASToken(casTokenFlag)
if err != nil {
return err
}

return nil
// A CAS credential replaces the API token and the control plane
var cpConn *grpc.ClientConn
if casToken == "" {
if err := ensureActionOpts(cmd); err != nil {
return err
}
actionOpts, cpConn = ActionOpts, ActionOpts.CPConnection
} else {
logger.Debug().Msg("using the given CAS credential, the control plane is not contacted")
actionOpts = newActionOpts(logger, nil, "")
}

artifactCASConn, err = wrappedArtifactConn(cpConn,
pb.CASCredentialsServiceGetRequest_ROLE_DOWNLOADER, digest, casToken)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
opts := &action.ArtifactDownloadOpts{
ActionsOpts: ActionOpts,
ActionsOpts: actionOpts,
ArtifactsCASConn: artifactCASConn,
Stdout: cmd.OutOrStdout(),
}

return action.NewArtifactDownload(opts).Run(downloadPath, outputFile, digest)
},
PostRunE: func(cmd *cobra.Command, args []string) error {
PostRunE: func(_ *cobra.Command, _ []string) error {
if artifactCASConn != nil {
return artifactCASConn.Close()
}
Expand All @@ -70,10 +91,31 @@ func newArtifactDownloadCmd() *cobra.Command {
cobra.CheckErr(err)
cmd.Flags().StringVar(&downloadPath, "path", "", "download path, default to current directory")
cmd.Flags().StringVar(&outputFile, "output", "", "The `file` to write a single asset to (use \"-\" to write to standard output")
cmd.Flags().StringVar(&casTokenFlag, "artifact-cas-token", "", fmt.Sprintf("CAS download credential for --digest, used instead of the API token and the control plane. NOTE: Prefer the env variable %s", artifactCASTokenEnvVarName))

return cmd
}

// artifactCASToken returns the CAS credential from the flag or, when the flag is empty,
// from the environment. The CAS verifies the credential; this only rejects a non-JWT value.
func artifactCASToken(flagValue string) (string, error) {
token := flagValue
if token == "" {
token = os.Getenv(artifactCASTokenEnvVarName)
}

token = strings.TrimSpace(token)
if token == "" {
return "", nil
}

if parts := strings.Split(token, "."); len(parts) != 3 || slices.Contains(parts, "") {
return "", errors.New("the CAS credential is not a JWT")
}

return token, nil
}

// validateFlags checks if the flags are valid
func validateFlags(downloadPath, outputFile string) error {
if downloadPath != "" && outputFile != "" {
Expand Down
Loading
Loading