Skip to content

Commit 111b082

Browse files
authored
Merge pull request #3356 from mxpv/binary-io-path
BinaryIO/LogFile creator bug fixing
2 parents 7ac57b6 + fbf96d3 commit 111b082

3 files changed

Lines changed: 67 additions & 10 deletions

File tree

cio/io.go

Lines changed: 22 additions & 7 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,17 +245,24 @@ 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()
261+
res := uri.String()
252262
return &logURI{
253263
config: Config{
254-
Stdout: uri.String(),
255-
Stderr: uri.String(),
264+
Stdout: res,
265+
Stderr: res,
256266
},
257267
}, nil
258268
}
@@ -262,14 +272,19 @@ func BinaryIO(binary string, args map[string]string) Creator {
262272
// If the log file already exists, the logs will be appended to the file.
263273
func LogFile(path string) Creator {
264274
return func(_ string) (IO, error) {
275+
path = filepath.Clean(path)
276+
if !strings.HasPrefix(path, "/") {
277+
return nil, errors.New("absolute path needed")
278+
}
265279
uri := &url.URL{
266280
Scheme: "file",
267-
Host: path,
281+
Path: path,
268282
}
283+
res := uri.String()
269284
return &logURI{
270285
config: Config{
271-
Stdout: uri.String(),
272-
Stderr: uri.String(),
286+
Stdout: res,
287+
Stderr: res,
273288
},
274289
}, nil
275290
}

cio/io_test.go

Lines changed: 42 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,44 @@ 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+
}
180+
181+
func TestLogFileAbsolutePath(t *testing.T) {
182+
res, err := LogFile("/full/path/file.txt")("!")
183+
assert.NilError(t, err)
184+
assert.Equal(t, "file:///full/path/file.txt", res.Config().Stdout)
185+
assert.Equal(t, "file:///full/path/file.txt", res.Config().Stderr)
186+
187+
// Test parse back
188+
parsed, err := url.Parse(res.Config().Stdout)
189+
assert.NilError(t, err)
190+
assert.Equal(t, "file", parsed.Scheme)
191+
assert.Equal(t, "/full/path/file.txt", parsed.Path)
192+
}
193+
194+
func TestLogFileFailOnRelativePath(t *testing.T) {
195+
_, err := LogFile("./file.txt")("!")
196+
assert.Error(t, err, "absolute path needed")
197+
}

runtime/v1/linux/proc/io.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ func createIO(ctx context.Context, id string, ioUID, ioGID int, stdio proc.Stdio
103103
case "binary":
104104
pio.io, err = NewBinaryIO(ctx, id, u)
105105
case "file":
106-
if err := os.MkdirAll(filepath.Dir(u.Host), 0755); err != nil {
106+
if err := os.MkdirAll(filepath.Dir(u.Path), 0755); err != nil {
107107
return nil, err
108108
}
109109
var f *os.File
110-
f, err = os.OpenFile(u.Host, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
110+
f, err = os.OpenFile(u.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
111111
if err != nil {
112112
return nil, err
113113
}
@@ -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)