diff --git a/cmd/protecodeExecuteScan.go b/cmd/protecodeExecuteScan.go index fefc2f07b..3db98a7b0 100644 --- a/cmd/protecodeExecuteScan.go +++ b/cmd/protecodeExecuteScan.go @@ -141,12 +141,16 @@ func getDockerImage(dClient piperDocker.Download, config *protecodeExecuteScanOp } func executeProtecodeScan(influx *protecodeExecuteScanInflux, client protecode.Protecode, config *protecodeExecuteScanOptions, fileName string, writeReportToFile func(resp io.ReadCloser, reportFileName string) error) error { - //load existing product by filename - log.Entry().Debugf("Load existing product Group:%v Reuse:%v", config.Group, config.VerifyOnly) - productID := client.LoadExistingProduct(config.Group, config.VerifyOnly) - - // check if no existing is found or reuse existing is false - productID = uploadScanOrDeclareFetch(*config, productID, client, fileName) + productID := -1 + if config.VerifyOnly { + //load existing product by filename + log.Entry().Debugf("Load existing product Group:%v Reuse:%v", config.Group, config.VerifyOnly) + productID = client.LoadExistingProduct(config.Group) + } + if !config.VerifyOnly || productID <= 0 { + // check if no existing is found or reuse existing is false + productID = uploadScanOrDeclareFetch(*config, client, fileName) + } if productID <= 0 { return fmt.Errorf("the product id is not valid '%d'", productID) } @@ -260,33 +264,31 @@ func createDockerClient(config *protecodeExecuteScanOptions) piperDocker.Downloa return dClient } -func uploadScanOrDeclareFetch(config protecodeExecuteScanOptions, productID int, client protecode.Protecode, fileName string) int { - //check if the LoadExistingProduct) before returns an valid product id, than scip this - if !hasExisting(productID, config.VerifyOnly) { - if len(config.FetchURL) > 0 { - log.Entry().Debugf("Declare fetch url %v", config.FetchURL) - resultData := client.DeclareFetchURL(config.CleanupMode, config.Group, config.FetchURL) - productID = resultData.Result.ProductID - } else { - log.Entry().Debugf("Upload file path: %v", config.FilePath) - if len(config.FilePath) <= 0 { - log.Entry().Fatalf("There is no file path configured for upload : %v", config.FilePath) - } - pathToFile := filepath.Join(config.FilePath, fileName) - if !(fileExists(pathToFile)) { - log.Entry().Fatalf("There is no file for upload: %v", pathToFile) - } - - combinedFileName := fileName - if len(config.PullRequestName) > 0 { - combinedFileName = fmt.Sprintf("%v_%v", config.PullRequestName, fileName) - } - - resultData := client.UploadScanFile(config.CleanupMode, config.Group, pathToFile, combinedFileName) - productID = resultData.Result.ProductID +func uploadScanOrDeclareFetch(config protecodeExecuteScanOptions, client protecode.Protecode, fileName string) int { + if len(config.FetchURL) > 0 { + log.Entry().Debugf("Declare fetch url %v", config.FetchURL) + resultData := client.DeclareFetchURL(config.CleanupMode, config.Group, config.FetchURL) + return resultData.Result.ProductID + } else { + log.Entry().Debugf("Upload file path: %v", config.FilePath) + if len(config.FilePath) <= 0 { + //TODO: bubble up error + log.Entry().Fatalf("There is no file path configured for upload: %v", config.FilePath) } + pathToFile := filepath.Join(config.FilePath, fileName) + if !(fileExists(pathToFile)) { + //TODO: bubble up error + log.Entry().Fatalf("There is no file for upload: %v", pathToFile) + } + + combinedFileName := fileName + if len(config.PullRequestName) > 0 { + combinedFileName = fmt.Sprintf("%v_%v", config.PullRequestName, fileName) + } + + resultData := client.UploadScanFile(config.CleanupMode, config.Group, pathToFile, combinedFileName) + return resultData.Result.ProductID } - return productID } func fileExists(filename string) bool { @@ -297,13 +299,6 @@ func fileExists(filename string) bool { return !info.IsDir() } -func hasExisting(productID int, verifyOnly bool) bool { - if (productID > 0) || verifyOnly { - return true - } - return false -} - var writeReportToFile = func(resp io.ReadCloser, reportFileName string) error { filePath := filepath.Join(reportPath, reportFileName) f, err := os.Create(filePath) diff --git a/cmd/protecodeExecuteScan_test.go b/cmd/protecodeExecuteScan_test.go index d38e5ec0e..532e14cad 100644 --- a/cmd/protecodeExecuteScan_test.go +++ b/cmd/protecodeExecuteScan_test.go @@ -266,7 +266,7 @@ func TestUploadScanOrDeclareFetch(t *testing.T) { for _, c := range cases { // test config := protecodeExecuteScanOptions{VerifyOnly: c.reuse, CleanupMode: c.clean, Group: c.group, FetchURL: c.fetchURL, FilePath: c.filePath} - got := uploadScanOrDeclareFetch(config, 0, pc, fileName) + got := uploadScanOrDeclareFetch(config, pc, fileName) // assert assert.Equal(t, c.want, got) } diff --git a/go.sum b/go.sum index 84f3f8455..7d199fd53 100644 --- a/go.sum +++ b/go.sum @@ -200,7 +200,6 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV github.com/armon/go-metrics v0.0.0-20190430140413-ec5e00d3c878/go.mod h1:3AMJUQhVx52RsWOnlkpikZr01T/yAVN2gn0861vByNg= github.com/armon/go-metrics v0.3.0/go.mod h1:zXjbSimjXTd7vOpY8B0/2LpvNvDoXBuplAD+gJD3GYs= github.com/armon/go-metrics v0.3.3/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc= -github.com/armon/go-metrics v0.3.4 h1:Xqf+7f2Vhl9tsqDYmXhnXInUdcrtgpRNpIA15/uldSc= github.com/armon/go-metrics v0.3.4/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc= github.com/armon/go-metrics v0.3.7 h1:c/oCtWzYpboy6+6f6LjXRlyW7NwA2SWf+a9KMlHq/bM= github.com/armon/go-metrics v0.3.7/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc= @@ -403,7 +402,6 @@ github.com/evanphx/json-patch v4.9.0+incompatible h1:kLcOMZeuLAJvL2BPWLMIj5oaZQo github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= @@ -769,7 +767,6 @@ github.com/hashicorp/go-hclog v0.9.1/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrj github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= github.com/hashicorp/go-hclog v0.12.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= -github.com/hashicorp/go-hclog v0.15.0 h1:qMuK0wxsoW4D0ddCCYwPSTm4KQv1X1ke3WmPWZ0Mvsk= github.com/hashicorp/go-hclog v0.15.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= github.com/hashicorp/go-hclog v0.16.0 h1:uCeOEwSWGMwhJUdpUjk+1cVKIEfGu2/1nFXukimi2MU= github.com/hashicorp/go-hclog v0.16.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= @@ -784,7 +781,6 @@ github.com/hashicorp/go-kms-wrapping/entropy v0.1.0/go.mod h1:d1g9WGtAunDNpek8jU github.com/hashicorp/go-memdb v1.0.2 h1:AIjzJlwIxz2inhZqRJZfe6D15lPeF0/cZyS1BVlnlHg= github.com/hashicorp/go-memdb v1.0.2/go.mod h1:I6dKdmYhZqU0RJSheVEWgTNWdVQH5QvTgIUQ0t/t32M= github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= -github.com/hashicorp/go-msgpack v0.5.5 h1:i9R9JSrqIz0QVLz3sz+i3YJdT7TTSLcfLLzJi9aZTuI= github.com/hashicorp/go-msgpack v0.5.5/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= github.com/hashicorp/go-msgpack v1.1.5 h1:9byZdVjKTe5mce63pRVNP1L7UAmdHOTEMGehn6KvJWs= github.com/hashicorp/go-msgpack v1.1.5/go.mod h1:gWVc3sv/wbDmR3rQsj1CAktEZzoz1YNK9NfGLXJ69/4= @@ -845,7 +841,6 @@ github.com/hashicorp/nomad/api v0.0.0-20191220223628-edc62acd919d h1:BXqsASWhyiA github.com/hashicorp/nomad/api v0.0.0-20191220223628-edc62acd919d/go.mod h1:WKCL+tLVhN1D+APwH3JiTRZoxcdwRk86bWu1LVCUPaE= github.com/hashicorp/raft v1.0.1/go.mod h1:DVSAWItjLjTOkVbSpWQ0j0kUADIvDaCtBxIcbNAQLkI= github.com/hashicorp/raft v1.1.2-0.20191002163536-9c6bd3e3eb17/go.mod h1:vPAJM8Asw6u8LxC3eJCUZmRP/E4QmUGE1R7g7k8sG/8= -github.com/hashicorp/raft v1.2.0 h1:mHzHIrF0S91d3A7RPBvuqkgB4d/7oFJZyvf1Q4m7GA0= github.com/hashicorp/raft v1.2.0/go.mod h1:vPAJM8Asw6u8LxC3eJCUZmRP/E4QmUGE1R7g7k8sG/8= github.com/hashicorp/raft v1.3.0 h1:Wox4J4R7J2FOJLtTa6hdk0VJfiNUSP32pYoYR738bkE= github.com/hashicorp/raft v1.3.0/go.mod h1:4Ak7FSPnuvmb0GV6vgIAJ4vYT4bek9bb6Q+7HVbyzqM= @@ -1096,7 +1091,6 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw= github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= diff --git a/pkg/protecode/protecode.go b/pkg/protecode/protecode.go index 1e46fd2ff..4de396b05 100644 --- a/pkg/protecode/protecode.go +++ b/pkg/protecode/protecode.go @@ -126,6 +126,7 @@ func (pc *Protecode) createURL(path string, pValue string, fParam string) string protecodeURL, err := url.Parse(pc.serverURL) if err != nil { + //TODO: bubble up error pc.logger.WithError(err).Fatal("Malformed URL") } @@ -162,6 +163,7 @@ func (pc *Protecode) mapResponse(r io.ReadCloser, response interface{}) { if err != nil { err = json.Unmarshal([]byte(newStr), response) if err != nil { + //TODO: bubble up error pc.logger.WithError(err).Fatalf("Error during unqote response: %v", newStr) } } else { @@ -169,6 +171,7 @@ func (pc *Protecode) mapResponse(r io.ReadCloser, response interface{}) { } if err != nil { + //TODO: bubble up error pc.logger.WithError(err).Fatalf("Error during decode response: %v", newStr) } } @@ -275,6 +278,7 @@ func (pc *Protecode) DeleteScan(cleanupMode string, productID int) { pc.sendAPIRequest("DELETE", protecodeURL, headers) default: + //TODO: bubble up error pc.logger.Fatalf("Unknown cleanup mode %v", cleanupMode) } } @@ -291,6 +295,7 @@ func (pc *Protecode) LoadReport(reportFileName string, productID int) *io.ReadCl readCloser, err := pc.sendAPIRequest(http.MethodGet, protecodeURL, headers) if err != nil { + //TODO: bubble up error pc.logger.WithError(err).Fatalf("It is not possible to load report %v", protecodeURL) } @@ -306,6 +311,7 @@ func (pc *Protecode) UploadScanFile(cleanupMode, group, filePath, fileName strin r, err := pc.client.UploadRequest(http.MethodPut, uploadURL, filePath, "file", headers, nil) if err != nil { + //TODO: bubble up error pc.logger.WithError(err).Fatalf("Error during %v upload request", uploadURL) } else { pc.logger.Info("Upload successful") @@ -325,6 +331,7 @@ func (pc *Protecode) DeclareFetchURL(cleanupMode, group, fetchURL string) *Resul protecodeURL := fmt.Sprintf("%v/api/fetch/", pc.serverURL) r, err := pc.sendAPIRequest(http.MethodPost, protecodeURL, headers) if err != nil { + //TODO: bubble up error pc.logger.WithError(err).Fatalf("Error during declare fetch url: %v", protecodeURL) } @@ -394,6 +401,7 @@ func (pc *Protecode) PollForResult(productID int, timeOutInMinutes string) Resul } if err != nil || response.Result.Status == statusBusy { + //TODO: bubble up error pc.logger.Fatalf("No result after polling err: %v protecode status: %v", err, response.Result.Status) } } @@ -418,23 +426,20 @@ func (pc *Protecode) pullResult(productID int) (ResultData, error) { } // LoadExistingProduct loads the existing product from protecode service -func (pc *Protecode) LoadExistingProduct(group string, reuseExisting bool) int { - var productID int = -1 +func (pc *Protecode) LoadExistingProduct(group string) int { + productID := -1 - if reuseExisting { - - protecodeURL := pc.createURL("/api/apps/", fmt.Sprintf("%v/", group), "") - headers := map[string][]string{ - "acceptType": {"application/json"}, - } - - response := pc.loadExisting(protecodeURL, headers) - // by definition we will take the first one and trigger rescan - productID = response.Products[0].ProductID - - pc.logger.Infof("Re-use existing Protecode scan - group: %v, productID: %v", group, productID) + protecodeURL := pc.createURL("/api/apps/", fmt.Sprintf("%v/", group), "") + headers := map[string][]string{ + "acceptType": {"application/json"}, } + response := pc.loadExisting(protecodeURL, headers) + // by definition we will take the first one and trigger rescan + productID = response.Products[0].ProductID + + pc.logger.Infof("Re-use existing Protecode scan - group: %v, productID: %v", group, productID) + return productID } @@ -442,6 +447,7 @@ func (pc *Protecode) loadExisting(protecodeURL string, headers map[string][]stri r, err := pc.sendAPIRequest(http.MethodGet, protecodeURL, headers) if err != nil { + //TODO: bubble up error pc.logger.WithError(err).Fatalf("Error during load existing product: %v", protecodeURL) } diff --git a/pkg/protecode/protecode_test.go b/pkg/protecode/protecode_test.go index 9a1fcf89a..c05808338 100644 --- a/pkg/protecode/protecode_test.go +++ b/pkg/protecode/protecode_test.go @@ -169,15 +169,13 @@ func TestLoadExistingProductSuccess(t *testing.T) { cases := []struct { pc Protecode protecodeGroup string - reuseExisting bool want int }{ - {makeProtecode(Options{ServerURL: server.URL}), "group", true, 1}, - {makeProtecode(Options{ServerURL: server.URL}), "group32", false, -1}, + {makeProtecode(Options{ServerURL: server.URL}), "group", 1}, } for _, c := range cases { - got := c.pc.LoadExistingProduct(c.protecodeGroup, c.reuseExisting) + got := c.pc.LoadExistingProduct(c.protecodeGroup) assert.Equal(t, c.want, got) } }