From 4933a3f56f95731bf5f62354444b7070bffd37d2 Mon Sep 17 00:00:00 2001 From: ejdre Date: Wed, 10 Jul 2024 19:38:08 +0100 Subject: [PATCH 1/2] Added property to force http server to bind only to IPv4 address --- .../commonsteps/HTTPConfig-not-required.mdx | 2 ++ multistep/commonsteps/http_config.go | 2 ++ multistep/commonsteps/step_http_server.go | 28 +++++++++++-------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cmd/packer-sdc/internal/renderdocs/docs-partials/packer-plugin-sdk/multistep/commonsteps/HTTPConfig-not-required.mdx b/cmd/packer-sdc/internal/renderdocs/docs-partials/packer-plugin-sdk/multistep/commonsteps/HTTPConfig-not-required.mdx index 83f8a4c20..adfff7435 100644 --- a/cmd/packer-sdc/internal/renderdocs/docs-partials/packer-plugin-sdk/multistep/commonsteps/HTTPConfig-not-required.mdx +++ b/cmd/packer-sdc/internal/renderdocs/docs-partials/packer-plugin-sdk/multistep/commonsteps/HTTPConfig-not-required.mdx @@ -34,4 +34,6 @@ - `http_bind_address` (string) - This is the bind address for the HTTP server. Defaults to 0.0.0.0 so that it will work with any network interface. +- `http_only_ipv4` (bool) - If true the HTTP server will only be bound to an IPv4 interface + diff --git a/multistep/commonsteps/http_config.go b/multistep/commonsteps/http_config.go index c5577f370..b84c14ef8 100644 --- a/multistep/commonsteps/http_config.go +++ b/multistep/commonsteps/http_config.go @@ -58,6 +58,8 @@ type HTTPConfig struct { // interface with a non-loopback address. Either `http_bind_address` or // `http_interface` can be specified. HTTPInterface string `mapstructure:"http_interface" undocumented:"true"` + // If true the HTTP server will only be bound to an IPv4 interface + HTTPOnlyIPv4 bool `mapstructure:"http_only_ipv4"` } func (c *HTTPConfig) Prepare(ctx *interpolate.Context) []error { diff --git a/multistep/commonsteps/step_http_server.go b/multistep/commonsteps/step_http_server.go index 43462aac6..78e12cff6 100644 --- a/multistep/commonsteps/step_http_server.go +++ b/multistep/commonsteps/step_http_server.go @@ -20,11 +20,12 @@ import ( func HTTPServerFromHTTPConfig(cfg *HTTPConfig) *StepHTTPServer { return &StepHTTPServer{ - HTTPDir: cfg.HTTPDir, - HTTPContent: cfg.HTTPContent, - HTTPPortMin: cfg.HTTPPortMin, - HTTPPortMax: cfg.HTTPPortMax, - HTTPAddress: cfg.HTTPAddress, + HTTPDir: cfg.HTTPDir, + HTTPContent: cfg.HTTPContent, + HTTPPortMin: cfg.HTTPPortMin, + HTTPPortMax: cfg.HTTPPortMax, + HTTPAddress: cfg.HTTPAddress, + HTTPOnlyIPv4: cfg.HTTPOnlyIPv4, } } @@ -40,11 +41,12 @@ func HTTPServerFromHTTPConfig(cfg *HTTPConfig) *StepHTTPServer { // // http_port int - The port the HTTP server started on. type StepHTTPServer struct { - HTTPDir string - HTTPContent map[string]string - HTTPPortMin int - HTTPPortMax int - HTTPAddress string + HTTPDir string + HTTPContent map[string]string + HTTPPortMin int + HTTPPortMax int + HTTPAddress string + HTTPOnlyIPv4 bool l *net.Listener } @@ -102,11 +104,15 @@ func (s *StepHTTPServer) Run(ctx context.Context, state multistep.StateBag) mult // Find an available TCP port for our HTTP server var err error + network := "tcp" + if s.HTTPOnlyIPv4 { + network = "tcp4" + } s.l, err = net.ListenRangeConfig{ Min: s.HTTPPortMin, Max: s.HTTPPortMax, Addr: s.HTTPAddress, - Network: "tcp", + Network: network, }.Listen(ctx) if err != nil { From 3eeaa90c31f4d7adbdb7e63317d9bd63bbe0d050 Mon Sep 17 00:00:00 2001 From: ejdre Date: Mon, 19 Aug 2024 01:38:25 +0100 Subject: [PATCH 2/2] Changed HTTPOnlyIPV4 to HTTPNetworkProtocol where the user can specify the protocol --- .../commonsteps/HTTPConfig-not-required.mdx | 3 +- multistep/commonsteps/http_config.go | 40 ++++++++++++++++++- multistep/commonsteps/step_http_server.go | 30 ++++++-------- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/cmd/packer-sdc/internal/renderdocs/docs-partials/packer-plugin-sdk/multistep/commonsteps/HTTPConfig-not-required.mdx b/cmd/packer-sdc/internal/renderdocs/docs-partials/packer-plugin-sdk/multistep/commonsteps/HTTPConfig-not-required.mdx index adfff7435..3f09a30a9 100644 --- a/cmd/packer-sdc/internal/renderdocs/docs-partials/packer-plugin-sdk/multistep/commonsteps/HTTPConfig-not-required.mdx +++ b/cmd/packer-sdc/internal/renderdocs/docs-partials/packer-plugin-sdk/multistep/commonsteps/HTTPConfig-not-required.mdx @@ -34,6 +34,7 @@ - `http_bind_address` (string) - This is the bind address for the HTTP server. Defaults to 0.0.0.0 so that it will work with any network interface. -- `http_only_ipv4` (bool) - If true the HTTP server will only be bound to an IPv4 interface +- `http_network_protocol` (string) - Defines the HTTP Network protocol. Valid options are `tcp`, `tcp4`, `tcp6`, + `unix`, and `unixpacket`. This value defaults to `tcp`. diff --git a/multistep/commonsteps/http_config.go b/multistep/commonsteps/http_config.go index b84c14ef8..eb9b790c2 100644 --- a/multistep/commonsteps/http_config.go +++ b/multistep/commonsteps/http_config.go @@ -7,10 +7,20 @@ package commonsteps import ( "errors" + "fmt" "github.com/hashicorp/packer-plugin-sdk/template/interpolate" ) +// These are the different valid network procotol values for "http_network_protocol" +const ( + NetworkProtocolTCP string = "tcp" + NetworkProcotolTCP4 = "tcp4" + NetworkProtocolTCP6 = "tcp6" + NetworkProtocolUnix = "unix" + NetworkProcotlUnixPacket = "unixpacket" +) + // Packer will create an http server serving `http_directory` when it is set, a // random free port will be selected and the architecture of the directory // referenced will be available in your builder. @@ -58,8 +68,9 @@ type HTTPConfig struct { // interface with a non-loopback address. Either `http_bind_address` or // `http_interface` can be specified. HTTPInterface string `mapstructure:"http_interface" undocumented:"true"` - // If true the HTTP server will only be bound to an IPv4 interface - HTTPOnlyIPv4 bool `mapstructure:"http_only_ipv4"` + // Defines the HTTP Network protocol. Valid options are `tcp`, `tcp4`, `tcp6`, + // `unix`, and `unixpacket`. This value defaults to `tcp`. + HTTPNetworkProtocol string `mapstructure:"http_network_protocol"` } func (c *HTTPConfig) Prepare(ctx *interpolate.Context) []error { @@ -93,5 +104,30 @@ func (c *HTTPConfig) Prepare(ctx *interpolate.Context) []error { 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")) } + if c.HTTPNetworkProtocol == "" { + c.HTTPNetworkProtocol = "tcp" + } + + validProtocol := false + validProtocols := []string{ + NetworkProtocolTCP, + NetworkProcotolTCP4, + NetworkProtocolTCP6, + NetworkProtocolUnix, + NetworkProcotlUnixPacket, + } + + for _, protocol := range validProtocols { + if c.HTTPNetworkProtocol == protocol { + validProtocol = true + break + } + } + + if !validProtocol { + errs = append(errs, + fmt.Errorf("http_network_protocol is invalid. Must be one of: %v", validProtocols)) + } + return errs } diff --git a/multistep/commonsteps/step_http_server.go b/multistep/commonsteps/step_http_server.go index 78e12cff6..64fa02656 100644 --- a/multistep/commonsteps/step_http_server.go +++ b/multistep/commonsteps/step_http_server.go @@ -20,12 +20,12 @@ import ( func HTTPServerFromHTTPConfig(cfg *HTTPConfig) *StepHTTPServer { return &StepHTTPServer{ - HTTPDir: cfg.HTTPDir, - HTTPContent: cfg.HTTPContent, - HTTPPortMin: cfg.HTTPPortMin, - HTTPPortMax: cfg.HTTPPortMax, - HTTPAddress: cfg.HTTPAddress, - HTTPOnlyIPv4: cfg.HTTPOnlyIPv4, + HTTPDir: cfg.HTTPDir, + HTTPContent: cfg.HTTPContent, + HTTPPortMin: cfg.HTTPPortMin, + HTTPPortMax: cfg.HTTPPortMax, + HTTPAddress: cfg.HTTPAddress, + HTTPNetworkProcotol: cfg.HTTPNetworkProtocol, } } @@ -41,12 +41,12 @@ func HTTPServerFromHTTPConfig(cfg *HTTPConfig) *StepHTTPServer { // // http_port int - The port the HTTP server started on. type StepHTTPServer struct { - HTTPDir string - HTTPContent map[string]string - HTTPPortMin int - HTTPPortMax int - HTTPAddress string - HTTPOnlyIPv4 bool + HTTPDir string + HTTPContent map[string]string + HTTPPortMin int + HTTPPortMax int + HTTPAddress string + HTTPNetworkProcotol string l *net.Listener } @@ -104,15 +104,11 @@ func (s *StepHTTPServer) Run(ctx context.Context, state multistep.StateBag) mult // Find an available TCP port for our HTTP server var err error - network := "tcp" - if s.HTTPOnlyIPv4 { - network = "tcp4" - } s.l, err = net.ListenRangeConfig{ Min: s.HTTPPortMin, Max: s.HTTPPortMax, Addr: s.HTTPAddress, - Network: network, + Network: s.HTTPNetworkProcotol, }.Listen(ctx) if err != nil {