Skip to content

Commit

Permalink
fix: sftp support using public key authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Jan 24, 2024
1 parent dcff2ad commit bedb302
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ func (s *Server) AddRepo(ctx context.Context, req *connect.Request[v1.Repo]) (*c
}

r := restic.NewRepo(bin, req.Msg)

// use background context such that the init op can try to complete even if the connection is closed.
if err := r.Init(context.Background()); err != nil {
if err := r.Init(context.Background(), restic.WithPropagatedEnvVars(restic.EnvToPropagate...)); err != nil {
return nil, fmt.Errorf("failed to init repo: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/orchestrator/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (r *RepoOrchestrator) Backup(ctx context.Context, plan *v1.Plan, progressCa
defer r.mu.Unlock()
if !r.initialized {

if err := r.repo.Init(ctx); err != nil {
if err := r.repo.Init(ctx, restic.WithPropagatedEnvVars(restic.EnvToPropagate...)); err != nil {
return nil, fmt.Errorf("failed to initialize repo: %w", err)
}
r.initialized = true
Expand Down
16 changes: 12 additions & 4 deletions pkg/restic/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"os"
"os/exec"
"slices"
"strings"
"sync"

Expand All @@ -20,7 +21,6 @@ var ErrPartialBackup = errors.New("incomplete backup")
var ErrBackupFailed = errors.New("backup failed")

type Repo struct {
mu sync.Mutex
cmd string
repo *v1.Repo
initialized bool
Expand All @@ -36,6 +36,10 @@ func NewRepo(resticBin string, repo *v1.Repo, opts ...GenericOption) *Repo {
o(opt)
}

if slices.Index(opt.extraArgs, "sftp.args") == -1 {
opt.extraArgs = append(opt.extraArgs, "-o", "sftp.args=-oBatchMode=yes")
}

return &Repo{
cmd: resticBin, // TODO: configurable binary path
repo: repo,
Expand All @@ -56,16 +60,20 @@ func (r *Repo) buildEnv() []string {
}

// init initializes the repo, the command will be cancelled with the context.
func (r *Repo) init(ctx context.Context) error {
func (r *Repo) init(ctx context.Context, opts ...GenericOption) error {
if r.initialized {
return nil
}

opt := resolveOpts(opts)

var args = []string{"init", "--json"}
args = append(args, r.extraArgs...)
args = append(args, opt.extraArgs...)

cmd := exec.CommandContext(ctx, r.cmd, args...)
cmd.Env = append(cmd.Env, r.buildEnv()...)
cmd.Env = append(cmd.Env, opt.extraEnv...)

if output, err := cmd.CombinedOutput(); err != nil {
if strings.Contains(string(output), "config file already exists") || strings.Contains(string(output), "already initialized") {
Expand All @@ -78,8 +86,8 @@ func (r *Repo) init(ctx context.Context) error {
return nil
}

func (r *Repo) Init(ctx context.Context) error {
if err := r.init(ctx); err != nil && !errors.Is(err, errAlreadyInitialized) {
func (r *Repo) Init(ctx context.Context, opts ...GenericOption) error {
if err := r.init(ctx, opts...); err != nil && !errors.Is(err, errAlreadyInitialized) {
return fmt.Errorf("init failed: %w", err)
}
return nil
Expand Down

0 comments on commit bedb302

Please sign in to comment.