-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api, ui): add vulnerability trend (#3130)
- Loading branch information
Showing
32 changed files
with
727 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
|
||
"github.com/ovh/cds/engine/api/application" | ||
"github.com/ovh/cds/sdk" | ||
) | ||
|
||
func (api *API) postVulnerabilityHandler() Handler { | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
vars := mux.Vars(r) | ||
key := vars["key"] | ||
appName := vars["permApplicationName"] | ||
|
||
vulnID, errV := requestVarInt(r, "id") | ||
if errV != nil { | ||
return sdk.WrapError(errV, "postVulnerabilityHandler> Unable to read ID") | ||
} | ||
|
||
var v sdk.Vulnerability | ||
if err := UnmarshalBody(r, &v); err != nil { | ||
return sdk.WrapError(err, "postVulnerabilityHandler> Unable to read body") | ||
} | ||
|
||
app, errL := application.LoadByName(api.mustDB(), api.Cache, key, appName, getUser(ctx)) | ||
if errL != nil { | ||
return sdk.WrapError(errL, "postVulnerabilityHandler> Unable to load application") | ||
} | ||
|
||
vulnDB, errV := application.LoadVulnerability(api.mustDB(), app.ID, vulnID) | ||
if errV != nil { | ||
return sdk.WrapError(errV, "postVulnerabilityHandler> Unable to load vulnerability") | ||
} | ||
|
||
vulnDB.Ignored = v.Ignored | ||
|
||
if err := application.UpdateVulnerability(api.mustDB(), vulnDB); err != nil { | ||
return sdk.WrapError(err, "postVulnerabilityHandler> Unable to update vulnerability") | ||
} | ||
|
||
return WriteJSON(w, vulnDB, http.StatusOK) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"bytes" | ||
"fmt" | ||
"github.com/ovh/cds/engine/api/application" | ||
"github.com/ovh/cds/engine/api/test" | ||
"github.com/ovh/cds/engine/api/test/assets" | ||
"github.com/ovh/cds/sdk" | ||
) | ||
|
||
func Test_postVulnerabilityHandler(t *testing.T) { | ||
api, db, router := newTestAPI(t) | ||
|
||
//Create admin user | ||
u, pass := assets.InsertAdminUser(api.mustDB()) | ||
|
||
//Insert Project | ||
pkey := sdk.RandomString(10) | ||
proj := assets.InsertTestProject(t, db, api.Cache, pkey, pkey, u) | ||
|
||
app := &sdk.Application{ | ||
Name: sdk.RandomString(10), | ||
} | ||
if err := application.Insert(api.mustDB(), api.Cache, proj, app, u); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
v := sdk.Vulnerability{} | ||
v.ApplicationID = app.ID | ||
|
||
assert.NoError(t, application.InsertVulnerabilities(db, []sdk.Vulnerability{v}, app.ID)) | ||
|
||
vulns, err := application.LoadVulnerabilities(db, app.ID) | ||
assert.NoError(t, err) | ||
|
||
vars := map[string]string{ | ||
"key": proj.Key, | ||
"permApplicationName": app.Name, | ||
"id": fmt.Sprintf("%d", vulns[0].ID), | ||
} | ||
|
||
uri := router.GetRoute("POST", api.postVulnerabilityHandler, vars) | ||
|
||
vulns[0].Ignored = true | ||
jsonBody, _ := json.Marshal(vulns[0]) | ||
body := bytes.NewBuffer(jsonBody) | ||
|
||
req, err := http.NewRequest("POST", uri, body) | ||
test.NoError(t, err) | ||
assets.AuthentifyRequest(t, req, u, pass) | ||
|
||
// Do the request | ||
w := httptest.NewRecorder() | ||
router.Mux.ServeHTTP(w, req) | ||
assert.Equal(t, 200, w.Code) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.