Skip to content

Commit ad9fd69

Browse files
authored
[runner] Don't bind to public addresses (#3575)
Closes: #3078
1 parent 5132bb9 commit ad9fd69

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

runner/cmd/runner/main.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func main() {
3131

3232
func mainInner() int {
3333
var tempDir string
34+
var httpAddress string
3435
var httpPort int
3536
var sshPort int
3637
var sshAuthorizedKeys []string
@@ -61,6 +62,13 @@ func mainInner() int {
6162
Destination: &tempDir,
6263
TakesFile: true,
6364
},
65+
&cli.StringFlag{
66+
Name: "http-address",
67+
Usage: "Set a http bind address",
68+
Value: "",
69+
DefaultText: "all interfaces",
70+
Destination: &httpAddress,
71+
},
6472
&cli.IntFlag{
6573
Name: "http-port",
6674
Usage: "Set a http port",
@@ -86,7 +94,7 @@ func mainInner() int {
8694
},
8795
},
8896
Action: func(ctx context.Context, cmd *cli.Command) error {
89-
return start(ctx, tempDir, httpPort, sshPort, sshAuthorizedKeys, logLevel, Version)
97+
return start(ctx, logLevel, tempDir, httpAddress, httpPort, sshPort, sshAuthorizedKeys)
9098
},
9199
},
92100
},
@@ -103,7 +111,12 @@ func mainInner() int {
103111
return 0
104112
}
105113

106-
func start(ctx context.Context, tempDir string, httpPort int, sshPort int, sshAuthorizedKeys []string, logLevel int, version string) error {
114+
func start(
115+
ctx context.Context,
116+
logLevel int, tempDir string,
117+
httpAddress string, httpPort int,
118+
sshPort int, sshAuthorizedKeys []string,
119+
) error {
107120
if err := os.MkdirAll(tempDir, 0o755); err != nil {
108121
return fmt.Errorf("create temp directory: %w", err)
109122
}
@@ -191,7 +204,7 @@ func start(ctx context.Context, tempDir string, httpPort int, sshPort int, sshAu
191204
return fmt.Errorf("create executor: %w", err)
192205
}
193206

194-
server, err := api.NewServer(ctx, fmt.Sprintf(":%d", httpPort), version, ex)
207+
server, err := api.NewServer(ctx, fmt.Sprintf("%s:%d", httpAddress, httpPort), Version, ex)
195208
if err != nil {
196209
return fmt.Errorf("create server: %w", err)
197210
}

runner/internal/shim/docker.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,6 @@ func (d *DockerRunner) createContainer(ctx context.Context, task *Task) error {
806806
}
807807
mounts = append(mounts, instanceMounts...)
808808

809-
ports := d.dockerParams.DockerPorts()
810-
811809
// Set the environment variables
812810
envVars := []string{}
813811
if d.dockerParams.DockerPJRTDevice() != "" {
@@ -827,9 +825,19 @@ func (d *DockerRunner) createContainer(ctx context.Context, task *Task) error {
827825
}
828826
}
829827

828+
networkMode := getNetworkMode(task.config.NetworkMode)
829+
ports := d.dockerParams.DockerPorts()
830+
831+
// Bridge mode - all interfaces
832+
runnerHttpAddress := ""
833+
if networkMode.IsHost() {
834+
runnerHttpAddress = "localhost"
835+
}
836+
shellCommands := d.dockerParams.DockerShellCommands(task.config.ContainerSshKeys, runnerHttpAddress)
837+
830838
containerConfig := &container.Config{
831839
Image: task.config.ImageName,
832-
Cmd: []string{strings.Join(d.dockerParams.DockerShellCommands(task.config.ContainerSshKeys), " && ")},
840+
Cmd: []string{strings.Join(shellCommands, " && ")},
833841
Entrypoint: []string{"/bin/sh", "-c"},
834842
ExposedPorts: exposePorts(ports),
835843
Env: envVars,
@@ -843,7 +851,7 @@ func (d *DockerRunner) createContainer(ctx context.Context, task *Task) error {
843851
}
844852
hostConfig := &container.HostConfig{
845853
Privileged: task.config.Privileged || d.dockerParams.DockerPrivileged(),
846-
NetworkMode: getNetworkMode(task.config.NetworkMode),
854+
NetworkMode: networkMode,
847855
PortBindings: bindPorts(ports),
848856
Mounts: mounts,
849857
ShmSize: task.config.ShmSize,
@@ -1182,7 +1190,7 @@ func (c *CLIArgs) DockerPJRTDevice() string {
11821190
return c.Docker.PJRTDevice
11831191
}
11841192

1185-
func (c *CLIArgs) DockerShellCommands(publicKeys []string) []string {
1193+
func (c *CLIArgs) DockerShellCommands(authorizedKeys []string, runnerHttpAddress string) []string {
11861194
commands := getSSHShellCommands()
11871195
runnerCommand := []string{
11881196
consts.RunnerBinaryPath,
@@ -1192,7 +1200,10 @@ func (c *CLIArgs) DockerShellCommands(publicKeys []string) []string {
11921200
"--http-port", strconv.Itoa(c.Runner.HTTPPort),
11931201
"--ssh-port", strconv.Itoa(c.Runner.SSHPort),
11941202
}
1195-
for _, key := range publicKeys {
1203+
if runnerHttpAddress != "" {
1204+
runnerCommand = append(runnerCommand, "--http-address", runnerHttpAddress)
1205+
}
1206+
for _, key := range authorizedKeys {
11961207
runnerCommand = append(runnerCommand, "--ssh-authorized-key", fmt.Sprintf("'%s'", key))
11971208
}
11981209
return append(commands, strings.Join(runnerCommand, " "))

runner/internal/shim/docker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (c *dockerParametersMock) DockerPJRTDevice() string {
110110
return ""
111111
}
112112

113-
func (c *dockerParametersMock) DockerShellCommands(publicKeys []string) []string {
113+
func (c *dockerParametersMock) DockerShellCommands(authorizedKeys []string, runnerHttpAddress string) []string {
114114
commands := make([]string, 0)
115115
if c.sshShellCommands {
116116
commands = append(commands, getSSHShellCommands()...)

runner/internal/shim/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
type DockerParameters interface {
88
DockerPrivileged() bool
9-
DockerShellCommands([]string) []string
9+
DockerShellCommands(authorizedKeys []string, runnerHttpAddress string) []string
1010
DockerMounts(string) ([]mount.Mount, error)
1111
DockerPorts() []int
1212
MakeRunnerDir(name string) (string, error)

0 commit comments

Comments
 (0)