sftp icon indicating copy to clipboard operation
sftp copied to clipboard

Fix bug #283: Multiple keys are added to authorized_keys without line breaks

Open blaisemGH opened this issue 1 year ago • 1 comments

Bug

While ssh-keygen creates public keys that end in a newline (which is used in current tests), sometimes this newline is lost, e.g., when the key is mounted by a configmap in k8s. In this scenario, the current implementation breaks the authorized_keys file, making sftp with subsequent public keys impossible.

Before

Multiple keys for a single host are merged into authorized_keys via cat and append, e.g., cat $keyFile >> authorized_keys. When a key does not have a trailing newline, the append concatenates the next key on the same line as the previous key, and the keys can no longer be parsed.

After

Using paste -d "\\n" -s inserts a newline between public keys if and only if the key is missing a trailing newline.

Example (pseudo code)

Before:

cat $keyFile1 >> authorized_keys.tmp
cat $keyFile2 >> authorized_keys.tmp
cat authorized_keys.tmp
<key1><key2>

After:

paste -d "\\n" -s "$keyDir"/* > authorized_keys.tmp
cat authorized_keys.tmp
<key1>
<key2>

blaisemGH avatar Nov 29 '24 17:11 blaisemGH

Fix for issue #283. I didn't find the issue until after implementation, so there is already PR #289. I guess my PR adds a unit test. Would be fine with either one being merged.

blaisemGH avatar Nov 29 '24 17:11 blaisemGH