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

Trim osquery version correctly on Windows #1903

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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: 6 additions & 2 deletions pkg/packaging/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ func (p *PackageOptions) setOsqueryVersionInCtx(ctx context.Context) {
return
}

osquerydVersionTrimmed := strings.TrimPrefix(strings.TrimSpace(stdout), "osqueryd version ")
packagekit.SetInContext(ctx, packagekit.ContextOsqueryVersionKey, osqueryVersionFromVersionOutput(stdout))
}

packagekit.SetInContext(ctx, packagekit.ContextOsqueryVersionKey, osquerydVersionTrimmed)
func osqueryVersionFromVersionOutput(output string) string {
// Output looks like `osquery version x.y.z`, so split on `version` and return the last part of the string
parts := strings.SplitAfter(output, "version")
return strings.TrimSpace(parts[len(parts)-1])
}

// osqueryLocation returns the location of the osquery binary within `binDir`. For darwin,
Expand Down
34 changes: 34 additions & 0 deletions pkg/packaging/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,37 @@ func TestSanitizeHostname(t *testing.T) {
require.Equal(t, tt.out, sanitizeHostname(tt.in))
}
}

func Test_osqueryVersionFromVersionOutput(t *testing.T) {
t.Parallel()

for _, tt := range []struct {
testCaseName string
versionOutput string
expectedVersion string
}{
{
testCaseName: "windows",
versionOutput: "osqueryd.exe version 5.14.1",
expectedVersion: "5.14.1",
},
{
testCaseName: "non-windows",
versionOutput: "osqueryd version 5.13.1",
expectedVersion: "5.13.1",
},
{
testCaseName: "extra spaces",
versionOutput: `
osqueryd version 5.13.1
`,
expectedVersion: "5.13.1",
},
} {
tt := tt
t.Run(tt.testCaseName, func(t *testing.T) {
t.Parallel()
require.Equal(t, tt.expectedVersion, osqueryVersionFromVersionOutput(tt.versionOutput))
})
}
}
Loading