Skip to content
Merged
30 changes: 15 additions & 15 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ linters:
forbid:
- pattern: "^new$"
msg: "Use &Type{} instead."
# - pattern: os\.Getenv
# msg: "Add your field to the configuration model instead."
# - pattern: os\.Getenv
# msg: "Add your field to the configuration model instead."

gocritic:
disabled-checks:
- appendAssign
revive:
enable-all-rules: true
rules:
- { disabled: true, name: add-constant } # todo: enable this
- { disabled: true, name: add-constant } # todo: enable this
- { disabled: true, name: argument-limit }
- { disabled: true, name: bare-return }
- { disabled: true, name: blank-imports }
Expand All @@ -96,7 +96,7 @@ linters:
- { disabled: true, name: early-return }
- { disabled: true, name: enforce-switch-style }
- { disabled: true, name: exported }
- { disabled: true, name: flag-parameter } # todo: enable this
- { disabled: true, name: flag-parameter } # todo: enable this
- { disabled: true, name: function-length }
- { disabled: true, name: function-result-limit }
- { disabled: true, name: get-return }
Expand All @@ -109,24 +109,24 @@ linters:
- { disabled: true, name: package-comments }
- { disabled: true, name: unchecked-type-assertion }
- { disabled: true, name: unexported-naming }
- { disabled: true, name: unhandled-error } # todo: enable this
- { disabled: true, name: unnecessary-format } # todo: enable this
- { disabled: true, name: unnecessary-stmt } # todo: enable this
- { disabled: true, name: unhandled-error } # todo: enable this
- { disabled: true, name: unnecessary-format } # todo: enable this
- { disabled: true, name: unnecessary-stmt } # todo: enable this
- { disabled: true, name: unused-receiver }
- { disabled: true, name: use-errors-new } # todo: enable this
- { disabled: true, name: use-errors-new } # todo: enable this
- { disabled: true, name: var-declaration }
- { disabled: true, name: var-naming }

staticcheck:
checks:
- all
- -S1002 # Omit comparison with boolean constant
- -SA1019 # TODO: Remove (Using a deprecated function, variable, constant or field)
- -ST1000 # Incorrect or missing package comment
- -ST1020 # The documentation of an exported function should start with the function’s name
- -ST1021 # The documentation of an exported type should start with type’s name
- -ST1003 # Poorly chosen identifier
- -QF1008 # Omit embedded fields from selector expression
- -S1002 # Omit comparison with boolean constant
- -SA1019 # TODO: Remove (Using a deprecated function, variable, constant or field)
- -ST1000 # Incorrect or missing package comment
- -ST1020 # The documentation of an exported function should start with the function’s name
- -ST1021 # The documentation of an exported type should start with type’s name
- -ST1003 # Poorly chosen identifier
- -QF1008 # Omit embedded fields from selector expression

run:
go: 1.24.7
Expand Down
226 changes: 114 additions & 112 deletions packages/api/internal/api/spec.gen.go

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions packages/api/internal/api/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions packages/api/internal/handlers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
"go.uber.org/zap"

"github.com/e2b-dev/infra/packages/api/internal/api"
"github.com/e2b-dev/infra/packages/api/internal/db/types"
typesteam "github.com/e2b-dev/infra/packages/api/internal/db/types"
"github.com/e2b-dev/infra/packages/db/queries"
"github.com/e2b-dev/infra/packages/db/types"
sbxlogger "github.com/e2b-dev/infra/packages/shared/pkg/logger/sandbox"
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
)
Expand All @@ -28,7 +29,7 @@ func (a *APIStore) startSandbox(
envVars map[string]string,
metadata map[string]string,
alias string,
team *types.Team,
team *typesteam.Team,
build queries.EnvBuild,
requestHeader *http.Header,
isResume bool,
Expand All @@ -37,6 +38,7 @@ func (a *APIStore) startSandbox(
autoPause bool,
envdAccessToken *string,
allowInternetAccess *bool,
network *types.SandboxNetworkConfig,
mcp api.Mcp,
) (*api.Sandbox, *api.APIError) {
startTime := time.Now()
Expand All @@ -62,6 +64,7 @@ func (a *APIStore) startSandbox(
autoPause,
envdAccessToken,
allowInternetAccess,
network,
)
if instanceErr != nil {
telemetry.ReportError(ctx, "error when creating instance", instanceErr.Err)
Expand Down
3 changes: 2 additions & 1 deletion packages/api/internal/handlers/sandbox_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ func (a *APIStore) PostSandboxesSandboxIDConnect(c *gin.Context, sandboxID api.S
autoPause,
envdAccessToken,
snap.AllowInternetAccess,
nil,
snap.Config.Network,
nil, // mcp
)
if createErr != nil {
zap.L().Error("Failed to resume sandbox", zap.Error(createErr.Err))
Expand Down
16 changes: 14 additions & 2 deletions packages/api/internal/handlers/sandbox_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (

"github.com/e2b-dev/infra/packages/api/internal/api"
"github.com/e2b-dev/infra/packages/api/internal/auth"
"github.com/e2b-dev/infra/packages/api/internal/db/types"
typesteam "github.com/e2b-dev/infra/packages/api/internal/db/types"
"github.com/e2b-dev/infra/packages/api/internal/middleware/otel/metrics"
"github.com/e2b-dev/infra/packages/api/internal/sandbox"
"github.com/e2b-dev/infra/packages/api/internal/utils"
"github.com/e2b-dev/infra/packages/db/types"
"github.com/e2b-dev/infra/packages/shared/pkg/id"
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
sbxlogger "github.com/e2b-dev/infra/packages/shared/pkg/logger/sandbox"
Expand All @@ -43,7 +44,7 @@ func (a *APIStore) PostSandboxes(c *gin.Context) {
ctx := c.Request.Context()

// Get team from context, use TeamContextKey
teamInfo := c.Value(auth.TeamContextKey).(*types.Team)
teamInfo := c.Value(auth.TeamContextKey).(*typesteam.Team)

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

Expand Down Expand Up @@ -157,6 +158,16 @@ func (a *APIStore) PostSandboxes(c *gin.Context) {

allowInternetAccess := body.AllowInternetAccess

var network *types.SandboxNetworkConfig
if body.Network != nil {
network = &types.SandboxNetworkConfig{
Egress: &types.SandboxNetworkEgressConfig{
AllowedAddresses: sharedUtils.DerefOrDefault(body.Network.AllowOut, nil),
BlockedAddresses: sharedUtils.DerefOrDefault(body.Network.BlockOut, nil),
},
}
}

sbx, createErr := a.startSandbox(
ctx,
sandboxID,
Expand All @@ -173,6 +184,7 @@ func (a *APIStore) PostSandboxes(c *gin.Context) {
autoPause,
envdAccessToken,
allowInternetAccess,
network,
mcp,
)
if createErr != nil {
Expand Down
3 changes: 2 additions & 1 deletion packages/api/internal/handlers/sandbox_resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ func (a *APIStore) PostSandboxesSandboxIDResume(c *gin.Context, sandboxID api.Sa
autoPause,
envdAccessToken,
snap.AllowInternetAccess,
nil,
snap.Config.Network,
nil, // mcp
)
if createErr != nil {
zap.L().Error("Failed to resume sandbox", zap.Error(createErr.Err))
Expand Down
34 changes: 32 additions & 2 deletions packages/api/internal/orchestrator/create_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,50 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/e2b-dev/infra/packages/api/internal/api"
"github.com/e2b-dev/infra/packages/api/internal/db/types"
teamtypes "github.com/e2b-dev/infra/packages/api/internal/db/types"
"github.com/e2b-dev/infra/packages/api/internal/orchestrator/nodemanager"
"github.com/e2b-dev/infra/packages/api/internal/orchestrator/placement"
"github.com/e2b-dev/infra/packages/api/internal/sandbox"
"github.com/e2b-dev/infra/packages/api/internal/utils"
"github.com/e2b-dev/infra/packages/db/queries"
"github.com/e2b-dev/infra/packages/db/types"
"github.com/e2b-dev/infra/packages/shared/pkg/consts"
"github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
ut "github.com/e2b-dev/infra/packages/shared/pkg/utils"
)

const internetBlockAddress = "0.0.0.0/0"

// buildNetworkConfig constructs the orchestrator network configuration from the input parameters
func buildNetworkConfig(network *types.SandboxNetworkConfig, allowInternetAccess *bool) *orchestrator.SandboxNetworkConfig {
orchNetwork := &orchestrator.SandboxNetworkConfig{
Egress: &orchestrator.SandboxNetworkEgressConfig{},
}

// Copy network configuration if provided
if network != nil && network.Egress != nil {
orchNetwork.Egress.AllowedAddresses = network.Egress.AllowedAddresses
orchNetwork.Egress.BlockedAddresses = network.Egress.BlockedAddresses
}

// Handle the case where internet access is explicitly disabled
// This should be applied after copying the network config to preserve allowed addresses
if allowInternetAccess != nil && !*allowInternetAccess {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should return error when both network egress allow/block lists and allow internet are provided. It can bring unexpected behavior for users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The allowOut takes precedence over blockOut

also, internet is defined by default, allowInternetAccess false does the same as blockOut=[0.0.0.0/0]

// Block all internet access - this overrides any other blocked addresses
orchNetwork.Egress.BlockedAddresses = []string{internetBlockAddress}
}

return orchNetwork
}

func (o *Orchestrator) CreateSandbox(
ctx context.Context,
sandboxID,
executionID,
alias string,
team *types.Team,
team *teamtypes.Team,
build queries.EnvBuild,
metadata map[string]string,
envVars map[string]string,
Expand All @@ -44,6 +69,7 @@ func (o *Orchestrator) CreateSandbox(
autoPause bool,
envdAuthToken *string,
allowInternetAccess *bool,
network *types.SandboxNetworkConfig,
) (sbx sandbox.Sandbox, apiErr *api.APIError) {
ctx, childSpan := tracer.Start(ctx, "create-sandbox")
defer childSpan.End()
Expand Down Expand Up @@ -138,6 +164,8 @@ func (o *Orchestrator) CreateSandbox(
sbxDomain = cluster.SandboxDomain
}

orchNetwork := buildNetworkConfig(network, allowInternetAccess)

sbxRequest := &orchestrator.SandboxCreateRequest{
Sandbox: &orchestrator.SandboxConfig{
BaseTemplateId: baseTemplateID,
Expand All @@ -160,6 +188,7 @@ func (o *Orchestrator) CreateSandbox(
Snapshot: isResume,
AutoPause: autoPause,
AllowInternetAccess: allowInternetAccess,
Network: orchNetwork,
TotalDiskSizeMb: ut.FromPtr(build.TotalDiskSizeMb),
},
StartTime: timestamppb.New(startTime),
Expand Down Expand Up @@ -237,6 +266,7 @@ func (o *Orchestrator) CreateSandbox(
allowInternetAccess,
baseTemplateID,
sbxDomain,
network,
)

o.sandboxStore.Add(ctx, sbx, true)
Expand Down
9 changes: 9 additions & 0 deletions packages/api/internal/orchestrator/nodemanager/sandboxes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/e2b-dev/infra/packages/api/internal/sandbox"
"github.com/e2b-dev/infra/packages/api/internal/utils"
"github.com/e2b-dev/infra/packages/db/types"
"github.com/e2b-dev/infra/packages/shared/pkg/consts"
)

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

network := &types.SandboxNetworkConfig{
Egress: &types.SandboxNetworkEgressConfig{
AllowedAddresses: config.GetNetwork().GetEgress().GetAllowedAddresses(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this call chain will work correctly when communicating with orchestrators that are not yet supporting network configuration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thing was if we will not receive nil. It not, that is okay.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll receive nil for the list, but nil is a valid empty list

BlockedAddresses: config.GetNetwork().GetEgress().GetBlockedAddresses(),
},
}

sandboxesInfo = append(
sandboxesInfo,
sandbox.NewSandbox(
Expand Down Expand Up @@ -73,6 +81,7 @@ func (n *Node) GetSandboxes(ctx context.Context) ([]sandbox.Sandbox, error) {
config.AllowInternetAccess, //nolint:protogetter // we need the nil check too
config.GetBaseTemplateId(),
n.SandboxDomain,
network,
),
)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/api/internal/orchestrator/pause_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (o *Orchestrator) pauseSandbox(ctx context.Context, node *nodemanager.Node,
EnvdSecured: sbx.EnvdAccessToken != nil,
AllowInternetAccess: sbx.AllowInternetAccess,
AutoPause: sbx.AutoPause,
Config: &types.PausedSandboxConfig{
Version: types.PausedSandboxConfigVersion,
Network: sbx.Network,
},
}

envBuild, err := o.dbClient.NewSnapshotBuild(
Expand Down
4 changes: 4 additions & 0 deletions packages/api/internal/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/google/uuid"

"github.com/e2b-dev/infra/packages/api/internal/api"
"github.com/e2b-dev/infra/packages/db/types"
sbxlogger "github.com/e2b-dev/infra/packages/shared/pkg/logger/sandbox"
)

Expand Down Expand Up @@ -34,6 +35,7 @@ func NewSandbox(
allowInternetAccess *bool,
baseTemplateID string,
domain *string,
network *types.SandboxNetworkConfig,
) Sandbox {
return Sandbox{
SandboxID: sandboxID,
Expand Down Expand Up @@ -62,6 +64,7 @@ func NewSandbox(
AutoPause: autoPause,
State: StateRunning,
BaseTemplateID: baseTemplateID,
Network: network,
}
}

Expand Down Expand Up @@ -91,6 +94,7 @@ type Sandbox struct {
NodeID string
ClusterID uuid.UUID
AutoPause bool
Network *types.SandboxNetworkConfig

State State
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- +goose Up
-- +goose StatementBegin

ALTER TABLE snapshots
ADD COLUMN config jsonb NULL;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TABLE snapshots
DROP COLUMN IF EXISTS config;
-- +goose StatementEnd
3 changes: 2 additions & 1 deletion packages/db/queries/get_last_snapshot.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading