Skip to content

Commit d38c902

Browse files
author
marc
committed
cli: support encrypted stores in debug commands.
Add the `--enterprise-encryption` flag to debug commands that open rocksdb. The flag is as specified in the start command. There are a few TODOs left: * support the ldb tool somethow * add tests for this, it'll need to be interactive tests in ccl/ Release note: None
1 parent 099fc95 commit d38c902

File tree

4 files changed

+121
-30
lines changed

4 files changed

+121
-30
lines changed

pkg/ccl/baseccl/encryption_spec.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package baseccl
1111
import (
1212
"bytes"
1313
"fmt"
14+
"path/filepath"
1415
"strings"
1516
"time"
1617

@@ -220,3 +221,24 @@ func PopulateStoreSpecWithEncryption(
220221
}
221222
return nil
222223
}
224+
225+
// EncryptionOptionsForStore takes a store directory and returns its ExtraOptions
226+
// if a matching entry if found in the StoreEncryptionSpecList.
227+
// The caller should appropriately set UseFileRegistry on a non-nil result.
228+
func EncryptionOptionsForStore(
229+
dir string, encryptionSpecs StoreEncryptionSpecList,
230+
) ([]byte, error) {
231+
// We need an absolute path, but the input may have come in relative.
232+
path, err := filepath.Abs(dir)
233+
if err != nil {
234+
return nil, errors.Wrapf(err, "could not find absolute path for %s ", dir)
235+
}
236+
237+
for _, es := range encryptionSpecs.Specs {
238+
if es.Path == path {
239+
return es.toEncryptionOptions()
240+
}
241+
}
242+
243+
return nil, nil
244+
}

pkg/ccl/cliccl/debug.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2018 The Cockroach Authors.
2+
//
3+
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
4+
// License (the "License"); you may not use this file except in compliance with
5+
// the License. You may obtain a copy of the License at
6+
//
7+
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
8+
9+
package cliccl
10+
11+
import (
12+
"github.com/cockroachdb/cockroach/pkg/ccl/baseccl"
13+
"github.com/cockroachdb/cockroach/pkg/ccl/cliccl/cliflagsccl"
14+
"github.com/cockroachdb/cockroach/pkg/cli"
15+
"github.com/cockroachdb/cockroach/pkg/storage/engine"
16+
)
17+
18+
// This does not define new commands, only adds the encryption flag to debug commands in
19+
// `pkg/cli/debug.go` and registers a callback to generate encryption options.
20+
21+
func init() {
22+
for _, cmd := range cli.DebugCmdsForRocksDB {
23+
// storeEncryptionSpecs is in start.go.
24+
cli.VarFlag(cmd.Flags(), &storeEncryptionSpecs, cliflagsccl.EnterpriseEncryption)
25+
}
26+
27+
cli.PopulateRocksDBConfigHook = fillEncryptionOptionsForStore
28+
}
29+
30+
// fillEncryptionOptionsForStore fills the RocksDBConfig fields
31+
// based on the --enterprise-encryption flag value.
32+
func fillEncryptionOptionsForStore(cfg *engine.RocksDBConfig) error {
33+
opts, err := baseccl.EncryptionOptionsForStore(cfg.Dir, storeEncryptionSpecs)
34+
if err != nil {
35+
return err
36+
}
37+
38+
if opts != nil {
39+
cfg.ExtraOptions = opts
40+
cfg.UseFileRegistry = true
41+
}
42+
return nil
43+
}

pkg/cli/debug.go

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ import (
5757
"github.com/spf13/cobra"
5858
)
5959

60-
var debugKeysCmd = &cobra.Command{
60+
// DebugKeysCmd dumps keys.
61+
var DebugKeysCmd = &cobra.Command{
6162
Use: "keys <directory>",
6263
Short: "dump all the keys in a store",
6364
Long: `
@@ -77,6 +78,11 @@ Create a ballast file to fill the store directory up to a given amount
7778
RunE: runDebugBallast,
7879
}
7980

81+
// PopulateRocksDBConfigHook is a callback set by CCL code.
82+
// It populates any needed fields in the RocksDBConfig.
83+
// It must do nothing in OSS code.
84+
var PopulateRocksDBConfigHook func(*engine.RocksDBConfig) error
85+
8086
func parseRangeID(arg string) (roachpb.RangeID, error) {
8187
rangeIDInt, err := strconv.ParseInt(arg, 10, 64)
8288
if err != nil {
@@ -95,19 +101,26 @@ func openExistingStore(dir string, stopper *stop.Stopper, readOnly bool) (*engin
95101
if err != nil {
96102
return nil, err
97103
}
98-
db, err := engine.NewRocksDB(
99-
engine.RocksDBConfig{
100-
Settings: serverCfg.Settings,
101-
Dir: dir,
102-
MaxOpenFiles: maxOpenFiles,
103-
MustExist: true,
104-
ReadOnly: readOnly,
105-
},
106-
cache,
107-
)
104+
105+
cfg := engine.RocksDBConfig{
106+
Settings: serverCfg.Settings,
107+
Dir: dir,
108+
MaxOpenFiles: maxOpenFiles,
109+
MustExist: true,
110+
ReadOnly: readOnly,
111+
}
112+
113+
if PopulateRocksDBConfigHook != nil {
114+
if err := PopulateRocksDBConfigHook(&cfg); err != nil {
115+
return nil, err
116+
}
117+
}
118+
119+
db, err := engine.NewRocksDB(cfg, cache)
108120
if err != nil {
109121
return nil, err
110122
}
123+
111124
stopper.AddCloser(db)
112125
return db, nil
113126
}
@@ -219,7 +232,8 @@ func runDebugBallast(cmd *cobra.Command, args []string) error {
219232
return nil
220233
}
221234

222-
var debugRangeDataCmd = &cobra.Command{
235+
// DebugRangeDataCmd dumps data for a range.
236+
var DebugRangeDataCmd = &cobra.Command{
223237
Use: "range-data <directory> <range id>",
224238
Short: "dump all the data in a range",
225239
Long: `
@@ -268,7 +282,8 @@ func runDebugRangeData(cmd *cobra.Command, args []string) error {
268282
return nil
269283
}
270284

271-
var debugRangeDescriptorsCmd = &cobra.Command{
285+
// DebugRangeDescriptorsCmd prints range descriptors.
286+
var DebugRangeDescriptorsCmd = &cobra.Command{
272287
Use: "range-descriptors <directory>",
273288
Short: "print all range descriptors in a store",
274289
Long: `
@@ -526,7 +541,8 @@ Decode a hexadecimal-encoded key and pretty-print it. For example:
526541
},
527542
}
528543

529-
var debugRaftLogCmd = &cobra.Command{
544+
// DebugRaftLogCmd prints raft log entries.
545+
var DebugRaftLogCmd = &cobra.Command{
530546
Use: "raft-log <directory> <range id>",
531547
Short: "print the raft log for a range",
532548
Long: `
@@ -607,7 +623,8 @@ func runDebugRaftLog(cmd *cobra.Command, args []string) error {
607623
return db.Iterate(start, end, printRaftLogEntry)
608624
}
609625

610-
var debugGCCmd = &cobra.Command{
626+
// DebugGCCmd print GC information.
627+
var DebugGCCmd = &cobra.Command{
611628
Use: "estimate-gc <directory> [range id]",
612629
Short: "find out what a GC run would do",
613630
Long: `
@@ -693,7 +710,8 @@ func runDebugGCCmd(cmd *cobra.Command, args []string) error {
693710
return nil
694711
}
695712

696-
var debugCheckStoreCmd = &cobra.Command{
713+
// DebugCheckStoreCmd checks store consistency.
714+
var DebugCheckStoreCmd = &cobra.Command{
697715
Use: "check-store <directory>",
698716
Short: "consistency check for a single store",
699717
Long: `
@@ -850,6 +868,7 @@ as 'ldb'.
850868
https://github.com/facebook/rocksdb/wiki/Administration-and-Data-Access-Tool#ldb-tool
851869
`,
852870
// LDB does its own flag parsing.
871+
// TODO(mberhault): support encrypted stores.
853872
DisableFlagParsing: true,
854873
Run: func(cmd *cobra.Command, args []string) {
855874
engine.RunLDB(args)
@@ -869,7 +888,8 @@ Output environment variables that influence configuration.
869888
},
870889
}
871890

872-
var debugCompactCmd = &cobra.Command{
891+
// DebugCompactCmd compacts sstables.
892+
var DebugCompactCmd = &cobra.Command{
873893
Use: "compact <directory>",
874894
Short: "compact the sstables in a store",
875895
Long: `
@@ -910,7 +930,8 @@ func runDebugCompact(cmd *cobra.Command, args []string) error {
910930
return nil
911931
}
912932

913-
var debugSSTablesCmd = &cobra.Command{
933+
// DebugSSTablesCmd lists sstables.
934+
var DebugSSTablesCmd = &cobra.Command{
914935
Use: "sstables <directory>",
915936
Short: "list the sstables in a store",
916937
Long: `
@@ -1097,23 +1118,28 @@ func init() {
10971118
"only write to the WAL, not to sstables")
10981119
}
10991120

1100-
var debugCmds = []*cobra.Command{
1121+
// DebugCmdsForRocksDB lists debug commands that access rocksdb.
1122+
var DebugCmdsForRocksDB = []*cobra.Command{
1123+
DebugCheckStoreCmd,
1124+
DebugCompactCmd,
1125+
DebugGCCmd,
1126+
DebugKeysCmd,
1127+
DebugRaftLogCmd,
1128+
DebugRangeDataCmd,
1129+
DebugRangeDescriptorsCmd,
1130+
DebugSSTablesCmd,
1131+
}
1132+
1133+
// All other debug commands go here.
1134+
var debugCmds = append(DebugCmdsForRocksDB,
11011135
debugBallastCmd,
1102-
debugKeysCmd,
1103-
debugRangeDataCmd,
1104-
debugRangeDescriptorsCmd,
11051136
debugDecodeKeyCmd,
1106-
debugRaftLogCmd,
1107-
debugGCCmd,
1108-
debugCheckStoreCmd,
11091137
debugRocksDBCmd,
1110-
debugCompactCmd,
1111-
debugSSTablesCmd,
11121138
debugGossipValuesCmd,
11131139
debugSyncTestCmd,
11141140
debugEnvCmd,
11151141
debugZipCmd,
1116-
}
1142+
)
11171143

11181144
var debugCmd = &cobra.Command{
11191145
Use: "debug [command]",

pkg/cli/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,14 @@ func init() {
361361

362362
// Debug commands.
363363
{
364-
f := debugKeysCmd.Flags()
364+
f := DebugKeysCmd.Flags()
365365
VarFlag(f, (*mvccKey)(&debugCtx.startKey), cliflags.From)
366366
VarFlag(f, (*mvccKey)(&debugCtx.endKey), cliflags.To)
367367
BoolFlag(f, &debugCtx.values, cliflags.Values, debugCtx.values)
368368
BoolFlag(f, &debugCtx.sizes, cliflags.Sizes, debugCtx.sizes)
369369
}
370370
{
371-
f := debugRangeDataCmd.Flags()
371+
f := DebugRangeDataCmd.Flags()
372372
BoolFlag(f, &debugCtx.replicated, cliflags.Replicated, debugCtx.replicated)
373373
}
374374
{

0 commit comments

Comments
 (0)