github.com/quay/claircore@v1.5.28/oracle/updater.go (about)

     1  package oracle
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strconv"
     7  
     8  	"github.com/quay/claircore/libvuln/driver"
     9  	"github.com/quay/claircore/pkg/ovalutil"
    10  )
    11  
    12  const (
    13  	allDB = `https://linux.oracle.com/security/oval/com.oracle.elsa-all.xml.bz2`
    14  	//doc:url updater
    15  	baseURL = `https://linux.oracle.com/security/oval/com.oracle.elsa-%d.xml.bz2`
    16  )
    17  
    18  // Updater implements driver.Updater for Oracle Linux.
    19  type Updater struct {
    20  	year             int
    21  	ovalutil.Fetcher // Fetch method promoted via embed
    22  }
    23  
    24  // Option configures the provided Updater.
    25  type Option func(*Updater) error
    26  
    27  // NewUpdater returns an updater configured according to the provided Options.
    28  //
    29  // If year is -1, the "all" database will be pulled.
    30  func NewUpdater(year int, opts ...Option) (*Updater, error) {
    31  	uri := allDB
    32  	if year != -1 {
    33  		uri = fmt.Sprintf(baseURL, year)
    34  	}
    35  	u := Updater{
    36  		year: year,
    37  	}
    38  	var err error
    39  	u.Fetcher.URL, err = url.Parse(uri)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	u.Fetcher.Compression = ovalutil.CompressionBzip2
    44  	for _, o := range opts {
    45  		if err := o(&u); err != nil {
    46  			return nil, err
    47  		}
    48  	}
    49  
    50  	return &u, nil
    51  }
    52  
    53  // WithURL overrides the default URL to fetch an OVAL database.
    54  func WithURL(uri, compression string) Option {
    55  	c, cerr := ovalutil.ParseCompressor(compression)
    56  	u, uerr := url.Parse(uri)
    57  	return func(up *Updater) error {
    58  		// Return any errors from the outer function.
    59  		switch {
    60  		case cerr != nil:
    61  			return cerr
    62  		case uerr != nil:
    63  			return uerr
    64  		}
    65  		up.Fetcher.Compression = c
    66  		up.Fetcher.URL = u
    67  		return nil
    68  	}
    69  }
    70  
    71  var (
    72  	_ driver.Updater      = (*Updater)(nil)
    73  	_ driver.Configurable = (*Updater)(nil)
    74  )
    75  
    76  // Name satifies the driver.Updater interface.
    77  func (u *Updater) Name() string {
    78  	which := `all`
    79  	if u.year != -1 {
    80  		which = strconv.Itoa(u.year)
    81  	}
    82  	return fmt.Sprintf("oracle-%s-updater", which)
    83  }