Skip to content

Commit

Permalink
feat(COD-1156): support sast tables in cds
Browse files Browse the repository at this point in the history
Add the parameter to specify the url type for sast tables
  • Loading branch information
Sergio Giro authored and sergiogiro committed Sep 29, 2023
1 parent 95b2f48 commit 30456b8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
36 changes: 33 additions & 3 deletions api/component_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ type ComponentDataService struct {
client *Client
}

const URL_TYPE_DEFAULT = "Default"
const URL_TYPE_SAST_TABLES = "SastTables"

var URL_TYPES = []string{URL_TYPE_DEFAULT, URL_TYPE_SAST_TABLES}

type ComponentDataInitialRequest struct {
Name string `json:"name"`
Tags []string `json:"tags"`
SupportedMethods []string `json:"supportedMethods"`
Documents []*DocumentSpec `json:"documents"`
UrlType string `json:"urlType"`
}

type DocumentSpec struct {
Expand All @@ -43,6 +49,7 @@ type ComponentDataUploadMethod struct {

type ComponentDataCompleteRequest struct {
UploadGuid string `json:"uploadGuid"`
UrlType string `json:"urlType"`
}

type ComponentDataCompleteResponseRaw struct {
Expand All @@ -53,8 +60,29 @@ type ComponentDataCompleteResponse struct {
Guid string `json:"guid,omitempty"`
}

func (svc *ComponentDataService) UploadFiles(name string, tags []string, paths []string) (string, error) {
initialRequest, err := buildComponentDataInitialRequest(name, tags, paths)
func (svc *ComponentDataService) UploadFiles(
name string, tags []string, paths []string) (string, error) {
return svc.doUploadFiles(name, tags, paths, URL_TYPE_DEFAULT)
}

func (svc *ComponentDataService) UploadSastTables(
name string, paths []string) (string, error) {
return svc.doUploadFiles(name, []string{"sast"}, paths, URL_TYPE_SAST_TABLES)
}

func (svc *ComponentDataService) doUploadFiles(
name string, tags []string, paths []string, urlType string) (string, error) {
var hasValidType = false
for _, validType := range URL_TYPES {
if urlType == validType {
hasValidType = true
break
}
}
if !hasValidType {
return "", errors.Errorf("Invalid URL type: (%s)", urlType)
}
initialRequest, err := buildComponentDataInitialRequest(name, tags, paths, urlType)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -88,6 +116,7 @@ func (svc *ComponentDataService) UploadFiles(name string, tags []string, paths [
}
completeRequest := ComponentDataCompleteRequest{
UploadGuid: initialResponse.Data.Guid,
UrlType: urlType,
}
var completeResponse ComponentDataCompleteResponseRaw
err = doWithExponentialBackoffWaiting(func() error {
Expand All @@ -107,7 +136,7 @@ func (svc *ComponentDataService) UploadFiles(name string, tags []string, paths [
}

func buildComponentDataInitialRequest(
name string, tags []string, paths []string,
name string, tags []string, paths []string, urlType string,
) (*ComponentDataInitialRequest, error) {
documents := make([]*DocumentSpec, 0, len(paths))
for _, path := range paths {
Expand All @@ -125,6 +154,7 @@ func buildComponentDataInitialRequest(
Tags: tags,
SupportedMethods: []string{"AwsS3"},
Documents: documents,
UrlType: urlType,
}, nil
}

Expand Down
60 changes: 60 additions & 0 deletions api/component_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,66 @@ func TestUploadFiles(t *testing.T) {
assert.Equal(t, "SOME-GUID", guid)
}

func TestDefaultUrlType(t *testing.T) {
fakeServer := lacework.MockServer()
fakeServer.MockToken("TOKEN")
defer fakeServer.Close()
fakeServer.MockAPI("ComponentData/requestUpload", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
assert.NotNil(t, r.Body)
body := httpBodySniffer(r)
assert.Contains(t, body, api.URL_TYPE_DEFAULT)
_, err := fmt.Fprintf(w, generateInitialResponse())
assert.Nil(t, err)
})
fakeServer.MockAPI("ComponentData/completeUpload", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
assert.NotNil(t, r.Body)
body := httpBodySniffer(r)
assert.Contains(t, body, api.URL_TYPE_DEFAULT)
_, err := fmt.Fprintf(w, generateCompleteResponse())
assert.Nil(t, err)
})
c, err := api.NewClient("test",
api.WithToken("TOKEN"),
api.WithURL(fakeServer.URL()),
)
assert.Nil(t, err)
guid, err := c.V2.ComponentData.UploadFiles("doc-set", []string{"sast"}, []string{})
assert.Nil(t, err)
assert.Equal(t, "SOME-GUID", guid)
}

func TestSastTablesUrlType(t *testing.T) {
fakeServer := lacework.MockServer()
fakeServer.MockToken("TOKEN")
defer fakeServer.Close()
fakeServer.MockAPI("ComponentData/requestUpload", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
assert.NotNil(t, r.Body)
body := httpBodySniffer(r)
assert.Contains(t, body, api.URL_TYPE_SAST_TABLES)
_, err := fmt.Fprintf(w, generateInitialResponse())
assert.Nil(t, err)
})
fakeServer.MockAPI("ComponentData/completeUpload", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
assert.NotNil(t, r.Body)
body := httpBodySniffer(r)
assert.Contains(t, body, api.URL_TYPE_SAST_TABLES)
_, err := fmt.Fprintf(w, generateCompleteResponse())
assert.Nil(t, err)
})
c, err := api.NewClient("test",
api.WithToken("TOKEN"),
api.WithURL(fakeServer.URL()),
)
assert.Nil(t, err)
guid, err := c.V2.ComponentData.UploadSastTables("doc-set", []string{})
assert.Nil(t, err)
assert.Equal(t, "SOME-GUID", guid)
}

func TestDoWithExponentialBackoffAlwaysFailing(t *testing.T) {
waited := 0
err := api.DoWithExponentialBackoff(func() error {
Expand Down

0 comments on commit 30456b8

Please sign in to comment.