Skip to content

Commit be976c0

Browse files
committed
Merge branch 'main' of github.com:e2b-dev/infra into lev-block-cache-bitmap
2 parents 7fc43fc + 701b085 commit be976c0

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

packages/db/pkg/pool/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/exaring/otelpgx"
8+
"github.com/jackc/pgx/v5"
89
"github.com/jackc/pgx/v5/pgxpool"
910
"go.opentelemetry.io/otel/attribute"
1011

@@ -28,6 +29,9 @@ func New(ctx context.Context, databaseURL string, poolName string, options ...Op
2829
// expose otel traces
2930
config.ConnConfig.Tracer = otelpgx.NewTracer()
3031

32+
// Disable statement caching to avoid issues with prepared statements in transactions
33+
config.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeExec
34+
3135
// Create the connection pool
3236
pool, err := pgxpool.NewWithConfig(ctx, config)
3337
if err != nil {
@@ -41,9 +45,5 @@ func New(ctx context.Context, databaseURL string, poolName string, options ...Op
4145
return nil, nil, fmt.Errorf("failed to record stats: %w", err)
4246
}
4347

44-
// TODO [ENG-3437]: Uncomment
45-
// Disable statement caching to avoid issues with prepared statements in transactions
46-
// config.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeExec
47-
4848
return retry.Wrap(pool, retryConfig), pool, nil
4949
}

packages/db/pkg/types/types.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
package types
22

3-
import "encoding/json"
3+
import (
4+
"database/sql/driver"
5+
"encoding/json"
6+
)
7+
8+
func jsonbValue(v any) (driver.Value, error) {
9+
buf, err := json.Marshal(v)
10+
if err != nil {
11+
return nil, err
12+
}
413

14+
return string(buf), nil
15+
}
16+
17+
//nolint:recvcheck // JSONBStringMap needs pointer receiver for unmarshal allocation and value receivers for marshal/driver.Valuer.
518
type JSONBStringMap map[string]string
619

720
// MarshalJSON ensures a nil map serializes as "{}" instead of "null",
@@ -14,6 +27,10 @@ func (m JSONBStringMap) MarshalJSON() ([]byte, error) {
1427
return json.Marshal(map[string]string(m))
1528
}
1629

30+
func (m JSONBStringMap) Value() (driver.Value, error) {
31+
return jsonbValue(m)
32+
}
33+
1734
// UnmarshalJSON ensures JSON null deserializes as an empty map instead of nil.
1835
func (m *JSONBStringMap) UnmarshalJSON(data []byte) error {
1936
if string(data) == "null" {
@@ -40,6 +57,10 @@ type BuildReason struct {
4057
Step *string `json:"step,omitempty"`
4158
}
4259

60+
func (r BuildReason) Value() (driver.Value, error) {
61+
return jsonbValue(r)
62+
}
63+
4364
const PausedSandboxConfigVersion = "v1"
4465

4566
type SandboxNetworkEgressConfig struct {
@@ -85,6 +106,10 @@ type PausedSandboxConfig struct {
85106
VolumeMounts []*SandboxVolumeMountConfig `json:"volumeMounts,omitempty"`
86107
}
87108

109+
func (c PausedSandboxConfig) Value() (driver.Value, error) {
110+
return jsonbValue(c)
111+
}
112+
88113
// BuildStatus represents the raw status value written to the env_builds table.
89114
// Use BuildStatusGroup for read-side comparisons.
90115
type BuildStatus string

packages/db/scripts/migrator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func main() {
4545

4646
return err
4747
}
48+
poolConfig.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeExec
4849

4950
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
5051
if err != nil {

packages/orchestrator/pkg/hyperloopserver/handlers/logs.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handlers
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"net"
78
"net/http"
89

@@ -33,6 +34,14 @@ func (h *APIStore) Logs(c *gin.Context) {
3334
return
3435
}
3536

37+
err = h.validatePayloadSandboxID(payload, sbxID)
38+
if err != nil {
39+
h.sendAPIStoreError(c, http.StatusBadRequest, "Invalid sandboxID in logs payload")
40+
h.logger.Error(ctx, "error when parsing sandbox logs request", zap.Error(err), logger.WithSandboxID(sbxID))
41+
42+
return
43+
}
44+
3645
// Overwrite instanceID and teamID to avoid spoofing
3746
payload["instanceID"] = sbxID
3847
payload["teamID"] = sbx.Runtime.TeamID
@@ -65,3 +74,21 @@ func (h *APIStore) Logs(c *gin.Context) {
6574

6675
c.Status(http.StatusOK)
6776
}
77+
78+
// validatePayloadSandboxID checks if the payload contains correct instanceID to prevent slow requests to contaminating the logs of other sandboxes.
79+
func (h *APIStore) validatePayloadSandboxID(payload map[string]any, sbxID string) error {
80+
if payload["instanceID"] == nil {
81+
return fmt.Errorf("missing sandboxID in logs payload")
82+
}
83+
84+
payloadSandboxID, ok := payload["instanceID"].(string)
85+
if !ok {
86+
return fmt.Errorf("instanceID in logs payload is not a string: %v", payload["instanceID"])
87+
}
88+
89+
if payloadSandboxID != sbxID {
90+
return fmt.Errorf("sandboxID in logs payload does not match the sandboxID of the source sandbox (%s != %s)", payloadSandboxID, sbxID)
91+
}
92+
93+
return nil
94+
}

packages/shared/scripts/package-lock.json

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

0 commit comments

Comments
 (0)