Skip to content

Commit

Permalink
Return identifier type from SplitKey too
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany committed Dec 11, 2024
1 parent 971ac3e commit ffb48cf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion ee/agent/startupsettings/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (s *startupSettingsWriter) extractAutoTableConstructionConfig(registrationI
func (s *startupSettingsWriter) extractKATCConstructionConfig(registrationId string) (string, error) {
kolideCfg := make(map[string]string)
if err := s.knapsack.KatcConfigStore().ForEach(func(k []byte, v []byte) error {
key, identifier := storage.SplitKey(k)
key, _, identifier := storage.SplitKey(k)
if string(identifier) == registrationId {
kolideCfg[string(key)] = string(v)
}
Expand Down
8 changes: 4 additions & 4 deletions ee/agent/storage/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ func KeyByIdentifier(key []byte, identifierType []byte, identifier []byte) []byt
return newKey
}

func SplitKey(key []byte) ([]byte, []byte) {
func SplitKey(key []byte) ([]byte, []byte, []byte) {
if !bytes.Contains(key, []byte{keyDelimiter}) {
return key, defaultIdentifier
return key, nil, defaultIdentifier
}

// Key takes the form `<key>:<identifierType>:<identifier>` -- split
// on the keyDelimiter.
parts := bytes.SplitN(key, []byte{keyDelimiter}, 3)
if len(parts) != 3 {
return key, defaultIdentifier
return key, nil, defaultIdentifier
}

return parts[0], parts[2]
return parts[0], parts[1], parts[2]
}
39 changes: 22 additions & 17 deletions ee/agent/storage/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,41 @@ func TestSplitKey(t *testing.T) {
t.Parallel()

for _, tt := range []struct {
testCaseName string
key []byte
expectedKey []byte
expectedIdentifier []byte
testCaseName string
key []byte
expectedKey []byte
expectedIdentifierType []byte
expectedIdentifier []byte
}{
{
testCaseName: "default node key",
key: []byte("nodeKey"),
expectedKey: []byte("nodeKey"),
expectedIdentifier: []byte("default"),
testCaseName: "default node key",
key: []byte("nodeKey"),
expectedKey: []byte("nodeKey"),
expectedIdentifierType: nil,
expectedIdentifier: []byte("default"),
},
{
testCaseName: "uuid by registration",
key: []byte("uuid:registration:some-test-registration-id"),
expectedKey: []byte("uuid"),
expectedIdentifier: []byte("some-test-registration-id"),
testCaseName: "uuid by registration",
key: []byte("uuid:registration:some-test-registration-id"),
expectedKey: []byte("uuid"),
expectedIdentifierType: IdentifierTypeRegistration,
expectedIdentifier: []byte("some-test-registration-id"),
},
{
testCaseName: "katc table by registration",
key: []byte("katc_some_test_table:registration:another-test-registration-id"),
expectedKey: []byte("katc_some_test_table"),
expectedIdentifier: []byte("another-test-registration-id"),
testCaseName: "katc table by registration",
key: []byte("katc_some_test_table:registration:another-test-registration-id"),
expectedKey: []byte("katc_some_test_table"),
expectedIdentifierType: IdentifierTypeRegistration,
expectedIdentifier: []byte("another-test-registration-id"),
},
} {
tt := tt
t.Run(tt.testCaseName, func(t *testing.T) {
t.Parallel()

splitKey, identifier := SplitKey(tt.key)
splitKey, identifierType, identifier := SplitKey(tt.key)
require.Equal(t, tt.expectedKey, splitKey)
require.Equal(t, tt.expectedIdentifierType, identifierType)
require.Equal(t, tt.expectedIdentifier, identifier)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/osquery/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func katcFromDb(k types.Knapsack, registrationId string) (map[string]string, err
}
katcCfg := make(map[string]string)
if err := k.KatcConfigStore().ForEach(func(k []byte, v []byte) error {
key, identifier := storage.SplitKey(k)
key, _, identifier := storage.SplitKey(k)
if string(identifier) == registrationId {
katcCfg[string(key)] = string(v)
}
Expand Down

0 comments on commit ffb48cf

Please sign in to comment.