Skip to content

Commit

Permalink
Merge branch 'hotfix/v1.0.33-migrate-apiv1-to-apiv2'
Browse files Browse the repository at this point in the history
  • Loading branch information
yeyisan committed Jun 5, 2024
2 parents 64b8629 + befa909 commit 38d8193
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 48 deletions.
23 changes: 11 additions & 12 deletions client/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *Client) ListProjects(name, repo string) ([]Project, error) {

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

req, err := c.newRequest("GET", "/api/v1/projects", nil)
req, err := c.newRequest("GET", "/api/v2/projects", nil)
if err != nil {
return projects, err
}
Expand Down Expand Up @@ -142,20 +142,19 @@ func (c *Client) CreateProject(pd ProjectDetail) (*Project, error) {
}

type ReleaseStatus struct {
Status string `json:"status" bson:"status"`
SAST PlaybookTypeDetail `json:"sast" bson:"sast"`
DAST PlaybookTypeDetail `json:"dast" bson:"dast"`
PENTEST PlaybookTypeDetail `json:"pentest" bson:"pentest"`
IAST PlaybookTypeDetail `json:"iast" bson:"iast"`
SCA PlaybookTypeDetail `json:"sca" bson:"sca"`
CS PlaybookTypeDetail `json:"cs" bson:"cs"`
IAC PlaybookTypeDetail `json:"iac" bson:"iac"`
Status string `json:"status"`
SAST PlaybookTypeDetail `json:"sast"`
DAST PlaybookTypeDetail `json:"dast"`
PENTEST PlaybookTypeDetail `json:"pentest"`
IAST PlaybookTypeDetail `json:"iast"`
SCA PlaybookTypeDetail `json:"sca"`
CS PlaybookTypeDetail `json:"cs"`
IAC PlaybookTypeDetail `json:"iac"`
MAST PlaybookTypeDetail `json:"mast"`
}

type PlaybookTypeDetail struct {
Tool string `json:"tool" bson:"tool"`
Status string `json:"status" bson:"status"`
Manual bool `json:"manual" bson:"manual"`
ScanID string `json:"scan_id,omitempty" bson:"scan_id"`
}

Expand All @@ -164,7 +163,7 @@ func (c *Client) ReleaseStatus(project, branch string) (*ReleaseStatus, error) {
return nil, errors.New("missing project id or name")
}

path := fmt.Sprintf("/api/v1/projects/%s/release", project)
path := fmt.Sprintf("/api/v2/projects/%s/release", project)

req, err := c.newRequest("GET", path, nil)
if err != nil {
Expand Down
62 changes: 58 additions & 4 deletions client/scanners.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ import (
"github.com/google/go-querystring/query"
)

type ScannerType string

const (
ScannerTypeSAST ScannerType = "sast"
ScannerTypeDAST ScannerType = "dast"
ScannerTypeSCA ScannerType = "sca"
ScannerTypeCS ScannerType = "cs"
ScannerTypeIAC ScannerType = "iac"
ScannerTypeIAST ScannerType = "iast"
ScannerTypeCSPM ScannerType = "cspm"
ScannerTypeMAST ScannerType = "mast"
)

func (s ScannerType) String() string {
return string(s)
}

func ScannerTypes() []ScannerType {
return []ScannerType{
ScannerTypeSAST, ScannerTypeDAST, ScannerTypeSCA, ScannerTypeCS,
ScannerTypeIAC, ScannerTypeIAST, ScannerTypeCSPM, ScannerTypeMAST,
}
}

type (
ScannersSearchParams struct {
Types string `url:"types"`
Expand Down Expand Up @@ -117,17 +141,45 @@ const (
ScannerLabelCreatableOnTool = "creatable-on-tool"
)

type ListActiveScannersInput struct {
Types []ScannerType
Labels string
Name string
Limit int
}

func (i *ListActiveScannersInput) prepareRequestQueryParameters() ScannersSearchParams {
var scannerTypes = make([]string, 0)

if len(i.Types) == 0 {
for _, t := range ScannerTypes() {
scannerTypes = append(scannerTypes, t.String())
}
} else {
for _, t := range i.Types {
scannerTypes = append(scannerTypes, t.String())
}
}

return ScannersSearchParams{
Types: strings.Join(scannerTypes, ","),
Labels: i.Labels,
Name: i.Name,
Limit: i.Limit,
}
}

// ListActiveScanners returns a list of active scanners
func (c *Client) ListActiveScanners(params *ScannersSearchParams) (*ScannersResponse, error) {
func (c *Client) ListActiveScanners(input *ListActiveScannersInput) (*ScannersResponse, error) {
klog.Debugf("retrieving active scanners")

path := fmt.Sprintf("/api/v1/scanners/active")
path := fmt.Sprintf("/api/v2/scanners/active")
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}

v, err := query.Values(params)
v, err := query.Values(input.prepareRequestQueryParameters())
if err != nil {
return nil, err
}
Expand All @@ -150,7 +202,9 @@ func (c *Client) ListActiveScanners(params *ScannersSearchParams) (*ScannersResp
func (c *Client) IsValidTool(tool string) bool {
klog.Debugf("validating given tool name [%s]", tool)

scanners, err := c.ListActiveScanners(&ScannersSearchParams{Name: tool})
scanners, err := c.ListActiveScanners(&ListActiveScannersInput{
Name: tool,
})
if err != nil {
klog.Debugf("failed to get active tools: %v", err)
return false
Expand Down
54 changes: 37 additions & 17 deletions client/scans.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ import (
)

type (
ImageScanParams struct {
Project string `json:"project"`
Tool string `json:"tool"`
Branch string `json:"branch"`
Image string `json:"image"`
MetaData string `json:"meta_data"`
Environment string `json:"environment"`
}

ScanDetail struct {
ID string `json:"id"`
Name string `json:"name"`
Expand Down Expand Up @@ -156,7 +165,7 @@ func (c *Client) CreateNewScan(scan *Scan) (string, error) {

func (c *Client) RestartScanByScanID(id string) (string, error) {
klog.Debug("starting scan by scan_id")
path := fmt.Sprintf("/api/v1/scans/%s/restart", id)
path := fmt.Sprintf("/api/v2/scans/%s/restart", id)
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
return "", err
Expand All @@ -180,7 +189,7 @@ func (c *Client) RestartScanWithOption(id string, opt *ScanPROptions) (string, e
return "", errors.New("missing scan options")
}

path := fmt.Sprintf("/api/v1/scans/%s/restart_with_option", id)
path := fmt.Sprintf("/api/v2/scans/%s/restart_with_option", id)
req, err := c.newRequest(http.MethodPost, path, opt)
if err != nil {
return "", err
Expand All @@ -207,19 +216,30 @@ func (c *Client) RestartScanWithOption(id string, opt *ScanPROptions) (string, e
return rsr.Event, nil
}

type ImageScanParams struct {
Project string `json:"project"`
Tool string `json:"tool"`
Branch string `json:"branch"`
Image string `json:"image"`
MetaData string `json:"meta_data"`
Environment string `json:"environment"`
type ScanByImageInput struct {
Project string
Tool string
Branch string
Image string
MetaData string
Environment string
}

func (i *ScanByImageInput) prepareRequestQueryParameters() ImageScanParams {
return ImageScanParams{
Project: i.Project,
Tool: i.Tool,
Branch: i.Branch,
Image: i.Image,
MetaData: i.MetaData,
Environment: i.Environment,
}
}

func (c *Client) ScanByImage(pr *ImageScanParams) (string, error) {
path := "/api/v1/scans/image"
func (c *Client) ScanByImage(pr *ScanByImageInput) (string, error) {
path := "/api/v2/scans/image"

req, err := c.newRequest(http.MethodPost, path, pr)
req, err := c.newRequest(http.MethodPost, path, pr.prepareRequestQueryParameters())
if err != nil {
return "", fmt.Errorf("failed to create HTTP request: %w", err)
}
Expand Down Expand Up @@ -247,7 +267,7 @@ type ImportForm map[string]string
func (c *Client) ImportScanResult(file string, form ImportForm) (string, error) {
klog.Debugf("importing scan results using the file:%s", file)

path := "/api/v1/scans/import"
path := "/api/v2/scans/import"
rel := &url.URL{Path: path}
u := c.BaseURL.ResolveReference(rel)

Expand Down Expand Up @@ -312,7 +332,7 @@ func (c *Client) ListScans(project string, params *ScanSearchParams) ([]ScanDeta
klog.Debugf("retrieving scans of the project: %s", project)

scans := make([]ScanDetail, 0)
path := fmt.Sprintf("/api/v1/projects/%s/scans", project)
path := fmt.Sprintf("/api/v2/projects/%s/scans", project)
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
return scans, err
Expand All @@ -325,7 +345,7 @@ func (c *Client) ListScans(project string, params *ScanSearchParams) ([]ScanDeta
req.URL.RawQuery = v.Encode()

type getProjectScansResponse struct {
Scans []ScanDetail `json:"data"`
Scans []ScanDetail `json:"scans"`
Total int `json:"total"`
}
var ps getProjectScansResponse
Expand Down Expand Up @@ -360,7 +380,7 @@ func (c *Client) FindScan(project string, params *ScanSearchParams) (*ScanDetail
}

func (c *Client) FindScanByID(id string) (*ScanDetail, error) {
path := fmt.Sprintf("/api/v1/scans/%s", id)
path := fmt.Sprintf("/api/v2/scans/%s", id)
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -400,7 +420,7 @@ func (c *Client) GetScanStatus(eventId string) (*Event, error) {
}

func (c *Client) GetLastResults(id string) (map[string]*ResultSet, error) {
path := fmt.Sprintf("/api/v1/scans/%s/last_results", id)
path := fmt.Sprintf("/api/v2/scans/%s/last_results", id)
req, err := c.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion client/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *Client) CreateTeam(teamName, responsible string) error {
},
}

req, err := c.newRequest(http.MethodPost, "/api/v3/teams", team)
req, err := c.newRequest(http.MethodPost, "/api/v2/teams", team)
if err != nil {
return err
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/listScanners.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ var listScannersCmd = &cobra.Command{
qwe(ExitCodeError, err, "could not initialize Kondukto client")
}

scannerType := cmd.Flag("type").Value.String()
var scannerTypes []client.ScannerType
var scannerTypeFlag = cmd.Flag("type").Value.String()
if scannerTypeFlag != "" {
scannerTypes = []client.ScannerType{client.ScannerType(scannerTypeFlag)}
}

scannerLabels := cmd.Flag("labels").Value.String()
activeScanners, err := c.ListActiveScanners(&client.ScannersSearchParams{
Types: scannerType,
activeScanners, err := c.ListActiveScanners(&client.ListActiveScannersInput{
Types: scannerTypes,
Labels: scannerLabels,
})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ const (
VCSToolBitbucket = "bitbucket"
// VCSToolBitbucketServer represents the Bitbucket server VCS tool name
VCSToolBitbucketServer = "bitbucketserver"
// VCSToolGitHub represents the Github VCS tool name
// VCSToolGitHub represents the GitHub VCS tool name
VCSToolGitHub = "github"
// VCSToolGitLab represents the Gitlab Cloud VCS tool name
// VCSToolGitLabCloud represents the Gitlab Cloud VCS tool name
VCSToolGitLabCloud = "gitlabcloud"
// VCSToolGitLab represents the Gitlab On-Prem VCS tool name
// VCSToolGitLabOnPrem represents the Gitlab On-Prem VCS tool name
VCSToolGitLabOnPrem = "gitlabonprem"
// VCSToolGit represents the Git VCS tool name
VCSToolGit = "git"
Expand Down
17 changes: 13 additions & 4 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func releaseRootCommand(cmd *cobra.Command, _ []string) {
}

releaseCriteriaRows := []Row{
{Columns: []string{"STATUS", "SAST", "DAST", "PENTEST", "IAST", "SCA", "CS", "IAC"}},
{Columns: []string{"------", "----", "----", "-------", "----", "---", "--", "---"}},
{Columns: []string{rs.Status, rs.SAST.Status, rs.DAST.Status, rs.PENTEST.Status, rs.IAST.Status, rs.SCA.Status, rs.CS.Status, rs.IAC.Status}},
{Columns: []string{"STATUS", "SAST", "DAST", "PENTEST", "IAST", "SCA", "CS", "IAC", "MAST"}},
{Columns: []string{"------", "----", "----", "-------", "----", "---", "--", "---", "----"}},
{Columns: []string{rs.Status, rs.SAST.Status, rs.DAST.Status, rs.PENTEST.Status, rs.IAST.Status, rs.SCA.Status, rs.CS.Status, rs.IAC.Status, rs.MAST.Status}},
}
TableWriter(releaseCriteriaRows...)

Expand Down Expand Up @@ -106,7 +106,12 @@ func releaseRootCommand(cmd *cobra.Command, _ []string) {
qwm(ExitCodeError, "failed to parse iac flag")
}

isSpecific := sast || dast || pentest || iast || sca || cs || iac
mast, err := cmd.Flags().GetBool("mast")
if err != nil {
qwm(ExitCodeError, "failed to parse mast flag")
}

isSpecific := sast || dast || pentest || iast || sca || cs || iac || mast

var spesificMap = make(map[string]bool, 0)
spesificMap["SAST"] = sast
Expand All @@ -116,6 +121,7 @@ func releaseRootCommand(cmd *cobra.Command, _ []string) {
spesificMap["SCA"] = sca
spesificMap["CS"] = cs
spesificMap["IAC"] = iac
spesificMap["MAST"] = mast

isReleaseFailed(rs, isSpecific, spesificMap)
}
Expand Down Expand Up @@ -150,6 +156,9 @@ func isReleaseFailed(release *client.ReleaseStatus, isSpecific bool, specificMap
if release.IAC.Status == statusFail {
failedScans["IAC"] = release.IAC.ScanID
}
if release.MAST.Status == statusFail {
failedScans["MAST"] = release.MAST.ScanID
}

if verbose {
c, err := client.New()
Expand Down
11 changes: 7 additions & 4 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (s *Scan) scanByImage() (string, error) {
if image == "" {
return "", errors.New("image name is required")
}
var pr = &client.ImageScanParams{
var pr = &client.ScanByImageInput{
Project: project.ID,
Tool: tool,
Branch: branch,
Expand Down Expand Up @@ -1025,7 +1025,7 @@ func (s *Scan) checkForRescanOnlyTool() (bool, *client.ScannerInfo, error) {
if err != nil || name == "" {
return false, nil, errors.New("missing require tool flag")
}
scanners, err := s.client.ListActiveScanners(&client.ScannersSearchParams{Name: name, Limit: 1})
scanners, err := s.client.ListActiveScanners(&client.ListActiveScannersInput{Name: name, Limit: 1})
if err != nil {
return false, nil, fmt.Errorf("failed to get active scanners: %w", err)
}
Expand Down Expand Up @@ -1237,6 +1237,9 @@ func isScanReleaseFailed(scan *client.ScanDetail, release *client.ReleaseStatus,
if release.IAC.Status == statusFail {
failedScans["IAC"] = scan.ID
}
if release.MAST.Status == statusFail {
failedScans["MAST"] = scan.ID
}

if breakByScannerType {
scannerType := strings.ToUpper(scan.ScannerType)
Expand Down Expand Up @@ -1449,8 +1452,8 @@ func appendKeyToParamsMap(key string, custom client.Custom, parsedValue interfac
custom.Params[key0] = key0map

default:
klog.Debugf("unsupportted key: [%s]", key)
qwm(ExitCodeError, "unsupportted key, key can only contain one or two dots")
klog.Debugf("unsupported key: [%s]", key)
qwm(ExitCodeError, "unsupported key, key can only contain one or two dots")
}
return custom
}

0 comments on commit 38d8193

Please sign in to comment.