Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to change the HTTP network protocol #257

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

<!-- End of code generated from the comments of the HTTPConfig struct in multistep/commonsteps/http_config.go; -->
38 changes: 38 additions & 0 deletions multistep/commonsteps/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
24 changes: 13 additions & 11 deletions multistep/commonsteps/step_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
Loading