Skip to content

Commit d9e12b3

Browse files
committed
fix PR comments
1 parent 1c4264b commit d9e12b3

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

packages/orchestrator/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/gin-contrib/size v1.0.2
3232
github.com/gin-gonic/gin v1.10.1
3333
github.com/go-openapi/strfmt v0.23.0
34+
github.com/gogo/protobuf v1.3.2
3435
github.com/google/go-containerregistry v0.20.6
3536
github.com/google/nftables v0.3.0
3637
github.com/google/uuid v1.6.0
@@ -152,7 +153,6 @@ require (
152153
github.com/go-playground/universal-translator v0.18.1 // indirect
153154
github.com/go-playground/validator/v10 v10.26.0 // indirect
154155
github.com/goccy/go-json v0.10.5 // indirect
155-
github.com/gogo/protobuf v1.3.2 // indirect
156156
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
157157
github.com/google/go-cmp v0.7.0 // indirect
158158
github.com/google/pprof v0.0.0-20250501235452-c0086092b71a // indirect

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package network
22

33
import (
44
"fmt"
5-
"net"
65
"net/netip"
76

87
"github.com/google/nftables"
@@ -207,8 +206,9 @@ func (fw *Firewall) AddAllowedIP(address string) error {
207206
return nil
208207
}
209208

210-
if isAddressInBlockedRanges(address) {
211-
return fmt.Errorf("address %s is blocked by the provider and cannot be added to the allow list", address)
209+
err := canAllowAddress(address)
210+
if err != nil {
211+
return err
212212
}
213213

214214
data, err := set.AddressStringsToSetData([]string{address})
@@ -261,20 +261,34 @@ func (fw *Firewall) ResetBlockedCustom() error {
261261

262262
// ResetAllowedCustom resets allow set back to original ranges.
263263
func (fw *Firewall) ResetAllowedCustom() error {
264-
return nil
264+
if err := fw.allowSet.ClearAndAddElements(fw.conn, nil); err != nil {
265+
return err
266+
}
267+
268+
return fw.conn.Flush()
265269
}
266270

267-
// isAddressInBlockedRanges checks if the address is in the default blocked ranges.
268-
func isAddressInBlockedRanges(address string) bool {
269-
for _, blockedRange := range blockedRanges {
270-
_, blockedRangeNet, err := net.ParseCIDR(blockedRange)
271-
if err != nil {
272-
return false
273-
}
274-
if blockedRangeNet.Contains(net.ParseIP(address)) {
275-
return true
271+
// canAllowAddress checks if the address is in the default blocked ranges.
272+
func canAllowAddress(address string) error {
273+
blockedData, err := set.AddressStringsToSetData(blockedRanges)
274+
if err != nil {
275+
return err
276+
}
277+
278+
addressPrefix, err := set.AddressStringsToSetData([]string{address})
279+
if err != nil {
280+
return err
281+
}
282+
283+
if len(addressPrefix) == 0 {
284+
return fmt.Errorf("address %s is not a valid IP address", address)
285+
}
286+
287+
for _, blockedRange := range blockedData {
288+
if blockedRange.Prefix.Overlaps(addressPrefix[0].Prefix) {
289+
return fmt.Errorf("address %s is blocked by the provider and cannot be added to the allow list", address)
276290
}
277291
}
278292

279-
return false
293+
return nil
280294
}

packages/orchestrator/internal/server/sandboxes.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"time"
88

9+
"github.com/gogo/protobuf/proto"
910
"github.com/google/uuid"
1011
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
1112
"go.opentelemetry.io/otel"
@@ -101,7 +102,11 @@ func (s *Server) Create(ctx context.Context, req *orchestrator.SandboxCreateRequ
101102
return nil, fmt.Errorf("failed to get template snapshot data: %w", err)
102103
}
103104

104-
network := req.GetSandbox().GetNetwork()
105+
// Clone the network config to avoid modifying the original request
106+
network := &orchestrator.SandboxNetworkConfig{}
107+
if req.GetSandbox().GetNetwork() != nil {
108+
network = proto.Clone(req.GetSandbox().GetNetwork()).(*orchestrator.SandboxNetworkConfig)
109+
}
105110

106111
// TODO: Temporarily set this based on global config, should be removed later
107112
// (it should be passed network config from API)
@@ -110,7 +115,9 @@ func (s *Server) Create(ctx context.Context, req *orchestrator.SandboxCreateRequ
110115
allowInternet = req.GetSandbox().GetAllowInternetAccess()
111116
}
112117
if !allowInternet {
113-
network.Egress = network.GetEgress()
118+
if network.GetEgress() == nil {
119+
network.Egress = &orchestrator.SandboxNetworkEgressConfig{}
120+
}
114121
network.Egress.BlockedAddresses = []string{internetBlockAddress}
115122
}
116123

@@ -125,7 +132,7 @@ func (s *Server) Create(ctx context.Context, req *orchestrator.SandboxCreateRequ
125132
TotalDiskSizeMB: req.GetSandbox().GetTotalDiskSizeMb(),
126133
HugePages: req.GetSandbox().GetHugePages(),
127134

128-
Network: req.GetSandbox().GetNetwork(),
135+
Network: network,
129136

130137
Envd: sandbox.EnvdMetadata{
131138
Version: req.GetSandbox().GetEnvdVersion(),

0 commit comments

Comments
 (0)