Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions pkg/api/escape/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// Debug is a flag to enable debug mode for the API client
var Debug = false

// newAPIV3Client creates a new API v3 client
func newAPIV3Client() (*v3.APIClient, error) {
log.Trace("Initializing v3 client")
url, err := env.GetAPIURL()
Expand Down
16 changes: 8 additions & 8 deletions pkg/api/escape/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (

// ListAssetsFilters holds optional filters for listing assets
type ListAssetsFilters struct {
AssetTypes []string
AssetStatuses []string
Search string
AssetTypes []string
AssetStatuses []string
Search string
ManuallyCreated bool
}

Expand Down Expand Up @@ -135,9 +135,9 @@ func UpdateAsset(
// normalizeAssetType normalizes asset type tokens to match generated method names
// e.g. KUBERNETES_CLUSTER -> KUBERNETESCLUSTER, http-endpoint -> httpendpoint
func normalizeAssetType(s string) string {
s = strings.ReplaceAll(s, "_", "")
s = strings.ReplaceAll(s, "-", "")
return s
s = strings.ReplaceAll(s, "_", "")
s = strings.ReplaceAll(s, "-", "")
return s
}

// CreateAsset creates an asset
Expand Down Expand Up @@ -168,7 +168,7 @@ func CreateAsset(ctx context.Context, data []byte, assetType string) (interface{
// unmarshal raw JSON to typed payload
payloadType := setter.Type().In(0)
payloadPtr := reflect.New(payloadType)

err = json.Unmarshal(data, payloadPtr.Interface())
if err != nil {
return nil, fmt.Errorf("invalid JSON for %s: %w", payloadType.Name(), err)
Expand All @@ -182,7 +182,7 @@ func CreateAsset(ctx context.Context, data []byte, assetType string) (interface{
return nil, errors.New("failed to find Execute method")
}
results := executeMethod.Call(nil)

return results[0].Interface(), nil
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/escape/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

// ListAuditLogsFilters holds optional filters for listing audit logs
type ListAuditLogsFilters struct {
DateFrom string
DateTo string
DateFrom string
DateTo string
ActionType string
Actor string
Search string
Actor string
Search string
}

// ListAuditLogs lists audit logs
Expand Down
64 changes: 32 additions & 32 deletions pkg/api/escape/custom-rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,42 @@ func GetCustomRule(ctx context.Context, id string) (*v3.CreateCustomRule200Respo

// CreateCustomRule creates a custom rule from raw JSON
func CreateCustomRule(ctx context.Context, data []byte) (*v3.CreateCustomRule200Response, error) {
client, err := newAPIV3Client()
if err != nil {
return nil, fmt.Errorf("unable to init client: %w", err)
}
var payload v3.CreateCustomRuleRequest
if err := json.Unmarshal(data, &payload); err != nil {
return nil, fmt.Errorf("invalid JSON: %w", err)
}
req := client.CustomRulesAPI.CreateCustomRule(ctx)
res, _, err := req.CreateCustomRuleRequest(payload).Execute()
if err != nil {
return nil, fmt.Errorf("api error: %w", err)
}
return res, nil
client, err := newAPIV3Client()
if err != nil {
return nil, fmt.Errorf("unable to init client: %w", err)
}
var payload v3.CreateCustomRuleRequest
if err := json.Unmarshal(data, &payload); err != nil {
return nil, fmt.Errorf("invalid JSON: %w", err)
}
req := client.CustomRulesAPI.CreateCustomRule(ctx)
res, _, err := req.CreateCustomRuleRequest(payload).Execute()
if err != nil {
return nil, fmt.Errorf("api error: %w", err)
}
return res, nil
}

// UpdateCustomRule updates a custom rule from raw JSON
func UpdateCustomRule(ctx context.Context, id string, data []byte) (*v3.CreateCustomRule200Response, error) {
client, err := newAPIV3Client()
if err != nil {
return nil, fmt.Errorf("unable to init client: %w", err)
}
var payload v3.UpdateCustomRuleRequest
if err := json.Unmarshal(data, &payload); err != nil {
return nil, fmt.Errorf("invalid JSON: %w", err)
}
req := client.CustomRulesAPI.UpdateCustomRule(ctx, id)
res, httpRes, err := req.UpdateCustomRuleRequest(payload).Execute()
if err != nil {
if httpRes != nil && httpRes.Body != nil {
body, _ := io.ReadAll(httpRes.Body)
return nil, fmt.Errorf("api error: %s", string(body))
}
return nil, fmt.Errorf("api error: %w", err)
}
return res, nil
client, err := newAPIV3Client()
if err != nil {
return nil, fmt.Errorf("unable to init client: %w", err)
}
var payload v3.UpdateCustomRuleRequest
if err := json.Unmarshal(data, &payload); err != nil {
return nil, fmt.Errorf("invalid JSON: %w", err)
}
req := client.CustomRulesAPI.UpdateCustomRule(ctx, id)
res, httpRes, err := req.UpdateCustomRuleRequest(payload).Execute()
if err != nil {
if httpRes != nil && httpRes.Body != nil {
body, _ := io.ReadAll(httpRes.Body)
return nil, fmt.Errorf("api error: %s", string(body))
}
return nil, fmt.Errorf("api error: %w", err)
}
return res, nil
}

// DeleteCustomRule deletes a custom rule
Expand Down
16 changes: 8 additions & 8 deletions pkg/api/escape/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (

// ListEventsFilters holds optional filters for listing events
type ListEventsFilters struct {
Search string
ScanIDs []string
AssetIDs []string
IssueIDs []string
Levels []string
Stages []string
Search string
ScanIDs []string
AssetIDs []string
IssueIDs []string
Levels []string
Stages []string
HasAttachments bool
Attachments []string
Attachments []string
}

// ListEvents lists events
Expand Down Expand Up @@ -78,4 +78,4 @@ func GetEvent(ctx context.Context, eventID string) (*v3.EventDetailed, error) {
return nil, fmt.Errorf("api error: %w", err)
}
return data, nil
}
}
85 changes: 85 additions & 0 deletions pkg/api/escape/integrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package escape

import (
"context"
"fmt"
"strings"

v3 "github.com/Escape-Technologies/cli/pkg/api/v3"
"github.com/Escape-Technologies/cli/pkg/log"
)

// ListKubernetesIntegrationsFilters holds optional filters for listing Kubernetes integrations
type ListKubernetesIntegrationsFilters struct {
ProjectIDs []string
LocationIDs string
Search string
}

// UpsertKubernetesIntegration creates a Kubernetes integration if it doesn't exist
func UpsertKubernetesIntegration(ctx context.Context, req v3.CreatekubernetesIntegrationRequest) (*v3.CreatekubernetesIntegration200Response, error) {
list, _, err := listKubernetesIntegrations(ctx, "", &ListKubernetesIntegrationsFilters{
LocationIDs: strings.Join([]string{*req.ProxyId}, ","),
})
if err != nil {
return nil, fmt.Errorf("api error: %w", err)
}
if len(list) > 0 {
for _, integration := range list {
if integration.Location.Id == *req.ProxyId {
log.Info("Kubernetes integration already exists")
return nil, nil
}
}
}
log.Info("Creating Kubernetes integration..")
resp, err := createKubernetesIntegration(ctx, req)
if err != nil {
return nil, fmt.Errorf("unable to create Kubernetes integration: %w", err)
}
log.Info("Kubernetes integration created")
return resp, nil
}

func createKubernetesIntegration(ctx context.Context, req v3.CreatekubernetesIntegrationRequest) (*v3.CreatekubernetesIntegration200Response, error) {
client, err := newAPIV3Client()
if err != nil {
return nil, fmt.Errorf("unable to init client: %w", err)
}
resp, _, err := client.IntegrationsAPI.CreatekubernetesIntegration(ctx).
CreatekubernetesIntegrationRequest(req).
Execute()
if err != nil {
return nil, fmt.Errorf("api error: %w", err)
}
return resp, nil
}

// ListKubernetesIntegrations lists Kubernetes integrations
func listKubernetesIntegrations(ctx context.Context, next string, filters *ListKubernetesIntegrationsFilters) ([]v3.ListIntegrations200ResponseDataInner, *string, error) {
client, err := newAPIV3Client()
if err != nil {
return nil, nil, fmt.Errorf("unable to init client: %w", err)
}
rSize := 50
req := client.IntegrationsAPI.ListkubernetesIntegrations(ctx).Size(rSize)
if next != "" {
req = req.Cursor(next)
}
if filters != nil {
if len(filters.ProjectIDs) > 0 {
req = req.ProjectIds(filters.ProjectIDs)
}
if len(filters.LocationIDs) > 0 {
req = req.LocationIds([]string{filters.LocationIDs})
}
if filters.Search != "" {
req = req.Search(filters.Search)
}
}
data, _, err := req.Execute()
if err != nil {
return nil, nil, fmt.Errorf("api error: %w", err)
}
return data.Data, data.NextCursor, nil
}
2 changes: 1 addition & 1 deletion pkg/api/escape/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func ListIssues(ctx context.Context, next string, filters *ListIssuesFilters) ([
req = req.ScanIds(strings.Join(filters.ScanIDs, ","))
}
if len(filters.TagsIDs) > 0 {
req = req.TagsIds(strings.Join(filters.TagsIDs, ","))
req = req.TagIds(strings.Join(filters.TagsIDs, ","))
}
if filters.Search != "" {
req = req.Search(filters.Search)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/escape/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

// ListLocationsFilters holds optional filters for listing locations
type ListLocationsFilters struct {
Search string
Enabled bool
Search string
Enabled bool
LocationTypes []string
}

Expand Down
34 changes: 17 additions & 17 deletions pkg/api/escape/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (

// ListProfilesFilters holds optional filters for listing profiles
type ListProfilesFilters struct {
AssetIDs []string
Domains []string
IssueIDs []string
TagsIDs []string
Search string
AssetIDs []string
Domains []string
IssueIDs []string
TagsIDs []string
Search string
Initiators []string
Kinds []string
Risks []string
Kinds []string
Risks []string
}

// ListProfiles lists all profiles
Expand Down Expand Up @@ -126,13 +126,13 @@ func CreateProfileGraphql(ctx context.Context, data []byte) (interface{}, error)
return nil, fmt.Errorf("unable to init client: %w", err)
}

var payload v3.CreateDastGraphqlProfileRequest
var payload v3.CreateDastGraphqlProfileRequest
if err := json.Unmarshal(data, &payload); err != nil {
return nil, fmt.Errorf("invalid JSON: %w", err)
}

req := client.ProfilesAPI.CreateDastGraphqlProfile(ctx)
profile, _, err := req.CreateDastGraphqlProfileRequest(payload).Execute()
profile, _, err := req.CreateDastGraphqlProfileRequest(payload).Execute()
if err != nil {
return nil, fmt.Errorf("api error: %w", err)
}
Expand All @@ -156,14 +156,14 @@ func DeleteProfile(ctx context.Context, profileID string) error {

// ListProblemsFilters holds optional filters for listing problems
type ListProblemsFilters struct {
AssetIDs []string
Domains []string
IssueIDs []string
TagsIDs []string
Search string
AssetIDs []string
Domains []string
IssueIDs []string
TagsIDs []string
Search string
Initiators []string
Kinds []string
Risks []string
Kinds []string
Risks []string
}

// ListProblems lists all scan problems
Expand Down Expand Up @@ -207,4 +207,4 @@ func ListProblems(ctx context.Context, next string, filters *ListProblemsFilters
return nil, nil, fmt.Errorf("api error: %w", err)
}
return data.Data, data.NextCursor, nil
}
}
2 changes: 1 addition & 1 deletion pkg/api/escape/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func CreateTag(ctx context.Context, name string, color string) (*v3.TagDetail, e
}
req := client.TagsAPI.CreateTag(ctx)
data, _, err := req.CreateTagRequest(v3.CreateTagRequest{
Name: name,
Name: name,
Color: color,
}).Execute()
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions pkg/api/escape/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func GetUploadSignedURL(ctx context.Context) (*v3.UploadSummary, error) {

// UploadSchema uploads a file to the signed url
func UploadSchema(ctx context.Context, url string, data []byte) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewBuffer(data))
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
if err != nil {
return fmt.Errorf("unable to create request: %w", err)
Expand All @@ -43,5 +43,3 @@ func UploadSchema(ctx context.Context, url string, data []byte) error {

return nil
}


Loading
Loading