-
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: implement sysdig_secure_accept_vulnerability_risk resource
- Loading branch information
1 parent
358a8d0
commit d980522
Showing
6 changed files
with
806 additions
and
0 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,98 @@ | ||
package v2 | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type ( | ||
EntityType string | ||
ReasonType string | ||
StatusType string | ||
StageType string | ||
ContextType string | ||
) | ||
|
||
const ( | ||
EntityTypeImageName EntityType = "imageName" | ||
EntityTypeImagePrefix EntityType = "imagePrefix" | ||
EntityTypeImageSuffix EntityType = "imageSuffix" | ||
EntityTypeImageNameContains EntityType = "imageNameContains" | ||
EntityTypeVulnerability EntityType = "vulnerability" | ||
EntityTypeHostName EntityType = "hostName" | ||
EntityTypeHostNameContains EntityType = "hostNameContains" | ||
EntityTypePolicyRule EntityType = "policyRule" | ||
) | ||
|
||
const ( | ||
ReasonRiskTransferred ReasonType = "RiskTransferred" | ||
ReasonRiskAvoided ReasonType = "RiskAvoided" | ||
ReasonRiskMitigated ReasonType = "RiskMitigated" | ||
ReasonRiskOwned ReasonType = "RiskOwned" | ||
ReasonRiskNotRelevant ReasonType = "RiskNotRelevant" | ||
ReasonCustom ReasonType = "Custom" | ||
) | ||
|
||
func ReasonTypeFromString(value string) (ReasonType, error) { | ||
t := ReasonType(value) | ||
switch t { | ||
case ReasonRiskTransferred, ReasonRiskAvoided, ReasonRiskMitigated, ReasonRiskOwned, ReasonRiskNotRelevant, ReasonCustom: | ||
return t, nil | ||
default: | ||
return "", fmt.Errorf("unsupported reason type: %s", value) | ||
} | ||
} | ||
|
||
const ( | ||
StatusActive StatusType = "active" | ||
StatusExpired StatusType = "expired" | ||
) | ||
|
||
const ( | ||
ContextTypeImageName ContextType = "imageName" | ||
ContextTypeImagePrefix ContextType = "imagePrefix" | ||
ContextTypeImageSuffix ContextType = "imageSuffix" | ||
ContextTypeImageNameContains ContextType = "imageNameContains" | ||
ContextTypeHostName ContextType = "hostName" | ||
ContextTypeHostNameContains ContextType = "hostNameContains" | ||
ContextTypePackageName ContextType = "packageName" | ||
ContextTypePackageVersion ContextType = "packageVersion" | ||
) | ||
|
||
type AcceptVulnerabilityRiskRequest struct { | ||
EntityType EntityType `json:"entityType"` | ||
EntityValue string `json:"entityValue"` | ||
Reason ReasonType `json:"reason"` | ||
Description string `json:"description"` | ||
ExpirationDate string `json:"expirationDate,omitempty"` | ||
Context []AcceptVulnerabilityRiskContext `json:"context"` | ||
Stages []StageType `json:"stages,omitempty"` | ||
} | ||
|
||
type UpdateAcceptVulnerabilityRiskRequest struct { | ||
ID string `json:"id"` | ||
ExpirationDate string `json:"expirationDate,omitempty"` | ||
Reason ReasonType `json:"reason"` | ||
Description string `json:"description"` | ||
} | ||
|
||
type AcceptVulnerabilityRisk struct { | ||
ID string `json:"id"` | ||
EntityType EntityType `json:"entityType"` | ||
EntityValue string `json:"entityValue"` | ||
Reason ReasonType `json:"reason"` | ||
Description string `json:"description"` | ||
ExpirationDate string `json:"expirationDate,omitempty"` | ||
Status StatusType `json:"status"` | ||
CreatedAt time.Time `json:"createdAt,omitempty"` | ||
UpdatedAt time.Time `json:"updatedAt,omitempty"` | ||
CreatedBy string `json:"createdBy,omitempty"` | ||
UpdatedBy string `json:"updatedBy,omitempty"` | ||
Context []AcceptVulnerabilityRiskContext `json:"context"` | ||
Stages []StageType `json:"stages,omitempty"` | ||
} | ||
|
||
type AcceptVulnerabilityRiskContext struct { | ||
ContextType ContextType `json:"contextType"` | ||
ContextValue string `json:"contextValue"` | ||
} |
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,107 @@ | ||
package v2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type PostureVulnerabilityAcceptRiskInterface interface { | ||
Base | ||
|
||
SaveAcceptVulnerabilityRisk(ctx context.Context, p *AcceptVulnerabilityRiskRequest) (*AcceptVulnerabilityRisk, string, error) | ||
GetAcceptanceVulnerabilityRisk(ctx context.Context, id string) (*AcceptVulnerabilityRisk, string, error) | ||
DeleteAcceptanceVulnerabilityRisk(ctx context.Context, id string) error | ||
UpdateAcceptanceVulnerabilityRisk(ctx context.Context, p *UpdateAcceptVulnerabilityRiskRequest) (*AcceptVulnerabilityRisk, string, error) | ||
} | ||
|
||
const ( | ||
AcceptVulnerabilityRiskCreatePath = "%s/secure/vulnerability/v1beta1/accepted-risks" | ||
AcceptVulnerabilityRiskGetPath = "%s/secure/vulnerability/v1beta1/accepted-risks/%s" | ||
AcceptVulnerabilityRiskDeletePath = "%s/secure/vulnerability/v1beta1/accepted-risks/%s" | ||
AcceptVulnerabilityRiskUpdatePath = "%s/secure/vulnerability/v1beta1/accepted-risks/%s" | ||
) | ||
|
||
func (c *Client) SaveAcceptVulnerabilityRisk(ctx context.Context, p *AcceptVulnerabilityRiskRequest) (*AcceptVulnerabilityRisk, string, error) { | ||
payload, err := Marshal(p) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
response, err := c.requester.Request(ctx, http.MethodPost, fmt.Sprintf(AcceptVulnerabilityRiskCreatePath, c.config.url), payload) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
defer response.Body.Close() | ||
|
||
if response.StatusCode != http.StatusCreated { | ||
errStatus, err := c.ErrorAndStatusFromResponse(response) | ||
return nil, errStatus, err | ||
} | ||
|
||
resp, err := Unmarshal[AcceptVulnerabilityRisk](response.Body) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
return &resp, "", nil | ||
} | ||
|
||
func (c *Client) GetAcceptanceVulnerabilityRisk(ctx context.Context, id string) (*AcceptVulnerabilityRisk, string, error) { | ||
response, err := c.requester.Request(ctx, http.MethodGet, fmt.Sprintf(AcceptVulnerabilityRiskGetPath, c.config.url, id), nil) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
defer response.Body.Close() | ||
|
||
if response.StatusCode != http.StatusOK { | ||
errStatus, err := c.ErrorAndStatusFromResponse(response) | ||
return nil, errStatus, err | ||
} | ||
|
||
resp, err := Unmarshal[AcceptVulnerabilityRisk](response.Body) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
return &resp, "", nil | ||
} | ||
|
||
func (c *Client) DeleteAcceptanceVulnerabilityRisk(ctx context.Context, id string) error { | ||
response, err := c.requester.Request(ctx, http.MethodDelete, fmt.Sprintf(AcceptVulnerabilityRiskDeletePath, c.config.url, id), nil) | ||
if err != nil { | ||
return err | ||
} | ||
defer response.Body.Close() | ||
|
||
if response.StatusCode != http.StatusNoContent { | ||
return c.ErrorFromResponse(response) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) UpdateAcceptanceVulnerabilityRisk(ctx context.Context, p *UpdateAcceptVulnerabilityRiskRequest) (*AcceptVulnerabilityRisk, string, error) { | ||
payload, err := Marshal(p) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
response, err := c.requester.Request(ctx, http.MethodPut, fmt.Sprintf(AcceptVulnerabilityRiskUpdatePath, c.config.url, p.ID), payload) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
defer response.Body.Close() | ||
|
||
if response.StatusCode != http.StatusOK { | ||
errStatus, err := c.ErrorAndStatusFromResponse(response) | ||
return nil, errStatus, err | ||
} | ||
|
||
resp, err := Unmarshal[AcceptVulnerabilityRisk](response.Body) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
return &resp, "", 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
Oops, something went wrong.