github.com/khulnasoft-lab/tunnel-db@v0.0.0-20231117205118-74e1113bd007/pkg/db/redhat_cpe.go (about) 1 package db 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 bolt "go.etcd.io/bbolt" 8 "golang.org/x/xerrors" 9 ) 10 11 const ( 12 redhatCPERootBucket = "Red Hat CPE" 13 redhatRepoBucket = "repository" 14 redhatNVRBucket = "nvr" 15 16 // This bucket will not be used during vulnerability scanning. Just for debugging. 17 redhatCPEBucket = "cpe" 18 ) 19 20 func (dbc Config) PutRedHatRepositories(tx *bolt.Tx, repository string, cpeIndices []int) error { 21 if err := dbc.put(tx, []string{redhatCPERootBucket, redhatRepoBucket}, repository, cpeIndices); err != nil { 22 return xerrors.Errorf("Red Hat CPE error: %w", err) 23 } 24 25 return nil 26 } 27 28 func (dbc Config) PutRedHatNVRs(tx *bolt.Tx, nvr string, cpeIndices []int) error { 29 if err := dbc.put(tx, []string{redhatCPERootBucket, redhatNVRBucket}, nvr, cpeIndices); err != nil { 30 return xerrors.Errorf("Red Hat CPE error: %w", err) 31 } 32 33 return nil 34 } 35 36 func (dbc Config) PutRedHatCPEs(tx *bolt.Tx, cpeIndex int, cpe string) error { 37 index := fmt.Sprint(cpeIndex) 38 if err := dbc.put(tx, []string{redhatCPERootBucket, redhatCPEBucket}, index, cpe); err != nil { 39 return xerrors.Errorf("Red Hat CPE error: %w", err) 40 } 41 42 return nil 43 } 44 45 func (dbc Config) RedHatRepoToCPEs(repository string) ([]int, error) { 46 return dbc.getCPEs(redhatRepoBucket, repository) 47 } 48 49 func (dbc Config) RedHatNVRToCPEs(repository string) ([]int, error) { 50 return dbc.getCPEs(redhatNVRBucket, repository) 51 } 52 53 func (dbc Config) getCPEs(bucket, key string) ([]int, error) { 54 value, err := dbc.get([]string{redhatCPERootBucket, bucket}, key) 55 if err != nil { 56 return nil, xerrors.Errorf("unable to get '%s': %w", key, err) 57 } else if len(value) == 0 { 58 return nil, nil 59 } 60 61 var cpes []int 62 if err = json.Unmarshal(value, &cpes); err != nil { 63 return nil, xerrors.Errorf("JSON unmarshal error: %w", err) 64 } 65 return cpes, nil 66 }