github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/buildinfo/content_manifest.go (about)

     1  package buildinfo
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"golang.org/x/xerrors"
    10  
    11  	"github.com/devseccon/trivy/pkg/fanal/analyzer"
    12  	"github.com/devseccon/trivy/pkg/fanal/types"
    13  )
    14  
    15  func init() {
    16  	analyzer.RegisterAnalyzer(&contentManifestAnalyzer{})
    17  }
    18  
    19  const contentManifestAnalyzerVersion = 1
    20  
    21  type contentManifest struct {
    22  	ContentSets []string `json:"content_sets"`
    23  }
    24  
    25  // For Red Hat products
    26  type contentManifestAnalyzer struct{}
    27  
    28  func (a contentManifestAnalyzer) Analyze(_ context.Context, target analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
    29  	var manifest contentManifest
    30  	if err := json.NewDecoder(target.Content).Decode(&manifest); err != nil {
    31  		return nil, xerrors.Errorf("invalid content manifest: %w", err)
    32  	}
    33  
    34  	return &analyzer.AnalysisResult{
    35  		BuildInfo: &types.BuildInfo{
    36  			ContentSets: manifest.ContentSets,
    37  		},
    38  	}, nil
    39  }
    40  
    41  func (a contentManifestAnalyzer) Required(filePath string, _ os.FileInfo) bool {
    42  	dir, file := filepath.Split(filepath.ToSlash(filePath))
    43  	if dir != "root/buildinfo/content_manifests/" {
    44  		return false
    45  	}
    46  	return filepath.Ext(file) == ".json"
    47  }
    48  
    49  func (a contentManifestAnalyzer) Type() analyzer.Type {
    50  	return analyzer.TypeRedHatContentManifestType
    51  }
    52  
    53  func (a contentManifestAnalyzer) Version() int {
    54  	return contentManifestAnalyzerVersion
    55  }