Skip to content

Commit

Permalink
Merge branch 'release/1.0.12'
Browse files Browse the repository at this point in the history
  • Loading branch information
sbalka committed Jun 15, 2021
2 parents 5c5798c + f46757e commit c173dd9
Show file tree
Hide file tree
Showing 16 changed files with 301 additions and 89 deletions.
3 changes: 2 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright © 2019 Kondukto
*/

package client

import (
Expand Down Expand Up @@ -82,7 +83,7 @@ func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
if err != nil {
return resp, err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"net/url"
"testing"

"github.com/kondukto-io/kdt/klog"
)

func TestNewRequestPath(t *testing.T) {
Expand Down Expand Up @@ -85,7 +87,8 @@ func TestDo(t *testing.T) {
}

if resp.StatusCode != http.StatusOK {
t.Fatal("http response not ok")
klog.Fatalf("HTTP response status code: %d", resp.StatusCode)
t.Fatal("HTTP response not OK")
}

j, err := json.Marshal(&someone)
Expand Down
12 changes: 9 additions & 3 deletions client/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
Copyright © 2019 Kondukto
*/

package client

import (
"errors"
"fmt"
"net/http"

"github.com/kondukto-io/kdt/klog"
)

type Project struct {
Expand All @@ -18,6 +21,8 @@ type Project struct {
func (c *Client) ListProjects(arg string) ([]Project, error) {
projects := make([]Project, 0)

klog.Debug("retrieving project list...")

req, err := c.newRequest("GET", "/api/v1/projects", nil)
if err != nil {
return projects, err
Expand All @@ -30,6 +35,7 @@ func (c *Client) ListProjects(arg string) ([]Project, error) {
type getProjectsResponse struct {
Projects []Project `json:"data"`
Total int `json:"total"`
Error string `json:"error"`
}
var ps getProjectsResponse

Expand All @@ -39,7 +45,7 @@ func (c *Client) ListProjects(arg string) ([]Project, error) {
}

if resp.StatusCode != http.StatusOK {
return projects, errors.New("response not ok")
return projects, fmt.Errorf("HTTP response not OK : %s", ps.Error)
}

return ps.Projects, nil
Expand All @@ -66,7 +72,7 @@ type ReleaseStatus struct {

func (c *Client) ReleaseStatus(project string) (*ReleaseStatus, error) {
if project == "" {
return nil, errors.New("invalid project id or name")
return nil, errors.New("missing project id or name")
}

path := fmt.Sprintf("/api/v1/projects/%s/release", project)
Expand All @@ -84,7 +90,7 @@ func (c *Client) ReleaseStatus(project string) (*ReleaseStatus, error) {
}

if resp.StatusCode != http.StatusOK {
return nil, errors.New("response not ok")
return nil, fmt.Errorf("HTTP response not OK: %d", resp.StatusCode)
}

return rs, nil
Expand Down
38 changes: 25 additions & 13 deletions client/scans.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright © 2019 Kondukto
*/

package client

import (
Expand All @@ -16,6 +17,8 @@ import (
"path/filepath"
"time"

"github.com/kondukto-io/kdt/klog"

"github.com/google/go-querystring/query"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -69,6 +72,8 @@ func (c *Client) ListScans(project string, params *ScanSearchParams) ([]Scan, er
// TODO: list scans call should be updated to take tool and metadata arguments
scans := make([]Scan, 0)

klog.Debugf("retrieving scans of the project: %s", project)

path := fmt.Sprintf("/api/v1/projects/%s/scans", project)
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
Expand All @@ -93,7 +98,7 @@ func (c *Client) ListScans(project string, params *ScanSearchParams) ([]Scan, er
}

if resp.StatusCode != http.StatusOK {
return scans, errors.New("response not ok")
return scans, fmt.Errorf("HTTP response not OK: %d", resp.StatusCode)
}

return ps.Scans, nil
Expand All @@ -117,6 +122,7 @@ func (c *Client) FindScan(project string, params *ScanSearchParams) (*Scan, erro
}

func (c *Client) StartScanByScanId(id string) (string, error) {
klog.Debug("starting scan by scan_id")
path := fmt.Sprintf("/api/v1/scans/%s/restart", id)
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
Expand All @@ -134,7 +140,7 @@ func (c *Client) StartScanByScanId(id string) (string, error) {
}

if resp.StatusCode != http.StatusCreated {
return "", errors.New("response not ok")
return "", fmt.Errorf("HTTP response not OK: %d", resp.StatusCode)
}

if rsr.Event == "" {
Expand Down Expand Up @@ -166,7 +172,7 @@ func (c *Client) StartScanByOption(id string, opt *ScanPROptions) (string, error
}

if resp.StatusCode != http.StatusCreated {
return "", errors.New("response not ok")
return "", fmt.Errorf("HTTP response not OK: %d", resp.StatusCode)
}

if rsr.Event == "" {
Expand All @@ -183,17 +189,17 @@ func (c *Client) GetScanStatus(eventId string) (*Event, error) {
return nil, err
}

e := &Event{}
var e Event
resp, err := c.do(req, &e)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, errors.New("response not ok")
return nil, fmt.Errorf("HTTP response not OK: %d", resp.StatusCode)
}

return e, nil
return &e, nil
}

func (c *Client) GetScanSummary(id string) (*Scan, error) {
Expand All @@ -203,17 +209,17 @@ func (c *Client) GetScanSummary(id string) (*Scan, error) {
return nil, err
}

scan := &Scan{}
var scan Scan
resp, err := c.do(req, &scan)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, errors.New("response not ok")
return nil, fmt.Errorf("HTTP response not OK: %d", resp.StatusCode)
}

return scan, nil
return &scan, nil
}

func (c *Client) GetLastResults(id string) (map[string]*ResultSet, error) {
Expand All @@ -230,13 +236,16 @@ func (c *Client) GetLastResults(id string) (map[string]*ResultSet, error) {
}

if resp.StatusCode != http.StatusOK {
return nil, errors.New("response not ok")
return nil, fmt.Errorf("HTTP response not OK: %d", resp.StatusCode)
}

return m, err
}

func (c *Client) ImportScanResult(project, branch, tool string, file string) (string, error) {

klog.Debugf("importing scan results using the file:%s", file)

path := "/api/v1/scans/import"
rel := &url.URL{Path: path}
u := c.BaseURL.ResolveReference(rel)
Expand Down Expand Up @@ -285,15 +294,17 @@ func (c *Client) ImportScanResult(project, branch, tool string, file string) (st
type importScanResultResponse struct {
EventID string `json:"event_id"`
Message string `json:"message"`
Error string `json:"error"`
}

var importResponse importScanResultResponse
resp, err := c.do(req, &importResponse)
if err != nil {
return "", err
}

if resp.StatusCode != http.StatusOK {
return "", errors.New("failed to import scan results")
return "", fmt.Errorf("failed to import scan results: %s", importResponse.Error)
}

return importResponse.EventID, nil
Expand Down Expand Up @@ -322,16 +333,17 @@ func (c *Client) ScanByImage(project, branch, tool, image string) (string, error

type responseBody struct {
EventID string `json:"event_id"`
Error string `json:"error"`
}
respBody := new(responseBody)

resp, err := c.do(req, respBody)
if err != nil {
return "", fmt.Errorf("HTTP response failed: %w", err)

}

if resp.StatusCode != http.StatusCreated {
return "", fmt.Errorf("HTTP response not OK")
return "", fmt.Errorf("HTTP response not OK: %s", respBody.Error)
}

return respBody.EventID, nil
Expand Down
1 change: 1 addition & 0 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright © 2019 Kondukto
*/

package cmd

import (
Expand Down
10 changes: 7 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright © 2019 Kondukto
*/

package cmd

import (
Expand All @@ -12,11 +13,14 @@ import (
var listCmd = &cobra.Command{
Use: "list",
Short: "base command for lists",
Run: listRootCommand,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
qwm(0, "")
}
},
}

func init() {
rootCmd.AddCommand(listCmd)
}

func listRootCommand(cmd *cobra.Command, args []string) {}
11 changes: 6 additions & 5 deletions cmd/listProjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright © 2019 Kondukto
*/

package cmd

import (
Expand All @@ -24,7 +25,7 @@ func init() {
listCmd.AddCommand(listProjectsCmd)
}

func projectsRootCommand(cmd *cobra.Command, args []string) {
func projectsRootCommand(_ *cobra.Command, args []string) {
c, err := client.New()
if err != nil {
qwe(1, err, "could not initialize Kondukto client")
Expand All @@ -45,11 +46,11 @@ func projectsRootCommand(cmd *cobra.Command, args []string) {
}

w := tabwriter.NewWriter(os.Stdout, 8, 8, 4, ' ', 0)
defer w.Flush()
defer func() { _ = w.Flush() }()

fmt.Fprintln(w, "NAME\tID")
fmt.Fprintln(w, "---\t---")
_, _ = fmt.Fprintln(w, "NAME\tID")
_, _ = fmt.Fprintln(w, "---\t---")
for _, project := range projects {
fmt.Fprintf(w, "%s\t%s\n", project.Name, project.ID)
_, _ = fmt.Fprintf(w, "%s\t%s\n", project.Name, project.ID)
}
}
28 changes: 28 additions & 0 deletions cmd/listScanners.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"fmt"
"os"
"text/tabwriter"

"github.com/spf13/cobra"
)

var listScannersCmd = &cobra.Command{
Use: "scanners",
Short: "list supported scanners",
Run: func(cmd *cobra.Command, args []string) {
w := tabwriter.NewWriter(os.Stdout, 8, 8, 4, ' ', 0)
defer func() { _ = w.Flush() }()

_, _ = fmt.Fprintf(w, "Tool Name\tScanner Type\n")
_, _ = fmt.Fprintf(w, "------\t------\n")
for k, v := range scanners {
_, _ = fmt.Fprintf(w, "%s\t%s\n", k, v)
}
},
}

func init() {
listCmd.AddCommand(listScannersCmd)
}
5 changes: 3 additions & 2 deletions cmd/listScans.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright © 2019 Kondukto
*/

package cmd

import (
Expand All @@ -27,7 +28,7 @@ func init() {
_ = listScansCmd.MarkFlagRequired("project")
}

func scanListRootCommand(cmd *cobra.Command, args []string) {
func scanListRootCommand(cmd *cobra.Command, _ []string) {
c, err := client.New()
if err != nil {
qwe(1, err, "could not initialize Kondukto client")
Expand All @@ -44,7 +45,7 @@ func scanListRootCommand(cmd *cobra.Command, args []string) {
}

w := tabwriter.NewWriter(os.Stdout, 8, 8, 4, ' ', 0)
defer w.Flush()
defer func() { _ = w.Flush() }()

_, _ = fmt.Fprintf(w, "NAME\tID\tBRANCH\tMETA\tTOOL\tCRIT\tHIGH\tMED\tLOW\tSCORE\tDATE\n")
_, _ = fmt.Fprintf(w, "---\t---\t---\t---\t---\t---\t---\t---\t---\t---\n")
Expand Down
Loading

0 comments on commit c173dd9

Please sign in to comment.