Skip to content

Commit 4c869b7

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

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-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 (
@@ -168,6 +169,14 @@ func add(cmd *cobra.Command, args []string) error {
168169
}
169170
switch uri.Scheme {
170171
case "ssh":
172+
if cOpts.Identity != "" {
173+
if err := fileutils.Exists(cOpts.Identity); err != nil {
174+
if errors.Is(err, os.ErrNotExist) {
175+
return fmt.Errorf("identity file does not exist: %w", err)
176+
}
177+
return err
178+
}
179+
}
171180
return ssh.Create(entities, sshMode)
172181
case "unix":
173182
if cmd.Flags().Changed("identity") {

test/system/272-system-connection.bats

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,31 @@ $c2[ ]\+tcp://localhost:54321[ ]\+true[ ]\+true" \
9494
run_podman context rm $c1
9595
}
9696

97+
# Test system connection add bad identities with ssh/unix/tcp
98+
@test "podman system connection --identity" {
99+
run_podman system connection ls -q
100+
assert "$output" == "" ""
101+
102+
run_podman 125 system connection add ssh-conn --identity $PODMAN_TMPDIR/nonexistent ssh://localhost
103+
assert "$output" =~ \
104+
"Error: failed to validate: failed to read identity *" ""
105+
run_podman 125 system connection add unix-conn --identity $PODMAN_TMPDIR/identity unix://path
106+
assert "$output" == \
107+
"Error: --identity option not supported for unix scheme" ""
108+
run_podman 125 system connection add tcp-conn --identity $PODMAN_TEMPDIR/identity tcp://path
109+
assert "$output" =~ \
110+
"Error: --identity option not supported for tcp scheme" ""
111+
112+
run touch $PODMAN_TEMPDIR/badfile
113+
run chmod -r $PODMAN_TEMPDIR/badfile
114+
run_podman 125 system connection add bad-conn --identity $PODMAN_TEMPDIR/badfile ssh://localhost
115+
assert "$output" =~ \
116+
"Error: failed to validate: failed to read identity*" ""
117+
# Ensure no connections were added
118+
run_podman system connection ls -q
119+
assert "$output" == "" ""
120+
}
121+
97122
# Test tcp socket; requires starting a local server
98123
@test "podman system connection - tcp" {
99124
unset REMOTESYSTEM_TRANSPORT REMOTESYSTEM_TLS_{CLIENT,SERVER,CA}_{CRT,KEY}

0 commit comments

Comments
 (0)