Skip to content

Commit 0748cfe

Browse files
committed
Continue work on TLS remote support
* Moved cert bundle read to separate package * Removed duplication for non-tcp tls flag check * Added tls info to `system connection list` * Removed TCP warning if TLS is enabled * Fixed not using TLS when using ABI instead of remote * Added central check for cert without key or vice-versa Signed-off-by: Andrew Melnick <[email protected]>
1 parent 0259e04 commit 0748cfe

File tree

8 files changed

+32
-32
lines changed

8 files changed

+32
-32
lines changed

cmd/podman/root.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,15 @@ func rootFlags(cmd *cobra.Command, podmanConfig *entities.PodmanConfig) {
518518
_ = cmd.RegisterFlagCompletionFunc(identityFlagName, completion.AutocompleteDefault)
519519

520520
tlsCertFileFlagName := "tls-cert"
521-
lFlags.StringVar(&podmanConfig.TLSCertFile, tlsCertFileFlagName, "", "path to TLS client certificate PEM file for remote, (CONTAINER_TLS_CERT)")
521+
lFlags.StringVar(&podmanConfig.TLSCertFile, tlsCertFileFlagName, podmanConfig.TLSCertFile, "path to TLS client certificate PEM file for remote, (CONTAINER_TLS_CERT)")
522522
_ = cmd.RegisterFlagCompletionFunc(tlsCertFileFlagName, completion.AutocompleteDefault)
523523

524524
tlsKeyFileFlagName := "tls-key"
525-
lFlags.StringVar(&podmanConfig.TLSKeyFile, tlsKeyFileFlagName, "", "path to TLS client certificate private key PEM file for remote, (CONTAINER_TLS_KEY)")
525+
lFlags.StringVar(&podmanConfig.TLSKeyFile, tlsKeyFileFlagName, podmanConfig.TLSKeyFile, "path to TLS client certificate private key PEM file for remote, (CONTAINER_TLS_KEY)")
526526
_ = cmd.RegisterFlagCompletionFunc(tlsKeyFileFlagName, completion.AutocompleteDefault)
527527

528528
tlsCAFileFlagName := "tls-ca"
529-
lFlags.StringVar(&podmanConfig.TLSCAFile, tlsCAFileFlagName, "", "path to TLS certificate Authority PEM file for remote, (CONTAINER_TLS_CA)")
529+
lFlags.StringVar(&podmanConfig.TLSCAFile, tlsCAFileFlagName, podmanConfig.TLSCAFile, "path to TLS certificate Authority PEM file for remote, (CONTAINER_TLS_CA)")
530530
_ = cmd.RegisterFlagCompletionFunc(tlsCAFileFlagName, completion.AutocompleteDefault)
531531

532532
// Flags that control or influence any kind of output.

cmd/podman/system/connection/add.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,32 +155,24 @@ func add(cmd *cobra.Command, args []string) error {
155155
return fmt.Errorf("invalid ssh mode")
156156
}
157157

158-
switch uri.Scheme {
159-
case "ssh":
158+
if uri.Scheme != "tcp" {
160159
if cmd.Flags().Changed("tls-cert") {
161-
return errors.New("--tls-cert option not supported for ssh scheme")
160+
return fmt.Errorf("--tls-cert option not supported for %s scheme", uri.Scheme)
162161
}
163162
if cmd.Flags().Changed("tls-key") {
164-
return errors.New("--tls-key option not supported for ssh scheme")
163+
return fmt.Errorf("--tls-key option not supported for %s scheme", uri.Scheme)
165164
}
166165
if cmd.Flags().Changed("tls-ca") {
167-
return errors.New("--tls-ca option not supported for ssh scheme")
166+
return fmt.Errorf("--tls-ca option not supported for %s scheme", uri.Scheme)
168167
}
168+
}
169+
switch uri.Scheme {
170+
case "ssh":
169171
return ssh.Create(entities, sshMode)
170172
case "unix":
171173
if cmd.Flags().Changed("identity") {
172174
return errors.New("--identity option not supported for unix scheme")
173175
}
174-
if cmd.Flags().Changed("tls-cert") {
175-
return errors.New("--tls-cert option not supported for unix scheme")
176-
}
177-
if cmd.Flags().Changed("tls-key") {
178-
return errors.New("--tls-key option not supported for unix scheme")
179-
}
180-
if cmd.Flags().Changed("tls-ca") {
181-
return errors.New("--tls-ca option not supported for unix scheme")
182-
}
183-
184176
if cmd.Flags().Changed("socket-path") {
185177
uri.Path = cmd.Flag("socket-path").Value.String()
186178
}

cmd/podman/system/connection/list.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,22 @@ func inspect(cmd *cobra.Command, args []string) error {
118118
rpt, err = rpt.Parse(report.OriginUser, format)
119119
} else {
120120
rpt, err = rpt.Parse(report.OriginPodman,
121-
"{{range .}}{{.Name}}\t{{.URI}}\t{{.Identity}}\t{{.Default}}\t{{.ReadWrite}}\n{{end -}}")
121+
"{{range .}}{{.Name}}\t{{.URI}}\t{{.Identity}}\t{{.TLSCAFile}}\t{{.TLSCertFile}}\t{{.TLSKeyFile}}\t{{.Default}}\t{{.ReadWrite}}\n{{end -}}")
122122
}
123123
if err != nil {
124124
return err
125125
}
126126

127127
if rpt.RenderHeaders {
128128
err = rpt.Execute([]map[string]string{{
129-
"Default": "Default",
130-
"Identity": "Identity",
131-
"Name": "Name",
132-
"URI": "URI",
133-
"ReadWrite": "ReadWrite",
129+
"Default": "Default",
130+
"Identity": "Identity",
131+
"TLSCAFile": "TLSCAFile",
132+
"TLSCertFile": "TLSCertFile",
133+
"TLSKeyFile": "TLSKeyFile",
134+
"Name": "Name",
135+
"URI": "URI",
136+
"ReadWrite": "ReadWrite",
134137
}})
135138
if err != nil {
136139
return err

cmd/podman/system/service_abi.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ func restService(flags *pflag.FlagSet, cfg *entities.PodmanConfig, opts entities
7676
}
7777
}
7878
case "tcp":
79-
// We want to check if the user is requesting a TCP address.
79+
// We want to check if the user is requesting a TCP address if TLS is not active.
8080
// If so, warn that this is insecure.
8181
// Ignore errors here, the actual backend code will handle them
8282
// better than we can here.
83-
logrus.Warnf("Using the Podman API service with TCP sockets is not recommended, please see `podman system service` manpage for details")
83+
if opts.TLSKeyFile == "" || opts.TLSCertFile == "" {
84+
logrus.Warnf("Using the Podman API service with TCP sockets without TLS is not recommended, please see `podman system service` manpage for details")
85+
}
8486

8587
host := uri.Host
8688
if host == "" {

internal/domain/infra/runtime_abi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func NewTestingEngine(facts *entities.PodmanConfig) (ientities.TestingEngine, er
1919
r, err := NewLibpodTestingRuntime(facts.FlagSet, facts)
2020
return r, err
2121
case entities.TunnelMode:
22-
ctx, err := bindings.NewConnectionWithIdentity(context.Background(), facts.URI, facts.Identity, facts.MachineMode)
22+
ctx, err := bindings.NewConnectionWithIdentityOrTLS(context.Background(), facts.URI, facts.Identity, facts.TLSCertFile, facts.TLSKeyFile, facts.TLSCAFile, facts.MachineMode)
2323
return &tunnel.TestingEngine{ClientCtx: ctx}, err
2424
}
2525
return nil, fmt.Errorf("runtime mode '%v' is not supported", facts.EngineMode)

pkg/api/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/containers/podman/v5/pkg/api/server/idle"
2424
"github.com/containers/podman/v5/pkg/api/types"
2525
"github.com/containers/podman/v5/pkg/domain/entities"
26-
"github.com/containers/podman/v5/pkg/util"
26+
"github.com/containers/podman/v5/pkg/util/tlsutil"
2727
"github.com/coreos/go-systemd/v22/daemon"
2828
"github.com/gorilla/mux"
2929
"github.com/gorilla/schema"
@@ -108,7 +108,7 @@ func newServer(runtime *libpod.Runtime, listener net.Listener, opts entities.Ser
108108

109109
if opts.TLSClientCAFile != "" {
110110
logrus.Debugf("will validate client certs against %s", opts.TLSClientCAFile)
111-
pool, err := util.ReadCertBundle(opts.TLSClientCAFile)
111+
pool, err := tlsutil.ReadCertBundle(opts.TLSClientCAFile)
112112
if err != nil {
113113
return nil, err
114114
}

pkg/bindings/connection.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
"github.com/blang/semver/v4"
2121
"github.com/containers/common/pkg/ssh"
22-
"github.com/containers/podman/v5/pkg/util"
22+
"github.com/containers/podman/v5/pkg/util/tlsutil"
2323
"github.com/containers/podman/v5/version"
2424
"github.com/kevinburke/ssh_config"
2525
"github.com/sirupsen/logrus"
@@ -326,12 +326,15 @@ func tcpClient(_url *url.URL, tlsCertFile, tlsKeyFile, tlsCAFile string) (Connec
326326
connection.tls = true
327327
}
328328
if len(tlsCAFile) != 0 {
329-
pool, err := util.ReadCertBundle(tlsCAFile)
329+
pool, err := tlsutil.ReadCertBundle(tlsCAFile)
330330
if err != nil {
331331
return connection, fmt.Errorf("unable to read CA bundle: %w", err)
332332
}
333333
transport.TLSClientConfig.RootCAs = pool
334334
}
335+
if (len(tlsCertFile) == 0) != (len(tlsKeyFile) == 0) {
336+
return connection, fmt.Errorf("TLS Key and Certificate must both or neither be provided")
337+
}
335338
if len(tlsCertFile) != 0 && len(tlsKeyFile) != 0 {
336339
keyPair, err := tls.LoadX509KeyPair(tlsCertFile, tlsKeyFile)
337340
if err != nil {

pkg/util/tls.go renamed to pkg/util/tlsutil/tls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package tlsutil
22

33
import (
44
"crypto/x509"

0 commit comments

Comments
 (0)