-
Notifications
You must be signed in to change notification settings - Fork 38
Add dockutil table for macOS #82
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "dockutil", | ||
| srcs = ["dockutil.go"], | ||
| importpath = "github.com/macadmins/osquery-extension/tables/dockutil", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//pkg/utils", | ||
| "@com_github_osquery_osquery_go//plugin/table", | ||
| "@com_github_pkg_errors//:errors", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "dockutil_test", | ||
| srcs = ["dockutil_test.go"], | ||
| embed = [":dockutil"], | ||
| deps = [ | ||
| "//pkg/utils", | ||
| "@com_github_osquery_osquery_go//plugin/table", | ||
| "@com_github_pkg_errors//:errors", | ||
| "@com_github_stretchr_testify//assert", | ||
| "@com_github_stretchr_testify//require", | ||
| ], | ||
| ) | ||
This file contains hidden or 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,65 @@ | ||
| package dockutil | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/macadmins/osquery-extension/pkg/utils" | ||
| "github.com/osquery/osquery-go/plugin/table" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| const dockutilPath = "/usr/local/bin/dockutil" | ||
|
|
||
| func DockutilColumns() []table.ColumnDefinition { | ||
| return []table.ColumnDefinition{ | ||
| table.TextColumn("version"), | ||
| table.TextColumn("path"), | ||
| } | ||
| } | ||
|
|
||
| func DockutilGenerate(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) { | ||
| var results []map[string]string | ||
| r := utils.NewRunner() | ||
| fs := utils.OSFileSystem{} | ||
|
|
||
| version, path, err := runDockutil(r, fs) | ||
| if err != nil { | ||
| return results, err | ||
| } | ||
|
|
||
| // Only add a row if dockutil is installed | ||
| if version != "" { | ||
| results = append(results, map[string]string{ | ||
| "version": version, | ||
| "path": path, | ||
| }) | ||
| } | ||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| func runDockutil(r utils.Runner, fs utils.FileSystem) (string, string, error) { | ||
| // Check if dockutil exists | ||
| _, err := fs.Stat(dockutilPath) | ||
| if err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| // Not an error, just not installed | ||
| return "", "", nil | ||
| } | ||
| return "", "", err | ||
| } | ||
|
|
||
| // Run dockutil --version | ||
| out, err := r.Runner.RunCmd(dockutilPath, "--version") | ||
| if err != nil { | ||
| return "", "", errors.Wrap(err, "dockutil --version") | ||
| } | ||
|
|
||
| // Parse the output - typically "x.x.x" or "dockutil-x.x.x" | ||
| version := strings.TrimSpace(string(out)) | ||
| version = strings.TrimPrefix(version, "dockutil-") | ||
|
|
||
| return version, dockutilPath, nil | ||
| } |
This file contains hidden or 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,87 @@ | ||
| package dockutil | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/macadmins/osquery-extension/pkg/utils" | ||
| "github.com/pkg/errors" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDockutilColumns(t *testing.T) { | ||
| columns := DockutilColumns() | ||
| require.Len(t, columns, 2) | ||
| assert.Equal(t, "version", columns[0].Name) | ||
| assert.Equal(t, "path", columns[1].Name) | ||
| } | ||
|
|
||
| func TestRunDockutil(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| mockOutput string | ||
| mockErr error | ||
| fileExists bool | ||
| expectedVer string | ||
| expectedPath string | ||
| shouldError bool | ||
| }{ | ||
| { | ||
| name: "dockutil installed - simple version", | ||
| mockOutput: "3.0.2\n", | ||
| mockErr: nil, | ||
| fileExists: true, | ||
| expectedVer: "3.0.2", | ||
| expectedPath: dockutilPath, | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "dockutil installed - version with prefix", | ||
| mockOutput: "dockutil-3.0.2\n", | ||
| mockErr: nil, | ||
| fileExists: true, | ||
| expectedVer: "3.0.2", | ||
| expectedPath: dockutilPath, | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "dockutil not installed", | ||
| mockOutput: "", | ||
| mockErr: nil, | ||
| fileExists: false, | ||
| expectedVer: "", | ||
| expectedPath: "", | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "dockutil command fails", | ||
| mockOutput: "", | ||
| mockErr: errors.New("command failed"), | ||
| fileExists: true, | ||
| expectedVer: "", | ||
| expectedPath: "", | ||
| shouldError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| mockCmd := utils.MockCmdRunner{ | ||
| Output: tt.mockOutput, | ||
| Err: tt.mockErr, | ||
| } | ||
| runner := utils.Runner{Runner: mockCmd} | ||
| fs := utils.MockFileSystem{FileExists: tt.fileExists} | ||
|
|
||
| version, path, err := runDockutil(runner, fs) | ||
|
|
||
| if tt.shouldError { | ||
| require.Error(t, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.expectedVer, version) | ||
| assert.Equal(t, tt.expectedPath, path) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The test file imports
github.com/pkg/errors(line 7 of dockutil_test.go) but this dependency is not declared in the test's deps list. Add@com_github_pkg_errors//:errorsto the deps array to fix the missing dependency.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.
Addressed in 77878f0