Files
fleet/orbit/pkg/table/go_binaries/go_binaries.go
T
Josh RoskosandLucas Manuel Rodriguez ba2c5b5e5c Add go_binaries table (#39877)
**Related issue:** Resolves #40138

# Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually

Installed: 
```
go install golang.org/x/tools/cmd/goimports@latest
go install golang.org/x/tools/gopls@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
```

Validated:
```
osquery> SELECT * FROM go_packages;
+---------------+---------+-----------------------------------+-----------------------------------------------------+------------+----------------------------------+
| name          | version | module_path                       | import_path                                         | go_version | installed_path                   |
+---------------+---------+-----------------------------------+-----------------------------------------------------+------------+----------------------------------+
| goimports     | v0.42.0 | golang.org/x/tools                | golang.org/x/tools/cmd/goimports                    | go1.25.5   | /Users/josh/go/bin/goimports     |
| golangci-lint | v1.64.8 | github.com/golangci/golangci-lint | github.com/golangci/golangci-lint/cmd/golangci-lint | go1.25.5   | /Users/josh/go/bin/golangci-lint |
| gopls         | v0.21.1 | golang.org/x/tools/gopls          | golang.org/x/tools/gopls                            | go1.25.5   | /Users/josh/go/bin/gopls         |
+---------------+---------+-----------------------------------+-----------------------------------------------------+------------+----------------------------------+
```

## fleetd/orbit/Fleet Desktop

- [x] Verified compatibility with the latest released version of Fleet
(see [Must
rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md))
- [x] If the change applies to only one platform, confirmed that
`runtime.GOOS` is used as needed to isolate changes
- [x] Verified that fleetd runs on macOS, Linux and Windows

---------

Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2026-03-16 13:27:00 -05:00

65 lines
1.7 KiB
Go

package go_binaries
import (
"debug/buildinfo"
"os"
"path/filepath"
"github.com/osquery/osquery-go/plugin/table"
"github.com/rs/zerolog/log"
)
// GoBinariesColumns is the schema of the go_binaries table.
func GoBinariesColumns() []table.ColumnDefinition {
return []table.ColumnDefinition{
table.TextColumn("name"),
table.TextColumn("version"),
table.TextColumn("module_path"),
table.TextColumn("import_path"),
table.TextColumn("go_version"),
table.TextColumn("installed_path"),
}
}
// generateForDirs scans each directory's go/bin subdirectory for Go binaries
// and returns rows with embedded build metadata.
func generateForDirs(homeDirs []string) []map[string]string {
var results []map[string]string
for _, homeDir := range homeDirs {
goBinDir := filepath.Join(homeDir, "go", "bin")
entries, err := os.ReadDir(goBinDir)
if err != nil {
continue // directory doesn't exist or can't be read
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
fullPath := filepath.Join(goBinDir, entry.Name())
row := readGoBinary(fullPath)
if row != nil {
results = append(results, row)
}
}
}
return results
}
// readGoBinary extracts embedded build information from a Go binary.
// Returns nil if the file is not a Go binary or cannot be read.
func readGoBinary(path string) map[string]string {
info, err := buildinfo.ReadFile(path)
if err != nil {
log.Debug().Err(err).Str("path", path).Msg("go_binaries: failed to read build info")
return nil
}
return map[string]string{
"name": filepath.Base(path),
"version": info.Main.Version,
"module_path": info.Main.Path,
"import_path": info.Path,
"go_version": info.GoVersion,
"installed_path": path,
}
}