-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ccl/cclcli: add
debug encryption-decrypt
command
During storage-level L2 investigations, files from problematic stores are often requested (e.g. the MANIFEST file(s), SSTables, etc.). In cases where the store is using encryption-at-rest, the debug artifacts are useless unless they have been decrypted. Add a new debug command that can be used to decrypt a file in-situ, given the encryption spec for the store, and a path to an encrypted file in the store. For example: ```bash $ cockroach encryption-decrypt \ /path/to/store \ /path/to/encrypted/file \ /path/to/decrypted/output/file ``` Touches #89095. Release note (ops change): A new debug tool was added to allow for decrypting files in a store using encryption-at-rest. This tool is intended for use while debugging, or for providing debug artifacts to Cockroach Labs to aid with support investigations. It is intended to be run "in-situ" (i.e. on site), as it prevents having to move sensitive key material.
- Loading branch information
Showing
6 changed files
with
220 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package cliccl | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/cli" | ||
"github.com/cockroachdb/cockroach/pkg/storage" | ||
"github.com/cockroachdb/cockroach/pkg/util/stop" | ||
"github.com/cockroachdb/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func runDecrypt(_ *cobra.Command, args []string) (returnErr error) { | ||
dir, inPath := args[0], args[1] | ||
var outPath string | ||
if len(args) > 2 { | ||
outPath = args[2] | ||
} | ||
|
||
stopper := stop.NewStopper() | ||
defer stopper.Stop(context.Background()) | ||
|
||
db, err := cli.OpenEngine(dir, stopper, storage.MustExist, storage.ReadOnly) | ||
if err != nil { | ||
return errors.Wrap(err, "could not open store") | ||
} | ||
|
||
// Open the specified file through the FS, decrypting it. | ||
f, err := db.Open(inPath) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not open input file %s", inPath) | ||
} | ||
defer f.Close() | ||
|
||
// Copy the raw bytes into the destination file. | ||
outFile := os.Stdout | ||
if outPath != "" { | ||
outFile, err = os.OpenFile(outPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not open output file %s", outPath) | ||
} | ||
defer outFile.Close() | ||
} | ||
if _, err = io.Copy(outFile, f); err != nil { | ||
return errors.Wrapf(err, "could not write to output file") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package cliccl | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/ccl/baseccl" | ||
// The following import is required for the hook that populates | ||
// NewEncryptedEnvFunc in `pkg/storage`. | ||
_ "github.com/cockroachdb/cockroach/pkg/ccl/storageccl/engineccl" | ||
"github.com/cockroachdb/cockroach/pkg/cli" | ||
"github.com/cockroachdb/cockroach/pkg/storage" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDecrypt(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
ctx := context.Background() | ||
dir := t.TempDir() | ||
|
||
// Generate a new encryption key to use. | ||
keyPath := filepath.Join(dir, "aes.key") | ||
err := cli.GenEncryptionKeyCmd.RunE(nil, []string{keyPath}) | ||
require.NoError(t, err) | ||
|
||
// Spin up a new encrypted store. | ||
encSpecStr := fmt.Sprintf("path=%s,key=%s,old-key=plain", dir, keyPath) | ||
encSpec, err := baseccl.NewStoreEncryptionSpec(encSpecStr) | ||
require.NoError(t, err) | ||
encOpts, err := encSpec.ToEncryptionOptions() | ||
require.NoError(t, err) | ||
p, err := storage.Open(ctx, storage.Filesystem(dir), storage.EncryptionAtRest(encOpts)) | ||
require.NoError(t, err) | ||
|
||
// Find a manifest file to check. | ||
files, err := p.List(dir) | ||
require.NoError(t, err) | ||
var manifestPath string | ||
for _, basename := range files { | ||
if strings.HasPrefix(basename, "MANIFEST-") { | ||
manifestPath = filepath.Join(dir, basename) | ||
break | ||
} | ||
} | ||
// Should have found a manifest file. | ||
require.NotEmpty(t, manifestPath) | ||
|
||
// Close the DB. | ||
p.Close() | ||
|
||
// Pluck the `pebble manifest dump` command out of the debug command. | ||
dumpCmd := getTool(cli.DebugPebbleCmd, []string{"pebble", "manifest", "dump"}) | ||
require.NotNil(t, dumpCmd) | ||
|
||
dumpManifest := func(cmd *cobra.Command, path string) string { | ||
var b bytes.Buffer | ||
dumpCmd.SetOut(&b) | ||
dumpCmd.SetErr(&b) | ||
dumpCmd.Run(cmd, []string{path}) | ||
return b.String() | ||
} | ||
out := dumpManifest(dumpCmd, manifestPath) | ||
// Check for the presence of the comparator line in the manifest dump, as a | ||
// litmus test for the manifest file being readable. This line should only | ||
// appear once the file has been decrypted. | ||
const checkStr = "comparer: cockroach_comparator" | ||
require.NotContains(t, out, checkStr) | ||
|
||
// Decrypt the manifest file. | ||
outPath := filepath.Join(dir, "manifest.plain") | ||
decryptCmd := getTool(cli.DebugCmd, []string{"debug", "encryption-decrypt"}) | ||
require.NotNil(t, decryptCmd) | ||
err = decryptCmd.Flags().Set("enterprise-encryption", encSpecStr) | ||
require.NoError(t, err) | ||
err = decryptCmd.RunE(decryptCmd, []string{dir, manifestPath, outPath}) | ||
require.NoError(t, err) | ||
|
||
// Check that the decrypted manifest file can now be read. | ||
out = dumpManifest(dumpCmd, outPath) | ||
require.Contains(t, out, checkStr) | ||
} | ||
|
||
// getTool traverses the given cobra.Command recursively, searching for a tool | ||
// matching the given command. | ||
func getTool(cmd *cobra.Command, want []string) *cobra.Command { | ||
// Base cases. | ||
if cmd.Name() != want[0] { | ||
return nil | ||
} | ||
if len(want) == 1 { | ||
return cmd | ||
} | ||
// Recursive case. | ||
for _, subCmd := range cmd.Commands() { | ||
found := getTool(subCmd, want[1:]) | ||
if found != nil { | ||
return found | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters