github.com/quay/claircore@v1.5.28/libvuln/updates.go (about)

     1  package libvuln
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  
     8  	"github.com/google/uuid"
     9  	"github.com/jackc/pgx/v4/pgxpool"
    10  	"github.com/quay/zlog"
    11  
    12  	"github.com/quay/claircore/datastore/postgres"
    13  	"github.com/quay/claircore/libvuln/driver"
    14  	"github.com/quay/claircore/libvuln/jsonblob"
    15  )
    16  
    17  // OfflineImport takes the format written into the io.Writer provided to
    18  // NewOfflineUpdater and imports the contents into the provided pgxpool.Pool.
    19  //
    20  // The format provided on "in" should be the same output from [jsonblob.Store], with
    21  // any compression undone.
    22  func OfflineImport(ctx context.Context, pool *pgxpool.Pool, in io.Reader) error {
    23  	// BUG(hank) The OfflineImport function is a wart, needed to work around
    24  	// some package namespacing issues. It should get refactored if claircore
    25  	// gets merged into clair.
    26  	ctx = zlog.ContextWithValues(ctx, "component", "libvuln/OfflineImporter")
    27  
    28  	s := postgres.NewMatcherStore(pool)
    29  	l, err := jsonblob.Load(ctx, in)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	ops, err := s.GetUpdateOperations(ctx, driver.VulnerabilityKind)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  Update:
    40  	for l.Next() {
    41  		e := l.Entry()
    42  		for _, op := range ops[e.Updater] {
    43  			// This only helps if updaters don't keep something that
    44  			// changes in the fingerprint.
    45  			if op.Fingerprint == e.Fingerprint {
    46  				zlog.Info(ctx).
    47  					Str("updater", e.Updater).
    48  					Msg("fingerprint match, skipping")
    49  				continue Update
    50  			}
    51  		}
    52  		var ref uuid.UUID
    53  		if e.Enrichment != nil {
    54  			if ref, err = s.UpdateEnrichments(ctx, e.Updater, e.Fingerprint, e.Enrichment); err != nil {
    55  				return fmt.Errorf("updating enrichements: %w", err)
    56  			}
    57  		}
    58  		if e.Vuln != nil {
    59  			if ref, err = s.UpdateVulnerabilities(ctx, e.Updater, e.Fingerprint, e.Vuln); err != nil {
    60  				return fmt.Errorf("updating vulnerabilities: %w", err)
    61  			}
    62  		}
    63  		zlog.Info(ctx).
    64  			Str("updater", e.Updater).
    65  			Str("ref", ref.String()).
    66  			Int("vuln_count", len(e.Vuln)).
    67  			Int("enrichment_count", len(e.Enrichment)).
    68  			Msg("update imported")
    69  	}
    70  	if err := l.Err(); err != nil {
    71  		return err
    72  	}
    73  	return nil
    74  }