From 8e9c65d3d49f610b683f02c58c45d038c2171e0d Mon Sep 17 00:00:00 2001 From: Asad-Ali-Dynatrace Date: Mon, 5 Apr 2021 11:32:14 -0400 Subject: [PATCH] Added process instance cache --- main.go | 68 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 19a1013..1484bfc 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,7 @@ const enforceLeadingDigits = true var fileNameFormat = regexp.MustCompile("(^\\d+[-].+)") var endpoints = Endpoints{ + "SecurityProblemsAll": "/api/v2/securityProblems?pageSize=500", "SecurityProblems": "/api/v2/securityProblems", "Processes": "/api/v1/entity/infrastructure/processes", } @@ -47,6 +48,7 @@ func main() { processor := &Processor{ Config: new(Config).Parse(), Client: client, + ProcessInstanceCache: &ProcessInstanceCache{}, } if err = processor.Process(); err != nil { panic(err) @@ -57,25 +59,32 @@ func main() { type Processor struct { Config *Config Client *http.Client + ProcessInstanceCache *ProcessInstanceCache } // Process has no documentation func (p *Processor) Process() error { var err error - pList, err := p.getSecurityProblemList("SecurityProblems") + pList, err := p.getSecurityProblemList("SecurityProblemsAll") + if (p.Config.Verbose) { + fmt.Printf("Total number of vulnerabilities=%d\n", len(pList)) + } if ( err != nil) { panic(err) } processesByLibrary := ProcessesByLibrary{} librariesByProcess := LibrariesByProcess{} - for _, problemId := range pList { + for elem, problemId := range pList { securityProblemInfoList, err := p.getSecurityProblemInfo(problemId) if (err != nil ) { log.Fatal(err) } + if (p.Config.Verbose) { + fmt.Printf("%d. %s\n", elem, problemId) + } for _, securityProblem := range securityProblemInfoList { if (p.Config.Verbose) { fmt.Println("***************************") @@ -129,6 +138,16 @@ func (p *Processor) Process() error { } } + if (p.Config.Verbose) { + fmt.Println("===========================================") + fmt.Println("Process Cache Entries") + fmt.Println("===========================================") + for processId, name := range *p.ProcessInstanceCache { + fmt.Printf("%s: %s\n", processId, *name) + } + fmt.Println("===========================================") + } + if (!p.Config.GroupByProcess) { for key, pByLibrary := range processesByLibrary { fmt.Printf(key + "\t") @@ -216,9 +235,9 @@ func (p *Processor) setupHTTPRequest(method string, endpointURL string) (*http.R } req.Header.Set("accept", "application/json; charset=utf-8") - if p.Config.Debug { + /*if p.Config.Debug { log.Println(fmt.Sprintf(" [HTTP] %s: %s", "Authorization", "Api-Token "+p.Config.APIToken)) - } + }*/ req.Header.Add("Authorization", "Api-Token "+p.Config.APIToken) return req, nil @@ -308,22 +327,26 @@ func (p *Processor) getProcessInstanceData(list []*SecurityProblemInfo) error{ for index, info := range list { processes := info.ProcessInstanceIdList var processNames []string + for _, process := range processes { - endpointURL := p.Config.URL + endpoints["Processes"] + "/" + process - if req, err = p.setupHTTPRequest("GET", endpointURL); err != nil { - return err - } - var resp *http.Response - if resp, err = p.Client.Do(req); err != nil { - return err - } - defer resp.Body.Close() + processName, found := p.checkProcessCache(process) + if (!found) { + endpointURL := p.Config.URL + endpoints["Processes"] + "/" + process + if req, err = p.setupHTTPRequest("GET", endpointURL); err != nil { + return err + } + var resp *http.Response + if resp, err = p.Client.Do(req); err != nil { + return err + } + defer resp.Body.Close() - processName, err := p.getProcessData(resp) - if ( err != nil) { - return err + processName, err = p.getProcessData(resp) + if ( err != nil) { + return err + } + (*p.ProcessInstanceCache)[process] = &processName } - processNames = append(processNames, processName) } info.ProcessInstanceNameList = processNames @@ -359,6 +382,14 @@ func (p *Processor) getProcessData(resp *http.Response) (string, error) { return processEnvelope.DisplayName, nil } +func (p *Processor) checkProcessCache(processId string) (string, bool) { + pName, found := (*p.ProcessInstanceCache)[processId] + if ( found) { + return *pName, found + } + return "", found +} + /********************* CONFIGURATION *********************/ // Config a simple configuration object @@ -398,6 +429,7 @@ func (c *Config) Lookup(envVar string, current string) string { return current } + /********************* VARIABLE SUBSTITUTION *********************/ type variables map[string]string @@ -477,6 +509,8 @@ type ProcessNames struct { } type ProcessesByLibrary map[string]*ProcessNames +type ProcessInstanceCache map[string]*string + /********************* API PAYLOAD *********************/ // ErrorEnvelope is potentially the JSON response code