Skip to content
Merged
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
7 changes: 6 additions & 1 deletion app/cli/cmd/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"

pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
"github.com/chainloop-dev/chainloop/internal/grpcconn"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
Expand All @@ -44,5 +45,9 @@ func wrappedArtifactConn(cpConn *grpc.ClientConn, role pb.CASCredentialsServiceG
return nil, err
}

return newGRPCConnection(viper.GetString(confOptions.CASAPI.viperKey), resp.Result.Token, flagInsecure, logger)
if flagInsecure {
logger.Warn().Msg("API contacted in insecure mode")
}

return grpcconn.New(viper.GetString(confOptions.CASAPI.viperKey), resp.Result.Token, flagInsecure)
}
2 changes: 1 addition & 1 deletion app/cli/cmd/artifact_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newArtifactDownloadCmd() *cobra.Command {
var err error

// Retrieve temporary credentials for uploading
artifactCASConn, err = wrappedArtifactConn(actionOpts.CPConnecction,
artifactCASConn, err = wrappedArtifactConn(actionOpts.CPConnection,
pb.CASCredentialsServiceGetRequest_ROLE_DOWNLOADER)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion app/cli/cmd/artifact_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newArtifactUploadCmd() *cobra.Command {
var err error

// Retrieve temporary credentials for uploading
artifactCASConn, err = wrappedArtifactConn(actionOpts.CPConnecction, pb.CASCredentialsServiceGetRequest_ROLE_UPLOADER)
artifactCASConn, err = wrappedArtifactConn(actionOpts.CPConnection, pb.CASCredentialsServiceGetRequest_ROLE_UPLOADER)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions app/cli/cmd/attestation_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/chainloop-dev/chainloop/app/cli/internal/action"
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
"github.com/chainloop-dev/chainloop/internal/grpcconn"
)

func newAttestationAddCmd() *cobra.Command {
Expand All @@ -42,13 +43,13 @@ func newAttestationAddCmd() *cobra.Command {

// Retrieve temporary credentials for uploading
// TODO: only do it for artifact uploads
client := pb.NewAttestationServiceClient(actionOpts.CPConnecction)
client := pb.NewAttestationServiceClient(actionOpts.CPConnection)
resp, err := client.GetUploadCreds(context.Background(), &pb.AttestationServiceGetUploadCredsRequest{})
if err != nil {
return newGracefulError(err)
}

artifactCASConn, err = newGRPCConnection(viper.GetString(confOptions.CASAPI.viperKey), resp.Result.Token, flagInsecure, logger)
artifactCASConn, err = grpcconn.New(viper.GetString(confOptions.CASAPI.viperKey), resp.Result.Token, flagInsecure)
if err != nil {
return newGracefulError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion app/cli/cmd/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func openbrowser(url string) error {

// Retrieve loginURL from the control plane
func retrieveLoginURL() (string, error) {
client := pb.NewStatusServiceClient(actionOpts.CPConnecction)
client := pb.NewStatusServiceClient(actionOpts.CPConnection)
resp, err := client.Infoz(context.Background(), &pb.InfozRequest{})
if err != nil {
return "", err
Expand Down
50 changes: 8 additions & 42 deletions app/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@
package cmd

import (
"crypto/x509"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/adrg/xdg"
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
"github.com/chainloop-dev/chainloop/app/cli/internal/bearertoken"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
"github.com/chainloop-dev/chainloop/internal/grpcconn"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
grpc_insecure "google.golang.org/grpc/credentials/insecure"
)

var (
Expand Down Expand Up @@ -75,7 +71,11 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
}
}

conn, err := newGRPCConnection(viper.GetString(confOptions.controlplaneAPI.viperKey), storedToken, flagInsecure, logger)
if flagInsecure {
logger.Warn().Msg("API contacted in insecure mode")
}

conn, err := grpcconn.New(viper.GetString(confOptions.controlplaneAPI.viperKey), storedToken, flagInsecure)
if err != nil {
return err
}
Expand All @@ -85,7 +85,7 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
return nil
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
return cleanup(logger, actionOpts.CPConnecction)
return cleanup(logger, actionOpts.CPConnection)
},
}

Expand Down Expand Up @@ -163,42 +163,8 @@ func initConfigFile() {
cobra.CheckErr(viper.ReadInConfig())
}

func newGRPCConnection(uri, authToken string, insecure bool, logger zerolog.Logger) (*grpc.ClientConn, error) {
var opts []grpc.DialOption
if authToken != "" {
grpcCreds := bearertoken.NewTokenAuth(authToken, flagInsecure)

opts = []grpc.DialOption{
grpc.WithPerRPCCredentials(grpcCreds),
// Retry using default configuration
grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor()),
}
}

var tlsDialOption grpc.DialOption
if insecure {
logger.Warn().Msg("API contacted in insecure mode")
tlsDialOption = grpc.WithTransportCredentials(grpc_insecure.NewCredentials())
} else {
certsPool, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
tlsDialOption = grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(certsPool, ""))
}

opts = append(opts, tlsDialOption)

conn, err := grpc.Dial(uri, opts...)
if err != nil {
return nil, err
}

return conn, nil
}

func newActionOpts(logger zerolog.Logger, conn *grpc.ClientConn) *action.ActionsOpts {
return &action.ActionsOpts{CPConnecction: conn, Logger: logger}
return &action.ActionsOpts{CPConnection: conn, Logger: logger}
}

func cleanup(logger zerolog.Logger, conn *grpc.ClientConn) error {
Expand Down
4 changes: 2 additions & 2 deletions app/cli/internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
)

type ActionsOpts struct {
CPConnecction *grpc.ClientConn
Logger zerolog.Logger
CPConnection *grpc.ClientConn
Logger zerolog.Logger
}

func toTimePtr(t time.Time) *time.Time {
Expand Down
11 changes: 5 additions & 6 deletions app/cli/internal/action/artifact_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (a *ArtifactDownload) Run(downloadPath, digest string) error {
return fmt.Errorf("invalid digest: %w", err)
}

client := casclient.NewDownloader(a.artifactsCASConn)
client := casclient.New(a.artifactsCASConn)
ctx := context.Background()
info, err := client.Describe(ctx, h.Hex)
if err != nil {
Expand Down Expand Up @@ -81,10 +81,10 @@ func (a *ArtifactDownload) Run(downloadPath, digest string) error {
a.Logger.Info().Str("name", info.Filename).Str("to", downloadPath).Msg("downloading file")

// render progress bar
go renderOperationStatus(ctx, client.ProgressStatus, a.Logger)
go renderOperationStatus(ctx, client.ProgressStatus, a.Logger, info.Size)
defer close(client.ProgressStatus)

err = client.Download(ctx, w, h.Hex, info.Size)
err = client.Download(ctx, w, h.Hex)
if err != nil {
a.Logger.Debug().Err(err).Msg("problem downloading file")
return errors.New("problem downloading file")
Expand All @@ -103,7 +103,7 @@ func (a *ArtifactDownload) Run(downloadPath, digest string) error {
return nil
}

func renderOperationStatus(ctx context.Context, progressChan casclient.ProgressStatusChan, output io.Writer) {
func renderOperationStatus(ctx context.Context, progressChan casclient.ProgressStatusChan, output io.Writer, totalSize int64) {
pw := progress.NewWriter()
pw.Style().Visibility.ETA = true
pw.Style().Visibility.Speed = true
Expand All @@ -127,8 +127,7 @@ func renderOperationStatus(ctx context.Context, progressChan casclient.ProgressS
// Hack: Add 1 to the total to make sure the tracker is not marked as done before the upload is finished
// this way the current value will never reach the total
// but instead the tracker will be marked as done by the defer statement
total := status.TotalSizeBytes + 1
tracker = &progress.Tracker{Total: total, Units: progress.UnitsBytes}
tracker = &progress.Tracker{Total: totalSize + 1, Units: progress.UnitsBytes}
defer tracker.MarkAsDone()
pw.AppendTracker(tracker)
}
Expand Down
19 changes: 17 additions & 2 deletions app/cli/internal/action/artifact_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package action

import (
"context"
"fmt"
"os"
"time"

"github.com/chainloop-dev/chainloop/internal/casclient"
Expand All @@ -43,9 +45,22 @@ func NewArtifactUpload(opts *ArtifactUploadOpts) *ArtifactUpload {
}

func (a *ArtifactUpload) Run(filePath string) (*CASArtifact, error) {
client := casclient.NewUploader(a.artifactsCASConn, casclient.WithLogger(a.Logger))
client := casclient.New(a.artifactsCASConn, casclient.WithLogger(a.Logger))

// open file and calculate size
f, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("can't open file to upload: %w", err)
}
defer f.Close()

info, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("retrieving file information: %w", err)
}

// render progress bar
go renderOperationStatus(context.Background(), client.ProgressStatus, a.Logger)
go renderOperationStatus(context.Background(), client.ProgressStatus, a.Logger, info.Size())
defer close(client.ProgressStatus)

res, err := client.Upload(context.Background(), filePath)
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/attestation_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewAttestationAdd(cfg *AttestationAddOpts) *AttestationAdd {
ActionsOpts: cfg.ActionsOpts,
c: crafter.NewCrafter(
crafter.WithLogger(&cfg.Logger),
crafter.WithUploader(casclient.NewUploader(cfg.ArtifactsCASConn, casclient.WithLogger(cfg.Logger))),
crafter.WithUploader(casclient.New(cfg.ArtifactsCASConn, casclient.WithLogger(cfg.Logger))),
),
artifactsCASConn: cfg.ArtifactsCASConn,
}
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/attestation_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (action *AttestationInit) Run(contractRevision int) error {
}

action.Logger.Debug().Msg("Retrieving attestation definition")
client := pb.NewAttestationServiceClient(action.ActionsOpts.CPConnecction)
client := pb.NewAttestationServiceClient(action.ActionsOpts.CPConnection)
// get information of the workflow
ctx := context.Background()
resp, err := client.GetContract(ctx, &pb.AttestationServiceGetContractRequest{ContractRevision: int32(contractRevision)})
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/attestation_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (action *AttestationPush) Run() (interface{}, error) {
return res, nil
}

if err := pushToControlPlane(action.ActionsOpts.CPConnecction, res, action.c.CraftingState.Attestation.GetWorkflow().GetWorkflowRunId()); err != nil {
if err := pushToControlPlane(action.ActionsOpts.CPConnection, res, action.c.CraftingState.Attestation.GetWorkflow().GetWorkflowRunId()); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/attestation_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (action *AttestationReset) Run(trigger, reason string) error {
}

if !action.c.CraftingState.DryRun {
client := pb.NewAttestationServiceClient(action.CPConnecction)
client := pb.NewAttestationServiceClient(action.CPConnection)
if _, err := client.Cancel(context.Background(), &pb.AttestationServiceCancelRequest{
WorkflowRunId: action.c.CraftingState.GetAttestation().GetWorkflow().GetWorkflowRunId(),
Reason: reason,
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/config_current_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (
)

func (action *ConfigCurrentContext) Run() (*ConfigContextItem, error) {
client := pb.NewContextServiceClient(action.cfg.CPConnecction)
client := pb.NewContextServiceClient(action.cfg.CPConnection)
resp, err := client.Current(context.Background(), &pb.ContextServiceCurrentRequest{})
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/delete_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewDeleteAccount(cfg *ActionsOpts) *DeleteAccount {
}

func (a *DeleteAccount) Run() error {
client := pb.NewAuthServiceClient(a.CPConnecction)
client := pb.NewAuthServiceClient(a.CPConnection)
_, err := client.DeleteAccount(context.Background(), &pb.AuthServiceDeleteAccountRequest{})
return err
}
2 changes: 1 addition & 1 deletion app/cli/internal/action/integration_add_deptrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewIntegrationAddDeptrack(cfg *ActionsOpts) *IntegrationAddDeptrack {
}

func (action *IntegrationAddDeptrack) Run(host, apiKey string, allowAutoProjectCreation bool) (*IntegrationItem, error) {
client := pb.NewIntegrationsServiceClient(action.cfg.CPConnecction)
client := pb.NewIntegrationsServiceClient(action.cfg.CPConnection)
i, err := client.AddDependencyTrack(context.Background(), &pb.AddDependencyTrackRequest{
ApiKey: apiKey,
Config: &pb.IntegrationConfig_DependencyTrack{
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/integration_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewIntegrationDelete(cfg *ActionsOpts) *IntegrationDelete {
}

func (action *IntegrationDelete) Run(id string) error {
client := pb.NewIntegrationsServiceClient(action.cfg.CPConnecction)
client := pb.NewIntegrationsServiceClient(action.cfg.CPConnection)
if _, err := client.Delete(context.Background(), &pb.IntegrationsServiceDeleteRequest{Id: id}); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/integration_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewIntegrationList(cfg *ActionsOpts) *IntegrationList {
}

func (action *IntegrationList) Run() ([]*IntegrationItem, error) {
client := pb.NewIntegrationsServiceClient(action.cfg.CPConnecction)
client := pb.NewIntegrationsServiceClient(action.cfg.CPConnection)
resp, err := client.List(context.Background(), &pb.IntegrationsServiceListRequest{})
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/membership_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewMembershipList(cfg *ActionsOpts) *MembershipList {
}

func (action *MembershipList) Run() ([]*MembershipItem, error) {
client := pb.NewOrganizationServiceClient(action.cfg.CPConnecction)
client := pb.NewOrganizationServiceClient(action.cfg.CPConnection)
resp, err := client.ListMemberships(context.Background(), &pb.OrganizationServiceListMembershipsRequest{})
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/membership_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewMembershipSet(cfg *ActionsOpts) *MembershipSetCurrent {
}

func (action *MembershipSetCurrent) Run(id string) (*MembershipItem, error) {
client := pb.NewOrganizationServiceClient(action.cfg.CPConnecction)
client := pb.NewOrganizationServiceClient(action.cfg.CPConnection)
resp, err := client.SetCurrentMembership(context.Background(), &pb.SetCurrentMembershipRequest{MembershipId: id})
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/ocirepository_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type NewOCIRepositorySaveOpts struct {
}

func (action *OCIRepositorySave) Run(opts *NewOCIRepositorySaveOpts) error {
client := pb.NewOCIRepositoryServiceClient(action.cfg.CPConnecction)
client := pb.NewOCIRepositoryServiceClient(action.cfg.CPConnection)

_, err := client.Save(context.Background(), &pb.OCIRepositoryServiceSaveRequest{
Repository: opts.Repo,
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/workflow_contract_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewWorkflowContractCreate(cfg *ActionsOpts) *WorkflowContractCreate {
}

func (action *WorkflowContractCreate) Run(name, contractPath string) (*WorkflowContractItem, error) {
client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnecction)
client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnection)

request := &pb.WorkflowContractServiceCreateRequest{Name: name}
if contractPath != "" {
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/workflow_contract_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewWorkflowContractDelete(cfg *ActionsOpts) *WorkflowContractDelete {
}

func (action *WorkflowContractDelete) Run(contractID string) error {
client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnecction)
client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnection)
if _, err := client.Delete(context.Background(), &pb.WorkflowContractServiceDeleteRequest{Id: contractID}); err != nil {
action.cfg.Logger.Debug().Err(err).Msg("making the API request")
return err
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/workflow_contract_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewWorkflowContractDescribe(cfg *ActionsOpts) *WorkflowContractDescribe {
}

func (action *WorkflowContractDescribe) Run(id string, rev int32) (*WorkflowContractWithVersionItem, error) {
client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnecction)
client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnection)

request := &pb.WorkflowContractServiceDescribeRequest{Id: id, Revision: rev}

Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/workflow_contract_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewWorkflowContractList(cfg *ActionsOpts) *WorkflowContractList {
}

func (action *WorkflowContractList) Run() ([]*WorkflowContractItem, error) {
client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnecction)
client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnection)
resp, err := client.List(context.Background(), &pb.WorkflowContractServiceListRequest{})
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion app/cli/internal/action/workflow_contract_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewWorkflowContractUpdate(cfg *ActionsOpts) *WorkflowContractUpdate {
}

func (action *WorkflowContractUpdate) Run(contractID, name, contractPath string) (*WorkflowContractWithVersionItem, error) {
client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnecction)
client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnection)

request := &pb.WorkflowContractServiceUpdateRequest{Id: contractID, Name: name}
if contractPath != "" {
Expand Down
Loading