Skip to content

Commit 99b2e09

Browse files
committed
storageconfig: use time.Duration for rotation period
Epic: none Release note: None
1 parent c7456e9 commit 99b2e09

File tree

5 files changed

+25
-28
lines changed

5 files changed

+25
-28
lines changed

pkg/ccl/storageccl/engineccl/encrypted_fs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package engineccl
88
import (
99
"context"
1010
"fmt"
11+
"time"
1112

1213
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
1314
"github.com/cockroachdb/cockroach/pkg/storage/fs"
@@ -319,7 +320,7 @@ func newEncryptedEnv(
319320
dataKeyManager := &DataKeyManager{
320321
fs: storeFS,
321322
dbDir: dbDir,
322-
rotationPeriod: options.DataKeyRotationPeriod,
323+
rotationPeriod: int64((options.RotationPeriod + time.Second - 1) / time.Second),
323324
readOnly: readOnly,
324325
}
325326
if err := dataKeyManager.Load(context.TODO()); err != nil {

pkg/ccl/storageccl/engineccl/encrypted_fs_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"strconv"
1717
"strings"
1818
"testing"
19+
"time"
1920

2021
"github.com/cockroachdb/cockroach/pkg/base"
2122
"github.com/cockroachdb/cockroach/pkg/roachpb"
@@ -242,7 +243,7 @@ func TestPebbleEncryption(t *testing.T) {
242243
CurrentKey: "16.key",
243244
OldKey: "plain",
244245
},
245-
DataKeyRotationPeriod: 1000, // arbitrary seconds
246+
RotationPeriod: time.Hour,
246247
}
247248

248249
func() {
@@ -385,7 +386,7 @@ func TestPebbleEncryption2(t *testing.T) {
385386
CurrentKey: encKeyFile,
386387
OldKey: oldEncFileKey,
387388
},
388-
DataKeyRotationPeriod: 1000,
389+
RotationPeriod: time.Hour,
389390
}
390391

391392
// Initialize the filesystem env.
@@ -580,7 +581,7 @@ func makeEncryptedTestFS(t *testing.T, errorProb float64, errorRand *rand.Rand)
580581
//
581582
// TODO(sumeer): Do deterministic data key rotation. Inject kmTimeNow and
582583
// operations that advance time.
583-
encOptions.DataKeyRotationPeriod = 100000
584+
encOptions.RotationPeriod = 100000 * time.Second
584585
etfs := &encryptedTestFS{
585586
mem: mem,
586587
encOptions: &encOptions,

pkg/cli/flags_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,35 +1774,35 @@ func TestParseEncryptionSpec(t *testing.T) {
17741774
"path=/data,key=/new.key,old-key=/old.key", "",
17751775
storeEncryptionSpec{Path: "/data",
17761776
Options: storageconfig.EncryptionOptions{
1777-
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "/new.key", OldKey: "/old.key"},
1778-
DataKeyRotationPeriod: int64(storageconfig.DefaultRotationPeriod / time.Second),
1777+
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "/new.key", OldKey: "/old.key"},
1778+
RotationPeriod: storageconfig.DefaultRotationPeriod,
17791779
},
17801780
},
17811781
},
17821782
{
17831783
"path=/data,key=/new.key,old-key=/old.key,rotation-period=1h", "",
17841784
storeEncryptionSpec{Path: "/data",
17851785
Options: storageconfig.EncryptionOptions{
1786-
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "/new.key", OldKey: "/old.key"},
1787-
DataKeyRotationPeriod: int64(time.Hour / time.Second),
1786+
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "/new.key", OldKey: "/old.key"},
1787+
RotationPeriod: time.Hour,
17881788
},
17891789
},
17901790
},
17911791
{
17921792
"path=/data,key=plain,old-key=/old.key,rotation-period=1h", "",
17931793
storeEncryptionSpec{Path: "/data",
17941794
Options: storageconfig.EncryptionOptions{
1795-
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "plain", OldKey: "/old.key"},
1796-
DataKeyRotationPeriod: int64(time.Hour / time.Second),
1795+
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "plain", OldKey: "/old.key"},
1796+
RotationPeriod: time.Hour,
17971797
},
17981798
},
17991799
},
18001800
{
18011801
"path=/data,key=/new.key,old-key=plain,rotation-period=1h", "",
18021802
storeEncryptionSpec{Path: "/data",
18031803
Options: storageconfig.EncryptionOptions{
1804-
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "/new.key", OldKey: "plain"},
1805-
DataKeyRotationPeriod: int64(time.Hour / time.Second),
1804+
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "/new.key", OldKey: "plain"},
1805+
RotationPeriod: time.Hour,
18061806
},
18071807
},
18081808
},
@@ -1812,8 +1812,8 @@ func TestParseEncryptionSpec(t *testing.T) {
18121812
"path=data,key=/new.key,old-key=/old.key", "",
18131813
storeEncryptionSpec{Path: absDataPath,
18141814
Options: storageconfig.EncryptionOptions{
1815-
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "/new.key", OldKey: "/old.key"},
1816-
DataKeyRotationPeriod: int64(storageconfig.DefaultRotationPeriod / time.Second),
1815+
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "/new.key", OldKey: "/old.key"},
1816+
RotationPeriod: storageconfig.DefaultRotationPeriod,
18171817
},
18181818
},
18191819
},
@@ -1823,8 +1823,8 @@ func TestParseEncryptionSpec(t *testing.T) {
18231823
"path=*,key=/new.key,old-key=/old.key", "",
18241824
storeEncryptionSpec{Path: "*",
18251825
Options: storageconfig.EncryptionOptions{
1826-
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "/new.key", OldKey: "/old.key"},
1827-
DataKeyRotationPeriod: int64(storageconfig.DefaultRotationPeriod / time.Second),
1826+
KeyFiles: &storageconfig.EncryptionKeyFiles{CurrentKey: "/new.key", OldKey: "/old.key"},
1827+
RotationPeriod: storageconfig.DefaultRotationPeriod,
18281828
},
18291829
},
18301830
},

pkg/cli/flags_util.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -521,15 +521,10 @@ type storeEncryptionSpec struct {
521521
func (es storeEncryptionSpec) String() string {
522522
// All fields are set.
523523
return fmt.Sprintf("path=%s,key=%s,old-key=%s,rotation-period=%s",
524-
es.Path, es.Options.KeyFiles.CurrentKey, es.Options.KeyFiles.OldKey, es.RotationPeriod(),
524+
es.Path, es.Options.KeyFiles.CurrentKey, es.Options.KeyFiles.OldKey, es.Options.RotationPeriod,
525525
)
526526
}
527527

528-
// RotationPeriod returns the rotation period as a duration.
529-
func (es storeEncryptionSpec) RotationPeriod() time.Duration {
530-
return time.Duration(es.Options.DataKeyRotationPeriod) * time.Second
531-
}
532-
533528
// PathMatches returns true if this storeEncryptionSpec matches the given store path.
534529
func (es storeEncryptionSpec) PathMatches(path string) bool {
535530
return es.Path == path || es.Path == "*"
@@ -546,9 +541,9 @@ func parseStoreEncryptionSpec(value string) (storeEncryptionSpec, error) {
546541
es := storeEncryptionSpec{
547542
Path: "",
548543
Options: storageconfig.EncryptionOptions{
549-
KeySource: storageconfig.EncryptionKeyFromFiles,
550-
KeyFiles: &storageconfig.EncryptionKeyFiles{},
551-
DataKeyRotationPeriod: int64(storageconfig.DefaultRotationPeriod / time.Second),
544+
KeySource: storageconfig.EncryptionKeyFromFiles,
545+
KeyFiles: &storageconfig.EncryptionKeyFiles{},
546+
RotationPeriod: storageconfig.DefaultRotationPeriod,
552547
},
553548
}
554549

@@ -611,7 +606,7 @@ func parseStoreEncryptionSpec(value string) (storeEncryptionSpec, error) {
611606
if err != nil {
612607
return storeEncryptionSpec{}, errors.Wrapf(err, "could not parse rotation-duration value: %s", value)
613608
}
614-
es.Options.DataKeyRotationPeriod = int64(dur / time.Second)
609+
es.Options.RotationPeriod = dur
615610
default:
616611
return storeEncryptionSpec{}, fmt.Errorf("%s is not a valid enterprise-encryption field", field)
617612
}

pkg/storage/storageconfig/encryption_spec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type EncryptionOptions struct {
1313
KeySource EncryptionKeySource
1414
// Set if key_source == KeyFiles.
1515
KeyFiles *EncryptionKeyFiles
16-
// Default data key rotation in seconds.
17-
DataKeyRotationPeriod int64
16+
// Data key rotation period.
17+
RotationPeriod time.Duration
1818
}
1919

2020
// EncryptionKeyFiles is used when plain key files are passed.

0 commit comments

Comments
 (0)