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
63 changes: 62 additions & 1 deletion app/cli/cmd/casbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package cmd

import (
"fmt"

"github.com/chainloop-dev/chainloop/app/cli/internal/action"
"github.com/spf13/cobra"
)

Expand All @@ -25,7 +28,7 @@ func newCASBackendCmd() *cobra.Command {
Short: "Operations on Artifact CAS backends",
}

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

Expand Down Expand Up @@ -54,3 +57,61 @@ func newCASBackendUpdateCmd() *cobra.Command {
cmd.AddCommand(newCASBackendUpdateOCICmd())
return cmd
}

// confirmDefaultCASBackendOverride asks the user to confirm the override of the default CAS backend
// in the event that there is one already set and its not the same as the one we are setting
func confirmDefaultCASBackendOverride(actionOpts *action.ActionsOpts, id string) (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)
}

// Find the default
var defaultB *action.CASBackendItem
for _, b := range backends {
if b.Default {
defaultB = b
break
}
}

// If there is none or there is but it's the same as the one we are setting, we are ok
if defaultB == nil || (id != "" && id == defaultB.ID) {
return true, nil
}

// Ask the user to confirm the override
return confirmationPrompt("You are changing the default CAS backend in your organization"), nil
}

// If we are removing the default we confirm too
func confirmDefaultCASBackendRemoval(actionOpts *action.ActionsOpts, id string) (bool, error) {
return confirmDefaultCASBackendUnset(id, "You are deleting the default CAS backend.", actionOpts)
}

func confirmDefaultCASBackendUnset(id, msg string, 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)
}

for _, b := range backends {
// We are removing ourselves as the default, ask the user to confirm
if b.Default && b.ID == id {
return confirmationPrompt(msg), nil
}
}

return true, nil
}

// y/n confirmation prompt
func confirmationPrompt(msg string) bool {
fmt.Printf("%s\nPlease confirm to continue y/N\n", msg)
var gotChallenge string
fmt.Scanln(&gotChallenge)

return gotChallenge == "y" || gotChallenge == "Y"
}
34 changes: 0 additions & 34 deletions app/cli/cmd/casbackend_add_oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package cmd

import (
"fmt"

"github.com/chainloop-dev/chainloop/app/cli/internal/action"
"github.com/go-kratos/kratos/v2/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -80,35 +78,3 @@ func newCASBackendAddOCICmd() *cobra.Command {
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 and its not the same as the one we are setting
func confirmDefaultCASBackendOverride(actionOpts *action.ActionsOpts, id string) (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)
}

// Find the default
var defaultB *action.CASBackendItem
for _, b := range backends {
if b.Default {
defaultB = b
break
}
}

// If there is none or there is but it's the same as the one we are setting, we are ok
if defaultB == nil || (id != "" && id == defaultB.ID) {
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
return gotChallenge == "y" || gotChallenge == "Y", nil
}
52 changes: 52 additions & 0 deletions app/cli/cmd/casbackend_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// 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 (
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
"github.com/go-kratos/kratos/v2/log"
"github.com/spf13/cobra"
)

func newCASBackendDeleteCmd() *cobra.Command {
var backendID string

cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Short: "Delete a CAS Backend from your organization",
RunE: func(cmd *cobra.Command, args []string) error {
if confirmed, err := confirmDefaultCASBackendRemoval(actionOpts, backendID); err != nil {
return err
} else if !confirmed {
log.Info("Aborting...")
return nil
}

if err := action.NewCASBackendDelete(actionOpts).Run(backendID); err != nil {
return err
}

logger.Info().Msg("Backend deleted")

return nil
},
}

cmd.Flags().StringVar(&backendID, "id", "", "CAS Backend ID")
cobra.CheckErr(cmd.MarkFlagRequired("id"))
return cmd
}
33 changes: 3 additions & 30 deletions app/cli/cmd/casbackend_update_oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package cmd

import (
"fmt"

"github.com/chainloop-dev/chainloop/app/cli/internal/action"
"github.com/go-kratos/kratos/v2/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -45,8 +43,9 @@ func newCASBackendUpdateOCICmd() *cobra.Command {
log.Info("Aborting...")
return nil
}
} else { // If we are removing the default we ask for confirmation too
if confirmed, err := confirmDefaultCASBackendRemoval(actionOpts, backendID); err != nil {
} else {
// If we are removing the default we ask for confirmation too
if confirmed, err := confirmDefaultCASBackendUnset(backendID, "You are setting the default CAS backend to false", actionOpts); err != nil {
return err
} else if !confirmed {
log.Info("Aborting...")
Expand Down Expand Up @@ -88,29 +87,3 @@ func newCASBackendUpdateOCICmd() *cobra.Command {
cmd.Flags().StringVarP(&password, "password", "p", "", "registry password")
return cmd
}

// If we are removing the default we confirm too
func confirmDefaultCASBackendRemoval(actionOpts *action.ActionsOpts, id string) (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)
}

for _, b := range backends {
// We are removing ourselves as the default, ask the user to confirm
if b.Default && b.ID == id {
// Ask the user to confirm the override
fmt.Println("This is the default CAS backend and your are setting default=false.\nPlease confirm to continue 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
}
39 changes: 39 additions & 0 deletions app/cli/internal/action/casbackend_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// 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"

pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
)

type CASBackendDelete struct {
cfg *ActionsOpts
}

func NewCASBackendDelete(cfg *ActionsOpts) *CASBackendDelete {
return &CASBackendDelete{cfg}
}

func (action *CASBackendDelete) Run(id string) error {
client := pb.NewCASBackendServiceClient(action.cfg.CPConnection)
_, err := client.Delete(context.Background(), &pb.CASBackendServiceDeleteRequest{
Id: id,
})

return err
}
Loading