Skip to content

Commit

Permalink
Revert "users: Retain authorized_keys file permissions"
Browse files Browse the repository at this point in the history
With fixed fsreplace1 we don't need to call `sed` any more.
cockpit.file.modify() already reads the tag and passes it to fsreplace1,
so no further changes are necessary.

This partially reverts commit a369480 (but of
course keeps the integration test). Adjust the test as our file.modify()
doesn't append an extra empty line.
  • Loading branch information
martinpitt committed Mar 25, 2024
1 parent a88690a commit b09a5e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions pkg/users/authorized-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,24 @@ function AuthorizedKeys (user_name, home_dir) {
});
};

// don't use cockpit.file.modify() here, as that doesn't preserve permissions
// (https://github.com/cockpit-project/cockpit/issues/18033)
self.remove_key = key => cockpit.spawn(
["sed", "-i", "\\!^" + key + "$!d", filename],
{ superuser: "try", err: "message" }
);
self.remove_key = function(key) {
return file.modify(function(content) {
let lines = null;
const new_lines = [];

if (!content)
return "";

lines = content.trim().split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i] === key)
key = undefined;
else
new_lines.push(lines[i]);
}
return new_lines.join("\n");
});
};

self.close = function() {
if (watch)
Expand Down
2 changes: 1 addition & 1 deletion test/verify/check-shell-keys
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class TestKeys(testlib.MachineCase):
b.wait_not_in_text("#account-authorized-keys", "Invalid key")
b.wait_js_func("ph_count_check", "#account-authorized-keys-list tr", 1)
data = m.execute("cat /home/user/.ssh/authorized_keys")
self.assertEqual(data, KEY + "\n\n")
self.assertEqual(data, KEY + "\n")
# Permissions are still ok
check_perms()
b.logout()
Expand Down

0 comments on commit b09a5e3

Please sign in to comment.