Repository navigation
Node trusted key pruning #945
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,35 +35,6 @@ import ( | |||||||||||||
| "google.golang.org/protobuf/proto" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| func TestMergeTrustedKeys(t *testing.T) { | ||||||||||||||
| now := time.Now() | ||||||||||||||
| earlier := now.Add(-2 * time.Hour) | ||||||||||||||
| keyA, _, _ := ed25519.GenerateKey(nil) | ||||||||||||||
| keyB, _, _ := ed25519.GenerateKey(nil) | ||||||||||||||
| keyC, _, _ := ed25519.GenerateKey(nil) | ||||||||||||||
|
|
||||||||||||||
| existing := []TrustedKey{ | ||||||||||||||
| {Key: keyA, ReceivedAt: earlier}, | ||||||||||||||
| {Key: keyC, ReceivedAt: earlier}, // expired at the CP: absent from /keys | ||||||||||||||
| } | ||||||||||||||
| got := mergeTrustedKeys(existing, []ed25519.PublicKey{keyA, keyB}, now) | ||||||||||||||
|
|
||||||||||||||
| if len(got) != 2 { | ||||||||||||||
| t.Fatalf("expected 2 keys, got %d", len(got)) | ||||||||||||||
| } | ||||||||||||||
| if !got[0].Key.Equal(keyA) || !got[0].ReceivedAt.Equal(earlier) { | ||||||||||||||
| t.Errorf("known key must keep its ReceivedAt: got %+v", got[0]) | ||||||||||||||
| } | ||||||||||||||
| if !got[1].Key.Equal(keyB) || !got[1].ReceivedAt.Equal(now) { | ||||||||||||||
| t.Errorf("new key must get the sync time: got %+v", got[1]) | ||||||||||||||
| } | ||||||||||||||
| for _, tk := range got { | ||||||||||||||
| if tk.Key.Equal(keyC) { | ||||||||||||||
| t.Error("key expired at the control plane must be dropped") | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // mustGenerateKey is an ed25519 key pair or a failed test. | ||||||||||||||
| func mustGenerateKey(t *testing.T) (ed25519.PublicKey, ed25519.PrivateKey) { | ||||||||||||||
| t.Helper() | ||||||||||||||
|
|
@@ -153,6 +124,29 @@ func TestSyncTrustedKeys(t *testing.T) { | |||||||||||||
| } | ||||||||||||||
| }) | ||||||||||||||
|
|
||||||||||||||
| // The node got the current key at enrollment and the retiring key from a | ||||||||||||||
| // later sync. While /keys lists both, pruning must keep both, whatever | ||||||||||||||
| // the time since the node first saw each (#916). A key that /keys no | ||||||||||||||
| // longer lists is dropped at the sync. | ||||||||||||||
| t.Run("a key /keys still lists outlives the grace period", func(t *testing.T) { | ||||||||||||||
| expiredPub, _ := mustGenerateKey(t) | ||||||||||||||
| srv := keysServer(t, []ed25519.PublicKey{retiredPub, currentPub}, []ed25519.PrivateKey{retiredPriv, currentPriv}) | ||||||||||||||
| defer srv.Close() | ||||||||||||||
| n := newNode(t) | ||||||||||||||
| enrolled := time.Now().Add(-25 * time.Hour) | ||||||||||||||
| n.trustedKeys = []TrustedKey{{Key: currentPub, ReceivedAt: enrolled}, {Key: expiredPub, ReceivedAt: enrolled}} | ||||||||||||||
| if err := n.syncTrustedKeys(context.Background(), srv.URL); err != nil { | ||||||||||||||
| t.Fatalf("syncTrustedKeys: %v", err) | ||||||||||||||
| } | ||||||||||||||
| if containsTrustedKey(n.trustedKeys, expiredPub) { | ||||||||||||||
| t.Error("a key /keys does not list stayed in the trust set") | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+141
to
+143
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to Section 3, Rule 8 of the Repository Style Guide, assertion messages must state both the observed and the expected value. Let's update the assertion message to explicitly show what was observed and what was expected.
Suggested change
References
|
||||||||||||||
| got := pruneTrustedKeys(n.trustedKeys, time.Now(), 24*time.Hour) | ||||||||||||||
| if len(got) != 2 || !containsTrustedKey(got, retiredPub) || !containsTrustedKey(got, currentPub) { | ||||||||||||||
| t.Errorf("pruning kept %d keys, want the retired and the current key that /keys lists", len(got)) | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+145
to
+147
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to Section 3, Rule 8 of the Repository Style Guide, assertion messages must state both the observed and the expected value. Let's update this assertion message to explicitly show the observed number of keys and the expected keys.
Suggested change
References
|
||||||||||||||
| }) | ||||||||||||||
|
|
||||||||||||||
| t.Run("rejects a set no trusted key signed", func(t *testing.T) { | ||||||||||||||
| srv := keysServer(t, []ed25519.PublicKey{strangerPub}, []ed25519.PrivateKey{strangerPriv}) | ||||||||||||||
| defer srv.Close() | ||||||||||||||
|
|
||||||||||||||
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.
Since the size of
keysis exactly known, we can pre-allocate the slice with the correct length and assign elements directly by index instead of usingappend. This avoids slice growth checks and is more efficient.