Skip to content

Commit 756401f

Browse files
ejdre-vestaslbajolet-hashicorp
authored andcommitted
Changed HTTPOnlyIPV4 to HTTPNetworkProtocol where the user can specify the protocol
1 parent 11cc364 commit 756401f

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
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
37+
- `http_network_protocol` (string) - Defines the HTTP Network protocol. Valid options are `tcp`, `tcp4`, `tcp6`,
38+
`unix`, and `unixpacket`. This value defaults to `tcp`.
3839

3940
<!-- End of code generated from the comments of the HTTPConfig struct in multistep/commonsteps/http_config.go; -->

multistep/commonsteps/http_config.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@ package commonsteps
77

88
import (
99
"errors"
10+
"fmt"
1011

1112
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
1213
)
1314

15+
// These are the different valid network procotol values for "http_network_protocol"
16+
const (
17+
NetworkProtocolTCP string = "tcp"
18+
NetworkProcotolTCP4 = "tcp4"
19+
NetworkProtocolTCP6 = "tcp6"
20+
NetworkProtocolUnix = "unix"
21+
NetworkProcotlUnixPacket = "unixpacket"
22+
)
23+
1424
// Packer will create an http server serving `http_directory` when it is set, a
1525
// random free port will be selected and the architecture of the directory
1626
// referenced will be available in your builder.
@@ -58,8 +68,9 @@ type HTTPConfig struct {
5868
// interface with a non-loopback address. Either `http_bind_address` or
5969
// `http_interface` can be specified.
6070
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"`
71+
// Defines the HTTP Network protocol. Valid options are `tcp`, `tcp4`, `tcp6`,
72+
// `unix`, and `unixpacket`. This value defaults to `tcp`.
73+
HTTPNetworkProtocol string `mapstructure:"http_network_protocol"`
6374
}
6475

6576
func (c *HTTPConfig) Prepare(ctx *interpolate.Context) []error {
@@ -93,5 +104,30 @@ func (c *HTTPConfig) Prepare(ctx *interpolate.Context) []error {
93104
errors.New("http_content cannot be used in conjunction with http_dir. Consider using the file function to load file in memory and serve them with http_content: https://www.packer.io/docs/templates/hcl_templates/functions/file/file"))
94105
}
95106

107+
if c.HTTPNetworkProtocol == "" {
108+
c.HTTPNetworkProtocol = "tcp"
109+
}
110+
111+
validProtocol := false
112+
validProtocols := []string{
113+
NetworkProtocolTCP,
114+
NetworkProcotolTCP4,
115+
NetworkProtocolTCP6,
116+
NetworkProtocolUnix,
117+
NetworkProcotlUnixPacket,
118+
}
119+
120+
for _, protocol := range validProtocols {
121+
if c.HTTPNetworkProtocol == protocol {
122+
validProtocol = true
123+
break
124+
}
125+
}
126+
127+
if !validProtocol {
128+
errs = append(errs,
129+
fmt.Errorf("http_network_protocol is invalid. Must be one of: %v", validProtocols))
130+
}
131+
96132
return errs
97133
}

multistep/commonsteps/step_http_server.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +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,
28-
HTTPOnlyIPv4: cfg.HTTPOnlyIPv4,
23+
HTTPDir: cfg.HTTPDir,
24+
HTTPContent: cfg.HTTPContent,
25+
HTTPPortMin: cfg.HTTPPortMin,
26+
HTTPPortMax: cfg.HTTPPortMax,
27+
HTTPAddress: cfg.HTTPAddress,
28+
HTTPNetworkProcotol: cfg.HTTPNetworkProtocol,
2929
}
3030
}
3131

@@ -41,12 +41,12 @@ func HTTPServerFromHTTPConfig(cfg *HTTPConfig) *StepHTTPServer {
4141
//
4242
// http_port int - The port the HTTP server started on.
4343
type StepHTTPServer struct {
44-
HTTPDir string
45-
HTTPContent map[string]string
46-
HTTPPortMin int
47-
HTTPPortMax int
48-
HTTPAddress string
49-
HTTPOnlyIPv4 bool
44+
HTTPDir string
45+
HTTPContent map[string]string
46+
HTTPPortMin int
47+
HTTPPortMax int
48+
HTTPAddress string
49+
HTTPNetworkProcotol string
5050

5151
l *net.Listener
5252
}
@@ -104,15 +104,11 @@ func (s *StepHTTPServer) Run(ctx context.Context, state multistep.StateBag) mult
104104

105105
// Find an available TCP port for our HTTP server
106106
var err error
107-
network := "tcp"
108-
if s.HTTPOnlyIPv4 {
109-
network = "tcp4"
110-
}
111107
s.l, err = net.ListenRangeConfig{
112108
Min: s.HTTPPortMin,
113109
Max: s.HTTPPortMax,
114110
Addr: s.HTTPAddress,
115-
Network: network,
111+
Network: s.HTTPNetworkProcotol,
116112
}.Listen(ctx)
117113

118114
if err != nil {

0 commit comments

Comments
 (0)