Skip to content

Commit

Permalink
Make SetAuthKeys action aware of previous keys
Browse files Browse the repository at this point in the history
Because SSHAuthorizedKeyring needs them at the time of write to
know if file should be written or not. Slightly related to
#320
  • Loading branch information
dgdavid committed Oct 28, 2021
1 parent f765f62 commit 9d948f5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/lib/y2users/linux/set_auth_keys_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,29 @@ class SetAuthKeysAction < Action
# Constructor
#
# @see Action
def initialize(user, commit_config = nil)
# @param user [User] user to perform the action
# @param commit_config [CommitConfig, nil] optional configuration for the commit
# @param previous_keys [Array<String>, nil] optional collection holding previous keys, if any
def initialize(user, commit_config = nil, previous_keys = nil)
textdomain "users"

super
super(user, commit_config)
@previous_keys = previous_keys || []
end

private

attr_accessor :previous_keys

alias_method :user, :action_element

# @see Action#run_action
#
# Issues are generated when the authorized keys cannot be set.
def run_action
Yast::Users::SSHAuthorizedKeyring.new(user.home, user.authorized_keys).write_keys
keyring = Yast::Users::SSHAuthorizedKeyring.new(user.home.path, previous_keys)
keyring.add_keys(user.authorized_keys)
keyring.write_keys
true
rescue Yast::Users::SSHAuthorizedKeyring::PathError => e
issues << Y2Issues::Issue.new(
Expand Down
8 changes: 5 additions & 3 deletions test/lib/y2users/linux/set_auth_keys_action_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@
it "calls SSHAuthorizedKeyring#write_keys" do
obj = double
expect(obj).to receive(:write_keys)
expect(Yast::Users::SSHAuthorizedKeyring).to receive(:new).with(user.home, ["test"])
expect(obj).to receive(:add_keys).with(["test"])
expect(Yast::Users::SSHAuthorizedKeyring).to receive(:new).with("/home/test", [])
.and_return(obj)

subject.perform
end

it "returns result without success and with issues if cmd failed" do
obj = double
expect(obj).to receive(:add_keys).with(["test"])
expect(obj).to receive(:write_keys)
.and_raise(Yast::Users::SSHAuthorizedKeyring::PathError, user.home)
expect(Yast::Users::SSHAuthorizedKeyring).to receive(:new).with(user.home, ["test"])
.and_raise(Yast::Users::SSHAuthorizedKeyring::PathError, "/home/test")
expect(Yast::Users::SSHAuthorizedKeyring).to receive(:new).with("/home/test", [])
.and_return(obj)

result = action.perform
Expand Down

0 comments on commit 9d948f5

Please sign in to comment.