Skip to content

Commit 11cc364

Browse files
ejdre-vestaslbajolet-hashicorp
authored andcommitted
Added property to force http server to bind only to IPv4 address
1 parent ec2468d commit 11cc364

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

cmd/packer-sdc/internal/renderdocs/docs-partials/packer-plugin-sdk/multistep/commonsteps/HTTPConfig-not-required.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@
3434
- `http_bind_address` (string) - This is the bind address for the HTTP server. Defaults to 0.0.0.0 so that
3535
it will work with any network interface.
3636

37+
- `http_only_ipv4` (bool) - If true the HTTP server will only be bound to an IPv4 interface
38+
3739
<!-- End of code generated from the comments of the HTTPConfig struct in multistep/commonsteps/http_config.go; -->

multistep/commonsteps/http_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ type HTTPConfig struct {
5858
// interface with a non-loopback address. Either `http_bind_address` or
5959
// `http_interface` can be specified.
6060
HTTPInterface string `mapstructure:"http_interface" undocumented:"true"`
61+
// If true the HTTP server will only be bound to an IPv4 interface
62+
HTTPOnlyIPv4 bool `mapstructure:"http_only_ipv4"`
6163
}
6264

6365
func (c *HTTPConfig) Prepare(ctx *interpolate.Context) []error {

multistep/commonsteps/step_http_server.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import (
2020

2121
func HTTPServerFromHTTPConfig(cfg *HTTPConfig) *StepHTTPServer {
2222
return &StepHTTPServer{
23-
HTTPDir: cfg.HTTPDir,
24-
HTTPContent: cfg.HTTPContent,
25-
HTTPPortMin: cfg.HTTPPortMin,
26-
HTTPPortMax: cfg.HTTPPortMax,
27-
HTTPAddress: cfg.HTTPAddress,
23+
HTTPDir: cfg.HTTPDir,
24+
HTTPContent: cfg.HTTPContent,
25+
HTTPPortMin: cfg.HTTPPortMin,
26+
HTTPPortMax: cfg.HTTPPortMax,
27+
HTTPAddress: cfg.HTTPAddress,
28+
HTTPOnlyIPv4: cfg.HTTPOnlyIPv4,
2829
}
2930
}
3031

@@ -40,11 +41,12 @@ func HTTPServerFromHTTPConfig(cfg *HTTPConfig) *StepHTTPServer {
4041
//
4142
// http_port int - The port the HTTP server started on.
4243
type StepHTTPServer struct {
43-
HTTPDir string
44-
HTTPContent map[string]string
45-
HTTPPortMin int
46-
HTTPPortMax int
47-
HTTPAddress string
44+
HTTPDir string
45+
HTTPContent map[string]string
46+
HTTPPortMin int
47+
HTTPPortMax int
48+
HTTPAddress string
49+
HTTPOnlyIPv4 bool
4850

4951
l *net.Listener
5052
}
@@ -102,11 +104,15 @@ func (s *StepHTTPServer) Run(ctx context.Context, state multistep.StateBag) mult
102104

103105
// Find an available TCP port for our HTTP server
104106
var err error
107+
network := "tcp"
108+
if s.HTTPOnlyIPv4 {
109+
network = "tcp4"
110+
}
105111
s.l, err = net.ListenRangeConfig{
106112
Min: s.HTTPPortMin,
107113
Max: s.HTTPPortMax,
108114
Addr: s.HTTPAddress,
109-
Network: "tcp",
115+
Network: network,
110116
}.Listen(ctx)
111117

112118
if err != nil {

0 commit comments

Comments
 (0)