From 4cbb123f016fb716b62387e4fc30de596674eed6 Mon Sep 17 00:00:00 2001 From: Buckley Ross Date: Fri, 28 Jan 2022 12:49:34 -0500 Subject: [PATCH 1/3] feat: allow custom hostnames for new containers The new `--hostname` flag is now available when invoking `toolbox create` in order to create a container with a custom hostname. The new flag does not enforce network-resolvability of any custom hostnames, but it does require that the hostname is valid, per RFC 1123. To preserve backward-compatibility, the hostname will still default to "toolbox" if the flag is not specified. Signed-off-by: Buckley Ross Change-Id: Ib3c0706e3a93a5748453e39998b9893e3e57c305 --- src/cmd/create.go | 27 ++++++++++++++++++++++++--- src/cmd/run.go | 2 +- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/cmd/create.go b/src/cmd/create.go index f10cf4350..6a37d0a81 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -21,6 +21,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "strings" "time" @@ -45,6 +46,7 @@ var ( authFile string container string distro string + hostname string image string release string } @@ -85,6 +87,12 @@ func init() { "", "Create a toolbox container for a different operating system distribution than the host") + flags.StringVarP(&createFlags.hostname, + "hostname", + "", + "", + "Set the container's hostname (defaults to 'toolbox')") + flags.StringVarP(&createFlags.image, "image", "i", @@ -113,6 +121,10 @@ func init() { } func create(cmd *cobra.Command, args []string) error { + // This regex filters out strings which are not valid hostnames, according to RFC-1123. + // Source: https://stackoverflow.com/a/106223 + var hostnameRegexp = regexp.MustCompile("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])$") + if utils.IsInsideContainer() { if !utils.IsInsideToolboxContainer() { return errors.New("this is not a toolbox container") @@ -155,8 +167,13 @@ func create(cmd *cobra.Command, args []string) error { } } + if cmd.Flag("hostname").Changed && !hostnameRegexp.MatchString(cmd.Flag("hostname").Value.String()) { + return errors.New("invalid hostname") + } + var container string var containerArg string + var hostname = cmd.Flag("hostname").Value.String() if len(args) != 0 { container = args[0] @@ -176,14 +193,14 @@ func create(cmd *cobra.Command, args []string) error { return err } - if err := createContainer(container, image, release, true); err != nil { + if err := createContainer(container, image, release, hostname, true); err != nil { return err } return nil } -func createContainer(container, image, release string, showCommandToEnter bool) error { +func createContainer(container, image, release string, hostname string, showCommandToEnter bool) error { if container == "" { panic("container not specified") } @@ -196,6 +213,10 @@ func createContainer(container, image, release string, showCommandToEnter bool) panic("release not specified") } + if hostname == "" { + hostname = "toolbox" + } + enterCommand := getEnterCommand(container) logrus.Debugf("Checking if container %s already exists", container) @@ -410,7 +431,7 @@ func createContainer(container, image, release string, showCommandToEnter bool) createArgs = append(createArgs, xdgRuntimeDirEnv...) createArgs = append(createArgs, []string{ - "--hostname", "toolbox", + "--hostname", hostname, "--ipc", "host", "--label", "com.github.containers.toolbox=true", }...) diff --git a/src/cmd/run.go b/src/cmd/run.go index 7db7d9ef7..f54042d00 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -206,7 +206,7 @@ func runCommand(container string, return nil } - if err := createContainer(container, image, release, false); err != nil { + if err := createContainer(container, image, release, "", false); err != nil { return err } } else if containersCount == 1 && defaultContainer { From bcc620593f3a505ad74d3e7bdc6a0fb7686853d7 Mon Sep 17 00:00:00 2001 From: Buckley Ross Date: Fri, 28 Jan 2022 12:49:46 -0500 Subject: [PATCH 2/3] test: add tests for `toolbox create --hostname ??` These tests ensure that the new `--hostname` flag works as intended. Signed-off-by: Buckley Ross Change-Id: Id7b7775fe73a6600afe2dabb0d905be5ba17bb57 --- test/system/101-create.bats | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/system/101-create.bats b/test/system/101-create.bats index 4393df67e..60a155c7e 100644 --- a/test/system/101-create.bats +++ b/test/system/101-create.bats @@ -96,6 +96,25 @@ teardown() { assert [ ${#lines[@]} -eq 3 ] } +@test "create: Create a container with a custom hostname ('test.host-name.local')" { + run $TOOLBOX -y create --hostname test.host-name.local + + assert_success + assert_output --partial "Enter with: toolbox enter" + + run $TOOLBOX -y run -- hostname + + assert_success + assert_output --partial "test.host-name.local" +} + +@test "create: Create a container with an invalid hostname ('test.host-name.local.')" { + run $TOOLBOX -y create --hostname test.host-name.local. + + assert_failure + assert_line --index 0 "Error: invalid hostname" +} + @test "create: Try to create a container based on non-existent image" { run $TOOLBOX -y create -i foo.org/bar From 200abab92d9b5cbc55be6abae989fea45da8e26b Mon Sep 17 00:00:00 2001 From: Buckley Ross Date: Fri, 28 Jan 2022 12:51:59 -0500 Subject: [PATCH 3/3] doc: cover new `toolbox create --hostname` flag Updated the `toolbox create` manual page to cover the usage of the new `--hostname` flag for specifying custom hostnames when creating new containers. Signed-off-by: Buckley Ross Change-Id: I16d7f63769c17aec1ff69d34ac8fe28f48827777 --- doc/toolbox-create.1.md | 6 ++++++ src/cmd/create.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/toolbox-create.1.md b/doc/toolbox-create.1.md index 83efbb72b..ff7e48b90 100644 --- a/doc/toolbox-create.1.md +++ b/doc/toolbox-create.1.md @@ -6,6 +6,7 @@ toolbox\-create - Create a new toolbox container ## SYNOPSIS **toolbox create** [*--authfile AUTHFILE*] [*--distro DISTRO* | *-d DISTRO*] + [*--hostname HOSTNAME*] [*--image NAME* | *-i NAME*] [*--release RELEASE* | *-r RELEASE*] [*CONTAINER*] @@ -95,6 +96,11 @@ Create a toolbox container for a different operating system DISTRO than the host. Cannot be used with `--image`. Has to be coupled with `--release` unless the selected DISTRO matches the host. +**--hostname** HOSTNAME + +Initializes the netowork hostname of the toolbox container to the specified value. +If not specified, this will default to **toolbox**. + **--image** NAME, **-i** NAME Change the NAME of the image used to create the toolbox container. This is diff --git a/src/cmd/create.go b/src/cmd/create.go index 6a37d0a81..3d330e236 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -123,7 +123,7 @@ func init() { func create(cmd *cobra.Command, args []string) error { // This regex filters out strings which are not valid hostnames, according to RFC-1123. // Source: https://stackoverflow.com/a/106223 - var hostnameRegexp = regexp.MustCompile("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])$") + var hostnameRegexp = regexp.MustCompile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])$`) if utils.IsInsideContainer() { if !utils.IsInsideToolboxContainer() {