Skip to main content

rotate.go

rotate.go - Overview

This file defines the rotate command, which is responsible for rotating the encryption key used by Badger.

Detailed Documentation

var oldKeyPath string

Stores the file path to the old encryption key.

var newKeyPath string

Stores the file path to the new encryption key.

var rotateCmd *cobra.Command

Defines the rotate command.

  • Use: "rotate"
  • Short: "Rotate encryption key."
  • Long: "Rotate will rotate the old key with new encryption key."
  • RunE: doRotate

func init()

Initializes the rotateCmd by adding it to the RootCmd and defining flags for the old and new key paths.

func doRotate(cmd *cobra.Command, args []string) error

Rotates the encryption key.

  • Parameters:
    • cmd *cobra.Command: The Cobra command.
    • args []string: Command line arguments.
  • Returns:
    • error: An error if the key rotation fails, nil otherwise.

The function performs the following steps:

  1. Retrieves the old encryption key using getKey(oldKeyPath).
  2. Opens the key registry with the old key.
  3. Retrieves the new encryption key using getKey(newKeyPath).
  4. Writes the new key to the key registry.

func getKey(path string) ([]byte, error)

Reads the encryption key from the specified file path.

  • Parameters:
    • path string: The file path to the encryption key.
  • Returns:
    • []byte: The encryption key as a byte slice. Returns an empty byte slice if the path is empty, indicating plain text.
    • error: An error if the file cannot be opened or read, nil otherwise.

Getting Started Relevance