-
Notifications
You must be signed in to change notification settings - Fork 2
encryption: add sidecar and KEK metrics (Stage 9C-2, §9.2) #1223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bootjp
wants to merge
2
commits into
main
Choose a base branch
from
design/encryption-9c2-sidecar-kek-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,149 @@ | ||
| package encryption_test | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/bootjp/elastickv/internal/encryption" | ||
| "github.com/bootjp/elastickv/internal/encryption/fsmwire" | ||
| etcdraftengine "github.com/bootjp/elastickv/internal/raftengine/etcd" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestStateCacheMirrorsRaftSlotAndSidecarIndex pins the §9.2 | ||
| // observability mirrors. They are refreshed by the same | ||
| // RefreshFromSidecar call that maintains the decision mirrors, so a | ||
| // refresh that updated one and not the other would let the metrics | ||
| // report stale sidecar state indefinitely. | ||
| func TestStateCacheMirrorsRaftSlotAndSidecarIndex(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cache := encryption.NewStateCache() | ||
|
|
||
| // Pre-bootstrap posture. | ||
| id, ok := cache.ActiveRaftKeyID() | ||
| require.Zero(t, id) | ||
| require.False(t, ok) | ||
| require.Zero(t, cache.SidecarRaftAppliedIndex()) | ||
|
|
||
| cache.RefreshFromSidecar(&encryption.Sidecar{ | ||
| Version: 1, | ||
| RaftAppliedIndex: 9182, | ||
| Active: encryption.ActiveKeys{Storage: 7, Raft: 8}, | ||
| }) | ||
|
|
||
| id, ok = cache.ActiveRaftKeyID() | ||
| require.Equal(t, uint32(8), id) | ||
| require.True(t, ok) | ||
| require.Equal(t, uint64(9182), cache.SidecarRaftAppliedIndex()) | ||
|
|
||
| // The storage mirror must still track its own slot. | ||
| storageID, ok := cache.ActiveStorageKeyID() | ||
| require.Equal(t, uint32(7), storageID) | ||
| require.True(t, ok) | ||
|
|
||
| // A later refresh must advance the index, not latch it. | ||
| cache.RefreshFromSidecar(&encryption.Sidecar{ | ||
| Version: 1, | ||
| RaftAppliedIndex: 9200, | ||
| Active: encryption.ActiveKeys{Storage: 7, Raft: 8}, | ||
| }) | ||
| require.Equal(t, uint64(9200), cache.SidecarRaftAppliedIndex()) | ||
| } | ||
|
|
||
| func TestStateCacheObservabilityMirrorsAreNilSafe(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var cache *encryption.StateCache | ||
| id, ok := cache.ActiveRaftKeyID() | ||
| require.Zero(t, id) | ||
| require.False(t, ok) | ||
| require.Zero(t, cache.SidecarRaftAppliedIndex()) | ||
| } | ||
|
|
||
| // TestApplierRefreshesTheCacheOnNoOpSidecarWrites is the regression for | ||
| // the metric-lag defect: the idempotency and stale-DEKID branches | ||
| // advance and persist sc.RaftAppliedIndex but do no other work, so a | ||
| // branch that skipped RefreshFromSidecar left the cache — and the §9.2 | ||
| // elastickv_encryption_sidecar_raft_index gauge — behind the durable | ||
| // sidecar until the next fresh mutation or a restart. That reads as a | ||
| // false sidecar-divergence signal. | ||
| func TestApplierRefreshesTheCacheOnNoOpSidecarWrites(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| const storageDEK = uint32(7) | ||
| const raftDEK = uint32(8) | ||
| nodeID := etcdraftengine.DeriveNodeID("n1") | ||
|
|
||
| dir := t.TempDir() | ||
| sidecarPath := filepath.Join(dir, "keys.json") | ||
| cache := encryption.NewStateCache() | ||
| applier := newObservabilityApplier(t, sidecarPath, cache) | ||
|
|
||
| require.NoError(t, applier.ApplyBootstrap(1, fsmwire.BootstrapPayload{ | ||
| StorageDEKID: storageDEK, | ||
| WrappedStorage: []byte("wrapped-storage-dek"), | ||
| RaftDEKID: raftDEK, | ||
| WrappedRaft: []byte("wrapped-raft-dek-distinct"), | ||
| BatchRegistry: []fsmwire.RegistrationPayload{ | ||
| {DEKID: storageDEK, FullNodeID: nodeID, LocalEpoch: 0}, | ||
| }, | ||
| })) | ||
| require.Equal(t, uint64(1), cache.SidecarRaftAppliedIndex()) | ||
|
|
||
| // Fresh cutover. | ||
| require.NoError(t, applier.ApplyRotation(2, fsmwire.RotationPayload{ | ||
| SubTag: fsmwire.RotateSubEnableStorageEnvelope, | ||
| DEKID: storageDEK, | ||
| Purpose: fsmwire.PurposeStorage, | ||
| Wrapped: []byte{}, | ||
| ProposerRegistration: fsmwire.RegistrationPayload{DEKID: storageDEK, FullNodeID: nodeID, LocalEpoch: 1}, | ||
| })) | ||
| require.Equal(t, uint64(2), cache.SidecarRaftAppliedIndex()) | ||
|
|
||
| // A DUPLICATE cutover entry: the already-active no-op branch. It | ||
| // persists the new applied index, so the cache must follow. | ||
| require.NoError(t, applier.ApplyRotation(3, fsmwire.RotationPayload{ | ||
| SubTag: fsmwire.RotateSubEnableStorageEnvelope, | ||
| DEKID: storageDEK, | ||
| Purpose: fsmwire.PurposeStorage, | ||
| Wrapped: []byte{}, | ||
| ProposerRegistration: fsmwire.RegistrationPayload{DEKID: storageDEK, FullNodeID: nodeID, LocalEpoch: 2}, | ||
| })) | ||
| onDisk, err := encryption.ReadSidecar(sidecarPath) | ||
| require.NoError(t, err) | ||
| require.Equal(t, uint64(3), onDisk.RaftAppliedIndex, "the no-op branch must persist the index") | ||
| require.Equal(t, onDisk.RaftAppliedIndex, cache.SidecarRaftAppliedIndex(), | ||
| "the cache must never lag the durable sidecar after a no-op write") | ||
|
|
||
| // A STALE-DEKID cutover entry: the other no-op branch. | ||
| require.NoError(t, applier.ApplyRotation(4, fsmwire.RotationPayload{ | ||
| SubTag: fsmwire.RotateSubEnableStorageEnvelope, | ||
| DEKID: storageDEK + 100, // no longer the active DEK | ||
| Purpose: fsmwire.PurposeStorage, | ||
| Wrapped: []byte{}, | ||
| ProposerRegistration: fsmwire.RegistrationPayload{DEKID: storageDEK + 100, FullNodeID: nodeID, LocalEpoch: 3}, | ||
| })) | ||
| onDisk, err = encryption.ReadSidecar(sidecarPath) | ||
| require.NoError(t, err) | ||
| require.Equal(t, uint64(4), onDisk.RaftAppliedIndex) | ||
| require.Equal(t, onDisk.RaftAppliedIndex, cache.SidecarRaftAppliedIndex(), | ||
| "the stale-DEKID no-op branch must refresh the cache too") | ||
| } | ||
|
|
||
| // newObservabilityApplier builds an applier wired to a real sidecar | ||
| // path and the supplied cache, matching the production topology in | ||
| // main_encryption_write_wiring.go. | ||
| func newObservabilityApplier( | ||
| t *testing.T, sidecarPath string, cache *encryption.StateCache, | ||
| ) *encryption.Applier { | ||
| t.Helper() | ||
| app, err := encryption.NewApplier(newMapRegistryStore(), | ||
| encryption.WithKEK(&fakeKEK{}), | ||
| encryption.WithKeystore(encryption.NewKeystore()), | ||
| encryption.WithSidecarPath(sidecarPath), | ||
| encryption.WithStateCache(cache), | ||
| ) | ||
| require.NoError(t, err) | ||
| return app | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a stale or duplicate enable-storage-envelope/enable-raft-envelope entry is applied, the no-op branches in
applyEnableStorageEnvelopeandapplyEnableRaftEnvelopeadvance and persistsc.RaftAppliedIndexbut return without callingRefreshFromSidecar. Because the newsidecarRaftAppliedIndexmirror is the sole source forelastickv_encryption_sidecar_raft_index, the metric remains behind the actual persisted sidecar until another fresh encryption mutation or restart, potentially producing a false sidecar-divergence signal.Useful? React with 👍 / 👎.