-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support score api, refine severity part in audit pkg
- Loading branch information
Showing
10 changed files
with
245 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package audit | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
"github.com/go-chi/render" | ||
) | ||
|
||
// decode detects the correct decoder for use on an HTTP request and | ||
// marshals into a given interface. | ||
func decode(r *http.Request, payload *Payload) error { | ||
// Check if the content type is plain text, read it as such. | ||
if render.GetRequestContentType(r) == render.ContentTypePlainText { | ||
// Read the request body. | ||
body, err := io.ReadAll(r.Body) | ||
defer r.Body.Close() // Ensure the body is closed after reading. | ||
if err != nil { | ||
// // Handle any reading errors by sending a failure response. | ||
// render.Render(w, r, handler.FailureResponse(ctx, err)) | ||
return err | ||
} | ||
// Set the read content as the manifest payload. | ||
payload.Manifest = string(body) | ||
} else { | ||
// For non-plain text, decode the JSON body into the payload. | ||
if err := render.DecodeJSON(r.Body, payload); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,25 @@ | ||
package audit | ||
|
||
// ScoreData encapsulates the results of scoring an audited manifest. It provides | ||
// a numerical score along with statistics about the total number of issues and | ||
// their severities. | ||
type ScoreData struct { | ||
// Score represents the calculated score of the audited manifest based on | ||
// the number and severity of issues. It provides a quantitative measure | ||
// of the security posture of the resources in the manifest. | ||
Score float64 `json:"score"` | ||
|
||
// IssuesTotal is the total count of all issues found during the audit. | ||
// This count can be used to understand the overall number of problems | ||
// that need to be addressed. | ||
IssuesTotal int `json:"issuesTotal"` | ||
|
||
// SeveritySum is the sum of severity scores of all issues, which can be | ||
// used to gauge the cumulative severity of all problems found. | ||
SeveritySum int `json:"severitySum"` | ||
|
||
// SeverityStatistic is a mapping of severity levels to their respective | ||
// number of occurrences. It allows for a quick overview of the distribution | ||
// of issues across different severity categories. | ||
SeverityStatistic map[string]int `json:"severityStatistic"` | ||
} |
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,18 @@ | ||
package audit | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
// P is the number of issues, and S is the sum of the severity (range 1-5) of | ||
// the issue S will not be less than P. | ||
// | ||
// Example: | ||
// - When there is one high-level issue, P=1 and S=3. | ||
// - When there are three high-level issues, P=3 and S=9. | ||
// - When there are ten low-level issues, P=10 and S=10. | ||
func CalculateScore(p, s int) float64 { | ||
a, b := -0.04, -0.06 | ||
param := a*float64(p) + b*float64(s) | ||
return 100 * math.Exp(param) | ||
} |
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
Oops, something went wrong.