-
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.
Add addUsernameFromFilePathStep to support moving chrome_login_data_e…
…mails to KATC
- Loading branch information
1 parent
3404ab6
commit 2c4d18e
Showing
13 changed files
with
93 additions
and
12 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
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
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,43 @@ | ||
package katc | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log/slog" | ||
"os" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
var homeDirLocations = map[string][]string{ | ||
"windows": {"/Users"}, | ||
"darwin": {"/Users"}, | ||
"linux": {"/home"}, | ||
} | ||
|
||
// addUsernameFromFilePath is a dataProcessingStep that adds a `username` field to the row | ||
// by checking the `sourcePath` for a user home directory prefix. | ||
func addUsernameFromFilePath(_ context.Context, _ *slog.Logger, sourcePath string, row map[string][]byte) (map[string][]byte, error) { | ||
homeDirs, ok := homeDirLocations[runtime.GOOS] | ||
if !ok { | ||
return row, errors.New("cannot determine home directories") | ||
} | ||
|
||
for _, homeDir := range homeDirs { | ||
if !strings.HasPrefix(sourcePath, homeDir) { | ||
continue | ||
} | ||
|
||
// Trim the home directory and the leading path separator. | ||
// The next component of the path will then be the username. | ||
remainingPath := strings.TrimPrefix(strings.TrimPrefix(sourcePath, homeDir), string(os.PathSeparator)) | ||
remainingPathComponents := strings.Split(remainingPath, string(os.PathSeparator)) | ||
if len(remainingPathComponents) < 1 { | ||
continue | ||
} | ||
row["username"] = []byte(remainingPathComponents[0]) | ||
return row, nil | ||
} | ||
|
||
return row, 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,32 @@ | ||
package katc | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/kolide/launcher/pkg/log/multislogger" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_addUsernameFromFilePath(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Create a path with an expected home directory | ||
homeDirs := homeDirLocations[runtime.GOOS] | ||
expectedUsername := "test-user" | ||
firstRow := make(map[string][]byte) | ||
sourcePath := filepath.Join(homeDirs[0], expectedUsername, "some", "path", "to", "db.sqlite") | ||
result, err := addUsernameFromFilePath(context.TODO(), multislogger.NewNopLogger(), sourcePath, firstRow) | ||
require.NoError(t, err) | ||
require.Contains(t, result, "username") | ||
require.Equal(t, expectedUsername, string(result["username"])) | ||
|
||
// Create a path without an expected home directory | ||
otherSourcePath := filepath.Join("some", "other", "path", "to", "db.sqlite") | ||
secondRow := make(map[string][]byte) | ||
resultWithoutUsername, err := addUsernameFromFilePath(context.TODO(), multislogger.NewNopLogger(), otherSourcePath, secondRow) | ||
require.NoError(t, err) | ||
require.NotContains(t, resultWithoutUsername, "username") | ||
} |
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