Skip to content

Commit

Permalink
Continue work on TLS API Support
Browse files Browse the repository at this point in the history
* Fixed attach endpoint client using unencrypted socket when TLS is enabled
* Duplicated libpod remote unix socket e2e suite for plaintext TCP, TLS,
  and mTLS

Signed-off-by: Andrew Melnick <[email protected]>
  • Loading branch information
meln5674 committed Dec 30, 2024
1 parent 0748cfe commit 46db840
Show file tree
Hide file tree
Showing 12 changed files with 440 additions and 67 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,10 @@ ginkgo:

.PHONY: ginkgo-remote
ginkgo-remote:
$(MAKE) ginkgo-run TAGS="$(REMOTETAGS) remote_testing"
$(MAKE) ginkgo-run TAGS="$(REMOTETAGS) remote_testing remote_unix_testing"
$(MAKE) ginkgo-run TAGS="$(REMOTETAGS) remote_testing remote_tcp_testing"
$(MAKE) ginkgo-run TAGS="$(REMOTETAGS) remote_testing remote_tls_testing"
$(MAKE) ginkgo-run TAGS="$(REMOTETAGS) remote_testing remote_mtls_testing"

.PHONY: testbindings
# bindings tests need access to podman-registry
Expand Down
47 changes: 47 additions & 0 deletions pkg/bindings/containers/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containers
import (
"bytes"
"context"
"crypto/tls"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -115,9 +116,11 @@ func Attach(ctx context.Context, nameOrID string, stdin io.Reader, stdout io.Wri
headers.Add("Connection", "Upgrade")
headers.Add("Upgrade", "tcp")

// FIXME: This is one giant race condition. Let's hope no-one uses this same client until we're done!
var socket net.Conn
socketSet := false
dialContext := conn.Client.Transport.(*http.Transport).DialContext
tlsConfig := conn.Client.Transport.(*http.Transport).TLSClientConfig
t := &http.Transport{
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
c, err := dialContext(ctx, network, address)
Expand All @@ -130,7 +133,28 @@ func Attach(ctx context.Context, nameOrID string, stdin io.Reader, stdout io.Wri
}
return c, err
},
DialTLSContext: func(ctx context.Context, network, address string) (net.Conn, error) {
c, err := dialContext(ctx, network, address)
if err != nil {
return nil, err
}
cfg := tlsConfig.Clone()
if cfg.ServerName == "" {
var firstTLSHost string
if firstTLSHost, _, err = net.SplitHostPort(address); err != nil {
return nil, err
}
cfg.ServerName = firstTLSHost
}
c = tls.Client(c, cfg)
if !socketSet {
socket = c
socketSet = true
}
return c, err
},
IdleConnTimeout: time.Duration(0),
TLSClientConfig: tlsConfig,
}
conn.Client.Transport = t
response, err := conn.DoRequest(ctx, nil, http.MethodPost, "/containers/%s/attach", params, headers, nameOrID)
Expand Down Expand Up @@ -463,9 +487,11 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, options *ExecStar
return err
}

// FIXME: This is one giant race condition. Let's hope no-one uses this same client until we're done!
var socket net.Conn
socketSet := false
dialContext := conn.Client.Transport.(*http.Transport).DialContext
tlsConfig := conn.Client.Transport.(*http.Transport).TLSClientConfig
t := &http.Transport{
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
c, err := dialContext(ctx, network, address)
Expand All @@ -478,7 +504,28 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, options *ExecStar
}
return c, err
},
DialTLSContext: func(ctx context.Context, network, address string) (net.Conn, error) {
c, err := dialContext(ctx, network, address)
if err != nil {
return nil, err
}
cfg := tlsConfig.Clone()
if cfg.ServerName == "" {
var firstTLSHost string
if firstTLSHost, _, err = net.SplitHostPort(address); err != nil {
return nil, err
}
cfg.ServerName = firstTLSHost
}
c = tls.Client(c, cfg)
if !socketSet {
socket = c
socketSet = true
}
return c, err
},
IdleConnTimeout: time.Duration(0),
TLSClientConfig: tlsConfig,
}
conn.Client.Transport = t
response, err := conn.DoRequest(ctx, bytes.NewReader(bodyJSON), http.MethodPost, "/exec/%s/start", nil, nil, sessionID)
Expand Down
Loading

0 comments on commit 46db840

Please sign in to comment.