fix: sftp support using public key authentication

This commit is contained in:
garethgeorge
2024-01-23 22:52:04 -08:00
parent 8310f23451
commit fddb2aecb2
3 changed files with 15 additions and 6 deletions
+2 -1
View File
@@ -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)
}
+1 -1
View File
@@ -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
+12 -4
View File
@@ -9,6 +9,7 @@ import (
"io"
"os"
"os/exec"
"slices"
"strings"
"sync"
@@ -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
@@ -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,
@@ -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") {
@@ -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