Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
patch velero to handle self-signed certs on client
Browse files Browse the repository at this point in the history
you'll get this error otherwise:
x509: certificate signed by unknown authority
  • Loading branch information
Steven Chung committed Aug 1, 2019
1 parent 72f5cad commit 40ac4bc
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 23 deletions.
8 changes: 5 additions & 3 deletions pkg/cmd/cli/backup/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import (

func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
var (
listOptions metav1.ListOptions
details bool
listOptions metav1.ListOptions
details bool
insecureSkipVerify bool
)

c := &cobra.Command{
Expand Down Expand Up @@ -71,7 +72,7 @@ func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
fmt.Fprintf(os.Stderr, "error getting PodVolumeBackups for backup %s: %v\n", backup.Name, err)
}

s := output.DescribeBackup(&backup, deleteRequestList.Items, podVolumeBackupList.Items, details, veleroClient)
s := output.DescribeBackup(&backup, deleteRequestList.Items, podVolumeBackupList.Items, details, veleroClient, insecureSkipVerify)
if first {
first = false
fmt.Print(s)
Expand All @@ -85,6 +86,7 @@ func NewDescribeCommand(f client.Factory, use string) *cobra.Command {

c.Flags().StringVarP(&listOptions.LabelSelector, "selector", "l", listOptions.LabelSelector, "only show items matching this label selector")
c.Flags().BoolVar(&details, "details", details, "display additional detail in the command output")
c.Flags().BoolVar(&insecureSkipVerify, "insecureskipverify", insecureSkipVerify, "accept any TLS certificate presented by the storage service")

return c
}
14 changes: 8 additions & 6 deletions pkg/cmd/cli/backup/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ func NewDownloadCommand(f client.Factory) *cobra.Command {
}

type DownloadOptions struct {
Name string
Output string
Force bool
Timeout time.Duration
writeOptions int
Name string
Output string
Force bool
Timeout time.Duration
InsecureSkipVerify bool
writeOptions int
}

func NewDownloadOptions() *DownloadOptions {
Expand All @@ -69,6 +70,7 @@ func (o *DownloadOptions) BindFlags(flags *pflag.FlagSet) {
flags.StringVarP(&o.Output, "output", "o", o.Output, "path to output file. Defaults to <NAME>-data.tar.gz in the current directory")
flags.BoolVar(&o.Force, "force", o.Force, "forces the download and will overwrite file if it exists already")
flags.DurationVar(&o.Timeout, "timeout", o.Timeout, "maximum time to wait to process download request")
flags.BoolVar(&o.InsecureSkipVerify, "insecureskipverify", o.InsecureSkipVerify, "accept any TLS certificate presented by the storage service")
}

func (o *DownloadOptions) Validate(c *cobra.Command, args []string, f client.Factory) error {
Expand Down Expand Up @@ -111,7 +113,7 @@ func (o *DownloadOptions) Run(c *cobra.Command, f client.Factory) error {
}
defer backupDest.Close()

err = downloadrequest.Stream(veleroClient.VeleroV1(), f.Namespace(), o.Name, v1.DownloadTargetKindBackupContents, backupDest, o.Timeout)
err = downloadrequest.Stream(veleroClient.VeleroV1(), f.Namespace(), o.Name, v1.DownloadTargetKindBackupContents, backupDest, o.Timeout, o.InsecureSkipVerify)
if err != nil {
os.Remove(o.Output)
cmd.CheckError(err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/cli/backup/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

func NewLogsCommand(f client.Factory) *cobra.Command {
timeout := time.Minute
insecureSkipVerify := false

c := &cobra.Command{
Use: "logs BACKUP",
Expand All @@ -58,12 +59,13 @@ func NewLogsCommand(f client.Factory) *cobra.Command {
"until the backup has a phase of Completed or Failed and try again.", backupName)
}

err = downloadrequest.Stream(veleroClient.VeleroV1(), f.Namespace(), backupName, v1.DownloadTargetKindBackupLog, os.Stdout, timeout)
err = downloadrequest.Stream(veleroClient.VeleroV1(), f.Namespace(), backupName, v1.DownloadTargetKindBackupLog, os.Stdout, timeout, insecureSkipVerify)
cmd.CheckError(err)
},
}

c.Flags().DurationVar(&timeout, "timeout", timeout, "how long to wait to receive logs")
c.Flags().BoolVar(&insecureSkipVerify, "insecureskipverify", insecureSkipVerify, "accept any TLS certificate presented by the storage service")

return c
}
8 changes: 5 additions & 3 deletions pkg/cmd/cli/restore/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ import (

func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
var (
listOptions metav1.ListOptions
details bool
listOptions metav1.ListOptions
details bool
insecureSkipVerify bool
)

c := &cobra.Command{
Expand Down Expand Up @@ -64,7 +65,7 @@ func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
fmt.Fprintf(os.Stderr, "error getting PodVolumeRestores for restore %s: %v\n", restore.Name, err)
}

s := output.DescribeRestore(&restore, podvolumeRestoreList.Items, details, veleroClient)
s := output.DescribeRestore(&restore, podvolumeRestoreList.Items, details, veleroClient, insecureSkipVerify)
if first {
first = false
fmt.Print(s)
Expand All @@ -78,6 +79,7 @@ func NewDescribeCommand(f client.Factory, use string) *cobra.Command {

c.Flags().StringVarP(&listOptions.LabelSelector, "selector", "l", listOptions.LabelSelector, "only show items matching this label selector")
c.Flags().BoolVar(&details, "details", details, "display additional detail in the command output")
c.Flags().BoolVar(&insecureSkipVerify, "insecureskipverify", insecureSkipVerify, "accept any TLS certificate presented by the storage service")

return c
}
4 changes: 3 additions & 1 deletion pkg/cmd/cli/restore/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

func NewLogsCommand(f client.Factory) *cobra.Command {
timeout := time.Minute
insecureSkipVerify := false

c := &cobra.Command{
Use: "logs RESTORE",
Expand All @@ -58,12 +59,13 @@ func NewLogsCommand(f client.Factory) *cobra.Command {
"until the restore has a phase of Completed or Failed and try again.", restoreName)
}

err = downloadrequest.Stream(veleroClient.VeleroV1(), f.Namespace(), restoreName, v1.DownloadTargetKindRestoreLog, os.Stdout, timeout)
err = downloadrequest.Stream(veleroClient.VeleroV1(), f.Namespace(), restoreName, v1.DownloadTargetKindRestoreLog, os.Stdout, timeout, insecureSkipVerify)
cmd.CheckError(err)
},
}

c.Flags().DurationVar(&timeout, "timeout", timeout, "how long to wait to receive logs")
c.Flags().BoolVar(&insecureSkipVerify, "insecureskipverify", insecureSkipVerify, "accept any TLS certificate presented by the storage service")

return c
}
15 changes: 14 additions & 1 deletion pkg/cmd/util/downloadrequest/downloadrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ package downloadrequest

import (
"compress/gzip"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"

"github.com/pkg/errors"
Expand All @@ -32,7 +35,7 @@ import (
velerov1client "github.com/heptio/velero/pkg/generated/clientset/versioned/typed/velero/v1"
)

func Stream(client velerov1client.DownloadRequestsGetter, namespace, name string, kind v1.DownloadTargetKind, w io.Writer, timeout time.Duration) error {
func Stream(client velerov1client.DownloadRequestsGetter, namespace, name string, kind v1.DownloadTargetKind, w io.Writer, timeout time.Duration, insecureSkipVerify bool) error {
req := &v1.DownloadRequest{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Expand Down Expand Up @@ -101,6 +104,11 @@ Loop:
}

httpClient := new(http.Client)
if insecureSkipVerify {
httpClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}

httpReq, err := http.NewRequest("GET", req.Status.DownloadURL, nil)
if err != nil {
Expand All @@ -114,6 +122,11 @@ Loop:

resp, err := httpClient.Do(httpReq)
if err != nil {
if urlErr, ok := err.(*url.Error); ok {
if _, ok := urlErr.Err.(x509.UnknownAuthorityError); ok {
return fmt.Errorf(err.Error() + "\n\nThe --insecureskipverify flag can also be used to accept any TLS certificate for the download, but it is susceptible to man-in-the-middle attacks.")
}
}
return err
}
defer resp.Body.Close()
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/util/downloadrequest/downloadrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestStream(t *testing.T) {
output := new(bytes.Buffer)
errCh := make(chan error)
go func() {
err := Stream(client.VeleroV1(), "namespace", "name", test.kind, output, timeout)
err := Stream(client.VeleroV1(), "namespace", "name", test.kind, output, timeout, false)
errCh <- err
}()

Expand Down
7 changes: 4 additions & 3 deletions pkg/cmd/util/output/backup_describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func DescribeBackup(
podVolumeBackups []velerov1api.PodVolumeBackup,
details bool,
veleroClient clientset.Interface,
insecureSkipVerify bool,
) string {
return Describe(func(d *Describer) {
d.DescribeMetadata(backup.ObjectMeta)
Expand Down Expand Up @@ -74,7 +75,7 @@ func DescribeBackup(
DescribeBackupSpec(d, backup.Spec)

d.Println()
DescribeBackupStatus(d, backup, details, veleroClient)
DescribeBackupStatus(d, backup, details, veleroClient, insecureSkipVerify)

if len(deleteRequests) > 0 {
d.Println()
Expand Down Expand Up @@ -211,7 +212,7 @@ func DescribeBackupSpec(d *Describer, spec velerov1api.BackupSpec) {
}

// DescribeBackupStatus describes a backup status in human-readable format.
func DescribeBackupStatus(d *Describer, backup *velerov1api.Backup, details bool, veleroClient clientset.Interface) {
func DescribeBackupStatus(d *Describer, backup *velerov1api.Backup, details bool, veleroClient clientset.Interface, insecureSkipVerify bool) {
status := backup.Status

d.Printf("Backup Format Version:\t%d\n", status.Version)
Expand Down Expand Up @@ -240,7 +241,7 @@ func DescribeBackupStatus(d *Describer, backup *velerov1api.Backup, details bool
}

buf := new(bytes.Buffer)
if err := downloadrequest.Stream(veleroClient.VeleroV1(), backup.Namespace, backup.Name, velerov1api.DownloadTargetKindBackupVolumeSnapshots, buf, downloadRequestTimeout); err != nil {
if err := downloadrequest.Stream(veleroClient.VeleroV1(), backup.Namespace, backup.Name, velerov1api.DownloadTargetKindBackupVolumeSnapshots, buf, downloadRequestTimeout, insecureSkipVerify); err != nil {
d.Printf("Persistent Volumes:\t<error getting volume snapshot info: %v>\n", err)
return
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/util/output/restore_describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
pkgrestore "github.com/heptio/velero/pkg/restore"
)

func DescribeRestore(restore *v1.Restore, podVolumeRestores []v1.PodVolumeRestore, details bool, veleroClient clientset.Interface) string {
func DescribeRestore(restore *v1.Restore, podVolumeRestores []v1.PodVolumeRestore, details bool, veleroClient clientset.Interface, insecureSkipVerify bool) string {
return Describe(func(d *Describer) {
d.DescribeMetadata(restore.ObjectMeta)

Expand All @@ -56,7 +56,7 @@ func DescribeRestore(restore *v1.Restore, podVolumeRestores []v1.PodVolumeRestor
}
}

describeRestoreResults(d, restore, veleroClient)
describeRestoreResults(d, restore, veleroClient, insecureSkipVerify)

d.Println()
d.Printf("Backup:\t%s\n", restore.Spec.BackupName)
Expand Down Expand Up @@ -114,15 +114,15 @@ func DescribeRestore(restore *v1.Restore, podVolumeRestores []v1.PodVolumeRestor
})
}

func describeRestoreResults(d *Describer, restore *v1.Restore, veleroClient clientset.Interface) {
func describeRestoreResults(d *Describer, restore *v1.Restore, veleroClient clientset.Interface, insecureSkipVerify bool) {
if restore.Status.Warnings == 0 && restore.Status.Errors == 0 {
return
}

var buf bytes.Buffer
var resultMap map[string]pkgrestore.Result

if err := downloadrequest.Stream(veleroClient.VeleroV1(), restore.Namespace, restore.Name, v1.DownloadTargetKindRestoreResults, &buf, downloadRequestTimeout); err != nil {
if err := downloadrequest.Stream(veleroClient.VeleroV1(), restore.Namespace, restore.Name, v1.DownloadTargetKindRestoreResults, &buf, downloadRequestTimeout, insecureSkipVerify); err != nil {
d.Printf("Warnings:\t<error getting warnings: %v>\n\nErrors:\t<error getting errors: %v>\n", err, err)
return
}
Expand Down

0 comments on commit 40ac4bc

Please sign in to comment.