Skip to content

Commit

Permalink
podman machine ssh handling
Browse files Browse the repository at this point in the history
add the key used in newly initialized machines to the user's known_hosts file. This ensures that golang will be able to ssh into the machine using
podman-remote. Also, remove the /dev/null redirection for podman machine ssh's known_hosts file.

resolves containers#15347

Signed-off-by: Charlie Doern <[email protected]>
Signed-off-by: cdoern <[email protected]>
  • Loading branch information
cdoern authored and cdoern committed Sep 26, 2022
1 parent 5fc6d95 commit 2e4e1bb
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 60 deletions.
2 changes: 1 addition & 1 deletion cmd/podman/machine/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func remoteConnectionUsername() (string, error) {
if err != nil {
return "", err
}
dest, _, err := cfg.ActiveDestination()
dest, _, _, err := cfg.ActiveDestination()
if err != nil {
return "", err
}
Expand Down
19 changes: 11 additions & 8 deletions cmd/podman/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func persistentPreRunE(cmd *cobra.Command, args []string) error {

setupConnection := func() error {
var err error
cfg.URI, cfg.Identity, err = cfg.ActiveDestination()
cfg.URI, cfg.Identity, cfg.MachineMode, err = cfg.ActiveDestination()
if err != nil {
return fmt.Errorf("failed to resolve active destination: %w", err)
}
Expand Down Expand Up @@ -368,10 +368,13 @@ func loggingHook() {

func rootFlags(cmd *cobra.Command, opts *entities.PodmanConfig) {
cfg := opts.Config
srv, uri, ident := resolveDestination()
srv, uri, ident, machine := resolveDestination()

lFlags := cmd.Flags()

// non configurable option to help ssh dialing
opts.MachineMode = machine

sshFlagName := "ssh"
lFlags.StringVar(&opts.SSHMode, sshFlagName, string(ssh.GolangMode), "define the ssh mode")
_ = cmd.RegisterFlagCompletionFunc(sshFlagName, common.AutocompleteSSH)
Expand Down Expand Up @@ -513,26 +516,26 @@ func rootFlags(cmd *cobra.Command, opts *entities.PodmanConfig) {
}
}

func resolveDestination() (string, string, string) {
func resolveDestination() (string, string, string, bool) {
if uri, found := os.LookupEnv("CONTAINER_HOST"); found {
var ident string
if v, found := os.LookupEnv("CONTAINER_SSHKEY"); found {
ident = v
}
return "", uri, ident
return "", uri, ident, false
}

cfg, err := config.ReadCustomConfig()
if err != nil {
logrus.Warning(fmt.Errorf("unable to read local containers.conf: %w", err))
return "", registry.DefaultAPIAddress(), ""
return "", registry.DefaultAPIAddress(), "", false
}

uri, ident, err := cfg.ActiveDestination()
uri, ident, machine, err := cfg.ActiveDestination()
if err != nil {
return "", registry.DefaultAPIAddress(), ""
return "", registry.DefaultAPIAddress(), "", false
}
return cfg.Engine.ActiveService, uri, ident
return cfg.Engine.ActiveService, uri, ident, machine
}

func formatError(err error) string {
Expand Down
5 changes: 3 additions & 2 deletions cmd/podman/system/connection/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ func inspect(cmd *cobra.Command, args []string) error {
r := namedDestination{
Name: k,
Destination: config.Destination{
Identity: v.Identity,
URI: v.URI,
Identity: v.Identity,
URI: v.URI,
IsMachine: v.IsMachine,
},
Default: def,
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/containernetworking/cni v1.1.2
github.com/containernetworking/plugins v1.1.1
github.com/containers/buildah v1.27.1-0.20220921131114-d3064796af36
github.com/containers/common v0.49.2-0.20220920205255-8062f81c5497
github.com/containers/common v0.49.2-0.20220926195839-590004b80685
github.com/containers/conmon v2.0.20+incompatible
github.com/containers/image/v5 v5.22.1-0.20220919112403-fe51f7ffca50
github.com/containers/ocicrypt v1.1.5
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,9 @@ github.com/containernetworking/plugins v1.1.1 h1:+AGfFigZ5TiQH00vhR8qPeSatj53eNG
github.com/containernetworking/plugins v1.1.1/go.mod h1:Sr5TH/eBsGLXK/h71HeLfX19sZPp3ry5uHSkI4LPxV8=
github.com/containers/buildah v1.27.1-0.20220921131114-d3064796af36 h1:LTSEbPUbs0slJSJ+IH6atAjYDe0IDzA0sPgBLjT1yAo=
github.com/containers/buildah v1.27.1-0.20220921131114-d3064796af36/go.mod h1:cY3pGPyMmrNp/sEDK8ESoBOf4hoNovptZSI0oyo8eQM=
github.com/containers/common v0.49.2-0.20220920205255-8062f81c5497 h1:LB9SxcAglqSAHiiHGacN1Abi0ZL9haJpQ1numVlqtxM=
github.com/containers/common v0.49.2-0.20220920205255-8062f81c5497/go.mod h1:ZnhOPR/07UOkfIg5bezUpBilGjxEUdaeoUpu7gRBGc0=
github.com/containers/common v0.49.2-0.20220926195839-590004b80685 h1:rutCgIKcew85mTUO0JTnh7XDXQfaTz/qQ3HyQHb0jZE=
github.com/containers/common v0.49.2-0.20220926195839-590004b80685/go.mod h1:ZnhOPR/07UOkfIg5bezUpBilGjxEUdaeoUpu7gRBGc0=
github.com/containers/conmon v2.0.20+incompatible h1:YbCVSFSCqFjjVwHTPINGdMX1F6JXHGTUje2ZYobNrkg=
github.com/containers/conmon v2.0.20+incompatible/go.mod h1:hgwZ2mtuDrppv78a/cOBNiCm6O0UMWGx1mu7P00nu5I=
github.com/containers/image/v5 v5.22.1-0.20220907162003-651744379993/go.mod h1:/Ruurd87C6Ap45t1PWNOD8+SGwiZbk79XCgs1iUTvYA=
Expand Down
13 changes: 7 additions & 6 deletions pkg/bindings/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func JoinURL(elements ...string) string {

// NewConnection creates a new service connection without an identity
func NewConnection(ctx context.Context, uri string) (context.Context, error) {
return NewConnectionWithIdentity(ctx, uri, "")
return NewConnectionWithIdentity(ctx, uri, "", false)
}

// NewConnectionWithIdentity takes a URI as a string and returns a context with the
Expand All @@ -70,7 +70,7 @@ func NewConnection(ctx context.Context, uri string) (context.Context, error) {
// For example tcp://localhost:<port>
// or unix:///run/podman/podman.sock
// or ssh://<user>@<host>[:port]/run/podman/podman.sock?secure=True
func NewConnectionWithIdentity(ctx context.Context, uri string, identity string) (context.Context, error) {
func NewConnectionWithIdentity(ctx context.Context, uri string, identity string, machine bool) (context.Context, error) {
var (
err error
)
Expand All @@ -96,10 +96,11 @@ func NewConnectionWithIdentity(ctx context.Context, uri string, identity string)
return nil, err
}
conn, err := ssh.Dial(&ssh.ConnectionDialOptions{
Host: uri,
Identity: identity,
User: _url.User,
Port: port,
Host: uri,
Identity: identity,
User: _url.User,
Port: port,
InsecureIsMachineConnection: machine,
}, "golang")
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions pkg/domain/entities/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ type PodmanConfig struct {
StorageDriver string
StorageOpts []string
SSHMode string
MachineMode bool
}
4 changes: 2 additions & 2 deletions pkg/domain/infra/runtime_abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewContainerEngine(facts *entities.PodmanConfig) (entities.ContainerEngine,
r, err := NewLibpodRuntime(facts.FlagSet, facts)
return r, err
case entities.TunnelMode:
ctx, err := bindings.NewConnectionWithIdentity(context.Background(), facts.URI, facts.Identity)
ctx, err := bindings.NewConnectionWithIdentity(context.Background(), facts.URI, facts.Identity, facts.MachineMode)
return &tunnel.ContainerEngine{ClientCtx: ctx}, err
}
return nil, fmt.Errorf("runtime mode '%v' is not supported", facts.EngineMode)
Expand All @@ -35,7 +35,7 @@ func NewImageEngine(facts *entities.PodmanConfig) (entities.ImageEngine, error)
return r, err
case entities.TunnelMode:
// TODO: look at me!
ctx, err := bindings.NewConnectionWithIdentity(context.Background(), facts.URI, facts.Identity)
ctx, err := bindings.NewConnectionWithIdentity(context.Background(), facts.URI, facts.Identity, facts.MachineMode)
return &tunnel.ImageEngine{ClientCtx: ctx}, err
}
return nil, fmt.Errorf("runtime mode '%v' is not supported", facts.EngineMode)
Expand Down
8 changes: 4 additions & 4 deletions pkg/domain/infra/runtime_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ var (
connection *context.Context
)

func newConnection(uri string, identity string) (context.Context, error) {
func newConnection(uri string, identity string, machine bool) (context.Context, error) {
connectionMutex.Lock()
defer connectionMutex.Unlock()

if connection == nil {
ctx, err := bindings.NewConnectionWithIdentity(context.Background(), uri, identity)
ctx, err := bindings.NewConnectionWithIdentity(context.Background(), uri, identity, machine)
if err != nil {
return ctx, err
}
Expand All @@ -37,7 +37,7 @@ func NewContainerEngine(facts *entities.PodmanConfig) (entities.ContainerEngine,
case entities.ABIMode:
return nil, fmt.Errorf("direct runtime not supported")
case entities.TunnelMode:
ctx, err := newConnection(facts.URI, facts.Identity)
ctx, err := newConnection(facts.URI, facts.Identity, facts.MachineMode)
return &tunnel.ContainerEngine{ClientCtx: ctx}, err
}
return nil, fmt.Errorf("runtime mode '%v' is not supported", facts.EngineMode)
Expand All @@ -49,7 +49,7 @@ func NewImageEngine(facts *entities.PodmanConfig) (entities.ImageEngine, error)
case entities.ABIMode:
return nil, fmt.Errorf("direct image runtime not supported")
case entities.TunnelMode:
ctx, err := newConnection(facts.URI, facts.Identity)
ctx, err := newConnection(facts.URI, facts.Identity, facts.MachineMode)
return &tunnel.ImageEngine{ClientCtx: ctx}, err
}
return nil, fmt.Errorf("runtime mode '%v' is not supported", facts.EngineMode)
Expand Down
3 changes: 2 additions & 1 deletion pkg/machine/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func AddConnection(uri fmt.Stringer, name, identity string, isDefault bool) erro
cfg.Engine.ActiveService = name
}
dst := config.Destination{
URI: uri.String(),
URI: uri.String(),
IsMachine: true,
}
dst.Identity = identity
if cfg.Engine.ServiceDestinations == nil {
Expand Down
6 changes: 0 additions & 6 deletions pkg/machine/e2e/basic_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package e2e_test

import (
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
Expand All @@ -24,10 +22,6 @@ var _ = Describe("run basic podman commands", func() {
It("Basic ops", func() {
// golangci-lint has trouble with actually skipping tests marked Skip
// so skip it on cirrus envs and where CIRRUS_CI isn't set.
if os.Getenv("CIRRUS_CI") != "false" {
Skip("FIXME: #15347 - ssh know hosts broken - fails on PR runs and on x86_64")
}

name := randomString()
i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run()
Expand Down
3 changes: 2 additions & 1 deletion pkg/machine/qemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ func (v *MachineVM) Init(opts machine.InitOptions) (bool, error) {
WritePath: v.getIgnitionFile(),
UID: v.UID,
}

err = machine.NewIgnitionFile(ign)
return err == nil, err
}
Expand Down Expand Up @@ -1033,7 +1034,7 @@ func (v *MachineVM) SSH(_ string, opts machine.SSHOptions) error {
sshDestination := username + "@localhost"
port := strconv.Itoa(v.Port)

args := []string{"-i", v.IdentityPath, "-p", port, sshDestination, "-o", "UserKnownHostsFile=/dev/null",
args := []string{"-i", v.IdentityPath, "-p", port, sshDestination,
"-o", "StrictHostKeyChecking=no", "-o", "LogLevel=ERROR", "-o", "SetEnv=LC_ALL="}
if len(opts.Args) > 0 {
args = append(args, opts.Args...)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions vendor/github.com/containers/common/pkg/config/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2e4e1bb

Please sign in to comment.