Skip to content

Commit 35fdb12

Browse files
committed
wip: change firewall to network, save config in db
1 parent 4ba7bc7 commit 35fdb12

File tree

38 files changed

+537
-557
lines changed

38 files changed

+537
-557
lines changed

packages/api/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require (
4848
github.com/hashicorp/nomad/api v0.0.0-20231208134655-099ee06a607c
4949
github.com/jackc/pgx/v5 v5.7.4
5050
github.com/jellydator/ttlcache/v3 v3.4.0
51+
github.com/jinzhu/copier v0.4.0
5152
github.com/launchdarkly/go-sdk-common/v3 v3.3.0
5253
github.com/oapi-codegen/gin-middleware v1.0.2
5354
github.com/oapi-codegen/runtime v1.1.1

packages/api/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/api/spec.gen.go

Lines changed: 114 additions & 114 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/api/types.gen.go

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/handlers/sandbox.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"go.uber.org/zap"
1414

1515
"github.com/e2b-dev/infra/packages/api/internal/api"
16-
"github.com/e2b-dev/infra/packages/api/internal/db/types"
17-
"github.com/e2b-dev/infra/packages/api/internal/utils"
16+
typesteam "github.com/e2b-dev/infra/packages/api/internal/db/types"
1817
"github.com/e2b-dev/infra/packages/db/queries"
18+
"github.com/e2b-dev/infra/packages/db/types"
1919
sbxlogger "github.com/e2b-dev/infra/packages/shared/pkg/logger/sandbox"
2020
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
2121
)
@@ -29,7 +29,7 @@ func (a *APIStore) startSandbox(
2929
envVars map[string]string,
3030
metadata map[string]string,
3131
alias string,
32-
team *types.Team,
32+
team *typesteam.Team,
3333
build queries.EnvBuild,
3434
requestHeader *http.Header,
3535
isResume bool,
@@ -38,15 +38,12 @@ func (a *APIStore) startSandbox(
3838
autoPause bool,
3939
envdAccessToken *string,
4040
allowInternetAccess *bool,
41-
firewall *api.SandboxFirewallConfig,
41+
network *types.SandboxNetworkConfig,
4242
mcp api.Mcp,
4343
) (*api.Sandbox, *api.APIError) {
4444
startTime := time.Now()
4545
endTime := startTime.Add(timeout)
4646

47-
// Convert API firewall config to orchestrator firewall config
48-
orchFirewall := utils.APIToOrchestratorFirewall(firewall)
49-
5047
// Unique ID for the execution (from start/resume to stop/pause)
5148
executionID := uuid.New().String()
5249
sandbox, instanceErr := a.orchestrator.CreateSandbox(
@@ -67,7 +64,7 @@ func (a *APIStore) startSandbox(
6764
autoPause,
6865
envdAccessToken,
6966
allowInternetAccess,
70-
orchFirewall,
67+
network,
7168
)
7269
if instanceErr != nil {
7370
telemetry.ReportError(ctx, "error when creating instance", instanceErr.Err)

packages/api/internal/handlers/sandbox_connect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ func (a *APIStore) PostSandboxesSandboxIDConnect(c *gin.Context, sandboxID api.S
154154
autoPause,
155155
envdAccessToken,
156156
snap.AllowInternetAccess,
157-
utils.DBToAPIFirewall(&snap.Firewall), // firewall config from snapshot
158-
nil, // mcp
157+
snap.Config.Network,
158+
nil, // mcp
159159
)
160160
if createErr != nil {
161161
zap.L().Error("Failed to resume sandbox", zap.Error(createErr.Err))

packages/api/internal/handlers/sandbox_create.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ import (
77
"time"
88

99
"github.com/gin-gonic/gin"
10+
"github.com/jinzhu/copier"
1011
"go.opentelemetry.io/otel/attribute"
1112
"go.opentelemetry.io/otel/trace"
1213
"go.uber.org/zap"
1314

1415
"github.com/e2b-dev/infra/packages/api/internal/api"
1516
"github.com/e2b-dev/infra/packages/api/internal/auth"
16-
"github.com/e2b-dev/infra/packages/api/internal/db/types"
17+
typesteam "github.com/e2b-dev/infra/packages/api/internal/db/types"
1718
"github.com/e2b-dev/infra/packages/api/internal/middleware/otel/metrics"
1819
"github.com/e2b-dev/infra/packages/api/internal/sandbox"
1920
"github.com/e2b-dev/infra/packages/api/internal/utils"
21+
"github.com/e2b-dev/infra/packages/db/types"
2022
"github.com/e2b-dev/infra/packages/shared/pkg/id"
2123
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
2224
sbxlogger "github.com/e2b-dev/infra/packages/shared/pkg/logger/sandbox"
@@ -43,7 +45,7 @@ func (a *APIStore) PostSandboxes(c *gin.Context) {
4345
ctx := c.Request.Context()
4446

4547
// Get team from context, use TeamContextKey
46-
teamInfo := c.Value(auth.TeamContextKey).(*types.Team)
48+
teamInfo := c.Value(auth.TeamContextKey).(*typesteam.Team)
4749

4850
c.Set("teamID", teamInfo.Team.ID.String())
4951

@@ -156,7 +158,15 @@ func (a *APIStore) PostSandboxes(c *gin.Context) {
156158
}
157159

158160
allowInternetAccess := body.AllowInternetAccess
159-
firewall := body.Firewall
161+
162+
var network *types.SandboxNetworkConfig
163+
err = copier.CopyWithOption(&network, body.Network, copier.Option{DeepCopy: true})
164+
if err != nil {
165+
telemetry.ReportError(ctx, "failed to create sandbox", err)
166+
a.sendAPIStoreError(c, http.StatusInternalServerError, "Failed to create sandbox network configuration")
167+
168+
return
169+
}
160170

161171
sbx, createErr := a.startSandbox(
162172
ctx,
@@ -174,7 +184,7 @@ func (a *APIStore) PostSandboxes(c *gin.Context) {
174184
autoPause,
175185
envdAccessToken,
176186
allowInternetAccess,
177-
firewall,
187+
network,
178188
mcp,
179189
)
180190
if createErr != nil {

packages/api/internal/handlers/sandbox_resume.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ func (a *APIStore) PostSandboxesSandboxIDResume(c *gin.Context, sandboxID api.Sa
153153
autoPause,
154154
envdAccessToken,
155155
snap.AllowInternetAccess,
156-
utils.DBToAPIFirewall(&snap.Firewall), // firewall config from snapshot
157-
nil, // mcp
156+
snap.Config.Network,
157+
nil, // mcp
158158
)
159159
if createErr != nil {
160160
zap.L().Error("Failed to resume sandbox", zap.Error(createErr.Err))

packages/api/internal/orchestrator/create_instance.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ import (
77
"net/http"
88
"time"
99

10+
"github.com/jinzhu/copier"
1011
"go.opentelemetry.io/otel/attribute"
1112
"go.opentelemetry.io/otel/metric"
1213
"go.uber.org/zap"
1314
"google.golang.org/protobuf/types/known/timestamppb"
1415

1516
"github.com/e2b-dev/infra/packages/api/internal/api"
16-
"github.com/e2b-dev/infra/packages/api/internal/db/types"
17+
teamtypes "github.com/e2b-dev/infra/packages/api/internal/db/types"
1718
"github.com/e2b-dev/infra/packages/api/internal/orchestrator/nodemanager"
1819
"github.com/e2b-dev/infra/packages/api/internal/orchestrator/placement"
1920
"github.com/e2b-dev/infra/packages/api/internal/sandbox"
2021
"github.com/e2b-dev/infra/packages/api/internal/utils"
2122
"github.com/e2b-dev/infra/packages/db/queries"
23+
"github.com/e2b-dev/infra/packages/db/types"
2224
"github.com/e2b-dev/infra/packages/shared/pkg/consts"
2325
"github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
2426
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
@@ -31,7 +33,7 @@ func (o *Orchestrator) CreateSandbox(
3133
sandboxID,
3234
executionID,
3335
alias string,
34-
team *types.Team,
36+
team *teamtypes.Team,
3537
build queries.EnvBuild,
3638
metadata map[string]string,
3739
envVars map[string]string,
@@ -44,7 +46,7 @@ func (o *Orchestrator) CreateSandbox(
4446
autoPause bool,
4547
envdAuthToken *string,
4648
allowInternetAccess *bool,
47-
firewall *orchestrator.SandboxFirewallConfig,
49+
network *types.SandboxNetworkConfig,
4850
) (sbx sandbox.Sandbox, apiErr *api.APIError) {
4951
ctx, childSpan := tracer.Start(ctx, "create-sandbox")
5052
defer childSpan.End()
@@ -139,9 +141,21 @@ func (o *Orchestrator) CreateSandbox(
139141
sbxDomain = cluster.SandboxDomain
140142
}
141143

144+
var orchNetwork *orchestrator.SandboxNetworkConfig
145+
err = copier.CopyWithOption(&orchNetwork, network, copier.Option{DeepCopy: true})
146+
if err != nil {
147+
telemetry.ReportError(ctx, "failed to create sandbox", err)
148+
149+
return sandbox.Sandbox{}, &api.APIError{
150+
Code: http.StatusInternalServerError,
151+
ClientMsg: "Failed to create sandbox network configuration",
152+
Err: fmt.Errorf("failed to copy sandbox network configuration: %w", err),
153+
}
154+
}
155+
142156
if allowInternetAccess != nil && !*allowInternetAccess {
143-
firewall.Egress = firewall.GetEgress()
144-
firewall.Egress.BlockedCidrs = []string{"0.0.0.0/0"}
157+
orchNetwork.Egress = orchNetwork.GetEgress()
158+
orchNetwork.Egress.BlockedCidrs = []string{"0.0.0.0/0"}
145159
}
146160

147161
sbxRequest := &orchestrator.SandboxCreateRequest{
@@ -166,7 +180,7 @@ func (o *Orchestrator) CreateSandbox(
166180
Snapshot: isResume,
167181
AutoPause: autoPause,
168182
AllowInternetAccess: allowInternetAccess,
169-
Firewall: firewall,
183+
Network: orchNetwork,
170184
TotalDiskSizeMb: ut.FromPtr(build.TotalDiskSizeMb),
171185
},
172186
StartTime: timestamppb.New(startTime),
@@ -219,6 +233,18 @@ func (o *Orchestrator) CreateSandbox(
219233
startTime = time.Now()
220234
endTime = startTime.Add(timeout)
221235

236+
var dbNetwork *types.SandboxNetworkConfig
237+
err = copier.CopyWithOption(&dbNetwork, network, copier.Option{DeepCopy: true})
238+
if err != nil {
239+
telemetry.ReportError(ctx, "failed to create sandbox", err)
240+
241+
return sandbox.Sandbox{}, &api.APIError{
242+
Code: http.StatusInternalServerError,
243+
ClientMsg: "Failed to create sandbox network configuration",
244+
Err: fmt.Errorf("failed to copy sandbox network configuration: %w", err),
245+
}
246+
}
247+
222248
sbx = sandbox.NewSandbox(
223249
sandboxID,
224250
build.EnvID,
@@ -244,7 +270,7 @@ func (o *Orchestrator) CreateSandbox(
244270
allowInternetAccess,
245271
baseTemplateID,
246272
sbxDomain,
247-
utils.OrchestratorToDBFirewall(firewall),
273+
dbNetwork,
248274
)
249275

250276
o.sandboxStore.Add(ctx, sbx, true)

packages/api/internal/orchestrator/nodemanager/sandboxes.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77

88
"github.com/golang/protobuf/ptypes/empty"
99
"github.com/google/uuid"
10+
"github.com/jinzhu/copier"
1011

1112
"github.com/e2b-dev/infra/packages/api/internal/sandbox"
1213
"github.com/e2b-dev/infra/packages/api/internal/utils"
14+
"github.com/e2b-dev/infra/packages/db/types"
1315
"github.com/e2b-dev/infra/packages/shared/pkg/consts"
1416
)
1517

@@ -46,6 +48,12 @@ func (n *Node) GetSandboxes(ctx context.Context) ([]sandbox.Sandbox, error) {
4648
return nil, fmt.Errorf("failed to parse build ID '%s' for job: %w", config.GetBuildId(), parseErr)
4749
}
4850

51+
var network *types.SandboxNetworkConfig
52+
err = copier.CopyWithOption(&network, config.GetNetwork(), copier.Option{DeepCopy: true})
53+
if err != nil {
54+
return nil, fmt.Errorf("failed to translate network config: %w", err)
55+
}
56+
4957
sandboxesInfo = append(
5058
sandboxesInfo,
5159
sandbox.NewSandbox(
@@ -73,7 +81,7 @@ func (n *Node) GetSandboxes(ctx context.Context) ([]sandbox.Sandbox, error) {
7381
config.AllowInternetAccess, //nolint:protogetter // we need the nil check too
7482
config.GetBaseTemplateId(),
7583
n.SandboxDomain,
76-
utils.OrchestratorToDBFirewall(config.GetFirewall()),
84+
network,
7785
),
7886
)
7987
}

0 commit comments

Comments
 (0)