github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/event/parsers/parsers.go (about)

     1  /*
     2  Package parsers provides parser helpers to extract payloads for each event type that the syft library publishes onto the event bus.
     3  */
     4  package parsers
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  
    10  	"github.com/wagoodman/go-partybus"
    11  	"github.com/wagoodman/go-progress"
    12  
    13  	"github.com/anchore/syft/syft/event"
    14  	"github.com/anchore/syft/syft/event/monitor"
    15  	"github.com/anchore/syft/syft/file/cataloger/secrets"
    16  	"github.com/anchore/syft/syft/pkg/cataloger"
    17  )
    18  
    19  type ErrBadPayload struct {
    20  	Type  partybus.EventType
    21  	Field string
    22  	Value interface{}
    23  }
    24  
    25  func (e *ErrBadPayload) Error() string {
    26  	return fmt.Sprintf("event='%s' has bad event payload field=%q: %q", string(e.Type), e.Field, e.Value)
    27  }
    28  
    29  func newPayloadErr(t partybus.EventType, field string, value interface{}) error {
    30  	return &ErrBadPayload{
    31  		Type:  t,
    32  		Field: field,
    33  		Value: value,
    34  	}
    35  }
    36  
    37  func checkEventType(actual, expected partybus.EventType) error {
    38  	if actual != expected {
    39  		return newPayloadErr(expected, "Type", actual)
    40  	}
    41  	return nil
    42  }
    43  
    44  func ParsePackageCatalogerStarted(e partybus.Event) (*cataloger.Monitor, error) {
    45  	if err := checkEventType(e.Type, event.PackageCatalogerStarted); err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	monitor, ok := e.Value.(cataloger.Monitor)
    50  	if !ok {
    51  		return nil, newPayloadErr(e.Type, "Value", e.Value)
    52  	}
    53  
    54  	return &monitor, nil
    55  }
    56  
    57  func ParseSecretsCatalogingStarted(e partybus.Event) (*secrets.Monitor, error) {
    58  	if err := checkEventType(e.Type, event.SecretsCatalogerStarted); err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	monitor, ok := e.Value.(secrets.Monitor)
    63  	if !ok {
    64  		return nil, newPayloadErr(e.Type, "Value", e.Value)
    65  	}
    66  
    67  	return &monitor, nil
    68  }
    69  
    70  func ParseFileMetadataCatalogingStarted(e partybus.Event) (progress.StagedProgressable, error) {
    71  	if err := checkEventType(e.Type, event.FileMetadataCatalogerStarted); err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	prog, ok := e.Value.(progress.StagedProgressable)
    76  	if !ok {
    77  		return nil, newPayloadErr(e.Type, "Value", e.Value)
    78  	}
    79  
    80  	return prog, nil
    81  }
    82  
    83  func ParseFileDigestsCatalogingStarted(e partybus.Event) (progress.StagedProgressable, error) {
    84  	if err := checkEventType(e.Type, event.FileDigestsCatalogerStarted); err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	prog, ok := e.Value.(progress.StagedProgressable)
    89  	if !ok {
    90  		return nil, newPayloadErr(e.Type, "Value", e.Value)
    91  	}
    92  
    93  	return prog, nil
    94  }
    95  
    96  func ParseFileIndexingStarted(e partybus.Event) (string, progress.StagedProgressable, error) {
    97  	if err := checkEventType(e.Type, event.FileIndexingStarted); err != nil {
    98  		return "", nil, err
    99  	}
   100  
   101  	path, ok := e.Source.(string)
   102  	if !ok {
   103  		return "", nil, newPayloadErr(e.Type, "Source", e.Source)
   104  	}
   105  
   106  	prog, ok := e.Value.(progress.StagedProgressable)
   107  	if !ok {
   108  		return "", nil, newPayloadErr(e.Type, "Value", e.Value)
   109  	}
   110  
   111  	return path, prog, nil
   112  }
   113  
   114  func ParseCatalogerTaskStarted(e partybus.Event) (*monitor.CatalogerTask, error) {
   115  	if err := checkEventType(e.Type, event.CatalogerTaskStarted); err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	source, ok := e.Source.(*monitor.CatalogerTask)
   120  	if !ok {
   121  		return nil, newPayloadErr(e.Type, "Source", e.Source)
   122  	}
   123  
   124  	return source, nil
   125  }
   126  
   127  func ParseAttestationStartedEvent(e partybus.Event) (io.Reader, progress.Progressable, *monitor.GenericTask, error) {
   128  	if err := checkEventType(e.Type, event.AttestationStarted); err != nil {
   129  		return nil, nil, nil, err
   130  	}
   131  
   132  	source, ok := e.Source.(monitor.GenericTask)
   133  	if !ok {
   134  		return nil, nil, nil, newPayloadErr(e.Type, "Source", e.Source)
   135  	}
   136  
   137  	sp, ok := e.Value.(*monitor.ShellProgress)
   138  	if !ok {
   139  		return nil, nil, nil, newPayloadErr(e.Type, "Value", e.Value)
   140  	}
   141  
   142  	return sp.Reader, sp.Progressable, &source, nil
   143  }
   144  
   145  // CLI event types
   146  
   147  type UpdateCheck struct {
   148  	New     string
   149  	Current string
   150  }
   151  
   152  func ParseCLIAppUpdateAvailable(e partybus.Event) (*UpdateCheck, error) {
   153  	if err := checkEventType(e.Type, event.CLIAppUpdateAvailable); err != nil {
   154  		return nil, err
   155  	}
   156  
   157  	updateCheck, ok := e.Value.(UpdateCheck)
   158  	if !ok {
   159  		return nil, newPayloadErr(e.Type, "Value", e.Value)
   160  	}
   161  
   162  	return &updateCheck, nil
   163  }
   164  
   165  func ParseCLIReport(e partybus.Event) (string, string, error) {
   166  	if err := checkEventType(e.Type, event.CLIReport); err != nil {
   167  		return "", "", err
   168  	}
   169  
   170  	context, ok := e.Source.(string)
   171  	if !ok {
   172  		// this is optional
   173  		context = ""
   174  	}
   175  
   176  	report, ok := e.Value.(string)
   177  	if !ok {
   178  		return "", "", newPayloadErr(e.Type, "Value", e.Value)
   179  	}
   180  
   181  	return context, report, nil
   182  }
   183  
   184  func ParseCLINotification(e partybus.Event) (string, string, error) {
   185  	if err := checkEventType(e.Type, event.CLINotification); err != nil {
   186  		return "", "", err
   187  	}
   188  
   189  	context, ok := e.Source.(string)
   190  	if !ok {
   191  		// this is optional
   192  		context = ""
   193  	}
   194  
   195  	notification, ok := e.Value.(string)
   196  	if !ok {
   197  		return "", "", newPayloadErr(e.Type, "Value", e.Value)
   198  	}
   199  
   200  	return context, notification, nil
   201  }