mirror of
https://github.com/luxfi/zapdb.git
synced 2026-10-30 15:12:20 +00:00
The module has been github.com/luxfi/zapdb for a while, but the package inside it still declared itself badger, so every consumer had to write zapdb "github.com/luxfi/zapdb" // package badger and the alias existed only to paper over the mismatch. 56 root files and the 134 qualified references inside the repo now say zapdb. Backward compatible in both directions: an existing consumer with the alias keeps compiling (the alias is now merely redundant), and one without it starts compiling instead of failing on an undefined zapdb. Directory badger/ and its cmd package are untouched — that is the CLI, and its binary is named badger. Build and vet clean; suite green except TestZRestore, which wants a /tmp/zb/src benchmark fixture and fails identically without this change. Co-authored-by: Hanzo Dev <[email protected]>
73 lines
1.4 KiB
Go
73 lines
1.4 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/luxfi/zapdb"
|
|
)
|
|
|
|
var oldKeyPath string
|
|
var newKeyPath string
|
|
var rotateCmd = &cobra.Command{
|
|
Use: "rotate",
|
|
Short: "Rotate encryption key.",
|
|
Long: "Rotate will rotate the old key with new encryption key.",
|
|
RunE: doRotate,
|
|
}
|
|
|
|
func init() {
|
|
RootCmd.AddCommand(rotateCmd)
|
|
rotateCmd.Flags().StringVarP(&oldKeyPath, "old-key-path", "o",
|
|
"", "Path of the old key")
|
|
rotateCmd.Flags().StringVarP(&newKeyPath, "new-key-path", "n",
|
|
"", "Path of the new key")
|
|
}
|
|
|
|
func doRotate(cmd *cobra.Command, args []string) error {
|
|
oldKey, err := getKey(oldKeyPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
opt := zapdb.KeyRegistryOptions{
|
|
Dir: sstDir,
|
|
ReadOnly: true,
|
|
EncryptionKey: oldKey,
|
|
EncryptionKeyRotationDuration: 10 * 24 * time.Hour,
|
|
}
|
|
kr, err := zapdb.OpenKeyRegistry(opt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newKey, err := getKey(newKeyPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
opt.EncryptionKey = newKey
|
|
err = zapdb.WriteKeyRegistry(kr, opt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getKey(path string) ([]byte, error) {
|
|
if path == "" {
|
|
// Empty bytes for plain text to encryption(vice versa).
|
|
return []byte{}, nil
|
|
}
|
|
fp, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return io.ReadAll(fp)
|
|
}
|