Skip to content
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

add primary key to keylist response #611

Merged
merged 2 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion serf/internal_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ type nodeKeyResponse struct {

// Keys is used in listing queries to relay a list of installed keys
Keys []string

// PrimaryKey is used in listing queries to relay the primary key
PrimaryKey string
}

// newSerfQueries is used to create a new serfQueries. We return an event
Expand Down Expand Up @@ -346,7 +349,7 @@ SEND:
func (s *serfQueries) handleListKeys(q *Query) {
response := nodeKeyResponse{Result: false}
keyring := s.serf.config.MemberlistConfig.Keyring

var primaryKeyBytes []byte
if !s.serf.EncryptionEnabled() {
response.Message = "Keyring is empty (encryption not enabled)"
s.logger.Printf("[ERR] serf: Keyring is empty (encryption not enabled)")
Expand All @@ -360,6 +363,9 @@ func (s *serfQueries) handleListKeys(q *Query) {
key := base64.StdEncoding.EncodeToString(keyBytes)
response.Keys = append(response.Keys, key)
}
primaryKeyBytes = keyring.GetPrimaryKey()
response.PrimaryKey = base64.StdEncoding.EncodeToString(primaryKeyBytes)

response.Result = true

SEND:
Expand Down
2 changes: 1 addition & 1 deletion serf/internal_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestSerfQueries_keyListResponseWithCorrectSize(t *testing.T) {
{expected: 0, hasMsg: false, resp: nodeKeyResponse{}},
{expected: 1, hasMsg: false, resp: nodeKeyResponse{Keys: []string{"KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg="}}},
// has 50 keys which makes the response bigger than 1024 bytes.
{expected: 19, hasMsg: true, resp: nodeKeyResponse{Keys: []string{
{expected: 18, hasMsg: true, resp: nodeKeyResponse{Keys: []string{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes because the primary key was added to the response. Which is why less keys fit into the message.

"KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=",
"KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=",
"KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=", "KfCPZAKdgHUOdb202afZfE8EbdZqj4+ReTbfJUkfKsg=",
Expand Down
15 changes: 13 additions & 2 deletions serf/keymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type KeyResponse struct {
// Keys is a mapping of the base64-encoded value of the key bytes to the
// number of nodes that have the key installed.
Keys map[string]int

// PrimaryKeys is a mapping of the base64-encoded value of the primary
// key bytes to the number of nodes that have the key installed.
PrimaryKeys map[string]int
}

// KeyRequestOptions is used to contain optional parameters for a keyring operation
Expand Down Expand Up @@ -83,6 +87,12 @@ func (k *KeyManager) streamKeyResp(resp *KeyResponse, ch <-chan NodeResponse) {
}
}

if _, ok := resp.PrimaryKeys[nodeResponse.PrimaryKey]; !ok {
resp.PrimaryKeys[nodeResponse.PrimaryKey] = 1
} else {
resp.PrimaryKeys[nodeResponse.PrimaryKey]++
}
hanshasselberg marked this conversation as resolved.
Show resolved Hide resolved

NEXT:
// Return early if all nodes have responded. This allows us to avoid
// waiting for the full timeout when there is nothing left to do.
Expand All @@ -97,8 +107,9 @@ func (k *KeyManager) streamKeyResp(resp *KeyResponse, ch <-chan NodeResponse) {
// KeyResponse for uniform response handling.
func (k *KeyManager) handleKeyRequest(key, query string, opts *KeyRequestOptions) (*KeyResponse, error) {
resp := &KeyResponse{
Messages: make(map[string]string),
Keys: make(map[string]int),
Messages: make(map[string]string),
Keys: make(map[string]int),
PrimaryKeys: make(map[string]int),
}
qName := internalQueryName(query)

Expand Down
14 changes: 13 additions & 1 deletion serf/keymanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func TestSerf_ListKeys(t *testing.T) {
}

found := false
for key, _ := range resp.Keys {
for key := range resp.Keys {
if key == extraKey {
found = true
}
Expand All @@ -304,4 +304,16 @@ func TestSerf_ListKeys(t *testing.T) {
t.Fatalf("Expected 1 nodes with key %s but have %d", extraKey, num)
}
}

// PrimaryKeys should be set
if len(resp.PrimaryKeys) != 1 {
t.Fatalf("Expected one primary key, but have %v", len(resp.PrimaryKeys))
}

// extraKey is not the primary
for key := range resp.PrimaryKeys {
if key == extraKey {
t.Fatal("extrakey shouldn't be the primary key")
}
}
}