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

DEEP-9655 Add build id to package info #16

Merged
merged 2 commits into from
Jan 2, 2024
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
35 changes: 33 additions & 2 deletions pkg/golang/binary/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package binary

import (
"debug/buildinfo"
"debug/elf"
"fmt"
"strings"

"golang.org/x/xerrors"
Expand Down Expand Up @@ -37,11 +39,22 @@ func NewParser() types.Parser {

// Parse scans file to try to report the Go and module versions.
func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var warnings []string
var buildID string

info, err := buildinfo.Read(r)
if err != nil {
return nil, nil, convertError(err)
}

if len(info.Deps) > 0 {
// get build id
buildID, err = getBuildID(r)
if err != nil {
warnings = []string{err.Error()}
}
}

libs := make([]types.Library, 0, len(info.Deps))

for _, dep := range info.Deps {
Expand All @@ -58,10 +71,28 @@ func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency,
}

libs = append(libs, types.Library{
Name: mod.Path,
Version: mod.Version,
Name: mod.Path,
Version: mod.Version,
BuildID: buildID,
Warnings: warnings,
})
}

return libs, nil, nil
}

func getBuildID(r dio.ReadSeekerAt) (string, error) {
file, err := elf.NewFile(r)
if err != nil {
return "", err
}
defer file.Close()

for _, section := range file.Sections {
if section.Name == ".note.go.buildid" {
data, _ := section.Data()
return string(data[16:]), nil
}
}
return "", fmt.Errorf("Go BuildID not found")
}
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Library struct {
Locations Locations `json:",omitempty"`
FilePath string `json:",omitempty"` // Required to show nested jars
Warnings []string `json:",omitempty"`
BuildID string `json:",omitempty"`
}

type Libraries []Library
Expand Down
Loading