|
| 1 | +package sandbox |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/coreos/go-iptables/iptables" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/e2b-dev/infra/packages/orchestrator/pkg/sandbox/envd" |
| 16 | + "github.com/e2b-dev/infra/packages/orchestrator/pkg/sandbox/network" |
| 17 | +) |
| 18 | + |
| 19 | +// mockEgressProxy is a test EgressProxy that returns a fixed set of CA certificates. |
| 20 | +type mockEgressProxy struct { |
| 21 | + certs []network.CACertificate |
| 22 | +} |
| 23 | + |
| 24 | +func (m *mockEgressProxy) OnSlotCreate(_ *network.Slot, _ *iptables.IPTables) error { return nil } |
| 25 | +func (m *mockEgressProxy) OnSlotDelete(_ *network.Slot, _ *iptables.IPTables) error { return nil } |
| 26 | +func (m *mockEgressProxy) CACertificates() []network.CACertificate { return m.certs } |
| 27 | + |
| 28 | +// newTestSandboxWithCerts builds a minimal Sandbox that has CACertificates set — |
| 29 | +// mirroring what Factory.CreateSandbox does with f.egressProxy.CACertificates(). |
| 30 | +func newTestSandboxWithCerts(certs []network.CACertificate) *Sandbox { |
| 31 | + return &Sandbox{ |
| 32 | + Metadata: &Metadata{ |
| 33 | + internalConfig: internalConfig{EnvdInitRequestTimeout: 5 * time.Second}, |
| 34 | + Config: NewConfig(Config{}), |
| 35 | + Runtime: RuntimeMetadata{SandboxID: "test-sandbox"}, |
| 36 | + }, |
| 37 | + CACertificates: certs, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestConvertCACertificates(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + |
| 44 | + t.Run("converts network certs to envd certs preserving name and PEM", func(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + |
| 47 | + proxy := &mockEgressProxy{ |
| 48 | + certs: []network.CACertificate{ |
| 49 | + {Name: "proxy-ca", Cert: "-----BEGIN CERTIFICATE-----\nABC\n-----END CERTIFICATE-----\n"}, |
| 50 | + {Name: "custom-ca", Cert: "-----BEGIN CERTIFICATE-----\nDEF\n-----END CERTIFICATE-----\n"}, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + sbx := newTestSandboxWithCerts(proxy.CACertificates()) |
| 55 | + result := sbx.convertCACertificates(sbx.CACertificates) |
| 56 | + |
| 57 | + require.Len(t, result, 2) |
| 58 | + assert.Equal(t, "proxy-ca", result[0].Name) |
| 59 | + assert.Equal(t, "-----BEGIN CERTIFICATE-----\nABC\n-----END CERTIFICATE-----\n", result[0].Cert) |
| 60 | + assert.Equal(t, "custom-ca", result[1].Name) |
| 61 | + assert.Equal(t, "-----BEGIN CERTIFICATE-----\nDEF\n-----END CERTIFICATE-----\n", result[1].Cert) |
| 62 | + }) |
| 63 | + |
| 64 | + t.Run("returns empty slice for nil certs", func(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + sbx := newTestSandboxWithCerts(nil) |
| 67 | + result := sbx.convertCACertificates(nil) |
| 68 | + assert.Empty(t, result) |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +// TestEnvdInitSendsCACertificates verifies the full injection chain: |
| 73 | +// EgressProxy.CACertificates() → Sandbox.CACertificates → POST /init body. |
| 74 | +func TestEnvdInitSendsCACertificates(t *testing.T) { |
| 75 | + // Not parallel: overrides the package-level sandboxHttpClient. |
| 76 | + |
| 77 | + proxy := &mockEgressProxy{ |
| 78 | + certs: []network.CACertificate{ |
| 79 | + {Name: "proxy-ca", Cert: "-----BEGIN CERTIFICATE-----\nPROXY\n-----END CERTIFICATE-----\n"}, |
| 80 | + {Name: "custom-ca", Cert: "-----BEGIN CERTIFICATE-----\nCUSTOM\n-----END CERTIFICATE-----\n"}, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + // Simulate what Factory.CreateSandbox does when assigning egress proxy certs. |
| 85 | + sbx := newTestSandboxWithCerts(proxy.CACertificates()) |
| 86 | + |
| 87 | + var captured envd.PostInitJSONBody |
| 88 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 89 | + require.Equal(t, http.MethodPost, r.Method) |
| 90 | + require.Equal(t, "/init", r.URL.Path) |
| 91 | + |
| 92 | + err := json.NewDecoder(r.Body).Decode(&captured) |
| 93 | + require.NoError(t, err) |
| 94 | + |
| 95 | + w.WriteHeader(http.StatusNoContent) |
| 96 | + })) |
| 97 | + defer server.Close() |
| 98 | + |
| 99 | + // Temporarily swap the package-level client so the sandbox reaches our test server. |
| 100 | + orig := sandboxHttpClient |
| 101 | + sandboxHttpClient = http.Client{Timeout: 5 * time.Second} |
| 102 | + defer func() { sandboxHttpClient = orig }() |
| 103 | + |
| 104 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 105 | + defer cancel() |
| 106 | + |
| 107 | + _, _, err := sbx.doRequestWithInfiniteRetries(ctx, http.MethodPost, server.URL+"/init") |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + require.Len(t, captured.CaCertificates, 2) |
| 111 | + assert.Equal(t, proxy.certs[0].Name, captured.CaCertificates[0].Name) |
| 112 | + assert.Equal(t, proxy.certs[0].Cert, captured.CaCertificates[0].Cert) |
| 113 | + assert.Equal(t, proxy.certs[1].Name, captured.CaCertificates[1].Name) |
| 114 | + assert.Equal(t, proxy.certs[1].Cert, captured.CaCertificates[1].Cert) |
| 115 | +} |
0 commit comments