Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

system service --log-level=trace: support hijack #17781

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkg/api/server/handler_logging.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package server

import (
"bufio"
"errors"
"io"
"net"
"net/http"
"time"

Expand Down Expand Up @@ -33,6 +36,28 @@ func (l responseWriter) Write(b []byte) (int, error) {
return l.ResponseWriter.Write(b)
}

func (l responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if wrapped, ok := l.ResponseWriter.(http.Hijacker); ok {
return wrapped.Hijack()
}

return nil, nil, errors.New("ResponseWriter does not support hijacking")
}

func (l responseWriter) Header() http.Header {
return l.ResponseWriter.Header()
}

func (l responseWriter) WriteHeader(statusCode int) {
l.ResponseWriter.WriteHeader(statusCode)
}

func (l responseWriter) Flush() {
if wrapped, ok := l.ResponseWriter.(http.Flusher); ok {
wrapped.Flush()
}
}

func loggingHandler() mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
19 changes: 19 additions & 0 deletions test/system/251-system-service.bats
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,22 @@ function teardown() {

systemctl stop $SERVICE_NAME
}

# Regression test for https://github.com/containers/podman/issues/17749
@test "podman-system-service --log-level=trace should be able to hijack" {
skip_if_remote "podman system service unavailable over remote"

port=$(random_free_port)
URL=tcp://127.0.0.1:$port

systemd-run --unit=$SERVICE_NAME $PODMAN --log-level=trace system service $URL --time=0
wait_for_port 127.0.0.1 $port

out=o-$(random_string)
cname=c-$(random_string)
run_podman --url $URL run --name $cname $IMAGE echo $out
assert "$output" == "$out" "service is able to hijack and stream output back"

run_podman --url $URL rm $cname
systemctl stop $SERVICE_NAME
}