etcdctl: add etcdctl snapshot pipe command

To improve the security of etcdctl. Added the ability to write snapshots to stdout without writing data to disk.

Signed-off-by: Ais8Ooz8 <47941654+Ais8Ooz8@users.noreply.github.com>
This commit is contained in:
Ais8Ooz8 2024-03-29 14:44:05 +03:00 committed by GitHub
parent fb107ec806
commit 447125c784
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"strings"
"time" "time"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
@ -84,15 +83,6 @@ func WriteSnapshotWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.
zap.Duration("took", time.Since(start)), zap.Duration("took", time.Since(start)),
zap.String("etcd-version", resp.Version), 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 return resp.Version, nil
} }
@ -107,7 +97,12 @@ func SaveWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, d
defer os.RemoveAll(partPath) defer os.RemoveAll(partPath)
defer f.Close() 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) { func PipeWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.Config) (string, error) {