From a3a13b4fe3195cce1f2feb85c1e30b3d6e3f60a7 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Wed, 17 Nov 2021 13:36:42 -0500 Subject: [PATCH] remove power-user document shape Signed-off-by: Alex Goodman --- internal/presenter/poweruser/json_document.go | 35 ----------- .../poweruser/json_file_classifications.go | 31 ---------- .../presenter/poweruser/json_file_contents.go | 28 --------- .../presenter/poweruser/json_file_metadata.go | 60 ------------------- .../presenter/poweruser/json_presenter.go | 8 +-- internal/presenter/poweruser/json_secrets.go | 29 --------- 6 files changed, 3 insertions(+), 188 deletions(-) delete mode 100644 internal/presenter/poweruser/json_document.go delete mode 100644 internal/presenter/poweruser/json_file_classifications.go delete mode 100644 internal/presenter/poweruser/json_file_contents.go delete mode 100644 internal/presenter/poweruser/json_file_metadata.go delete mode 100644 internal/presenter/poweruser/json_secrets.go diff --git a/internal/presenter/poweruser/json_document.go b/internal/presenter/poweruser/json_document.go deleted file mode 100644 index 2857772c2..000000000 --- a/internal/presenter/poweruser/json_document.go +++ /dev/null @@ -1,35 +0,0 @@ -package poweruser - -import ( - "github.com/anchore/syft/internal/formats/syftjson" - "github.com/anchore/syft/internal/formats/syftjson/model" - "github.com/anchore/syft/syft/sbom" -) - -type JSONDocument struct { - // note: poweruser.JSONDocument is meant to always be a superset of packages.JSONDocument, any additional fields - // here should be optional by supplying "omitempty" on these fields hint to the jsonschema generator to not - // require these fields. As an accepted rule in this repo all collections should still be initialized in the - // context of being used in a JSON document. - FileClassifications []JSONFileClassifications `json:"fileClassifications,omitempty"` // note: must have omitempty - FileContents []JSONFileContents `json:"fileContents,omitempty"` // note: must have omitempty - FileMetadata []JSONFileMetadata `json:"fileMetadata,omitempty"` // note: must have omitempty - Secrets []JSONSecrets `json:"secrets,omitempty"` // note: must have omitempty - model.Document -} - -// NewJSONDocument creates and populates a new JSON document struct from the given cataloging results. -func NewJSONDocument(s sbom.SBOM, appConfig interface{}) (JSONDocument, error) { - fileMetadata, err := NewJSONFileMetadata(s.Artifacts.FileMetadata, s.Artifacts.FileDigests) - if err != nil { - return JSONDocument{}, err - } - - return JSONDocument{ - FileClassifications: NewJSONFileClassifications(s.Artifacts.FileClassifications), - FileContents: NewJSONFileContents(s.Artifacts.FileContents), - FileMetadata: fileMetadata, - Secrets: NewJSONSecrets(s.Artifacts.Secrets), - Document: syftjson.ToFormatModel(s, appConfig), - }, nil -} diff --git a/internal/presenter/poweruser/json_file_classifications.go b/internal/presenter/poweruser/json_file_classifications.go deleted file mode 100644 index f72ce8c2e..000000000 --- a/internal/presenter/poweruser/json_file_classifications.go +++ /dev/null @@ -1,31 +0,0 @@ -package poweruser - -import ( - "sort" - - "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/source" -) - -type JSONFileClassifications struct { - Location source.Coordinates `json:"location"` - Classification file.Classification `json:"classification"` -} - -func NewJSONFileClassifications(data map[source.Coordinates][]file.Classification) []JSONFileClassifications { - results := make([]JSONFileClassifications, 0) - for coordinates, classifications := range data { - for _, classification := range classifications { - results = append(results, JSONFileClassifications{ - Location: coordinates, - Classification: classification, - }) - } - } - - // sort by real path then virtual path to ensure the result is stable across multiple runs - sort.SliceStable(results, func(i, j int) bool { - return results[i].Location.RealPath < results[j].Location.RealPath - }) - return results -} diff --git a/internal/presenter/poweruser/json_file_contents.go b/internal/presenter/poweruser/json_file_contents.go deleted file mode 100644 index d81a36504..000000000 --- a/internal/presenter/poweruser/json_file_contents.go +++ /dev/null @@ -1,28 +0,0 @@ -package poweruser - -import ( - "sort" - - "github.com/anchore/syft/syft/source" -) - -type JSONFileContents struct { - Location source.Coordinates `json:"location"` - Contents string `json:"contents"` -} - -func NewJSONFileContents(data map[source.Coordinates]string) []JSONFileContents { - results := make([]JSONFileContents, 0) - for coordinates, contents := range data { - results = append(results, JSONFileContents{ - Location: coordinates, - Contents: contents, - }) - } - - // sort by real path then virtual path to ensure the result is stable across multiple runs - sort.SliceStable(results, func(i, j int) bool { - return results[i].Location.RealPath < results[j].Location.RealPath - }) - return results -} diff --git a/internal/presenter/poweruser/json_file_metadata.go b/internal/presenter/poweruser/json_file_metadata.go deleted file mode 100644 index 0e24c2fe7..000000000 --- a/internal/presenter/poweruser/json_file_metadata.go +++ /dev/null @@ -1,60 +0,0 @@ -package poweruser - -import ( - "fmt" - "sort" - "strconv" - - "github.com/anchore/syft/syft/file" - - "github.com/anchore/syft/syft/source" -) - -type JSONFileMetadata struct { - Location source.Coordinates `json:"location"` - Metadata JSONFileMetadataEntry `json:"metadata"` -} - -type JSONFileMetadataEntry struct { - Mode int `json:"mode"` - Type source.FileType `json:"type"` - LinkDestination string `json:"linkDestination,omitempty"` - UserID int `json:"userID"` - GroupID int `json:"groupID"` - Digests []file.Digest `json:"digests,omitempty"` - MIMEType string `json:"mimeType"` -} - -func NewJSONFileMetadata(data map[source.Coordinates]source.FileMetadata, digests map[source.Coordinates][]file.Digest) ([]JSONFileMetadata, error) { - results := make([]JSONFileMetadata, 0) - for coordinates, metadata := range data { - mode, err := strconv.Atoi(fmt.Sprintf("%o", metadata.Mode)) - if err != nil { - return nil, fmt.Errorf("invalid mode found in file catalog @ location=%+v mode=%q: %w", coordinates, metadata.Mode, err) - } - - var digestResults []file.Digest - if digestsForLocation, exists := digests[coordinates]; exists { - digestResults = digestsForLocation - } - - results = append(results, JSONFileMetadata{ - Location: coordinates, - Metadata: JSONFileMetadataEntry{ - Mode: mode, - Type: metadata.Type, - LinkDestination: metadata.LinkDestination, - UserID: metadata.UserID, - GroupID: metadata.GroupID, - Digests: digestResults, - MIMEType: metadata.MIMEType, - }, - }) - } - - // sort by real path then virtual path to ensure the result is stable across multiple runs - sort.SliceStable(results, func(i, j int) bool { - return results[i].Location.RealPath < results[j].Location.RealPath - }) - return results, nil -} diff --git a/internal/presenter/poweruser/json_presenter.go b/internal/presenter/poweruser/json_presenter.go index 135386118..1bd5d4bcc 100644 --- a/internal/presenter/poweruser/json_presenter.go +++ b/internal/presenter/poweruser/json_presenter.go @@ -4,6 +4,8 @@ import ( "encoding/json" "io" + "github.com/anchore/syft/internal/formats/syftjson" + "github.com/anchore/syft/syft/sbom" ) @@ -23,11 +25,7 @@ func NewJSONPresenter(s sbom.SBOM, appConfig interface{}) *JSONPresenter { // Present the PackageCatalog results to the given writer. func (p *JSONPresenter) Present(output io.Writer) error { - doc, err := NewJSONDocument(p.sbom, p.config) - if err != nil { - return err - } - + doc := syftjson.ToFormatModel(p.sbom, p.config) enc := json.NewEncoder(output) // prevent > and < from being escaped in the payload enc.SetEscapeHTML(false) diff --git a/internal/presenter/poweruser/json_secrets.go b/internal/presenter/poweruser/json_secrets.go deleted file mode 100644 index 5c5d4c31d..000000000 --- a/internal/presenter/poweruser/json_secrets.go +++ /dev/null @@ -1,29 +0,0 @@ -package poweruser - -import ( - "sort" - - "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/source" -) - -type JSONSecrets struct { - Location source.Coordinates `json:"location"` - Secrets []file.SearchResult `json:"secrets"` -} - -func NewJSONSecrets(data map[source.Coordinates][]file.SearchResult) []JSONSecrets { - results := make([]JSONSecrets, 0) - for coordinates, secrets := range data { - results = append(results, JSONSecrets{ - Location: coordinates, - Secrets: secrets, - }) - } - - // sort by real path then virtual path to ensure the result is stable across multiple runs - sort.SliceStable(results, func(i, j int) bool { - return results[i].Location.RealPath < results[j].Location.RealPath - }) - return results -}