mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
* add new format pattern Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add syftjson format Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add internal formats helper Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add SBOM encode/decode to lib API Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * remove json presenter + update presenter tests to use common utils Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * remove presenter format enum type + add formats shim in presenter helper Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add MustCPE helper for tests Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * update usage of format enum Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add test fixtures for encode/decode tests Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * fix integration test Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * migrate format detection to use reader Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * address review comments Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package anchore
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/anchore/syft/internal/formats/syftjson"
|
|
|
|
"github.com/wagoodman/go-progress"
|
|
|
|
"github.com/anchore/syft/syft/distro"
|
|
"github.com/anchore/syft/syft/source"
|
|
|
|
"github.com/anchore/client-go/pkg/external"
|
|
"github.com/anchore/syft/internal/log"
|
|
"github.com/anchore/syft/syft/pkg"
|
|
)
|
|
|
|
type packageSBOMImportAPI interface {
|
|
ImportImagePackages(context.Context, string, external.ImagePackageManifest) (external.ImageImportContentResponse, *http.Response, error)
|
|
}
|
|
|
|
func packageSbomModel(s source.Metadata, catalog *pkg.Catalog, d *distro.Distro, scope source.Scope) (*external.ImagePackageManifest, error) {
|
|
var buf bytes.Buffer
|
|
|
|
err := syftjson.Format().Presenter(catalog, &s, d, scope).Present(&buf)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to serialize results: %w", err)
|
|
}
|
|
|
|
// the model is 1:1 the JSON output of today. As the schema changes, this will need to be converted into individual mappings.
|
|
var model external.ImagePackageManifest
|
|
if err = json.Unmarshal(buf.Bytes(), &model); err != nil {
|
|
return nil, fmt.Errorf("unable to convert JSON presenter output to import model: %w", err)
|
|
}
|
|
|
|
return &model, nil
|
|
}
|
|
|
|
func importPackageSBOM(ctx context.Context, api packageSBOMImportAPI, sessionID string, s source.Metadata, catalog *pkg.Catalog, d *distro.Distro, scope source.Scope, stage *progress.Stage) (string, error) {
|
|
log.Debug("importing package SBOM")
|
|
stage.Current = "package SBOM"
|
|
|
|
model, err := packageSbomModel(s, catalog, d, scope)
|
|
if err != nil {
|
|
return "", fmt.Errorf("unable to create PackageSBOM model: %w", err)
|
|
}
|
|
|
|
response, httpResponse, err := api.ImportImagePackages(ctx, sessionID, *model)
|
|
if err != nil {
|
|
var openAPIErr external.GenericOpenAPIError
|
|
if errors.As(err, &openAPIErr) {
|
|
log.Errorf("api response: %+v", string(openAPIErr.Body()))
|
|
}
|
|
return "", fmt.Errorf("unable to import PackageSBOM: %w", err)
|
|
}
|
|
|
|
defer httpResponse.Body.Close()
|
|
|
|
if httpResponse.StatusCode != 200 {
|
|
return "", fmt.Errorf("unable to import PackageSBOM: %s", httpResponse.Status)
|
|
}
|
|
|
|
return response.Digest, nil
|
|
}
|