Skip to content

Commit 21b877a

Browse files
authored
Merge pull request #81 from Escape-Technologies/feat/k8s-use-integration
feat: use k8s revamped integration
2 parents 030eef4 + 58dbd1f commit 21b877a

304 files changed

Lines changed: 73158 additions & 5876 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pkg/api/escape/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
// Debug is a flag to enable debug mode for the API client
1414
var Debug = false
1515

16+
// newAPIV3Client creates a new API v3 client
1617
func newAPIV3Client() (*v3.APIClient, error) {
1718
log.Trace("Initializing v3 client")
1819
url, err := env.GetAPIURL()

pkg/api/escape/assets.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import (
1616

1717
// ListAssetsFilters holds optional filters for listing assets
1818
type ListAssetsFilters struct {
19-
AssetTypes []string
20-
AssetStatuses []string
21-
Search string
19+
AssetTypes []string
20+
AssetStatuses []string
21+
Search string
2222
ManuallyCreated bool
2323
}
2424

@@ -135,9 +135,9 @@ func UpdateAsset(
135135
// normalizeAssetType normalizes asset type tokens to match generated method names
136136
// e.g. KUBERNETES_CLUSTER -> KUBERNETESCLUSTER, http-endpoint -> httpendpoint
137137
func normalizeAssetType(s string) string {
138-
s = strings.ReplaceAll(s, "_", "")
139-
s = strings.ReplaceAll(s, "-", "")
140-
return s
138+
s = strings.ReplaceAll(s, "_", "")
139+
s = strings.ReplaceAll(s, "-", "")
140+
return s
141141
}
142142

143143
// CreateAsset creates an asset
@@ -168,7 +168,7 @@ func CreateAsset(ctx context.Context, data []byte, assetType string) (interface{
168168
// unmarshal raw JSON to typed payload
169169
payloadType := setter.Type().In(0)
170170
payloadPtr := reflect.New(payloadType)
171-
171+
172172
err = json.Unmarshal(data, payloadPtr.Interface())
173173
if err != nil {
174174
return nil, fmt.Errorf("invalid JSON for %s: %w", payloadType.Name(), err)
@@ -182,7 +182,7 @@ func CreateAsset(ctx context.Context, data []byte, assetType string) (interface{
182182
return nil, errors.New("failed to find Execute method")
183183
}
184184
results := executeMethod.Call(nil)
185-
185+
186186
return results[0].Interface(), nil
187187
}
188188
}

pkg/api/escape/audit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99

1010
// ListAuditLogsFilters holds optional filters for listing audit logs
1111
type ListAuditLogsFilters struct {
12-
DateFrom string
13-
DateTo string
12+
DateFrom string
13+
DateTo string
1414
ActionType string
15-
Actor string
16-
Search string
15+
Actor string
16+
Search string
1717
}
1818

1919
// ListAuditLogs lists audit logs

pkg/api/escape/custom-rules.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,42 @@ func GetCustomRule(ctx context.Context, id string) (*v3.CreateCustomRule200Respo
3939

4040
// CreateCustomRule creates a custom rule from raw JSON
4141
func CreateCustomRule(ctx context.Context, data []byte) (*v3.CreateCustomRule200Response, error) {
42-
client, err := newAPIV3Client()
43-
if err != nil {
44-
return nil, fmt.Errorf("unable to init client: %w", err)
45-
}
46-
var payload v3.CreateCustomRuleRequest
47-
if err := json.Unmarshal(data, &payload); err != nil {
48-
return nil, fmt.Errorf("invalid JSON: %w", err)
49-
}
50-
req := client.CustomRulesAPI.CreateCustomRule(ctx)
51-
res, _, err := req.CreateCustomRuleRequest(payload).Execute()
52-
if err != nil {
53-
return nil, fmt.Errorf("api error: %w", err)
54-
}
55-
return res, nil
42+
client, err := newAPIV3Client()
43+
if err != nil {
44+
return nil, fmt.Errorf("unable to init client: %w", err)
45+
}
46+
var payload v3.CreateCustomRuleRequest
47+
if err := json.Unmarshal(data, &payload); err != nil {
48+
return nil, fmt.Errorf("invalid JSON: %w", err)
49+
}
50+
req := client.CustomRulesAPI.CreateCustomRule(ctx)
51+
res, _, err := req.CreateCustomRuleRequest(payload).Execute()
52+
if err != nil {
53+
return nil, fmt.Errorf("api error: %w", err)
54+
}
55+
return res, nil
5656
}
5757

5858
// UpdateCustomRule updates a custom rule from raw JSON
5959
func UpdateCustomRule(ctx context.Context, id string, data []byte) (*v3.CreateCustomRule200Response, error) {
60-
client, err := newAPIV3Client()
61-
if err != nil {
62-
return nil, fmt.Errorf("unable to init client: %w", err)
63-
}
64-
var payload v3.UpdateCustomRuleRequest
65-
if err := json.Unmarshal(data, &payload); err != nil {
66-
return nil, fmt.Errorf("invalid JSON: %w", err)
67-
}
68-
req := client.CustomRulesAPI.UpdateCustomRule(ctx, id)
69-
res, httpRes, err := req.UpdateCustomRuleRequest(payload).Execute()
70-
if err != nil {
71-
if httpRes != nil && httpRes.Body != nil {
72-
body, _ := io.ReadAll(httpRes.Body)
73-
return nil, fmt.Errorf("api error: %s", string(body))
74-
}
75-
return nil, fmt.Errorf("api error: %w", err)
76-
}
77-
return res, nil
60+
client, err := newAPIV3Client()
61+
if err != nil {
62+
return nil, fmt.Errorf("unable to init client: %w", err)
63+
}
64+
var payload v3.UpdateCustomRuleRequest
65+
if err := json.Unmarshal(data, &payload); err != nil {
66+
return nil, fmt.Errorf("invalid JSON: %w", err)
67+
}
68+
req := client.CustomRulesAPI.UpdateCustomRule(ctx, id)
69+
res, httpRes, err := req.UpdateCustomRuleRequest(payload).Execute()
70+
if err != nil {
71+
if httpRes != nil && httpRes.Body != nil {
72+
body, _ := io.ReadAll(httpRes.Body)
73+
return nil, fmt.Errorf("api error: %s", string(body))
74+
}
75+
return nil, fmt.Errorf("api error: %w", err)
76+
}
77+
return res, nil
7878
}
7979

8080
// DeleteCustomRule deletes a custom rule

pkg/api/escape/events.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111

1212
// ListEventsFilters holds optional filters for listing events
1313
type ListEventsFilters struct {
14-
Search string
15-
ScanIDs []string
16-
AssetIDs []string
17-
IssueIDs []string
18-
Levels []string
19-
Stages []string
14+
Search string
15+
ScanIDs []string
16+
AssetIDs []string
17+
IssueIDs []string
18+
Levels []string
19+
Stages []string
2020
HasAttachments bool
21-
Attachments []string
21+
Attachments []string
2222
}
2323

2424
// ListEvents lists events
@@ -78,4 +78,4 @@ func GetEvent(ctx context.Context, eventID string) (*v3.EventDetailed, error) {
7878
return nil, fmt.Errorf("api error: %w", err)
7979
}
8080
return data, nil
81-
}
81+
}

pkg/api/escape/integrations.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package escape
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
v3 "github.com/Escape-Technologies/cli/pkg/api/v3"
9+
"github.com/Escape-Technologies/cli/pkg/log"
10+
)
11+
12+
// ListKubernetesIntegrationsFilters holds optional filters for listing Kubernetes integrations
13+
type ListKubernetesIntegrationsFilters struct {
14+
ProjectIDs []string
15+
LocationIDs string
16+
Search string
17+
}
18+
19+
// UpsertKubernetesIntegration creates a Kubernetes integration if it doesn't exist
20+
func UpsertKubernetesIntegration(ctx context.Context, req v3.CreatekubernetesIntegrationRequest) (*v3.CreatekubernetesIntegration200Response, error) {
21+
list, _, err := listKubernetesIntegrations(ctx, "", &ListKubernetesIntegrationsFilters{
22+
LocationIDs: strings.Join([]string{*req.ProxyId}, ","),
23+
})
24+
if err != nil {
25+
return nil, fmt.Errorf("api error: %w", err)
26+
}
27+
if len(list) > 0 {
28+
for _, integration := range list {
29+
if integration.Location.Id == *req.ProxyId {
30+
log.Info("Kubernetes integration already exists")
31+
return nil, nil
32+
}
33+
}
34+
}
35+
log.Info("Creating Kubernetes integration..")
36+
resp, err := createKubernetesIntegration(ctx, req)
37+
if err != nil {
38+
return nil, fmt.Errorf("unable to create Kubernetes integration: %w", err)
39+
}
40+
log.Info("Kubernetes integration created")
41+
return resp, nil
42+
}
43+
44+
func createKubernetesIntegration(ctx context.Context, req v3.CreatekubernetesIntegrationRequest) (*v3.CreatekubernetesIntegration200Response, error) {
45+
client, err := newAPIV3Client()
46+
if err != nil {
47+
return nil, fmt.Errorf("unable to init client: %w", err)
48+
}
49+
resp, _, err := client.IntegrationsAPI.CreatekubernetesIntegration(ctx).
50+
CreatekubernetesIntegrationRequest(req).
51+
Execute()
52+
if err != nil {
53+
return nil, fmt.Errorf("api error: %w", err)
54+
}
55+
return resp, nil
56+
}
57+
58+
// ListKubernetesIntegrations lists Kubernetes integrations
59+
func listKubernetesIntegrations(ctx context.Context, next string, filters *ListKubernetesIntegrationsFilters) ([]v3.ListIntegrations200ResponseDataInner, *string, error) {
60+
client, err := newAPIV3Client()
61+
if err != nil {
62+
return nil, nil, fmt.Errorf("unable to init client: %w", err)
63+
}
64+
rSize := 50
65+
req := client.IntegrationsAPI.ListkubernetesIntegrations(ctx).Size(rSize)
66+
if next != "" {
67+
req = req.Cursor(next)
68+
}
69+
if filters != nil {
70+
if len(filters.ProjectIDs) > 0 {
71+
req = req.ProjectIds(filters.ProjectIDs)
72+
}
73+
if len(filters.LocationIDs) > 0 {
74+
req = req.LocationIds([]string{filters.LocationIDs})
75+
}
76+
if filters.Search != "" {
77+
req = req.Search(filters.Search)
78+
}
79+
}
80+
data, _, err := req.Execute()
81+
if err != nil {
82+
return nil, nil, fmt.Errorf("api error: %w", err)
83+
}
84+
return data.Data, data.NextCursor, nil
85+
}

pkg/api/escape/issues.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func ListIssues(ctx context.Context, next string, filters *ListIssuesFilters) ([
7878
req = req.ScanIds(strings.Join(filters.ScanIDs, ","))
7979
}
8080
if len(filters.TagsIDs) > 0 {
81-
req = req.TagsIds(strings.Join(filters.TagsIDs, ","))
81+
req = req.TagIds(strings.Join(filters.TagsIDs, ","))
8282
}
8383
if filters.Search != "" {
8484
req = req.Search(filters.Search)

pkg/api/escape/locations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212

1313
// ListLocationsFilters holds optional filters for listing locations
1414
type ListLocationsFilters struct {
15-
Search string
16-
Enabled bool
15+
Search string
16+
Enabled bool
1717
LocationTypes []string
1818
}
1919

pkg/api/escape/profiles.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111

1212
// ListProfilesFilters holds optional filters for listing profiles
1313
type ListProfilesFilters struct {
14-
AssetIDs []string
15-
Domains []string
16-
IssueIDs []string
17-
TagsIDs []string
18-
Search string
14+
AssetIDs []string
15+
Domains []string
16+
IssueIDs []string
17+
TagsIDs []string
18+
Search string
1919
Initiators []string
20-
Kinds []string
21-
Risks []string
20+
Kinds []string
21+
Risks []string
2222
}
2323

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

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

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

157157
// ListProblemsFilters holds optional filters for listing problems
158158
type ListProblemsFilters struct {
159-
AssetIDs []string
160-
Domains []string
161-
IssueIDs []string
162-
TagsIDs []string
163-
Search string
159+
AssetIDs []string
160+
Domains []string
161+
IssueIDs []string
162+
TagsIDs []string
163+
Search string
164164
Initiators []string
165-
Kinds []string
166-
Risks []string
165+
Kinds []string
166+
Risks []string
167167
}
168168

169169
// ListProblems lists all scan problems
@@ -207,4 +207,4 @@ func ListProblems(ctx context.Context, next string, filters *ListProblemsFilters
207207
return nil, nil, fmt.Errorf("api error: %w", err)
208208
}
209209
return data.Data, data.NextCursor, nil
210-
}
210+
}

pkg/api/escape/tags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func CreateTag(ctx context.Context, name string, color string) (*v3.TagDetail, e
2929
}
3030
req := client.TagsAPI.CreateTag(ctx)
3131
data, _, err := req.CreateTagRequest(v3.CreateTagRequest{
32-
Name: name,
32+
Name: name,
3333
Color: color,
3434
}).Execute()
3535
if err != nil {

0 commit comments

Comments
 (0)