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..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,4 +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_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 c5577f370..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,6 +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"` + // 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 { @@ -91,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 43462aac6..64fa02656 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, + HTTPNetworkProcotol: cfg.HTTPNetworkProtocol, } } @@ -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 + HTTPNetworkProcotol string l *net.Listener } @@ -106,7 +108,7 @@ func (s *StepHTTPServer) Run(ctx context.Context, state multistep.StateBag) mult Min: s.HTTPPortMin, Max: s.HTTPPortMax, Addr: s.HTTPAddress, - Network: "tcp", + Network: s.HTTPNetworkProcotol, }.Listen(ctx) if err != nil {