github.com/khulnasoft-lab/tunnel-db@v0.0.0-20231117205118-74e1113bd007/pkg/vulnsrc/mariner/oval/oval.go (about)

     1  package oval
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"path/filepath"
     7  
     8  	"golang.org/x/xerrors"
     9  
    10  	"github.com/khulnasoft-lab/tunnel-db/pkg/utils"
    11  )
    12  
    13  func ParseDefinitions(dir string) ([]Definition, error) {
    14  	dir = filepath.Join(dir, "definitions")
    15  	if exists, _ := utils.Exists(dir); !exists {
    16  		return nil, xerrors.Errorf("no definitions dir")
    17  	}
    18  
    19  	var defs []Definition
    20  
    21  	err := utils.FileWalk(dir, func(r io.Reader, path string) error {
    22  		var def Definition
    23  		if err := json.NewDecoder(r).Decode(&def); err != nil {
    24  			return xerrors.Errorf("failed to decode %s: %w", path, err)
    25  		}
    26  		defs = append(defs, def)
    27  		return nil
    28  	})
    29  	if err != nil {
    30  		return nil, xerrors.Errorf("CBL-Mariner OVAL walk error: %w", err)
    31  	}
    32  
    33  	return defs, nil
    34  }
    35  
    36  func ParseTests(dir string) (Tests, error) {
    37  	var tests Tests
    38  	if err := utils.UnmarshalJSONFile(&tests, filepath.Join(dir, "tests", "tests.json")); err != nil {
    39  		return tests, xerrors.Errorf("failed to unmarshal tests: %w", err)
    40  	}
    41  	return tests, nil
    42  }
    43  
    44  func ParseObjects(dir string) (map[string]string, error) {
    45  	var objects Objects
    46  	if err := utils.UnmarshalJSONFile(&objects, filepath.Join(dir, "objects", "objects.json")); err != nil {
    47  		return nil, xerrors.Errorf("failed to unmarshal objects: %w", err)
    48  	}
    49  	objs := map[string]string{}
    50  	for _, obj := range objects.RpminfoObjects {
    51  		objs[obj.ID] = obj.Name
    52  	}
    53  	return objs, nil
    54  }
    55  
    56  func ParseStates(dir string) (map[string]RpmInfoState, error) {
    57  	var ss States
    58  	if err := utils.UnmarshalJSONFile(&ss, filepath.Join(dir, "states", "states.json")); err != nil {
    59  		return nil, xerrors.Errorf("failed to unmarshal states: %w", err)
    60  	}
    61  
    62  	states := map[string]RpmInfoState{}
    63  	for _, state := range ss.RpminfoState {
    64  		states[state.ID] = state
    65  	}
    66  	return states, nil
    67  }