Skip to content

Commit 5857131

Browse files
committed
rename block to deny, use CIDR in the orchestrator
1 parent ef7e29b commit 5857131

File tree

16 files changed

+315
-282
lines changed

16 files changed

+315
-282
lines changed

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

Lines changed: 115 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: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/handlers/sandbox_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (a *APIStore) PostSandboxes(c *gin.Context) {
163163
network = &types.SandboxNetworkConfig{
164164
Egress: &types.SandboxNetworkEgressConfig{
165165
AllowedAddresses: sharedUtils.DerefOrDefault(body.Network.AllowOut, nil),
166-
BlockedAddresses: sharedUtils.DerefOrDefault(body.Network.BlockOut, nil),
166+
DeniedAddresses: sharedUtils.DerefOrDefault(body.Network.DenyOut, nil),
167167
},
168168
}
169169
}

packages/api/internal/orchestrator/create_instance.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8+
"strings"
89
"time"
910

1011
"go.opentelemetry.io/otel/attribute"
@@ -27,7 +28,21 @@ import (
2728
ut "github.com/e2b-dev/infra/packages/shared/pkg/utils"
2829
)
2930

30-
const internetBlockAddress = "0.0.0.0/0"
31+
const internetBlockCIDR = "0.0.0.0/0"
32+
33+
// Convert a list of string addresses to the SetData type
34+
func addressStringsToCIDRs(addressStrings []string) []string {
35+
data := make([]string, 0, len(addressStrings))
36+
37+
for _, addressString := range addressStrings {
38+
if !strings.Contains(addressString, "/") {
39+
addressString = addressString + "/32"
40+
}
41+
data = append(data, addressString)
42+
}
43+
44+
return data
45+
}
3146

3247
// buildNetworkConfig constructs the orchestrator network configuration from the input parameters
3348
func buildNetworkConfig(network *types.SandboxNetworkConfig, allowInternetAccess *bool) *orchestrator.SandboxNetworkConfig {
@@ -37,15 +52,15 @@ func buildNetworkConfig(network *types.SandboxNetworkConfig, allowInternetAccess
3752

3853
// Copy network configuration if provided
3954
if network != nil && network.Egress != nil {
40-
orchNetwork.Egress.AllowedAddresses = network.Egress.AllowedAddresses
41-
orchNetwork.Egress.BlockedAddresses = network.Egress.BlockedAddresses
55+
orchNetwork.Egress.AllowedCidrs = addressStringsToCIDRs(network.Egress.AllowedAddresses)
56+
orchNetwork.Egress.DeniedCidrs = addressStringsToCIDRs(network.Egress.DeniedAddresses)
4257
}
4358

4459
// Handle the case where internet access is explicitly disabled
4560
// This should be applied after copying the network config to preserve allowed addresses
4661
if allowInternetAccess != nil && !*allowInternetAccess {
4762
// Block all internet access - this overrides any other blocked addresses
48-
orchNetwork.Egress.BlockedAddresses = []string{internetBlockAddress}
63+
orchNetwork.Egress.DeniedCidrs = []string{internetBlockCIDR}
4964
}
5065

5166
return orchNetwork
@@ -164,7 +179,7 @@ func (o *Orchestrator) CreateSandbox(
164179
sbxDomain = cluster.SandboxDomain
165180
}
166181

167-
orchNetwork := buildNetworkConfig(network, allowInternetAccess)
182+
sbxNetwork := buildNetworkConfig(network, allowInternetAccess)
168183

169184
sbxRequest := &orchestrator.SandboxCreateRequest{
170185
Sandbox: &orchestrator.SandboxConfig{
@@ -188,7 +203,7 @@ func (o *Orchestrator) CreateSandbox(
188203
Snapshot: isResume,
189204
AutoPause: autoPause,
190205
AllowInternetAccess: allowInternetAccess,
191-
Network: orchNetwork,
206+
Network: sbxNetwork,
192207
TotalDiskSizeMb: ut.FromPtr(build.TotalDiskSizeMb),
193208
},
194209
StartTime: timestamppb.New(startTime),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func (n *Node) GetSandboxes(ctx context.Context) ([]sandbox.Sandbox, error) {
4949

5050
network := &types.SandboxNetworkConfig{
5151
Egress: &types.SandboxNetworkEgressConfig{
52-
AllowedAddresses: config.GetNetwork().GetEgress().GetAllowedAddresses(),
53-
BlockedAddresses: config.GetNetwork().GetEgress().GetBlockedAddresses(),
52+
AllowedAddresses: config.GetNetwork().GetEgress().GetAllowedCidrs(),
53+
DeniedAddresses: config.GetNetwork().GetEgress().GetDeniedCidrs(),
5454
},
5555
}
5656

packages/db/types/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const PausedSandboxConfigVersion = "v1"
1414

1515
type SandboxNetworkEgressConfig struct {
1616
AllowedAddresses []string `json:"allowedAddresses,omitempty"`
17-
BlockedAddresses []string `json:"blockedAddresses,omitempty"`
17+
DeniedAddresses []string `json:"deniedAddresses,omitempty"`
1818
}
1919

2020
type SandboxNetworkConfig struct {

packages/orchestrator/internal/sandbox/network/firewall.go

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,16 @@ func (fw *Firewall) installRules() error {
150150
return nil
151151
}
152152

153-
// AddBlockedIP adds a single CIDR to the block set at runtime.
154-
func (fw *Firewall) AddBlockedIP(address string) error {
153+
// AddDeniedCIDR adds a single CIDR to the block set at runtime.
154+
func (fw *Firewall) AddDeniedCIDR(cidr string) error {
155155
if fw.blockInternetTraffic {
156156
// If internet is blocked, we don't need to add any other addresses to the block set.
157157
// Because 0.0.0.0/0 is not valid IP per GoLang, we can't add new addresses to the block set.
158158
return nil
159159
}
160160

161161
// 0.0.0.0/0 is not valid IP per GoLang, so we handle it as a special case
162-
if address == allInternetTrafficAddress {
162+
if cidr == allInternetTrafficAddress {
163163
fw.blockInternetTraffic = true
164164

165165
fw.conn.FlushSet(fw.blockSet.Set())
@@ -181,7 +181,7 @@ func (fw *Firewall) AddBlockedIP(address string) error {
181181
return err
182182
}
183183

184-
data, err := set.AddressStringsToSetData([]string{address})
184+
data, err := set.AddressStringsToSetData([]string{cidr})
185185
if err != nil {
186186
return err
187187
}
@@ -199,19 +199,19 @@ func (fw *Firewall) AddBlockedIP(address string) error {
199199
return nil
200200
}
201201

202-
// AddAllowedIP adds a single CIDR to the allow set at runtime.
203-
func (fw *Firewall) AddAllowedIP(address string) error {
204-
if address == allInternetTrafficAddress {
202+
// AddAllowedCIDR adds a single CIDR to the allow set at runtime.
203+
func (fw *Firewall) AddAllowedCIDR(cidr string) error {
204+
if cidr == allInternetTrafficAddress {
205205
// Internet is enabled by default.
206206
return nil
207207
}
208208

209-
err := canAllowAddress(address)
209+
err := canAllowCIDR(cidr)
210210
if err != nil {
211211
return err
212212
}
213213

214-
data, err := set.AddressStringsToSetData([]string{address})
214+
data, err := set.AddressStringsToSetData([]string{cidr})
215215
if err != nil {
216216
return err
217217
}
@@ -268,34 +268,25 @@ func (fw *Firewall) ResetAllowedCustom() error {
268268
return fw.conn.Flush()
269269
}
270270

271-
// canAllowAddress checks if the address is in the default blocked ranges.
272-
func canAllowAddress(address string) error {
271+
// canAllowCIDR checks if the address is in the default blocked ranges.
272+
func canAllowCIDR(cidr string) error {
273273
blockedData, err := set.AddressStringsToSetData(blockedRanges)
274274
if err != nil {
275275
return err
276276
}
277277

278-
addressData, err := set.AddressStringsToSetData([]string{address})
278+
addressData, err := set.AddressStringsToSetData([]string{cidr})
279279
if err != nil {
280280
return err
281281
}
282282

283283
if len(addressData) == 0 {
284-
return fmt.Errorf("address %s is not a valid IP address", address)
285-
}
286-
287-
// Convert single IP address to prefix for comparison
288-
var addressPrefix netip.Prefix
289-
if !addressData[0].Prefix.IsValid() {
290-
// If it's a single IP (not a CIDR), convert it to a /32 or /128 prefix
291-
addressPrefix = netip.PrefixFrom(addressData[0].Address, addressData[0].Address.BitLen())
292-
} else {
293-
addressPrefix = addressData[0].Prefix
284+
return fmt.Errorf("address %s is not a valid IP address", cidr)
294285
}
295286

296287
for _, blockedRange := range blockedData {
297-
if blockedRange.Prefix.Overlaps(addressPrefix) {
298-
return fmt.Errorf("address %s is blocked by the provider and cannot be added to the allow list", address)
288+
if blockedRange.Prefix.Overlaps(addressData[0].Prefix) {
289+
return fmt.Errorf("address %s is blocked by the provider and cannot be added to the allow list", cidr)
299290
}
300291
}
301292

packages/orchestrator/internal/sandbox/network/firewall_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ func TestCanAllowAddress_PrivateRangesBlocked(t *testing.T) {
4242
},
4343
{
4444
name: "specific_ip_in_10.0.0.0/8",
45-
address: "10.1.2.3",
45+
address: "10.1.2.3/32",
4646
shouldErr: true,
4747
desc: "specific IP in 10.0.0.0/8 range should be blocked",
4848
},
4949
{
5050
name: "specific_ip_in_192.168.0.0/16",
51-
address: "192.168.1.1",
51+
address: "192.168.1.1/32",
5252
shouldErr: true,
5353
desc: "specific IP in 192.168.0.0/16 range should be blocked",
5454
},
5555
{
5656
name: "specific_ip_in_172.16.0.0/12",
57-
address: "172.16.0.1",
57+
address: "172.16.0.1/32",
5858
shouldErr: true,
5959
desc: "specific IP in 172.16.0.0/12 range should be blocked",
6060
},
@@ -66,13 +66,13 @@ func TestCanAllowAddress_PrivateRangesBlocked(t *testing.T) {
6666
},
6767
{
6868
name: "public_ip_8.8.8.8",
69-
address: "8.8.8.8",
69+
address: "8.8.8.8/32",
7070
shouldErr: false,
7171
desc: "public IP should be allowed",
7272
},
7373
{
7474
name: "public_ip_1.1.1.1",
75-
address: "1.1.1.1",
75+
address: "1.1.1.1/32",
7676
shouldErr: false,
7777
desc: "public IP should be allowed",
7878
},
@@ -86,7 +86,7 @@ func TestCanAllowAddress_PrivateRangesBlocked(t *testing.T) {
8686

8787
for _, tc := range testCases {
8888
t.Run(tc.name, func(t *testing.T) {
89-
err := canAllowAddress(tc.address)
89+
err := canAllowCIDR(tc.address)
9090
if tc.shouldErr {
9191
require.Error(t, err, tc.desc)
9292
require.Contains(t, err.Error(), "blocked by the provider", "Error message should indicate provider blocking")
@@ -123,7 +123,7 @@ func TestCanAllowAddress_InvalidAddresses(t *testing.T) {
123123

124124
for _, tc := range testCases {
125125
t.Run(tc.name, func(t *testing.T) {
126-
err := canAllowAddress(tc.address)
126+
err := canAllowCIDR(tc.address)
127127
require.Error(t, err, tc.desc)
128128
})
129129
}

packages/orchestrator/internal/sandbox/network/slot.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func (s *Slot) ConfigureInternet(ctx context.Context, network *orchestrator.Sand
255255
))
256256
defer span.End()
257257

258-
if network == nil || len(network.GetEgress().GetAllowedAddresses()) == 0 && len(network.GetEgress().GetBlockedAddresses()) == 0 {
258+
if e := network.GetEgress(); len(e.GetAllowedCidrs()) == 0 && len(e.GetDeniedCidrs()) == 0 {
259259
// Internet access is allowed by default.
260260
return nil
261261
}
@@ -269,15 +269,15 @@ func (s *Slot) ConfigureInternet(ctx context.Context, network *orchestrator.Sand
269269
defer n.Close()
270270

271271
err = n.Do(func(_ ns.NetNS) error {
272-
for _, address := range network.GetEgress().GetAllowedAddresses() {
273-
err = s.Firewall.AddAllowedIP(address)
272+
for _, cidr := range network.GetEgress().GetAllowedCidrs() {
273+
err = s.Firewall.AddAllowedCIDR(cidr)
274274
if err != nil {
275275
return fmt.Errorf("error setting firewall rules: %w", err)
276276
}
277277
}
278278

279-
for _, address := range network.GetEgress().GetBlockedAddresses() {
280-
err = s.Firewall.AddBlockedIP(address)
279+
for _, cidr := range network.GetEgress().GetDeniedCidrs() {
280+
err = s.Firewall.AddDeniedCIDR(cidr)
281281
if err != nil {
282282
return fmt.Errorf("error setting firewall rules: %w", err)
283283
}

packages/orchestrator/internal/server/sandboxes.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const (
3535
requestTimeout = 60 * time.Second
3636
maxStartingInstancesPerNode = 3
3737

38-
internetBlockAddress = "0.0.0.0/0"
38+
internetBlockCIDR = "0.0.0.0/0"
3939
)
4040

4141
func (s *Server) Create(ctx context.Context, req *orchestrator.SandboxCreateRequest) (*orchestrator.SandboxCreateResponse, error) {
@@ -106,6 +106,7 @@ func (s *Server) Create(ctx context.Context, req *orchestrator.SandboxCreateRequ
106106
network := proto.CloneOf(req.GetSandbox().GetNetwork())
107107

108108
// TODO: Temporarily set this based on global config, should be removed later
109+
// https://linear.app/e2b/issue/ENG-3291
109110
// (it should be passed network config from API)
110111
allowInternet := s.config.AllowSandboxInternet
111112
if req.GetSandbox().AllowInternetAccess != nil {
@@ -118,7 +119,7 @@ func (s *Server) Create(ctx context.Context, req *orchestrator.SandboxCreateRequ
118119
if network.GetEgress() == nil {
119120
network.Egress = &orchestrator.SandboxNetworkEgressConfig{}
120121
}
121-
network.Egress.BlockedAddresses = []string{internetBlockAddress}
122+
network.Egress.DeniedCidrs = []string{internetBlockCIDR}
122123
}
123124

124125
sbx, err := s.sandboxFactory.ResumeSandbox(

0 commit comments

Comments
 (0)