Skip to content

Commit 5e0d793

Browse files
committed
Fix bugs in BinaryIO creator
Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent 3e7c6f6 commit 5e0d793

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

cio/io.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ package cio
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"io"
2324
"net/url"
2425
"os"
26+
"path/filepath"
27+
"strings"
2528
"sync"
2629

2730
"github.com/containerd/containerd/defaults"
@@ -242,13 +245,19 @@ func LogURI(uri *url.URL) Creator {
242245
// BinaryIO forwards container STDOUT|STDERR directly to a logging binary
243246
func BinaryIO(binary string, args map[string]string) Creator {
244247
return func(_ string) (IO, error) {
248+
binary = filepath.Clean(binary)
249+
if !strings.HasPrefix(binary, "/") {
250+
return nil, errors.New("absolute path needed")
251+
}
245252
uri := &url.URL{
246253
Scheme: "binary",
247-
Host: binary,
254+
Path: binary,
248255
}
256+
q := uri.Query()
249257
for k, v := range args {
250-
uri.Query().Set(k, v)
258+
q.Set(k, v)
251259
}
260+
uri.RawQuery = q.Encode()
252261
return &logURI{
253262
config: Config{
254263
Stdout: uri.String(),

cio/io_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"context"
2424
"io"
2525
"io/ioutil"
26+
"net/url"
2627
"os"
2728
"path/filepath"
2829
"runtime"
@@ -153,3 +154,26 @@ func initProducers(t *testing.T, producers producers, stdout, stderr string) {
153154
assert.NilError(t, err)
154155
assert.NilError(t, producers.Stderr.Close())
155156
}
157+
158+
func TestBinaryIOArgs(t *testing.T) {
159+
res, err := BinaryIO("/file.bin", map[string]string{"id": "1"})("")
160+
assert.NilError(t, err)
161+
assert.Equal(t, "binary:///file.bin?id=1", res.Config().Stdout)
162+
assert.Equal(t, "binary:///file.bin?id=1", res.Config().Stderr)
163+
}
164+
165+
func TestBinaryIOAbsolutePath(t *testing.T) {
166+
res, err := BinaryIO("/full/path/bin", nil)("!")
167+
assert.NilError(t, err)
168+
169+
// Test parse back
170+
parsed, err := url.Parse(res.Config().Stdout)
171+
assert.NilError(t, err)
172+
assert.Equal(t, "binary", parsed.Scheme)
173+
assert.Equal(t, "/full/path/bin", parsed.Path)
174+
}
175+
176+
func TestBinaryIOFailOnRelativePath(t *testing.T) {
177+
_, err := BinaryIO("./bin", nil)("!")
178+
assert.Error(t, err, "absolute path needed")
179+
}

runtime/v1/linux/proc/io.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func NewBinaryIO(ctx context.Context, id string, uri *url.URL) (runc.IO, error)
265265
}
266266
}
267267
ctx, cancel := context.WithCancel(ctx)
268-
cmd := exec.CommandContext(ctx, uri.Host, args...)
268+
cmd := exec.CommandContext(ctx, uri.Path, args...)
269269
cmd.Env = append(cmd.Env,
270270
"CONTAINER_ID="+id,
271271
"CONTAINER_NAMESPACE="+ns,

0 commit comments

Comments
 (0)