func (r *run) ValidateParameters() (err error) { if r.Parameters.MaxConcurrentEval <= 0 || r.Parameters.MaxConcurrentEval > 10 { return fmt.Errorf("concurrent evaluation must be between > 0 and <= 10") } // Supplying a definition with other modes is invalid if r.Parameters.OvalDef != "" && len(r.Parameters.PkgMatch.Matches) > 0 { return fmt.Errorf("cannot specify definition mode with other modes") } // Make sure a mode has been specified if r.Parameters.OvalDef == "" && len(r.Parameters.PkgMatch.Matches) == 0 { return fmt.Errorf("must specify a mode of operation") } // If an oval definition has been supplied, try parsing it to validate it if r.Parameters.OvalDef != "" { ovalbuf, err := makeOvalString(r.Parameters.OvalDef) if err != nil { return err } _, err = ovallib.ParseBuffer(ovalbuf) if err != nil { return err } } // If package match parameters have been specified, make sure they all // compile here. for _, x := range r.Parameters.PkgMatch.Matches { _, err = regexp.Compile(x) if err != nil { return err } } return }
func (r *run) Run(in io.Reader) (resStr string) { defer func() { if e := recover(); e != nil { // return error in json r.Results.Errors = append(r.Results.Errors, fmt.Sprintf("%v", e)) r.Results.Success = false endCounters() r.Results.Statistics = stats err, _ := json.Marshal(r.Results) resStr = string(err) return } }() // Restrict go runtime processor utilization here, this might be moved // into a more generic agent module function at some point. runtime.GOMAXPROCS(1) startCounters() // Read module parameters from stdin err := modules.ReadInputParameters(in, &r.Parameters) if err != nil { panic(err) } err = r.ValidateParameters() if err != nil { panic(err) } ovallib.SetMaxChecks(r.Parameters.MaxConcurrentEval) e := &elements{} if len(r.Parameters.PkgMatch.Matches) > 0 { oresp := ovallib.PackageQuery(r.Parameters.PkgMatch.Matches, true) for _, x := range oresp { npi := &PkgInfo{PkgName: x.Name, PkgVersion: x.Version, PkgType: x.PkgType} e.Matches = append(e.Matches, *npi) } buf, err := buildResults(*e, &r.Results, len(e.Matches)) if err != nil { panic(err) } resStr = string(buf) return } else if r.Parameters.OvalDef != "" { stats.InDefSize = len(r.Parameters.OvalDef) ovalbuf, err := makeOvalString(r.Parameters.OvalDef) if err != nil { panic(err) } pst := time.Now() od, err := ovallib.ParseBuffer(ovalbuf) stats.Parsetime = time.Now().Sub(pst).String() if err != nil { panic(err) } ovalresults, err := ovallib.Execute(od) if err != nil { panic(err) } for _, x := range ovalresults { if !r.Parameters.IncludeFalse { if x.Status == ovallib.RESULT_FALSE { continue } } nmor := &MOResult{} nmor.Title = x.Title nmor.Status = x.StatusString() nmor.ID = x.ID e.OvalResults = append(e.OvalResults, *nmor) } buf, err := buildResults(*e, &r.Results, len(e.OvalResults)) if err != nil { panic(err) } resStr = string(buf) return } panic("no function specified") return }