Skip to content

Commit b187dfe

Browse files
Merge pull request #11390 from giuseppe/logging-passthrough
logging: new mode -l passthrough
2 parents d987f26 + 3ce98a5 commit b187dfe

File tree

13 files changed

+73
-23
lines changed

13 files changed

+73
-23
lines changed

cmd/podman/common/completion.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,10 +771,13 @@ func AutocompleteImageVolume(cmd *cobra.Command, args []string, toComplete strin
771771
}
772772

773773
// AutocompleteLogDriver - Autocomplete log-driver options.
774-
// -> "journald", "none", "k8s-file"
774+
// -> "journald", "none", "k8s-file", "passthrough"
775775
func AutocompleteLogDriver(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
776776
// don't show json-file
777777
logDrivers := []string{define.JournaldLogging, define.NoLogging, define.KubernetesLogging}
778+
if !registry.IsRemote() {
779+
logDrivers = append(logDrivers, define.PassthroughLogging)
780+
}
778781
return logDrivers, cobra.ShellCompDirectiveNoFileComp
779782
}
780783

cmd/podman/containers/create.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/containers/podman/v3/pkg/specgen"
2020
"github.com/containers/podman/v3/pkg/specgenutil"
2121
"github.com/containers/podman/v3/pkg/util"
22+
"github.com/mattn/go-isatty"
2223
"github.com/pkg/errors"
2324
"github.com/spf13/cobra"
2425
)
@@ -161,7 +162,9 @@ func create(cmd *cobra.Command, args []string) error {
161162
}
162163
}
163164

164-
fmt.Println(report.Id)
165+
if cliVals.LogDriver != define.PassthroughLogging {
166+
fmt.Println(report.Id)
167+
}
165168
return nil
166169
}
167170

@@ -188,6 +191,14 @@ func CreateInit(c *cobra.Command, vals entities.ContainerCreateOptions, isInfra
188191
vals.UserNS = "private"
189192
}
190193
}
194+
if cliVals.LogDriver == define.PassthroughLogging {
195+
if isatty.IsTerminal(0) || isatty.IsTerminal(1) || isatty.IsTerminal(2) {
196+
return vals, errors.New("the '--log-driver passthrough' option cannot be used on a TTY")
197+
}
198+
if registry.IsRemote() {
199+
return vals, errors.New("the '--log-driver passthrough' option is not supported in remote mode")
200+
}
201+
}
191202

192203
if !isInfra {
193204
if c.Flag("shm-size").Changed {

cmd/podman/containers/run.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,13 @@ func run(cmd *cobra.Command, args []string) error {
158158
runOpts.InputStream = nil
159159
}
160160

161+
passthrough := cliVals.LogDriver == define.PassthroughLogging
162+
161163
// If attach is set, clear stdin/stdout/stderr and only attach requested
162164
if cmd.Flag("attach").Changed {
165+
if passthrough {
166+
return errors.Wrapf(define.ErrInvalidArg, "cannot specify --attach with --log-driver=passthrough")
167+
}
163168
runOpts.OutputStream = nil
164169
runOpts.ErrorStream = nil
165170
if !cliVals.Interactive {
@@ -179,6 +184,7 @@ func run(cmd *cobra.Command, args []string) error {
179184
}
180185
}
181186
}
187+
182188
cliVals.PreserveFDs = runOpts.PreserveFDs
183189
s := specgen.NewSpecGenerator(imageName, cliVals.RootFS)
184190
if err := specgenutil.FillOutSpecGen(s, &cliVals, args); err != nil {
@@ -200,7 +206,7 @@ func run(cmd *cobra.Command, args []string) error {
200206
return err
201207
}
202208

203-
if runOpts.Detach {
209+
if runOpts.Detach && !passthrough {
204210
fmt.Println(report.Id)
205211
return nil
206212
}

docs/source/markdown/podman-create.1.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,11 @@ Not implemented
513513

514514
#### **--log-driver**="*k8s-file*"
515515

516-
Logging driver for the container. Currently available options are *k8s-file*, *journald*, and *none*, with *json-file* aliased to *k8s-file* for scripting compatibility.
516+
Logging driver for the container. Currently available options are *k8s-file*, *journald*, *none* and *passthrough*, with *json-file* aliased to *k8s-file* for scripting compatibility.
517+
518+
The *passthrough* driver passes down the standard streams (stdin, stdout, stderr) to the
519+
container. It is not allowed with the remote Podman client and on a tty, since it is
520+
vulnerable to attacks via TIOCSTI.
517521

518522
#### **--log-opt**=*name*=*value*
519523

docs/source/markdown/podman-run.1.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,12 @@ Not implemented.
538538

539539
#### **--log-driver**="*driver*"
540540

541-
Logging driver for the container. Currently available options are **k8s-file**, **journald**, and **none**, with **json-file** aliased to **k8s-file** for scripting compatibility.
541+
Logging driver for the container. Currently available options are **k8s-file**, **journald**, **none** and **passthrough**, with **json-file** aliased to **k8s-file** for scripting compatibility.
542+
543+
The **passthrough** driver passes down the standard streams (stdin, stdout, stderr) to the
544+
container. It is not allowed with the remote Podman client and on a tty, since it is
545+
vulnerable to attacks via TIOCSTI.
546+
542547

543548
#### **--log-opt**=*name*=*value*
544549

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ require (
4141
github.com/hpcloud/tail v1.0.0
4242
github.com/json-iterator/go v1.1.12
4343
github.com/mattn/go-colorable v0.1.8 // indirect
44+
github.com/mattn/go-isatty v0.0.12
4445
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
4546
github.com/mrunalp/fileutils v0.5.0
4647
github.com/onsi/ginkgo v1.16.4

libpod/container_api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ func (c *Container) Kill(signal uint) error {
229229
// This function returns when the attach finishes. It does not hold the lock for
230230
// the duration of its runtime, only using it at the beginning to verify state.
231231
func (c *Container) Attach(streams *define.AttachStreams, keys string, resize <-chan define.TerminalSize) error {
232+
switch c.LogDriver() {
233+
case define.PassthroughLogging:
234+
return errors.Wrapf(define.ErrNoLogs, "this container is using the 'passthrough' log driver, cannot attach")
235+
}
232236
if !c.batched {
233237
c.lock.Lock()
234238
if err := c.syncContainer(); err != nil {

libpod/container_log.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
var logDrivers []string
1919

2020
func init() {
21-
logDrivers = append(logDrivers, define.KubernetesLogging, define.NoLogging)
21+
logDrivers = append(logDrivers, define.KubernetesLogging, define.NoLogging, define.PassthroughLogging)
2222
}
2323

2424
// Log is a runtime function that can read one or more container logs.
@@ -34,6 +34,8 @@ func (r *Runtime) Log(ctx context.Context, containers []*Container, options *log
3434
// ReadLog reads a containers log based on the input options and returns log lines over a channel.
3535
func (c *Container) ReadLog(ctx context.Context, options *logs.LogOptions, logChannel chan *logs.LogLine) error {
3636
switch c.LogDriver() {
37+
case define.PassthroughLogging:
38+
return errors.Wrapf(define.ErrNoLogs, "this container is using the 'passthrough' log driver, cannot read logs")
3739
case define.NoLogging:
3840
return errors.Wrapf(define.ErrNoLogs, "this container is using the 'none' log driver, cannot read logs")
3941
case define.JournaldLogging:

libpod/define/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ const JSONLogging = "json-file"
7878
// NoLogging is the string conmon expects when specifying to use no log driver whatsoever
7979
const NoLogging = "none"
8080

81+
// PassthroughLogging is the string conmon expects when specifying to use the passthrough driver
82+
const PassthroughLogging = "passthrough"
83+
8184
// Strings used for --sdnotify option to podman
8285
const (
8386
SdNotifyModeContainer = "container"

libpod/oci_attach_linux.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ func openUnixSocket(path string) (*net.UnixConn, error) {
4040
// Does not check if state is appropriate
4141
// started is only required if startContainer is true
4242
func (c *Container) attach(streams *define.AttachStreams, keys string, resize <-chan define.TerminalSize, startContainer bool, started chan bool, attachRdy chan<- bool) error {
43-
if !streams.AttachOutput && !streams.AttachError && !streams.AttachInput {
43+
passthrough := c.LogDriver() == define.PassthroughLogging
44+
45+
if !streams.AttachOutput && !streams.AttachError && !streams.AttachInput && !passthrough {
4446
return errors.Wrapf(define.ErrInvalidArg, "must provide at least one stream to attach to")
4547
}
4648
if startContainer && started == nil {
@@ -52,24 +54,27 @@ func (c *Container) attach(streams *define.AttachStreams, keys string, resize <-
5254
return err
5355
}
5456

55-
logrus.Debugf("Attaching to container %s", c.ID())
57+
var conn *net.UnixConn
58+
if !passthrough {
59+
logrus.Debugf("Attaching to container %s", c.ID())
5660

57-
registerResizeFunc(resize, c.bundlePath())
61+
registerResizeFunc(resize, c.bundlePath())
5862

59-
attachSock, err := c.AttachSocketPath()
60-
if err != nil {
61-
return err
62-
}
63+
attachSock, err := c.AttachSocketPath()
64+
if err != nil {
65+
return err
66+
}
6367

64-
conn, err := openUnixSocket(attachSock)
65-
if err != nil {
66-
return errors.Wrapf(err, "failed to connect to container's attach socket: %v", attachSock)
67-
}
68-
defer func() {
69-
if err := conn.Close(); err != nil {
70-
logrus.Errorf("Unable to close socket: %q", err)
68+
conn, err = openUnixSocket(attachSock)
69+
if err != nil {
70+
return errors.Wrapf(err, "failed to connect to container's attach socket: %v", attachSock)
7171
}
72-
}()
72+
defer func() {
73+
if err := conn.Close(); err != nil {
74+
logrus.Errorf("unable to close socket: %q", err)
75+
}
76+
}()
77+
}
7378

7479
// If starting was requested, start the container and notify when that's
7580
// done.
@@ -80,6 +85,10 @@ func (c *Container) attach(streams *define.AttachStreams, keys string, resize <-
8085
started <- true
8186
}
8287

88+
if passthrough {
89+
return nil
90+
}
91+
8392
receiveStdoutError, stdinDone := setupStdioChannels(streams, conn, detachKeys)
8493
if attachRdy != nil {
8594
attachRdy <- true

0 commit comments

Comments
 (0)