Skip to content

Commit 00b20b4

Browse files
committed
Stat Identity file on sys con add
Fixes: #26016 Signed-off-by: Nicola Sella <[email protected]>
1 parent 277530e commit 00b20b4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

cmd/podman/system/connection/add.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"go.podman.io/common/pkg/completion"
1717
"go.podman.io/common/pkg/config"
1818
"go.podman.io/common/pkg/ssh"
19+
"go.podman.io/storage/pkg/fileutils"
1920
)
2021

2122
var (
@@ -141,6 +142,14 @@ func add(cmd *cobra.Command, args []string) error {
141142

142143
switch uri.Scheme {
143144
case "ssh":
145+
if cOpts.Identity != "" {
146+
if err := fileutils.Exists(cOpts.Identity); err != nil {
147+
if errors.Is(err, os.ErrNotExist) {
148+
return fmt.Errorf("identity file does not exist: %w", err)
149+
}
150+
return err
151+
}
152+
}
144153
return ssh.Create(entities, sshMode)
145154
case "unix":
146155
if cmd.Flags().Changed("identity") {

test/system/272-system-connection.bats

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@ $c2[ ]\+tcp://localhost:54321[ ]\+true[ ]\+true" \
8888
run_podman context rm $c1
8989
}
9090

91+
# Test system connection add bad identities with ssh/unix/tcp
92+
@test "podman system connection --identity" {
93+
run_podman system connection ls -q
94+
is "$output" ""
95+
96+
run_podman system connection add ssh-conn --identity ~/nonexistent ssh://localhost
97+
is "$output" \
98+
"Error: identity file does not exist: faccessat /home/nsella/nonexistent: no such file or directory"
99+
run_podman system connection add unix-conn --identity ~/identity unix://path
100+
is "$output"\
101+
"Error: --identity option not supported for unix scheme"
102+
run_podman system connection add tcp-conn --identity ~/identity tcp://path
103+
is "$output"\
104+
"Error: --identity option not supported for unix scheme"
105+
106+
run touch ~/badfile
107+
run chmod -r ~/badfile
108+
run_podman system connection add bad-conn --identity ~/badfile ssh://localhost
109+
is "$output" \
110+
"Error: failed to validate: failed to read identity*"
111+
run rm ~/badfile
112+
# Ensure no connections were added
113+
run_podman system connection ls -q
114+
is "$output" ""
115+
}
116+
91117
# Test tcp socket; requires starting a local server
92118
@test "podman system connection - tcp" {
93119
# Start server

0 commit comments

Comments
 (0)