Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CLI Logging and Error Handling #76

Merged
merged 29 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c48aa5f
Log Errors
AkshayBhansali18 Aug 24, 2021
fe57b16
dummy commit
AkshayBhansali18 Aug 30, 2021
8a0ab1c
trigger-build
AkshayBhansali18 Aug 31, 2021
a1a7fb6
fix readme typo
AkshayBhansali18 Aug 31, 2021
d824c78
add break line
AkshayBhansali18 Aug 31, 2021
63bb50d
Fix issues with logging in cli
AkshayBhansali18 Oct 5, 2021
aedbc6b
trigger build
AkshayBhansali18 Oct 5, 2021
5198a10
check tests
AkshayBhansali18 Oct 5, 2021
ee08993
trigger build
AkshayBhansali18 Oct 5, 2021
c123aa3
handle error for invalid response
AkshayBhansali18 Oct 5, 2021
15a68a1
trigger build
AkshayBhansali18 Oct 5, 2021
bbb8339
change to self hosted apicast URL
AkshayBhansali18 Oct 7, 2021
f6284a2
Merge branch 'main' into cli-logging
AkshayBhansali18 Oct 7, 2021
66a848d
Log Errors
AkshayBhansali18 Aug 24, 2021
78125e6
dummy commit
AkshayBhansali18 Aug 30, 2021
1e63ad5
trigger-build
AkshayBhansali18 Aug 31, 2021
8a3a40f
fix readme typo
AkshayBhansali18 Aug 31, 2021
6731fe4
add break line
AkshayBhansali18 Aug 31, 2021
903805c
Fix issues with logging in cli
AkshayBhansali18 Oct 5, 2021
8045b12
trigger build
AkshayBhansali18 Oct 5, 2021
f802337
check tests
AkshayBhansali18 Oct 5, 2021
c888117
trigger build
AkshayBhansali18 Oct 5, 2021
696db58
handle error for invalid response
AkshayBhansali18 Oct 5, 2021
2340b0e
trigger build
AkshayBhansali18 Oct 5, 2021
899921a
change to self hosted apicast URL
AkshayBhansali18 Oct 7, 2021
b02d5b5
print string response in case of invalid response from server
AkshayBhansali18 Oct 7, 2021
f36e0f6
restore host and token
AkshayBhansali18 Oct 7, 2021
0beae22
resolve
AkshayBhansali18 Oct 7, 2021
d76888b
trigger build
AkshayBhansali18 Oct 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</a>
</p>

This repo would server as an interface between the different CRDA clients and the platform. It contains tools that will be used by clients inorder to generate required input for platform APIs. One such tool is `gomanifest`.
This repo would serve as an interface between the different CRDA clients and the platform. It contains tools that will be used by clients inorder to generate required input for platform APIs. One such tool is `gomanifest`.
### Tools and Packages:

* `CRDA Cli`: CLI Tools to interact with CRDA Platform. [Learn more](docs/cli_README.md)
Expand Down
1 change: 1 addition & 0 deletions cmd/analyse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var verboseOut bool
var analyseCmd = &cobra.Command{
Use: "analyse",
Short: "Get detailed report of vulnerabilities.",
SilenceUsage: true,
Long: `Get detailed report of vulnerabilities. Supported ecosystems are Pypi (Python), Maven (Java), Npm (Node) and Golang (Go).
If stack has Vulnerabilities, command will exit with status code 2.`,
Args: validateFileArg,
Expand Down
22 changes: 18 additions & 4 deletions pkg/analyses/stackanalyses/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
Expand Down Expand Up @@ -153,8 +154,6 @@ func (mc *Controller) getRequest(requestParams driver.RequestType, postResponse
// validatePostResponse validates Stack Analyses POST API Response.
func (mc *Controller) validatePostResponse(apiResponse *http.Response) (*driver.PostResponseType, error) {
log.Debug().Msgf("Executing validatePostResponse.")
var body driver.PostResponseType
err := json.NewDecoder(apiResponse.Body).Decode(&body)

// In Case of Authentication Failure, json is not return from API, Need to catch before decoding.
if apiResponse.StatusCode == http.StatusForbidden {
Expand All @@ -163,7 +162,11 @@ func (mc *Controller) validatePostResponse(apiResponse *http.Response) (*driver.
return nil, fmt.Errorf("invalid authentication token")
}

var body driver.PostResponseType
err := json.NewDecoder(apiResponse.Body).Decode(&body)

if err != nil {

return nil, err
}
if apiResponse.StatusCode != http.StatusOK {
Expand All @@ -179,7 +182,18 @@ func (mc *Controller) validatePostResponse(apiResponse *http.Response) (*driver.
func (mc *Controller) validateGetResponse(apiResponse *http.Response) (*driver.GetResponseType, error) {
log.Debug().Msgf("Executing validateGetResponse.")
var body driver.GetResponseType
err := json.NewDecoder(apiResponse.Body).Decode(&body)
var buf bytes.Buffer

//use TeeReader to duplicate the contents of the Response Body of type io.ReaderCloser since data is streamed from the response body.
r := io.TeeReader(apiResponse.Body, &buf)
responseBodyContents, _ :=ioutil.ReadAll(r)
err := json.NewDecoder(&buf).Decode(&body)

if err != nil {
log.Error().Msg("analyse failed: Stack Analyses Get Request Failed. Please retry after sometime. If issue persists, Please raise at https://github.com/fabric8-analytics/cli-tools/issues.")
return nil, fmt.Errorf("Message from Server: "+string(responseBodyContents))
}

if apiResponse.StatusCode != http.StatusOK {
log.Debug().Msgf("Status from Server: %d", apiResponse.StatusCode)
log.Error().Msgf("Stack Analyses Get Request Failed with status code %d. Please retry after sometime. If issue persists, Please raise at https://github.com/fabric8-analytics/cli-tools/issues.\"", apiResponse.StatusCode)
Expand Down Expand Up @@ -211,7 +225,7 @@ func GetMatcher(manifestFile string) (driver.StackAnalysisInterface, error) {
return matcher, nil
}
}
return nil, errors.New("ecosystem not supported yet")
return nil, errors.New("analyse failed: \""+manifestFile+"\" does not appear to be a supported dependency manifest file. Supported manifest files include \"pom.xml\", \"package.json\", \"go.mod\", \"requirements.txt\". Please provide the path of a valid manifest file for analysis. ")
}

func (mc *Controller) buildFileStats(manifestFile string) *driver.ReadManifestResponse {
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package utils

// Flag Defaults
const (
CRDAHost string = "https://f8a-analytics-2445582058137.production.gw.apicast.io"
CRDAAuthToken string = "9e7da76708fe374d8c10fa752e72989f"
CRDAHost string = "https://gw.api.openshift.io"
CRDAAuthToken string = "207c527cfc2a6b8dcf4fa43ad7a976da"
Debug bool = false
ActualHost string = "https://recommender.api.openshift.io"
)