Skip to content

Commit 88b9fb9

Browse files
author
Valentin REJAUNIER
committed
fix: handle Kubernetes integration
1 parent a63ef05 commit 88b9fb9

40 files changed

Lines changed: 10305 additions & 1789 deletions

File tree

pkg/api/escape/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ 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
17-
func NewAPIV3Client() (*v3.APIClient, error) {
16+
// newAPIV3Client creates a new API v3 client
17+
func newAPIV3Client() (*v3.APIClient, error) {
1818
log.Trace("Initializing v3 client")
1919
url, err := env.GetAPIURL()
2020
if err != nil {

pkg/api/escape/assets.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type ListAssetsFilters struct {
2424

2525
// ListAssets lists all assets
2626
func ListAssets(ctx context.Context, next string, filters *ListAssetsFilters) ([]v3.AssetSummarized, *string, error) {
27-
client, err := NewAPIV3Client()
27+
client, err := newAPIV3Client()
2828
if err != nil {
2929
return nil, nil, fmt.Errorf("unable to init client: %w", err)
3030
}
@@ -56,7 +56,7 @@ func ListAssets(ctx context.Context, next string, filters *ListAssetsFilters) ([
5656

5757
// GetAsset gets an asset by ID
5858
func GetAsset(ctx context.Context, id string) (*v3.AssetDetailed, error) {
59-
client, err := NewAPIV3Client()
59+
client, err := newAPIV3Client()
6060
if err != nil {
6161
return nil, fmt.Errorf("unable to init client: %w", err)
6262
}
@@ -69,7 +69,7 @@ func GetAsset(ctx context.Context, id string) (*v3.AssetDetailed, error) {
6969

7070
// DeleteAsset deletes an asset by ID
7171
func DeleteAsset(ctx context.Context, id string) error {
72-
client, err := NewAPIV3Client()
72+
client, err := newAPIV3Client()
7373
if err != nil {
7474
return fmt.Errorf("unable to init client: %w", err)
7575
}
@@ -90,7 +90,7 @@ func UpdateAsset(
9090
assetStatus *v3.ENUMPROPERTIESDATAITEMSPROPERTIESASSETPROPERTIESSTATUS,
9191
assetTagIDs *[]string,
9292
) error {
93-
client, err := NewAPIV3Client()
93+
client, err := newAPIV3Client()
9494
if err != nil {
9595
return fmt.Errorf("unable to init client: %w", err)
9696
}
@@ -147,7 +147,7 @@ func CreateAsset(ctx context.Context, data []byte, assetType string) (interface{
147147
method := typ.Method(i)
148148
if strings.HasPrefix(method.Name, "Create") && !strings.HasSuffix(method.Name, "Execute") {
149149
if strings.Contains(strings.ToUpper(method.Name), strings.ToUpper(normalizeAssetType(assetType))) {
150-
client, err := NewAPIV3Client()
150+
client, err := newAPIV3Client()
151151
if err != nil {
152152
return nil, fmt.Errorf("unable to init client: %w", err)
153153
}

pkg/api/escape/audit.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ 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
2020
func ListAuditLogs(ctx context.Context, next string, filters *ListAuditLogsFilters) ([]v3.AuditLogSummarized, *string, error) {
21-
client, err := NewAPIV3Client()
21+
client, err := newAPIV3Client()
2222
if err != nil {
2323
return nil, nil, fmt.Errorf("unable to init client: %w", err)
2424
}

pkg/api/escape/custom-rules.go

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

1212
// ListCustomRules lists all custom rules
1313
func ListCustomRules(ctx context.Context) ([]v3.CustomRuleSummarized, error) {
14-
client, err := NewAPIV3Client()
14+
client, err := newAPIV3Client()
1515
if err != nil {
1616
return nil, fmt.Errorf("unable to init client: %w", err)
1717
}
@@ -25,7 +25,7 @@ func ListCustomRules(ctx context.Context) ([]v3.CustomRuleSummarized, error) {
2525

2626
// GetCustomRule gets a custom rule
2727
func GetCustomRule(ctx context.Context, id string) (*v3.CreateCustomRule200Response, error) {
28-
client, err := NewAPIV3Client()
28+
client, err := newAPIV3Client()
2929
if err != nil {
3030
return nil, fmt.Errorf("unable to init client: %w", err)
3131
}
@@ -39,47 +39,47 @@ 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
8181
func DeleteCustomRule(ctx context.Context, id string) (*v3.DeleteCustomRule200Response, error) {
82-
client, err := NewAPIV3Client()
82+
client, err := newAPIV3Client()
8383
if err != nil {
8484
return nil, fmt.Errorf("unable to init client: %w", err)
8585
}

pkg/api/escape/events.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ 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
2525
func ListEvents(ctx context.Context, next string, filters *ListEventsFilters) ([]v3.EventSummarized, *string, error) {
26-
client, err := NewAPIV3Client()
26+
client, err := newAPIV3Client()
2727
if err != nil {
2828
return nil, nil, fmt.Errorf("unable to init client: %w", err)
2929
}
@@ -69,7 +69,7 @@ func ListEvents(ctx context.Context, next string, filters *ListEventsFilters) ([
6969

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

pkg/api/escape/issues.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type ListIssuesFilters struct {
2828

2929
// GetIssue gets an issue by ID
3030
func GetIssue(ctx context.Context, issueID string) (*v3.IssueDetailed, error) {
31-
client, err := NewAPIV3Client()
31+
client, err := newAPIV3Client()
3232
if err != nil {
3333
return nil, fmt.Errorf("unable to init client: %w", err)
3434
}
@@ -43,7 +43,7 @@ func GetIssue(ctx context.Context, issueID string) (*v3.IssueDetailed, error) {
4343

4444
// ListIssues lists all issues.
4545
func ListIssues(ctx context.Context, next string, filters *ListIssuesFilters) ([]v3.IssueSummarized, *string, error) {
46-
client, err := NewAPIV3Client()
46+
client, err := newAPIV3Client()
4747
if err != nil {
4848
return nil, nil, fmt.Errorf("unable to init client: %w", err)
4949
}
@@ -106,7 +106,7 @@ func ListIssues(ctx context.Context, next string, filters *ListIssuesFilters) ([
106106

107107
// UpdateIssue updates an issue
108108
func UpdateIssue(ctx context.Context, issueID string, status v3.ENUMPROPERTIESDATAITEMSPROPERTIESSTATUS) (bool, error) {
109-
client, err := NewAPIV3Client()
109+
client, err := newAPIV3Client()
110110
if err != nil {
111111
return false, fmt.Errorf("unable to init client: %w", err)
112112
}
@@ -128,7 +128,7 @@ func UpdateIssue(ctx context.Context, issueID string, status v3.ENUMPROPERTIESDA
128128

129129
// ListIssueActivities lists the activities of an issue
130130
func ListIssueActivities(ctx context.Context, issueID string) ([]v3.ActivitySummarized, error) {
131-
client, err := NewAPIV3Client()
131+
client, err := newAPIV3Client()
132132
if err != nil {
133133
return nil, fmt.Errorf("unable to init client: %w", err)
134134
}

pkg/api/escape/locations.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ 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

2020
// ListLocations lists all locations
2121
func ListLocations(ctx context.Context, next string, filters *ListLocationsFilters) ([]v3.LocationSummarized, *string, error) {
22-
client, err := NewAPIV3Client()
22+
client, err := newAPIV3Client()
2323
if err != nil {
2424
return nil, nil, fmt.Errorf("unable to init client: %w", err)
2525
}
@@ -47,7 +47,7 @@ func ListLocations(ctx context.Context, next string, filters *ListLocationsFilte
4747

4848
// GetLocation gets a location by ID
4949
func GetLocation(ctx context.Context, id string) (*v3.LocationDetailed, error) {
50-
client, err := NewAPIV3Client()
50+
client, err := newAPIV3Client()
5151
if err != nil {
5252
return nil, fmt.Errorf("unable to init client: %w", err)
5353
}
@@ -61,7 +61,7 @@ func GetLocation(ctx context.Context, id string) (*v3.LocationDetailed, error) {
6161

6262
// CreateLocation creates a location
6363
func CreateLocation(ctx context.Context, name, sshPublicKey string) (string, error) {
64-
client, err := NewAPIV3Client()
64+
client, err := newAPIV3Client()
6565
if err != nil {
6666
return "", fmt.Errorf("unable to init client: %w", err)
6767
}
@@ -81,7 +81,7 @@ func CreateLocation(ctx context.Context, name, sshPublicKey string) (string, err
8181

8282
// UpdateLocation updates a location
8383
func UpdateLocation(ctx context.Context, id string, name, sshPublicKey string) error {
84-
client, err := NewAPIV3Client()
84+
client, err := newAPIV3Client()
8585
if err != nil {
8686
return fmt.Errorf("unable to init client: %w", err)
8787
}
@@ -100,7 +100,7 @@ func UpdateLocation(ctx context.Context, id string, name, sshPublicKey string) e
100100

101101
// DeleteLocation deletes a location
102102
func DeleteLocation(ctx context.Context, id string) error {
103-
client, err := NewAPIV3Client()
103+
client, err := newAPIV3Client()
104104
if err != nil {
105105
return fmt.Errorf("unable to init client: %w", err)
106106
}

0 commit comments

Comments
 (0)