fleet/pkg/file/rpm.go
Lucas Manuel Rodriguez f8f24e0a80
Add support to upload RPM packages (#22502)
#22473

- [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/Committing-Changes.md#changes-files)
for more information.
- [X] Added/updated tests
- [X] Manual QA for all new/changed functionality
- For Orbit and Fleet Desktop changes:
- [x] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.

---------

Co-authored-by: RachelElysia <71795832+RachelElysia@users.noreply.github.com>
Co-authored-by: Ian Littman <iansltx@gmail.com>
2024-10-01 13:02:13 -03:00

33 lines
696 B
Go

package file
import (
"crypto/sha256"
"fmt"
"io"
"github.com/cavaliergopher/rpm"
)
func ExtractRPMMetadata(r io.Reader) (*InstallerMetadata, error) {
h := sha256.New()
r = io.TeeReader(r, h)
// Read the package headers
pkg, err := rpm.Read(r)
if err != nil {
return nil, fmt.Errorf("read headers: %w", err)
}
// r is now positioned at the RPM payload.
// Ensure the whole file is read to get the correct hash
if _, err := io.Copy(io.Discard, r); err != nil {
return nil, fmt.Errorf("read all RPM content: %w", err)
}
return &InstallerMetadata{
Name: pkg.Name(),
Version: pkg.Version(),
SHASum: h.Sum(nil),
PackageIDs: []string{pkg.Name()},
}, nil
}