From e7cb5e368aaed417201d1b73764cb794bd8074ad Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Wed, 11 Oct 2023 10:26:18 +0100 Subject: [PATCH 01/12] preventing nil pointer when body closing --- internal/wrappers/azure-http.go | 3 ++- internal/wrappers/bfl-http.go | 3 ++- internal/wrappers/bitbucket-http.go | 5 +++-- internal/wrappers/codebashing-http.go | 2 +- internal/wrappers/feature-flags-http.go | 3 ++- internal/wrappers/github-http.go | 7 ++++--- internal/wrappers/gitlab-http.go | 5 +++-- internal/wrappers/groups-http.go | 3 ++- internal/wrappers/learn-more-http.go | 3 ++- internal/wrappers/logs-http.go | 3 ++- internal/wrappers/pr-http.go | 3 ++- internal/wrappers/predicates-http.go | 3 ++- internal/wrappers/projects-http.go | 17 +++++++++-------- internal/wrappers/response.go | 3 ++- internal/wrappers/results-http.go | 5 +++-- internal/wrappers/risks-overview-http.go | 3 ++- internal/wrappers/scans-http.go | 15 ++++++++------- internal/wrappers/utils/utils.go | 8 ++++++++ 18 files changed, 59 insertions(+), 35 deletions(-) diff --git a/internal/wrappers/azure-http.go b/internal/wrappers/azure-http.go index 48d489693..0609075a9 100644 --- a/internal/wrappers/azure-http.go +++ b/internal/wrappers/azure-http.go @@ -3,6 +3,7 @@ package wrappers import ( "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" "reflect" @@ -116,7 +117,7 @@ func (g *AzureHTTPWrapper) get( if err != nil { return false, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) logger.PrintResponse(resp, true) diff --git a/internal/wrappers/bfl-http.go b/internal/wrappers/bfl-http.go index b1c223b91..a5fe8fc6c 100644 --- a/internal/wrappers/bfl-http.go +++ b/internal/wrappers/bfl-http.go @@ -3,6 +3,7 @@ package wrappers import ( "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "log" "net/http" @@ -36,7 +37,7 @@ func (r *BflHTTPWrapper) GetBflByScanIDAndQueryID(params map[string]string) ( if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleBflResponseWithBody(resp, err) } diff --git a/internal/wrappers/bitbucket-http.go b/internal/wrappers/bitbucket-http.go index f840f76c2..be4afd910 100644 --- a/internal/wrappers/bitbucket-http.go +++ b/internal/wrappers/bitbucket-http.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" "strconv" @@ -154,7 +155,7 @@ func (g *BitBucketHTTPWrapper) getFromBitBucket( if err != nil { return err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) switch resp.StatusCode { case http.StatusOK: err = json.NewDecoder(resp.Body).Decode(target) @@ -264,7 +265,7 @@ func getBitBucket(client *http.Client, token, url string, target interface{}, qu if err != nil { return err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) switch resp.StatusCode { case http.StatusOK: diff --git a/internal/wrappers/codebashing-http.go b/internal/wrappers/codebashing-http.go index f125e54b5..1a4c04d4a 100644 --- a/internal/wrappers/codebashing-http.go +++ b/internal/wrappers/codebashing-http.go @@ -44,7 +44,7 @@ func (r *CodeBashingHTTPWrapper) GetCodeBashingLinks(params map[string]string, c if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) decoder := json.NewDecoder(resp.Body) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/feature-flags-http.go b/internal/wrappers/feature-flags-http.go index cb037c4a5..c56ca1a59 100644 --- a/internal/wrappers/feature-flags-http.go +++ b/internal/wrappers/feature-flags-http.go @@ -2,6 +2,7 @@ package wrappers import ( "encoding/json" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "strings" @@ -42,7 +43,7 @@ func (f FeatureFlagsHTTPWrapper) GetAll() (*FeatureFlagsResponseModel, error) { } decoder := json.NewDecoder(resp.Body) - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/github-http.go b/internal/wrappers/github-http.go index 575b6c74f..ba6e0220d 100644 --- a/internal/wrappers/github-http.go +++ b/internal/wrappers/github-http.go @@ -3,6 +3,7 @@ package wrappers import ( "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" "strings" @@ -163,7 +164,7 @@ func (g *GitHubHTTPWrapper) getTemplates() error { func (g *GitHubHTTPWrapper) get(url string, target interface{}) error { resp, err := get(g.client, url, target, map[string]string{}) if err != nil { - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) } return err } @@ -205,7 +206,7 @@ func collectPage( return "", err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) *pageCollection = append(*pageCollection, holder...) next := getNextPageLink(resp) @@ -240,7 +241,7 @@ func get(client *http.Client, url string, target interface{}, queryParams map[st if err != nil { return nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) logger.PrintResponse(resp, true) switch resp.StatusCode { diff --git a/internal/wrappers/gitlab-http.go b/internal/wrappers/gitlab-http.go index 2f90f31c2..6a8fe54a0 100644 --- a/internal/wrappers/gitlab-http.go +++ b/internal/wrappers/gitlab-http.go @@ -3,6 +3,7 @@ package wrappers import ( "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" "net/url" @@ -141,7 +142,7 @@ func getFromGitLab( if err != nil { return nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) logger.PrintResponse(resp, true) @@ -200,7 +201,7 @@ func collectPageForGitLab( if err != nil { return "", err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) *pageCollection = append(*pageCollection, holder...) nextPageURL := getNextPage(resp) diff --git a/internal/wrappers/groups-http.go b/internal/wrappers/groups-http.go index a7bdfbdc2..e9df780b1 100644 --- a/internal/wrappers/groups-http.go +++ b/internal/wrappers/groups-http.go @@ -2,6 +2,7 @@ package wrappers import ( "encoding/json" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "strings" @@ -37,7 +38,7 @@ func (g *GroupsHTTPWrapper) Get(groupName string) ([]Group, error) { if err != nil { return nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) decoder := json.NewDecoder(resp.Body) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/learn-more-http.go b/internal/wrappers/learn-more-http.go index 3d2a16060..e0f75e984 100644 --- a/internal/wrappers/learn-more-http.go +++ b/internal/wrappers/learn-more-http.go @@ -3,6 +3,7 @@ package wrappers import ( "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" @@ -35,7 +36,7 @@ func (r *LearnMoreHTTPWrapper) GetLearnMoreDetails(params map[string]string) ( if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleResponse(resp, err, params[commonParams.QueryIDQueryParam]) } diff --git a/internal/wrappers/logs-http.go b/internal/wrappers/logs-http.go index b94d2b474..155b574ef 100644 --- a/internal/wrappers/logs-http.go +++ b/internal/wrappers/logs-http.go @@ -3,6 +3,7 @@ package wrappers import ( "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io/ioutil" "net/http" @@ -32,7 +33,7 @@ func (l *LogsHTTPWrapper) GetLog(scanID, scanType string) (string, error) { if err != nil { return "", err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) decoder := json.NewDecoder(resp.Body) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/pr-http.go b/internal/wrappers/pr-http.go index 2dbd0d512..f57529f1d 100644 --- a/internal/wrappers/pr-http.go +++ b/internal/wrappers/pr-http.go @@ -3,6 +3,7 @@ package wrappers import ( "bytes" "encoding/json" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" @@ -39,7 +40,7 @@ func (r *PRHTTPWrapper) PostPRDecoration(model *PRModel) ( if err != nil { return "", nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handlePRResponseWithBody(resp, err) } diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index 882fe6f7b..e17fb1aef 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "strings" @@ -51,7 +52,7 @@ func (r *ResultsPredicatesHTTPWrapper) GetAllPredicatesForSimilarityID(similarit if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleResponseWithBody(resp, err) } diff --git a/internal/wrappers/projects-http.go b/internal/wrappers/projects-http.go index d369bf55c..9c80d942b 100644 --- a/internal/wrappers/projects-http.go +++ b/internal/wrappers/projects-http.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "github.com/pkg/errors" @@ -33,7 +34,7 @@ func (p *ProjectsHTTPWrapper) Create(model *Project) (*ProjectResponseModel, *Er if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleProjectResponseWithBody(resp, err, http.StatusCreated) } @@ -48,7 +49,7 @@ func (p *ProjectsHTTPWrapper) Update(projectID string, model *Project) error { if err != nil { return err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) switch resp.StatusCode { case http.StatusNoContent: return nil @@ -72,7 +73,7 @@ func (p *ProjectsHTTPWrapper) UpdateConfiguration(projectID string, configuratio if err != nil { return nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleProjectResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -91,7 +92,7 @@ func (p *ProjectsHTTPWrapper) Get(params map[string]string) ( } decoder := json.NewDecoder(resp.Body) - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: errorModel := ErrorModel{} @@ -122,7 +123,7 @@ func (p *ProjectsHTTPWrapper) GetByID(projectID string) ( if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleProjectResponseWithBody(resp, err, http.StatusOK) } @@ -138,7 +139,7 @@ func (p *ProjectsHTTPWrapper) GetBranchesByID(projectID string, params map[strin } decoder := json.NewDecoder(resp.Body) - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -167,7 +168,7 @@ func (p *ProjectsHTTPWrapper) Delete(projectID string) (*ErrorModel, error) { if err != nil { return nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleProjectResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -180,7 +181,7 @@ func (p *ProjectsHTTPWrapper) Tags() ( if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) decoder := json.NewDecoder(resp.Body) diff --git a/internal/wrappers/response.go b/internal/wrappers/response.go index 30b347a6c..52f2f76fb 100644 --- a/internal/wrappers/response.go +++ b/internal/wrappers/response.go @@ -2,6 +2,7 @@ package wrappers import ( "encoding/json" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "github.com/pkg/errors" @@ -94,7 +95,7 @@ func handleProjectResponseWithBody(resp *http.Response, err error, } decoder := json.NewDecoder(resp.Body) - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/results-http.go b/internal/wrappers/results-http.go index d635abffd..d6bb70c19 100644 --- a/internal/wrappers/results-http.go +++ b/internal/wrappers/results-http.go @@ -3,6 +3,7 @@ package wrappers import ( "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "github.com/checkmarx/ast-cli/internal/logger" @@ -140,7 +141,7 @@ func (r *ResultsHTTPWrapper) GetAllResultsTypeByScanID(params map[string]string) return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) decoder := json.NewDecoder(resp.Body) @@ -192,7 +193,7 @@ func (r *ResultsHTTPWrapper) GetScanSummariesByScanIDS(params map[string]string) return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) decoder := json.NewDecoder(resp.Body) diff --git a/internal/wrappers/risks-overview-http.go b/internal/wrappers/risks-overview-http.go index f2e2dfb19..37d8d9387 100644 --- a/internal/wrappers/risks-overview-http.go +++ b/internal/wrappers/risks-overview-http.go @@ -3,6 +3,7 @@ package wrappers import ( "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" @@ -32,7 +33,7 @@ func (r *RisksOverviewHTTPWrapper) GetAllAPISecRisksByScanID(scanID string) ( return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) decoder := json.NewDecoder(resp.Body) switch resp.StatusCode { diff --git a/internal/wrappers/scans-http.go b/internal/wrappers/scans-http.go index 5fe00946a..56b43c373 100644 --- a/internal/wrappers/scans-http.go +++ b/internal/wrappers/scans-http.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" @@ -39,7 +40,7 @@ func (s *ScansHTTPWrapper) Create(model *Scan) (*ScanResponseModel, *ErrorModel, if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleScanResponseWithBody(resp, err, http.StatusCreated) } @@ -51,7 +52,7 @@ func (s *ScansHTTPWrapper) Get(params map[string]string) (*ScansCollectionRespon } decoder := json.NewDecoder(resp.Body) - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -81,7 +82,7 @@ func (s *ScansHTTPWrapper) GetByID(scanID string) (*ScanResponseModel, *ErrorMod if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleScanResponseWithBody(resp, err, http.StatusOK) } @@ -92,7 +93,7 @@ func (s *ScansHTTPWrapper) GetWorkflowByID(scanID string) ([]*ScanTaskResponseMo if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleWorkflowResponseWithBody(resp, err) } @@ -129,7 +130,7 @@ func (s *ScansHTTPWrapper) Delete(scanID string) (*ErrorModel, error) { if err != nil { return nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleScanResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -146,7 +147,7 @@ func (s *ScansHTTPWrapper) Cancel(scanID string) (*ErrorModel, error) { if err != nil { return nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) return handleScanResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -156,7 +157,7 @@ func (s *ScansHTTPWrapper) Tags() (map[string][]string, *ErrorModel, error) { if err != nil { return nil, nil, err } - defer resp.Body.Close() + defer utils.CloseHTTPResponseBody(resp) decoder := json.NewDecoder(resp.Body) switch resp.StatusCode { diff --git a/internal/wrappers/utils/utils.go b/internal/wrappers/utils/utils.go index f4e88b18c..f58dc21e4 100644 --- a/internal/wrappers/utils/utils.go +++ b/internal/wrappers/utils/utils.go @@ -1,6 +1,7 @@ package utils import ( + "net/http" "net/url" "path" "strings" @@ -37,3 +38,10 @@ func ToStringArray(obj interface{}) []string { return []string{} } } + +// CloseHTTPResponseBody closes the response body, use it with defer +func CloseHTTPResponseBody(resp *http.Response) { + if resp != nil { + resp.Body.Close() + } +} From b3d39d9601b36b2aadbe38684ee3e28ac5f8a925 Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Wed, 11 Oct 2023 16:57:14 +0100 Subject: [PATCH 02/12] preventing nil pointer when body closing --- internal/wrappers/github-http.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/wrappers/github-http.go b/internal/wrappers/github-http.go index ba6e0220d..0c66fbd0a 100644 --- a/internal/wrappers/github-http.go +++ b/internal/wrappers/github-http.go @@ -253,7 +253,7 @@ func get(client *http.Client, url string, target interface{}, queryParams map[st } case http.StatusConflict: logger.PrintIfVerbose(fmt.Sprintf("Found empty repository in %s", req.URL)) - return nil, nil + return resp, nil default: body, err := io.ReadAll(resp.Body) if err != nil { From 33bfb12f12149dbb48f44fcec48b561c2d7d6cb3 Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Wed, 11 Oct 2023 17:07:54 +0100 Subject: [PATCH 03/12] go imports --- go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.sum b/go.sum index 96abdd6ec..729459ecb 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,6 @@ github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvq github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/gomarkdown/markdown v0.0.0-20230916125811-7478c230c7cd h1:laCEzrtkKEkT2424vMTGl6N1m0xN8kq371hksD5Be+8= -github.com/gomarkdown/markdown v0.0.0-20230916125811-7478c230c7cd/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 h1:EcQR3gusLHN46TAD+G+EbaaqJArt5vHhNpXAa12PQf4= github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= From 71322e0761577824848af7ce7b6f38f983706d0e Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Wed, 11 Oct 2023 17:15:14 +0100 Subject: [PATCH 04/12] go imports --- internal/wrappers/azure-http.go | 2 +- internal/wrappers/gitlab-http.go | 2 +- internal/wrappers/utils/utils.go | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/wrappers/azure-http.go b/internal/wrappers/azure-http.go index 0609075a9..51fe69b3a 100644 --- a/internal/wrappers/azure-http.go +++ b/internal/wrappers/azure-http.go @@ -3,7 +3,6 @@ package wrappers import ( "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" "reflect" @@ -13,6 +12,7 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) diff --git a/internal/wrappers/gitlab-http.go b/internal/wrappers/gitlab-http.go index 6a8fe54a0..1e572ccd7 100644 --- a/internal/wrappers/gitlab-http.go +++ b/internal/wrappers/gitlab-http.go @@ -3,13 +3,13 @@ package wrappers import ( "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" "net/url" "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" "github.com/tomnomnom/linkheader" diff --git a/internal/wrappers/utils/utils.go b/internal/wrappers/utils/utils.go index f58dc21e4..a65835741 100644 --- a/internal/wrappers/utils/utils.go +++ b/internal/wrappers/utils/utils.go @@ -39,7 +39,6 @@ func ToStringArray(obj interface{}) []string { } } -// CloseHTTPResponseBody closes the response body, use it with defer func CloseHTTPResponseBody(resp *http.Response) { if resp != nil { resp.Body.Close() From 6a49afb3c1917c196cec3e069e264f2b191afa76 Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Wed, 11 Oct 2023 17:20:01 +0100 Subject: [PATCH 05/12] go imports --- internal/wrappers/azure-http.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/wrappers/azure-http.go b/internal/wrappers/azure-http.go index 51fe69b3a..0609075a9 100644 --- a/internal/wrappers/azure-http.go +++ b/internal/wrappers/azure-http.go @@ -3,6 +3,7 @@ package wrappers import ( "encoding/json" "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" "reflect" @@ -12,7 +13,6 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) From 556194829d19ab9dd9287b2a2f6e512017bd2470 Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Wed, 11 Oct 2023 18:12:10 +0100 Subject: [PATCH 06/12] linter --- internal/wrappers/azure-http.go | 6 ++---- internal/wrappers/bfl-http.go | 2 +- internal/wrappers/github-http.go | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/wrappers/azure-http.go b/internal/wrappers/azure-http.go index 0609075a9..b10b46e8a 100644 --- a/internal/wrappers/azure-http.go +++ b/internal/wrappers/azure-http.go @@ -1,18 +1,17 @@ package wrappers import ( + b64 "encoding/base64" "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" "reflect" "time" - b64 "encoding/base64" - "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -112,7 +111,6 @@ func (g *AzureHTTPWrapper) get( queryParams map[string]string, authFormat string, ) (bool, error) { - resp, err := GetWithQueryParams(g.client, url, token, authFormat, queryParams) if err != nil { return false, err diff --git a/internal/wrappers/bfl-http.go b/internal/wrappers/bfl-http.go index a5fe8fc6c..70e07e68f 100644 --- a/internal/wrappers/bfl-http.go +++ b/internal/wrappers/bfl-http.go @@ -3,11 +3,11 @@ package wrappers import ( "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "log" "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) diff --git a/internal/wrappers/github-http.go b/internal/wrappers/github-http.go index 0c66fbd0a..41ee19a3e 100644 --- a/internal/wrappers/github-http.go +++ b/internal/wrappers/github-http.go @@ -3,13 +3,13 @@ package wrappers import ( "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" "strings" "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" "github.com/tomnomnom/linkheader" From adcf96811c855df00df8d0ec1ff52bfd248f2fe9 Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Wed, 11 Oct 2023 18:22:51 +0100 Subject: [PATCH 07/12] linter --- internal/wrappers/azure-http.go | 5 ++-- internal/wrappers/bfl-http.go | 5 ++-- internal/wrappers/bitbucket-http.go | 9 ++++--- internal/wrappers/codebashing-http.go | 4 ++- internal/wrappers/feature-flags-http.go | 5 ++-- internal/wrappers/github-http.go | 13 +++++++--- internal/wrappers/gitlab-http.go | 9 ++++--- internal/wrappers/groups-http.go | 5 ++-- internal/wrappers/learn-more-http.go | 5 ++-- internal/wrappers/logs-http.go | 5 ++-- internal/wrappers/pr-http.go | 5 ++-- internal/wrappers/predicates-http.go | 5 ++-- internal/wrappers/projects-http.go | 33 +++++++++++++++++------- internal/wrappers/response.go | 5 ++-- internal/wrappers/results-http.go | 9 ++++--- internal/wrappers/risks-overview-http.go | 5 ++-- internal/wrappers/scans-http.go | 29 +++++++++++++++------ internal/wrappers/utils/utils.go | 7 ----- 18 files changed, 105 insertions(+), 58 deletions(-) diff --git a/internal/wrappers/azure-http.go b/internal/wrappers/azure-http.go index b10b46e8a..b802d10bd 100644 --- a/internal/wrappers/azure-http.go +++ b/internal/wrappers/azure-http.go @@ -11,7 +11,6 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -115,7 +114,9 @@ func (g *AzureHTTPWrapper) get( if err != nil { return false, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } logger.PrintResponse(resp, true) diff --git a/internal/wrappers/bfl-http.go b/internal/wrappers/bfl-http.go index 70e07e68f..4e1524405 100644 --- a/internal/wrappers/bfl-http.go +++ b/internal/wrappers/bfl-http.go @@ -7,7 +7,6 @@ import ( "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -37,7 +36,9 @@ func (r *BflHTTPWrapper) GetBflByScanIDAndQueryID(params map[string]string) ( if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleBflResponseWithBody(resp, err) } diff --git a/internal/wrappers/bitbucket-http.go b/internal/wrappers/bitbucket-http.go index be4afd910..9ef8e3496 100644 --- a/internal/wrappers/bitbucket-http.go +++ b/internal/wrappers/bitbucket-http.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" "strconv" @@ -155,7 +154,9 @@ func (g *BitBucketHTTPWrapper) getFromBitBucket( if err != nil { return err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusOK: err = json.NewDecoder(resp.Body).Decode(target) @@ -265,7 +266,9 @@ func getBitBucket(client *http.Client, token, url string, target interface{}, qu if err != nil { return err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusOK: diff --git a/internal/wrappers/codebashing-http.go b/internal/wrappers/codebashing-http.go index 1a4c04d4a..af310d722 100644 --- a/internal/wrappers/codebashing-http.go +++ b/internal/wrappers/codebashing-http.go @@ -44,7 +44,9 @@ func (r *CodeBashingHTTPWrapper) GetCodeBashingLinks(params map[string]string, c if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } decoder := json.NewDecoder(resp.Body) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/feature-flags-http.go b/internal/wrappers/feature-flags-http.go index c56ca1a59..bd9c17dc5 100644 --- a/internal/wrappers/feature-flags-http.go +++ b/internal/wrappers/feature-flags-http.go @@ -2,7 +2,6 @@ package wrappers import ( "encoding/json" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "strings" @@ -43,7 +42,9 @@ func (f FeatureFlagsHTTPWrapper) GetAll() (*FeatureFlagsResponseModel, error) { } decoder := json.NewDecoder(resp.Body) - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/github-http.go b/internal/wrappers/github-http.go index 41ee19a3e..2c783690a 100644 --- a/internal/wrappers/github-http.go +++ b/internal/wrappers/github-http.go @@ -9,7 +9,6 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" "github.com/tomnomnom/linkheader" @@ -164,7 +163,9 @@ func (g *GitHubHTTPWrapper) getTemplates() error { func (g *GitHubHTTPWrapper) get(url string, target interface{}) error { resp, err := get(g.client, url, target, map[string]string{}) if err != nil { - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } } return err } @@ -206,7 +207,9 @@ func collectPage( return "", err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } *pageCollection = append(*pageCollection, holder...) next := getNextPageLink(resp) @@ -241,7 +244,9 @@ func get(client *http.Client, url string, target interface{}, queryParams map[st if err != nil { return nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } logger.PrintResponse(resp, true) switch resp.StatusCode { diff --git a/internal/wrappers/gitlab-http.go b/internal/wrappers/gitlab-http.go index 1e572ccd7..e58695649 100644 --- a/internal/wrappers/gitlab-http.go +++ b/internal/wrappers/gitlab-http.go @@ -9,7 +9,6 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" "github.com/tomnomnom/linkheader" @@ -142,7 +141,9 @@ func getFromGitLab( if err != nil { return nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } logger.PrintResponse(resp, true) @@ -201,7 +202,9 @@ func collectPageForGitLab( if err != nil { return "", err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } *pageCollection = append(*pageCollection, holder...) nextPageURL := getNextPage(resp) diff --git a/internal/wrappers/groups-http.go b/internal/wrappers/groups-http.go index e9df780b1..41c04930e 100644 --- a/internal/wrappers/groups-http.go +++ b/internal/wrappers/groups-http.go @@ -2,7 +2,6 @@ package wrappers import ( "encoding/json" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "strings" @@ -38,7 +37,9 @@ func (g *GroupsHTTPWrapper) Get(groupName string) ([]Group, error) { if err != nil { return nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } decoder := json.NewDecoder(resp.Body) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/learn-more-http.go b/internal/wrappers/learn-more-http.go index e0f75e984..fda4c2c13 100644 --- a/internal/wrappers/learn-more-http.go +++ b/internal/wrappers/learn-more-http.go @@ -3,7 +3,6 @@ package wrappers import ( "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" @@ -36,7 +35,9 @@ func (r *LearnMoreHTTPWrapper) GetLearnMoreDetails(params map[string]string) ( if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleResponse(resp, err, params[commonParams.QueryIDQueryParam]) } diff --git a/internal/wrappers/logs-http.go b/internal/wrappers/logs-http.go index 155b574ef..4bb76f39b 100644 --- a/internal/wrappers/logs-http.go +++ b/internal/wrappers/logs-http.go @@ -3,7 +3,6 @@ package wrappers import ( "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io/ioutil" "net/http" @@ -33,7 +32,9 @@ func (l *LogsHTTPWrapper) GetLog(scanID, scanType string) (string, error) { if err != nil { return "", err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } decoder := json.NewDecoder(resp.Body) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/pr-http.go b/internal/wrappers/pr-http.go index f57529f1d..11faf2ec4 100644 --- a/internal/wrappers/pr-http.go +++ b/internal/wrappers/pr-http.go @@ -3,7 +3,6 @@ package wrappers import ( "bytes" "encoding/json" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "io" "net/http" @@ -40,7 +39,9 @@ func (r *PRHTTPWrapper) PostPRDecoration(model *PRModel) ( if err != nil { return "", nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handlePRResponseWithBody(resp, err) } diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index e17fb1aef..0aacf9cfd 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "strings" @@ -52,7 +51,9 @@ func (r *ResultsPredicatesHTTPWrapper) GetAllPredicatesForSimilarityID(similarit if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleResponseWithBody(resp, err) } diff --git a/internal/wrappers/projects-http.go b/internal/wrappers/projects-http.go index 9c80d942b..4cde29dba 100644 --- a/internal/wrappers/projects-http.go +++ b/internal/wrappers/projects-http.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "github.com/pkg/errors" @@ -34,7 +33,9 @@ func (p *ProjectsHTTPWrapper) Create(model *Project) (*ProjectResponseModel, *Er if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleProjectResponseWithBody(resp, err, http.StatusCreated) } @@ -49,7 +50,9 @@ func (p *ProjectsHTTPWrapper) Update(projectID string, model *Project) error { if err != nil { return err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusNoContent: return nil @@ -73,7 +76,9 @@ func (p *ProjectsHTTPWrapper) UpdateConfiguration(projectID string, configuratio if err != nil { return nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleProjectResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -92,7 +97,9 @@ func (p *ProjectsHTTPWrapper) Get(params map[string]string) ( } decoder := json.NewDecoder(resp.Body) - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: errorModel := ErrorModel{} @@ -123,7 +130,9 @@ func (p *ProjectsHTTPWrapper) GetByID(projectID string) ( if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleProjectResponseWithBody(resp, err, http.StatusOK) } @@ -139,7 +148,9 @@ func (p *ProjectsHTTPWrapper) GetBranchesByID(projectID string, params map[strin } decoder := json.NewDecoder(resp.Body) - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -168,7 +179,9 @@ func (p *ProjectsHTTPWrapper) Delete(projectID string) (*ErrorModel, error) { if err != nil { return nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleProjectResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -181,7 +194,9 @@ func (p *ProjectsHTTPWrapper) Tags() ( if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } decoder := json.NewDecoder(resp.Body) diff --git a/internal/wrappers/response.go b/internal/wrappers/response.go index 52f2f76fb..4ae029619 100644 --- a/internal/wrappers/response.go +++ b/internal/wrappers/response.go @@ -2,7 +2,6 @@ package wrappers import ( "encoding/json" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "github.com/pkg/errors" @@ -95,7 +94,9 @@ func handleProjectResponseWithBody(resp *http.Response, err error, } decoder := json.NewDecoder(resp.Body) - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/results-http.go b/internal/wrappers/results-http.go index d6bb70c19..ccc9b29a4 100644 --- a/internal/wrappers/results-http.go +++ b/internal/wrappers/results-http.go @@ -3,7 +3,6 @@ package wrappers import ( "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" "github.com/checkmarx/ast-cli/internal/logger" @@ -141,7 +140,9 @@ func (r *ResultsHTTPWrapper) GetAllResultsTypeByScanID(params map[string]string) return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } decoder := json.NewDecoder(resp.Body) @@ -193,7 +194,9 @@ func (r *ResultsHTTPWrapper) GetScanSummariesByScanIDS(params map[string]string) return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } decoder := json.NewDecoder(resp.Body) diff --git a/internal/wrappers/risks-overview-http.go b/internal/wrappers/risks-overview-http.go index 37d8d9387..1044f7d89 100644 --- a/internal/wrappers/risks-overview-http.go +++ b/internal/wrappers/risks-overview-http.go @@ -3,7 +3,6 @@ package wrappers import ( "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" @@ -33,7 +32,9 @@ func (r *RisksOverviewHTTPWrapper) GetAllAPISecRisksByScanID(scanID string) ( return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } decoder := json.NewDecoder(resp.Body) switch resp.StatusCode { diff --git a/internal/wrappers/scans-http.go b/internal/wrappers/scans-http.go index 56b43c373..91b134da6 100644 --- a/internal/wrappers/scans-http.go +++ b/internal/wrappers/scans-http.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers/utils" "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" @@ -40,7 +39,9 @@ func (s *ScansHTTPWrapper) Create(model *Scan) (*ScanResponseModel, *ErrorModel, if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleScanResponseWithBody(resp, err, http.StatusCreated) } @@ -52,7 +53,9 @@ func (s *ScansHTTPWrapper) Get(params map[string]string) (*ScansCollectionRespon } decoder := json.NewDecoder(resp.Body) - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -82,7 +85,9 @@ func (s *ScansHTTPWrapper) GetByID(scanID string) (*ScanResponseModel, *ErrorMod if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleScanResponseWithBody(resp, err, http.StatusOK) } @@ -93,7 +98,9 @@ func (s *ScansHTTPWrapper) GetWorkflowByID(scanID string) ([]*ScanTaskResponseMo if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleWorkflowResponseWithBody(resp, err) } @@ -130,7 +137,9 @@ func (s *ScansHTTPWrapper) Delete(scanID string) (*ErrorModel, error) { if err != nil { return nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleScanResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -147,7 +156,9 @@ func (s *ScansHTTPWrapper) Cancel(scanID string) (*ErrorModel, error) { if err != nil { return nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } return handleScanResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -157,7 +168,9 @@ func (s *ScansHTTPWrapper) Tags() (map[string][]string, *ErrorModel, error) { if err != nil { return nil, nil, err } - defer utils.CloseHTTPResponseBody(resp) + if resp != nil { + defer resp.Body.Close() + } decoder := json.NewDecoder(resp.Body) switch resp.StatusCode { diff --git a/internal/wrappers/utils/utils.go b/internal/wrappers/utils/utils.go index a65835741..f4e88b18c 100644 --- a/internal/wrappers/utils/utils.go +++ b/internal/wrappers/utils/utils.go @@ -1,7 +1,6 @@ package utils import ( - "net/http" "net/url" "path" "strings" @@ -38,9 +37,3 @@ func ToStringArray(obj interface{}) []string { return []string{} } } - -func CloseHTTPResponseBody(resp *http.Response) { - if resp != nil { - resp.Body.Close() - } -} From bffc92a5ca9b713db50ba64b7f6bc960bafd581a Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Wed, 11 Oct 2023 18:37:01 +0100 Subject: [PATCH 08/12] preventing more nil pointers --- internal/wrappers/bfl-http.go | 6 +++++- internal/wrappers/codebashing-http.go | 6 +++++- internal/wrappers/feature-flags-http.go | 6 +++++- internal/wrappers/gitlab-http.go | 2 -- internal/wrappers/groups-http.go | 6 +++++- internal/wrappers/learn-more-http.go | 6 +++++- internal/wrappers/logs-http.go | 6 +++++- internal/wrappers/policy-http.go | 6 +++++- internal/wrappers/pr-http.go | 6 +++++- internal/wrappers/predicates-http.go | 6 +++++- internal/wrappers/projects-http.go | 18 +++++++++++++++--- internal/wrappers/response.go | 24 ++++++++++++++++++++---- internal/wrappers/results-http.go | 24 ++++++++++++++++++++---- internal/wrappers/results-pdf-http.go | 12 ++++++++++-- internal/wrappers/results-sbom-http.go | 12 ++++++++++-- internal/wrappers/risks-overview-http.go | 6 +++++- internal/wrappers/sca-realtime-http.go | 6 +++++- internal/wrappers/scans-http.go | 18 +++++++++++++++--- internal/wrappers/tenant-http.go | 6 +++++- internal/wrappers/uploads-http.go | 6 +++++- 20 files changed, 155 insertions(+), 33 deletions(-) diff --git a/internal/wrappers/bfl-http.go b/internal/wrappers/bfl-http.go index 4e1524405..b6924dfc1 100644 --- a/internal/wrappers/bfl-http.go +++ b/internal/wrappers/bfl-http.go @@ -47,7 +47,11 @@ func handleBflResponseWithBody(resp *http.Response, err error) (*BFLResponseMode return nil, nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/codebashing-http.go b/internal/wrappers/codebashing-http.go index af310d722..24933aa87 100644 --- a/internal/wrappers/codebashing-http.go +++ b/internal/wrappers/codebashing-http.go @@ -47,7 +47,11 @@ func (r *CodeBashingHTTPWrapper) GetCodeBashingLinks(params map[string]string, c if resp != nil { defer resp.Body.Close() } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: errorModel := WebError{} diff --git a/internal/wrappers/feature-flags-http.go b/internal/wrappers/feature-flags-http.go index bd9c17dc5..a690cc32a 100644 --- a/internal/wrappers/feature-flags-http.go +++ b/internal/wrappers/feature-flags-http.go @@ -40,7 +40,11 @@ func (f FeatureFlagsHTTPWrapper) GetAll() (*FeatureFlagsResponseModel, error) { if err != nil { return nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } if resp != nil { defer resp.Body.Close() diff --git a/internal/wrappers/gitlab-http.go b/internal/wrappers/gitlab-http.go index e58695649..12f47df81 100644 --- a/internal/wrappers/gitlab-http.go +++ b/internal/wrappers/gitlab-http.go @@ -163,8 +163,6 @@ func getFromGitLab( return nil, errors.New(message) } return resp, nil - - return nil, err } func fetchWithPagination( diff --git a/internal/wrappers/groups-http.go b/internal/wrappers/groups-http.go index 41c04930e..b4558c713 100644 --- a/internal/wrappers/groups-http.go +++ b/internal/wrappers/groups-http.go @@ -40,7 +40,11 @@ func (g *GroupsHTTPWrapper) Get(groupName string) ([]Group, error) { if resp != nil { defer resp.Body.Close() } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: errorMsg := ErrorMsg{} diff --git a/internal/wrappers/learn-more-http.go b/internal/wrappers/learn-more-http.go index fda4c2c13..c3e872a30 100644 --- a/internal/wrappers/learn-more-http.go +++ b/internal/wrappers/learn-more-http.go @@ -46,7 +46,11 @@ func handleResponse(resp *http.Response, err error, queryID string) (*[]*LearnMo return nil, nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/logs-http.go b/internal/wrappers/logs-http.go index 4bb76f39b..f3db8b72b 100644 --- a/internal/wrappers/logs-http.go +++ b/internal/wrappers/logs-http.go @@ -35,7 +35,11 @@ func (l *LogsHTTPWrapper) GetLog(scanID, scanType string) (string, error) { if resp != nil { defer resp.Body.Close() } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: errorModel := &WebError{} diff --git a/internal/wrappers/policy-http.go b/internal/wrappers/policy-http.go index 4f2a3ad78..ff7e99f4e 100644 --- a/internal/wrappers/policy-http.go +++ b/internal/wrappers/policy-http.go @@ -40,7 +40,11 @@ func (r *PolicyHTTPWrapper) EvaluatePolicy(params map[string]string) ( _ = resp.Body.Close() }() - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/pr-http.go b/internal/wrappers/pr-http.go index 11faf2ec4..2612fcab8 100644 --- a/internal/wrappers/pr-http.go +++ b/internal/wrappers/pr-http.go @@ -50,7 +50,11 @@ func handlePRResponseWithBody(resp *http.Response, err error) (string, *WebError return "", nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } defer func() { _ = resp.Body.Close() diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index 0aacf9cfd..5f1c0f775 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -120,7 +120,11 @@ func handleResponseWithBody(resp *http.Response, err error) (*PredicatesCollecti logger.PrintIfVerbose(fmt.Sprintf("Response : %s", resp.Status)) - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } defer func() { _ = resp.Body.Close() diff --git a/internal/wrappers/projects-http.go b/internal/wrappers/projects-http.go index 4cde29dba..9b66b4716 100644 --- a/internal/wrappers/projects-http.go +++ b/internal/wrappers/projects-http.go @@ -95,7 +95,11 @@ func (p *ProjectsHTTPWrapper) Get(params map[string]string) ( if err != nil { return nil, nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } if resp != nil { defer resp.Body.Close() @@ -147,7 +151,11 @@ func (p *ProjectsHTTPWrapper) GetBranchesByID(projectID string, params map[strin return nil, nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } if resp != nil { defer resp.Body.Close() } @@ -198,7 +206,11 @@ func (p *ProjectsHTTPWrapper) Tags() ( defer resp.Body.Close() } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/response.go b/internal/wrappers/response.go index 4ae029619..132f7ba89 100644 --- a/internal/wrappers/response.go +++ b/internal/wrappers/response.go @@ -16,7 +16,11 @@ func handleScanResponseWithNoBody(resp *http.Response, err error, if err != nil { return nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError, http.StatusNotFound: @@ -39,7 +43,11 @@ func handleScanResponseWithBody(resp *http.Response, err error, if err != nil { return nil, nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -69,7 +77,11 @@ func handleProjectResponseWithNoBody(resp *http.Response, err error, if err != nil { return nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -92,7 +104,11 @@ func handleProjectResponseWithBody(resp *http.Response, err error, if err != nil { return nil, nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } if resp != nil { defer resp.Body.Close() diff --git a/internal/wrappers/results-http.go b/internal/wrappers/results-http.go index ccc9b29a4..6664b9a13 100644 --- a/internal/wrappers/results-http.go +++ b/internal/wrappers/results-http.go @@ -50,7 +50,11 @@ func (r *ResultsHTTPWrapper) GetAllResultsByScanID(params map[string]string) ( _ = resp.Body.Close() }() - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -96,7 +100,11 @@ func (r *ResultsHTTPWrapper) GetAllResultsPackageByScanID(params map[string]stri _ = resp.Body.Close() }() - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -144,7 +152,11 @@ func (r *ResultsHTTPWrapper) GetAllResultsTypeByScanID(params map[string]string) defer resp.Body.Close() } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -198,7 +210,11 @@ func (r *ResultsHTTPWrapper) GetScanSummariesByScanIDS(params map[string]string) defer resp.Body.Close() } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/results-pdf-http.go b/internal/wrappers/results-pdf-http.go index 42c9133f4..e7cb20477 100644 --- a/internal/wrappers/results-pdf-http.go +++ b/internal/wrappers/results-pdf-http.go @@ -63,7 +63,11 @@ func (r *PdfHTTPWrapper) GeneratePdfReport(payload *PdfReportsPayload) (*PdfRepo _ = resp.Body.Close() }() - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusAccepted: @@ -91,7 +95,11 @@ func (r *PdfHTTPWrapper) CheckPdfReportStatus(reportID string) (*PdfPollingRespo _ = resp.Body.Close() }() - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusOK: diff --git a/internal/wrappers/results-sbom-http.go b/internal/wrappers/results-sbom-http.go index 3de1752da..1eb051b40 100644 --- a/internal/wrappers/results-sbom-http.go +++ b/internal/wrappers/results-sbom-http.go @@ -59,7 +59,11 @@ func (r *SbomHTTPWrapper) GenerateSbomReport(payload *SbomReportsPayload) (*Sbom switch resp.StatusCode { case http.StatusAccepted: model := SbomReportsResponse{} - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } err = decoder.Decode(&model) if err != nil { return nil, errors.Wrapf(err, "failed to parse response body") @@ -120,7 +124,11 @@ func (r *SbomHTTPWrapper) GetSbomReportStatus(reportID string) (*SbomPollingResp _ = resp.Body.Close() }() - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusOK: diff --git a/internal/wrappers/risks-overview-http.go b/internal/wrappers/risks-overview-http.go index 1044f7d89..affb1307a 100644 --- a/internal/wrappers/risks-overview-http.go +++ b/internal/wrappers/risks-overview-http.go @@ -35,7 +35,11 @@ func (r *RisksOverviewHTTPWrapper) GetAllAPISecRisksByScanID(scanID string) ( if resp != nil { defer resp.Body.Close() } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/sca-realtime-http.go b/internal/wrappers/sca-realtime-http.go index 9e3a5be3d..d085295fd 100644 --- a/internal/wrappers/sca-realtime-http.go +++ b/internal/wrappers/sca-realtime-http.go @@ -35,7 +35,11 @@ func (s ScaRealTimeHTTPWrapper) GetScaVulnerabilitiesPackages(scaRequest []ScaDe _ = resp.Body.Close() }() - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/scans-http.go b/internal/wrappers/scans-http.go index 91b134da6..d4b3681de 100644 --- a/internal/wrappers/scans-http.go +++ b/internal/wrappers/scans-http.go @@ -51,7 +51,11 @@ func (s *ScansHTTPWrapper) Get(params map[string]string) (*ScansCollectionRespon if err != nil { return nil, nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } if resp != nil { defer resp.Body.Close() @@ -108,7 +112,11 @@ func handleWorkflowResponseWithBody(resp *http.Response, err error) ([]*ScanTask if err != nil { return nil, nil, err } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -171,7 +179,11 @@ func (s *ScansHTTPWrapper) Tags() (map[string][]string, *ErrorModel, error) { if resp != nil { defer resp.Body.Close() } - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/tenant-http.go b/internal/wrappers/tenant-http.go index e605961d0..910342ed5 100644 --- a/internal/wrappers/tenant-http.go +++ b/internal/wrappers/tenant-http.go @@ -33,7 +33,11 @@ func (r *TenantConfigurationHTTPWrapper) GetTenantConfiguration() ( defer func() { _ = resp.Body.Close() }() - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/uploads-http.go b/internal/wrappers/uploads-http.go index 3191ead03..7c9e6eb2b 100644 --- a/internal/wrappers/uploads-http.go +++ b/internal/wrappers/uploads-http.go @@ -79,7 +79,11 @@ func (u *UploadsHTTPWrapper) getPresignedURLForUploading() (*string, error) { _ = resp.Body.Close() }() - decoder := json.NewDecoder(resp.Body) + var decoder *json.Decoder + if resp != nil { + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() + } switch resp.StatusCode { case http.StatusBadRequest: From 0a897f32c26d48723d79a481e40c2f580a3e37cd Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Thu, 12 Oct 2023 09:46:49 +0100 Subject: [PATCH 09/12] test --- go.mod | 14 +++++++++----- go.sum | 40 ++++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index bfc1481ca..2e9123ba3 100644 --- a/go.mod +++ b/go.mod @@ -13,9 +13,9 @@ require ( github.com/mssola/user_agent v0.6.0 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.7.0 - github.com/spf13/viper v1.16.0 + github.com/spf13/viper v1.17.0 github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 - golang.org/x/crypto v0.13.0 + golang.org/x/crypto v0.14.0 gotest.tools v2.2.0+incompatible ) @@ -27,13 +27,17 @@ require ( github.com/magiconair/properties v1.8.7 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect - github.com/spf13/afero v1.9.5 // indirect + github.com/sagikazarmark/locafero v0.3.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.10.0 // indirect github.com/spf13/cast v1.5.1 // indirect - github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect - golang.org/x/sys v0.12.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect + golang.org/x/sys v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 729459ecb..f248a1550 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,9 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -163,29 +164,33 @@ github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdU github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM= -github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= +github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9cJvm4SvQ= +github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY= +github.com/spf13/afero v1.10.0/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA= github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= -github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= -github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.16.0 h1:rGGH0XDZhdUOryiDWjmIvUSWpbNqisK8Wk0Vyefw8hc= -github.com/spf13/viper v1.16.0/go.mod h1:yg78JgCJcbrQOvV9YLXgkLaZqUidkY9K+Dd1FofRzQg= +github.com/spf13/viper v1.17.0 h1:I5txKw7MJasPL/BrfkbA0Jyo/oELqVmux4pR/UxOMfI= +github.com/spf13/viper v1.17.0/go.mod h1:BmMMMLQXSbcHK6KAOiFLz0l5JHrU89OdIRHvsk0+yVI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -209,6 +214,8 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -216,8 +223,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck= -golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -228,8 +235,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= -golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= -golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -340,8 +347,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -496,8 +503,9 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= From 481b75196cb8137a7d11ea36a5af7634716ae1bb Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Thu, 12 Oct 2023 10:46:51 +0100 Subject: [PATCH 10/12] linter --- internal/wrappers/predicates-http.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index 5f1c0f775..971e58a62 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -122,8 +122,10 @@ func handleResponseWithBody(resp *http.Response, err error) (*PredicatesCollecti var decoder *json.Decoder if resp != nil { - decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() + if resp.Body != nil { + defer resp.Body.Close() + decoder = json.NewDecoder(resp.Body) + } } defer func() { From c7fe0b32fc9fd1e9716c22e35b6bda37c8ff3128 Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Thu, 12 Oct 2023 10:53:26 +0100 Subject: [PATCH 11/12] linter --- internal/wrappers/predicates-http.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index 971e58a62..8e8ce0407 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -118,14 +118,11 @@ func handleResponseWithBody(resp *http.Response, err error) (*PredicatesCollecti return nil, nil, err } - logger.PrintIfVerbose(fmt.Sprintf("Response : %s", resp.Status)) - var decoder *json.Decoder if resp != nil { - if resp.Body != nil { - defer resp.Body.Close() - decoder = json.NewDecoder(resp.Body) - } + logger.PrintIfVerbose(fmt.Sprintf("Response : %s", resp.Status)) + decoder = json.NewDecoder(resp.Body) + defer resp.Body.Close() } defer func() { From 79322fe191003d16265e1e58ef31e0bb7bcdb7fa Mon Sep 17 00:00:00 2001 From: igorlombacx Date: Thu, 12 Oct 2023 14:06:54 +0100 Subject: [PATCH 12/12] testing CloseHTTPBody function --- internal/wrappers/azure-http.go | 5 ++-- internal/wrappers/bfl-http.go | 6 ++--- internal/wrappers/bitbucket-http.go | 9 +++---- internal/wrappers/codebashing-http.go | 5 +--- internal/wrappers/feature-flags-http.go | 6 ++--- internal/wrappers/github-http.go | 9 +++---- internal/wrappers/gitlab-http.go | 9 +++---- internal/wrappers/groups-http.go | 6 ++--- internal/wrappers/learn-more-http.go | 6 ++--- internal/wrappers/logs-http.go | 6 ++--- internal/wrappers/policy-http.go | 1 - internal/wrappers/pr-http.go | 6 ++--- internal/wrappers/predicates-http.go | 5 ++-- internal/wrappers/projects-http.go | 33 +++++++----------------- internal/wrappers/response.go | 9 ++----- internal/wrappers/results-http.go | 13 +++------- internal/wrappers/results-pdf-http.go | 2 -- internal/wrappers/results-sbom-http.go | 1 - internal/wrappers/risks-overview-http.go | 6 ++--- internal/wrappers/sca-realtime-http.go | 1 - internal/wrappers/scans-http.go | 32 ++++++----------------- internal/wrappers/tenant-http.go | 1 - internal/wrappers/uploads-http.go | 1 - internal/wrappers/utils/utils.go | 7 +++++ 24 files changed, 57 insertions(+), 128 deletions(-) diff --git a/internal/wrappers/azure-http.go b/internal/wrappers/azure-http.go index b802d10bd..88da1772f 100644 --- a/internal/wrappers/azure-http.go +++ b/internal/wrappers/azure-http.go @@ -11,6 +11,7 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -114,9 +115,7 @@ func (g *AzureHTTPWrapper) get( if err != nil { return false, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) logger.PrintResponse(resp, true) diff --git a/internal/wrappers/bfl-http.go b/internal/wrappers/bfl-http.go index b6924dfc1..ad0f6681c 100644 --- a/internal/wrappers/bfl-http.go +++ b/internal/wrappers/bfl-http.go @@ -7,6 +7,7 @@ import ( "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -36,9 +37,7 @@ func (r *BflHTTPWrapper) GetBflByScanIDAndQueryID(params map[string]string) ( if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleBflResponseWithBody(resp, err) } @@ -50,7 +49,6 @@ func handleBflResponseWithBody(resp *http.Response, err error) (*BFLResponseMode var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/bitbucket-http.go b/internal/wrappers/bitbucket-http.go index 9ef8e3496..a508db7ec 100644 --- a/internal/wrappers/bitbucket-http.go +++ b/internal/wrappers/bitbucket-http.go @@ -12,6 +12,7 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/spf13/viper" ) @@ -154,9 +155,7 @@ func (g *BitBucketHTTPWrapper) getFromBitBucket( if err != nil { return err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) switch resp.StatusCode { case http.StatusOK: err = json.NewDecoder(resp.Body).Decode(target) @@ -266,9 +265,7 @@ func getBitBucket(client *http.Client, token, url string, target interface{}, qu if err != nil { return err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) switch resp.StatusCode { case http.StatusOK: diff --git a/internal/wrappers/codebashing-http.go b/internal/wrappers/codebashing-http.go index 24933aa87..b09ad94f1 100644 --- a/internal/wrappers/codebashing-http.go +++ b/internal/wrappers/codebashing-http.go @@ -44,13 +44,10 @@ func (r *CodeBashingHTTPWrapper) GetCodeBashingLinks(params map[string]string, c if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/feature-flags-http.go b/internal/wrappers/feature-flags-http.go index a690cc32a..846308e8f 100644 --- a/internal/wrappers/feature-flags-http.go +++ b/internal/wrappers/feature-flags-http.go @@ -5,6 +5,7 @@ import ( "net/http" "strings" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/spf13/viper" commonParams "github.com/checkmarx/ast-cli/internal/params" @@ -43,12 +44,9 @@ func (f FeatureFlagsHTTPWrapper) GetAll() (*FeatureFlagsResponseModel, error) { var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/github-http.go b/internal/wrappers/github-http.go index 2c783690a..a9045b4f2 100644 --- a/internal/wrappers/github-http.go +++ b/internal/wrappers/github-http.go @@ -9,6 +9,7 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" "github.com/tomnomnom/linkheader" @@ -207,9 +208,7 @@ func collectPage( return "", err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) *pageCollection = append(*pageCollection, holder...) next := getNextPageLink(resp) @@ -244,9 +243,7 @@ func get(client *http.Client, url string, target interface{}, queryParams map[st if err != nil { return nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) logger.PrintResponse(resp, true) switch resp.StatusCode { diff --git a/internal/wrappers/gitlab-http.go b/internal/wrappers/gitlab-http.go index 12f47df81..cec5ed2dd 100644 --- a/internal/wrappers/gitlab-http.go +++ b/internal/wrappers/gitlab-http.go @@ -9,6 +9,7 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" "github.com/tomnomnom/linkheader" @@ -141,9 +142,7 @@ func getFromGitLab( if err != nil { return nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) logger.PrintResponse(resp, true) @@ -200,9 +199,7 @@ func collectPageForGitLab( if err != nil { return "", err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) *pageCollection = append(*pageCollection, holder...) nextPageURL := getNextPage(resp) diff --git a/internal/wrappers/groups-http.go b/internal/wrappers/groups-http.go index b4558c713..8b7ccc716 100644 --- a/internal/wrappers/groups-http.go +++ b/internal/wrappers/groups-http.go @@ -6,6 +6,7 @@ import ( "strings" commonParams "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -37,13 +38,10 @@ func (g *GroupsHTTPWrapper) Get(groupName string) ([]Group, error) { if err != nil { return nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/learn-more-http.go b/internal/wrappers/learn-more-http.go index c3e872a30..47b6a7c13 100644 --- a/internal/wrappers/learn-more-http.go +++ b/internal/wrappers/learn-more-http.go @@ -6,6 +6,7 @@ import ( "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -35,9 +36,7 @@ func (r *LearnMoreHTTPWrapper) GetLearnMoreDetails(params map[string]string) ( if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleResponse(resp, err, params[commonParams.QueryIDQueryParam]) } @@ -49,7 +48,6 @@ func handleResponse(resp *http.Response, err error, queryID string) (*[]*LearnMo var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/logs-http.go b/internal/wrappers/logs-http.go index f3db8b72b..6c53bedfb 100644 --- a/internal/wrappers/logs-http.go +++ b/internal/wrappers/logs-http.go @@ -7,6 +7,7 @@ import ( "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -32,13 +33,10 @@ func (l *LogsHTTPWrapper) GetLog(scanID, scanType string) (string, error) { if err != nil { return "", err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/policy-http.go b/internal/wrappers/policy-http.go index ff7e99f4e..567192fd1 100644 --- a/internal/wrappers/policy-http.go +++ b/internal/wrappers/policy-http.go @@ -43,7 +43,6 @@ func (r *PolicyHTTPWrapper) EvaluatePolicy(params map[string]string) ( var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/pr-http.go b/internal/wrappers/pr-http.go index 2612fcab8..116a15b62 100644 --- a/internal/wrappers/pr-http.go +++ b/internal/wrappers/pr-http.go @@ -7,6 +7,7 @@ import ( "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -39,9 +40,7 @@ func (r *PRHTTPWrapper) PostPRDecoration(model *PRModel) ( if err != nil { return "", nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handlePRResponseWithBody(resp, err) } @@ -53,7 +52,6 @@ func handlePRResponseWithBody(resp *http.Response, err error) (string, *WebError var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } defer func() { diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index 8e8ce0407..1fe511504 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -9,6 +9,7 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -51,9 +52,7 @@ func (r *ResultsPredicatesHTTPWrapper) GetAllPredicatesForSimilarityID(similarit if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleResponseWithBody(resp, err) } diff --git a/internal/wrappers/projects-http.go b/internal/wrappers/projects-http.go index 9b66b4716..73b61bde7 100644 --- a/internal/wrappers/projects-http.go +++ b/internal/wrappers/projects-http.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" @@ -33,9 +34,7 @@ func (p *ProjectsHTTPWrapper) Create(model *Project) (*ProjectResponseModel, *Er if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleProjectResponseWithBody(resp, err, http.StatusCreated) } @@ -50,9 +49,7 @@ func (p *ProjectsHTTPWrapper) Update(projectID string, model *Project) error { if err != nil { return err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) switch resp.StatusCode { case http.StatusNoContent: return nil @@ -76,9 +73,7 @@ func (p *ProjectsHTTPWrapper) UpdateConfiguration(projectID string, configuratio if err != nil { return nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleProjectResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -101,9 +96,7 @@ func (p *ProjectsHTTPWrapper) Get(params map[string]string) ( defer resp.Body.Close() } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: errorModel := ErrorModel{} @@ -134,9 +127,7 @@ func (p *ProjectsHTTPWrapper) GetByID(projectID string) ( if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleProjectResponseWithBody(resp, err, http.StatusOK) } @@ -156,9 +147,7 @@ func (p *ProjectsHTTPWrapper) GetBranchesByID(projectID string, params map[strin decoder = json.NewDecoder(resp.Body) defer resp.Body.Close() } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -187,9 +176,7 @@ func (p *ProjectsHTTPWrapper) Delete(projectID string) (*ErrorModel, error) { if err != nil { return nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleProjectResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -202,9 +189,7 @@ func (p *ProjectsHTTPWrapper) Tags() ( if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) var decoder *json.Decoder if resp != nil { diff --git a/internal/wrappers/response.go b/internal/wrappers/response.go index 132f7ba89..602fc05c9 100644 --- a/internal/wrappers/response.go +++ b/internal/wrappers/response.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" ) @@ -19,7 +20,6 @@ func handleScanResponseWithNoBody(resp *http.Response, err error, var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { @@ -46,7 +46,6 @@ func handleScanResponseWithBody(resp *http.Response, err error, var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { @@ -80,7 +79,6 @@ func handleProjectResponseWithNoBody(resp *http.Response, err error, var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { @@ -107,12 +105,9 @@ func handleProjectResponseWithBody(resp *http.Response, err error, var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: diff --git a/internal/wrappers/results-http.go b/internal/wrappers/results-http.go index 6664b9a13..2e18c7cf2 100644 --- a/internal/wrappers/results-http.go +++ b/internal/wrappers/results-http.go @@ -7,6 +7,7 @@ import ( "github.com/checkmarx/ast-cli/internal/logger" commonParams "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/spf13/viper" "github.com/pkg/errors" @@ -53,7 +54,6 @@ func (r *ResultsHTTPWrapper) GetAllResultsByScanID(params map[string]string) ( var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { @@ -103,7 +103,6 @@ func (r *ResultsHTTPWrapper) GetAllResultsPackageByScanID(params map[string]stri var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { @@ -148,14 +147,11 @@ func (r *ResultsHTTPWrapper) GetAllResultsTypeByScanID(params map[string]string) return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { @@ -206,14 +202,11 @@ func (r *ResultsHTTPWrapper) GetScanSummariesByScanIDS(params map[string]string) return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/results-pdf-http.go b/internal/wrappers/results-pdf-http.go index e7cb20477..1b1629e80 100644 --- a/internal/wrappers/results-pdf-http.go +++ b/internal/wrappers/results-pdf-http.go @@ -66,7 +66,6 @@ func (r *PdfHTTPWrapper) GeneratePdfReport(payload *PdfReportsPayload) (*PdfRepo var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { @@ -98,7 +97,6 @@ func (r *PdfHTTPWrapper) CheckPdfReportStatus(reportID string) (*PdfPollingRespo var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/results-sbom-http.go b/internal/wrappers/results-sbom-http.go index 1eb051b40..6c886a421 100644 --- a/internal/wrappers/results-sbom-http.go +++ b/internal/wrappers/results-sbom-http.go @@ -127,7 +127,6 @@ func (r *SbomHTTPWrapper) GetSbomReportStatus(reportID string) (*SbomPollingResp var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/risks-overview-http.go b/internal/wrappers/risks-overview-http.go index affb1307a..9d6f6a727 100644 --- a/internal/wrappers/risks-overview-http.go +++ b/internal/wrappers/risks-overview-http.go @@ -6,6 +6,7 @@ import ( "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -32,13 +33,10 @@ func (r *RisksOverviewHTTPWrapper) GetAllAPISecRisksByScanID(scanID string) ( return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/sca-realtime-http.go b/internal/wrappers/sca-realtime-http.go index d085295fd..27005b12d 100644 --- a/internal/wrappers/sca-realtime-http.go +++ b/internal/wrappers/sca-realtime-http.go @@ -38,7 +38,6 @@ func (s ScaRealTimeHTTPWrapper) GetScaVulnerabilitiesPackages(scaRequest []ScaDe var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/scans-http.go b/internal/wrappers/scans-http.go index d4b3681de..d7cf2c4ca 100644 --- a/internal/wrappers/scans-http.go +++ b/internal/wrappers/scans-http.go @@ -7,6 +7,7 @@ import ( "net/http" commonParams "github.com/checkmarx/ast-cli/internal/params" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -39,9 +40,7 @@ func (s *ScansHTTPWrapper) Create(model *Scan) (*ScanResponseModel, *ErrorModel, if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleScanResponseWithBody(resp, err, http.StatusCreated) } @@ -54,12 +53,9 @@ func (s *ScansHTTPWrapper) Get(params map[string]string) (*ScansCollectionRespon var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) switch resp.StatusCode { case http.StatusBadRequest, http.StatusInternalServerError: @@ -89,9 +85,7 @@ func (s *ScansHTTPWrapper) GetByID(scanID string) (*ScanResponseModel, *ErrorMod if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleScanResponseWithBody(resp, err, http.StatusOK) } @@ -102,9 +96,7 @@ func (s *ScansHTTPWrapper) GetWorkflowByID(scanID string) ([]*ScanTaskResponseMo if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleWorkflowResponseWithBody(resp, err) } @@ -115,7 +107,6 @@ func handleWorkflowResponseWithBody(resp *http.Response, err error) ([]*ScanTask var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { @@ -145,9 +136,7 @@ func (s *ScansHTTPWrapper) Delete(scanID string) (*ErrorModel, error) { if err != nil { return nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleScanResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -164,9 +153,7 @@ func (s *ScansHTTPWrapper) Cancel(scanID string) (*ErrorModel, error) { if err != nil { return nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) return handleScanResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -176,13 +163,10 @@ func (s *ScansHTTPWrapper) Tags() (map[string][]string, *ErrorModel, error) { if err != nil { return nil, nil, err } - if resp != nil { - defer resp.Body.Close() - } + defer utils.CloseHTTPBody(resp) var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/tenant-http.go b/internal/wrappers/tenant-http.go index 910342ed5..94b2af5bf 100644 --- a/internal/wrappers/tenant-http.go +++ b/internal/wrappers/tenant-http.go @@ -36,7 +36,6 @@ func (r *TenantConfigurationHTTPWrapper) GetTenantConfiguration() ( var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/uploads-http.go b/internal/wrappers/uploads-http.go index 7c9e6eb2b..3c8eb7be8 100644 --- a/internal/wrappers/uploads-http.go +++ b/internal/wrappers/uploads-http.go @@ -82,7 +82,6 @@ func (u *UploadsHTTPWrapper) getPresignedURLForUploading() (*string, error) { var decoder *json.Decoder if resp != nil { decoder = json.NewDecoder(resp.Body) - defer resp.Body.Close() } switch resp.StatusCode { diff --git a/internal/wrappers/utils/utils.go b/internal/wrappers/utils/utils.go index f4e88b18c..4f895e182 100644 --- a/internal/wrappers/utils/utils.go +++ b/internal/wrappers/utils/utils.go @@ -1,6 +1,7 @@ package utils import ( + "net/http" "net/url" "path" "strings" @@ -37,3 +38,9 @@ func ToStringArray(obj interface{}) []string { return []string{} } } + +func CloseHTTPBody(resp *http.Response) { + if resp != nil { + _ = resp.Body.Close() + } +}