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
15 changes: 14 additions & 1 deletion app/cli/cmd/casbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ func newCASBackendCmd() *cobra.Command {
Short: "Operations on Artifact CAS backends",
}

cmd.AddCommand(newCASBackendListCmd())
cmd.AddCommand(newCASBackendListCmd(), newCASBackendAddCmd())
return cmd
}

func newCASBackendAddCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Short: "Add a new Artifact CAS backend",
}

cmd.PersistentFlags().Bool("default", false, "set the backend as default in your organization")
cmd.PersistentFlags().String("description", "", "descriptive information for this registration")

cmd.AddCommand(newCASBackendAddOCICmd())
return cmd
}
109 changes: 109 additions & 0 deletions app/cli/cmd/casbackend_add_oci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// Copyright 2023 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 newCASBackendAddOCICmd() *cobra.Command {
var repo, username, password string
cmd := &cobra.Command{
Use: "oci",
Short: "Register a OCI CAS Backend",
RunE: func(cmd *cobra.Command, args []string) error {
// If we are setting the default, we list existing CAS backends
// and ask the user to confirm the rewrite
isDefault, err := cmd.Flags().GetBool("default")
cobra.CheckErr(err)

description, err := cmd.Flags().GetString("description")
cobra.CheckErr(err)

if isDefault {
if confirmed, err := confirmDefaultCASBackendOverride(actionOpts); err != nil {
return err
} else if !confirmed {
fmt.Println("Aborting...")
}
}

opts := &action.NewCASBackendOCIAddOpts{
Repo: repo, Username: username, Password: password, Default: isDefault, Description: description,
}

res, err := action.NewCASBackendAddOCI(actionOpts).Run(opts)
if err != nil {
return err
} else if res == nil {
return nil
}

return encodeOutput([]*action.CASBackendItem{res}, casBackendListTableOutput)
},
}

cmd.Flags().StringVar(&repo, "repo", "", "FQDN repository name, including path")
err := cmd.MarkFlagRequired("repo")
cobra.CheckErr(err)

cmd.Flags().StringVarP(&username, "username", "u", "", "registry username")
err = cmd.MarkFlagRequired("username")
cobra.CheckErr(err)

cmd.Flags().StringVarP(&password, "password", "p", "", "registry password")
err = cmd.MarkFlagRequired("password")
cobra.CheckErr(err)
return cmd
}

// confirmDefaultCASBackendOverride asks the user to confirm the override of the default CAS backend
// in the event that there is one already set.
func confirmDefaultCASBackendOverride(actionOpts *action.ActionsOpts) (bool, error) {
// get existing backends
backends, err := action.NewCASBackendList(actionOpts).Run()
if err != nil {
return false, fmt.Errorf("failed to list existing CAS backends: %w", err)
}

var hasDefault bool
for _, b := range backends {
if b.Default {
hasDefault = true
break
}
}

// If there is none, we are done
if !hasDefault {
return true, nil
}

// Ask the user to confirm the override
fmt.Println("There is already a default CAS backend in your organization.\nPlease confirm to override y/N: ")
var gotChallenge string
fmt.Scanln(&gotChallenge)

// If the user does not confirm, we are done
if gotChallenge != "y" && gotChallenge != "Y" {
return false, nil
}

return true, nil
}
4 changes: 2 additions & 2 deletions app/cli/cmd/casbackend_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func casBackendListTableOutput(backends []*action.CASBackendItem) error {
}

t := newTableWriter()
header := table.Row{"ID", "Name", "Provider", "Default"}
header := table.Row{"ID", "Location", "Provider", "Description", "Default"}
if full {
header = append(header, "Validation Status", "Created At", "Validated At")
}

t.AppendHeader(header)
for _, b := range backends {
r := table.Row{b.ID, b.Name, b.Provider, b.Default}
r := table.Row{b.ID, b.Location, b.Provider, b.Description, b.Default}
if full {
r = append(r, b.ValidationStatus,
b.CreatedAt.Format(time.RFC822),
Expand Down
63 changes: 63 additions & 0 deletions app/cli/internal/action/casbackend_add_oci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Copyright 2023 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"
"fmt"

pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
"google.golang.org/protobuf/types/known/structpb"
)

type CASBackendAddOCI struct {
cfg *ActionsOpts
}

type NewCASBackendOCIAddOpts struct {
Repo, Username, Password string
Description string
Default bool
}

func NewCASBackendAddOCI(cfg *ActionsOpts) *CASBackendAddOCI {
return &CASBackendAddOCI{cfg}
}

func (action *CASBackendAddOCI) Run(opts *NewCASBackendOCIAddOpts) (*CASBackendItem, error) {
// Custom configuration for OCI
credentials, err := structpb.NewStruct(map[string]any{
"username": opts.Username,
"password": opts.Password,
})
if err != nil {
return nil, fmt.Errorf("failed to parse arguments: %w", err)
}

client := pb.NewCASBackendServiceClient(action.cfg.CPConnection)
resp, err := client.Create(context.Background(), &pb.CASBackendServiceCreateRequest{
Location: opts.Repo,
Provider: "OCI",
Description: opts.Description,
Default: opts.Default,
Credentials: credentials,
})
if err != nil {
return nil, err
}

return pbCASBackendItemToAction(resp.Result), nil
}
6 changes: 4 additions & 2 deletions app/cli/internal/action/casbackend_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type CASBackendList struct {

type CASBackendItem struct {
ID string `json:"id"`
Name string `json:"name"`
Location string `json:"location"`
Description string `json:"description"`
Provider string `json:"provider"`
Default bool `json:"default"`
ValidationStatus ValidationStatus `json:"validationStatus"`
Expand Down Expand Up @@ -70,7 +71,8 @@ func pbCASBackendItemToAction(in *pb.CASBackendItem) *CASBackendItem {

b := &CASBackendItem{
ID: in.Id,
Name: in.Name,
Location: in.Location,
Description: in.Description,
Provider: in.Provider,
Default: in.Default,
CreatedAt: toTimePtr(in.CreatedAt.AsTime()),
Expand Down
Loading