Skip to content

Commit e631d66

Browse files
committed
Add local port binding support
1 parent 51009a5 commit e631d66

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

internal/components.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ type BuilderHubPostgres struct {
435435
func (b *BuilderHubPostgres) Run(service *service, ctx *ExContext) {
436436
service.
437437
WithImage("docker.io/flashbots/builder-hub-db").
438-
WithTag("latest").
439-
WithPort("postgres", 5432).
438+
WithTag("0.2.1").
439+
WithLocalPort("postgres", 5432).
440440
WithEnv("POSTGRES_USER", "postgres").
441441
WithEnv("POSTGRES_PASSWORD", "postgres").
442442
WithEnv("POSTGRES_DB", "postgres").
@@ -460,7 +460,7 @@ type BuilderHub struct {
460460
func (b *BuilderHub) Run(service *service, ctx *ExContext) {
461461
service.
462462
WithImage("docker.io/flashbots/builder-hub").
463-
WithTag("latest").
463+
WithTag("0.2.1").
464464
WithEntrypoint("/app/builder-hub").
465465
WithEnv("POSTGRES_DSN", "postgres://postgres:postgres@"+ConnectRaw(b.postgres, "postgres", "")+"/postgres?sslmode=disable").
466466
WithEnv("LISTEN_ADDR", "0.0.0.0:"+`{{Port "http" 8080}}`).
@@ -483,7 +483,7 @@ func (b *BuilderHubMockProxy) Run(service *service, ctx *ExContext) {
483483
service.
484484
WithImage("nginx").
485485
WithTag("1.27").
486-
WithPort("http", 8888).
486+
WithLocalPort("http", 8888).
487487
DependsOnRunning(b.TargetService).
488488
WithEntrypoint("/bin/sh").
489489
WithArgs("-c", fmt.Sprintf(`cat > /etc/nginx/conf.d/default.conf << 'EOF'

internal/local_runner.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,13 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
583583
if len(s.ports) > 0 {
584584
ports := []string{}
585585
for _, p := range s.ports {
586-
ports = append(ports, fmt.Sprintf("%d:%d", p.HostPort, p.Port))
586+
if p.Local {
587+
// Bind only to localhost (127.0.0.1)
588+
ports = append(ports, fmt.Sprintf("127.0.0.1:%d:%d", p.HostPort, p.Port))
589+
} else {
590+
// Bind to all interfaces (default)
591+
ports = append(ports, fmt.Sprintf("%d:%d", p.HostPort, p.Port))
592+
}
587593
}
588594
service["ports"] = ports
589595
}

internal/manifest.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ type Port struct {
222222
// container port. It is populated by the local runner
223223
// TODO: We might want to move this to the runner itself.
224224
HostPort int
225+
226+
// Local indicates if this port should only be bound to localhost (127.0.0.1)
227+
Local bool
225228
}
226229

227230
// NodeRef describes a reference from one service to another
@@ -356,7 +359,7 @@ func (s *service) WithTag(tag string) *service {
356359
return s
357360
}
358361

359-
func (s *service) WithPort(name string, portNumber int) *service {
362+
func (s *service) WithPort(name string, portNumber int, local bool) *service {
360363
// add the port if not already present with the same name.
361364
// if preset with the same name, they must have same port number
362365
for _, p := range s.ports {
@@ -367,16 +370,26 @@ func (s *service) WithPort(name string, portNumber int) *service {
367370
return s
368371
}
369372
}
370-
s.ports = append(s.ports, &Port{Name: name, Port: portNumber})
373+
s.ports = append(s.ports, &Port{Name: name, Port: portNumber, Local: local})
371374
return s
372375
}
373376

377+
// WithLocalPort is a convenience method to add a port that should only be bound to localhost
378+
func (s *service) WithLocalPort(name string, portNumber int) *service {
379+
return s.WithPort(name, portNumber, true)
380+
}
381+
382+
// WithPublicPort is a convenience method to add a port that should be bound to all interfaces (default behavior)
383+
func (s *service) WithPublicPort(name string, portNumber int) *service {
384+
return s.WithPort(name, portNumber, false)
385+
}
386+
374387
func (s *service) applyTemplate(arg string) {
375388
var port []Port
376389
var nodeRef []NodeRef
377390
_, port, nodeRef = applyTemplate(arg)
378391
for _, p := range port {
379-
s.WithPort(p.Name, p.Port)
392+
s.WithPort(p.Name, p.Port, false) // Default to non-local ports for backward compatibility
380393
}
381394
for _, n := range nodeRef {
382395
s.nodeRefs = append(s.nodeRefs, &n)
@@ -445,7 +458,7 @@ func applyTemplate(templateStr string) (string, []Port, []NodeRef) {
445458
return fmt.Sprintf(`{{Service "%s" "%s"}}`, name, portLabel)
446459
},
447460
"Port": func(name string, defaultPort int) string {
448-
portRef = append(portRef, Port{Name: name, Port: defaultPort})
461+
portRef = append(portRef, Port{Name: name, Port: defaultPort, Local: false})
449462
return fmt.Sprintf(`{{Port "%s" %d}}`, name, defaultPort)
450463
},
451464
}

main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var dryRun bool
2424
var interactive bool
2525
var timeout time.Duration
2626
var logLevelFlag string
27+
var localPortsFlag bool
2728

2829
var rootCmd = &cobra.Command{
2930
Use: "playground",
@@ -167,6 +168,7 @@ func main() {
167168
recipeCmd.Flags().BoolVar(&interactive, "interactive", false, "interactive mode")
168169
recipeCmd.Flags().DurationVar(&timeout, "timeout", 0, "") // Used for CI
169170
recipeCmd.Flags().StringVar(&logLevelFlag, "log-level", "info", "log level")
171+
recipeCmd.Flags().BoolVar(&localPortsFlag, "local-ports", false, "bind all ports to localhost only (127.0.0.1) for enhanced security")
170172

171173
cookCmd.AddCommand(recipeCmd)
172174
}
@@ -217,6 +219,15 @@ func runIt(recipe internal.Recipe) error {
217219
return fmt.Errorf("failed to validate manifest: %w", err)
218220
}
219221

222+
// set the local ports flag
223+
if localPortsFlag {
224+
for _, svc := range svcManager.Services() {
225+
for _, port := range svc.Ports() {
226+
port.Local = true
227+
}
228+
}
229+
}
230+
220231
// generate the dot graph
221232
dotGraph := svcManager.GenerateDotGraph()
222233
if err := artifacts.Out.WriteFile("graph.dot", dotGraph); err != nil {

0 commit comments

Comments
 (0)