-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into becca/katc-cfg-update
- Loading branch information
Showing
8 changed files
with
174 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,73 @@ | ||
package katc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/kolide/launcher/ee/indexeddb" | ||
"github.com/osquery/osquery-go/plugin/table" | ||
) | ||
|
||
// indexeddbLeveldbData retrieves data from the LevelDB-backed IndexedDB instances | ||
// found at the filepath in `sourcePattern`. It retrieves all rows from the database | ||
// and object store specified in `query`, which it expects to be in the format | ||
// `<db name>.<object store name>`. | ||
func indexeddbLeveldbData(ctx context.Context, slogger *slog.Logger, sourcePaths []string, query string, sourceConstraints *table.ConstraintList) ([]sourceData, error) { | ||
results := make([]sourceData, 0) | ||
for _, sourcePath := range sourcePaths { | ||
pathPattern := sourcePatternToGlobbablePattern(sourcePath) | ||
leveldbs, err := filepath.Glob(pathPattern) | ||
if err != nil { | ||
return nil, fmt.Errorf("globbing for leveldb files: %w", err) | ||
} | ||
|
||
// Extract database and table from query | ||
dbName, objectStoreName, err := extractQueryTargets(query) | ||
if err != nil { | ||
return nil, fmt.Errorf("getting db and object store names: %w", err) | ||
} | ||
|
||
// Query databases | ||
for _, db := range leveldbs { | ||
// Check to make sure `db` adheres to sourceConstraints | ||
valid, err := checkPathConstraints(db, sourceConstraints) | ||
if err != nil { | ||
return nil, fmt.Errorf("checking source path constraints: %w", err) | ||
} | ||
if !valid { | ||
continue | ||
} | ||
|
||
rowsFromDb, err := indexeddb.QueryIndexeddbObjectStore(db, dbName, objectStoreName) | ||
if err != nil { | ||
return nil, fmt.Errorf("querying %s: %w", db, err) | ||
} | ||
results = append(results, sourceData{ | ||
path: db, | ||
rows: rowsFromDb, | ||
}) | ||
} | ||
} | ||
|
||
return results, nil | ||
} | ||
|
||
// extractQueryTargets retrieves the targets of the query (the database name and the object store name) | ||
// from the query. IndexedDB is a NoSQL database, so we expect to retrieve all rows from the given | ||
// object store within the given database name. | ||
func extractQueryTargets(query string) (string, string, error) { | ||
parts := strings.Split(query, ".") | ||
if len(parts) != 2 { | ||
return "", "", fmt.Errorf("unable to extract query targets from query: expected `<db name>.<obj store name>`, got `%s`", query) | ||
} | ||
if len(parts[0]) == 0 { | ||
return "", "", fmt.Errorf("missing db name in query `%s`", query) | ||
} | ||
if len(parts[1]) == 0 { | ||
return "", "", fmt.Errorf("missing object store name in query `%s`", query) | ||
} | ||
return parts[0], parts[1], nil | ||
} |
This file contains 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,62 @@ | ||
package katc | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_extractQueryTargets(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tt := range []struct { | ||
testCaseName string | ||
query string | ||
expectedDbName string | ||
expectedObjectStoreName string | ||
expectErr bool | ||
}{ | ||
{ | ||
testCaseName: "correctly formed query", | ||
query: "some_db.some_obj_store", | ||
expectedDbName: "some_db", | ||
expectedObjectStoreName: "some_obj_store", | ||
expectErr: false, | ||
}, | ||
{ | ||
testCaseName: "missing db name", | ||
query: ".some_obj_store", | ||
expectErr: true, | ||
}, | ||
{ | ||
testCaseName: "missing object store name", | ||
query: "some_db.", | ||
expectErr: true, | ||
}, | ||
{ | ||
testCaseName: "query missing separator", | ||
query: "some_db some_obj_store", | ||
expectErr: true, | ||
}, | ||
{ | ||
testCaseName: "query has too many components", | ||
query: "some_db.some_obj_store.some_other_component", | ||
expectErr: true, | ||
}, | ||
} { | ||
tt := tt | ||
t.Run(tt.testCaseName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
dbName, objStoreName, err := extractQueryTargets(tt.query) | ||
|
||
if tt.expectErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expectedDbName, dbName) | ||
require.Equal(t, tt.expectedObjectStoreName, objStoreName) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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