Skip to content

Commit

Permalink
etcdctl: add etcdctl snapshot pipe command
Browse files Browse the repository at this point in the history
To improve the security of etcdctl. Added the ability to write snapshots to stdout without writing data to disk.

Signed-off-by: Ais8Ooz8 <[email protected]>
  • Loading branch information
Ais8Ooz8 authored Mar 29, 2024
1 parent fb107ec commit 447125c
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions client/v3/snapshot/v3_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"io"
"os"
"strings"
"time"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -84,15 +83,6 @@ func WriteSnapshotWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.
zap.Duration("took", time.Since(start)),
zap.String("etcd-version", resp.Version),
)

partPath := f.Name()
dbPath := strings.TrimSuffix(partPath, ".part")
if f != os.Stdout {
if err := os.Rename(partPath, dbPath); err != nil {
return resp.Version, fmt.Errorf("could not rename %s to %s (%v)", partPath, dbPath, err)
}
}
lg.Info("finished", zap.String("path", dbPath))
return resp.Version, nil
}

Expand All @@ -107,7 +97,12 @@ func SaveWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, d
defer os.RemoveAll(partPath)
defer f.Close()

return WriteSnapshotWithVersion(ctx, lg, cfg, f)
version, err := WriteSnapshotWithVersion(ctx, lg, cfg, f)
if err := os.Rename(partPath, dbPath); err != nil {
return version, fmt.Errorf("could not rename %s to %s (%v)", partPath, dbPath, err)
}
lg.Info("saved", zap.String("path", dbPath))
return version, err
}

func PipeWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.Config) (string, error) {
Expand Down

0 comments on commit 447125c

Please sign in to comment.