Skip to content

Commit

Permalink
Use the new PublicKeysList widget
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Oct 30, 2018
1 parent 7bb9c7f commit 86b13df
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
43 changes: 25 additions & 18 deletions src/lib/users/widgets/public_key_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

require "cwm"
require "users/widgets/disk_selector"
require "users/widgets/public_keys_list"
require "users/ssh_public_key"
require "yast2/popup"
require "tmpdir"
Expand Down Expand Up @@ -56,7 +57,7 @@ def contents
HStretch(),
PushButton(Id(:browse), "Browse..."),
),
ReplacePoint(Id(:fingerprint), Empty())
public_keys_list
)
end

Expand All @@ -65,15 +66,30 @@ def contents
# @see CWM::AbstractWdiget
def handle(event)
read_key if event["ID"] == :browse

nil
end

# @see CWM::AbstractWdiget
def store
Yast::SSHAuthorizedKeys.import_keys("/root", [value.to_s]) if value
Yast::SSHAuthorizedKeys.import_keys("/root", keys.map(&:to_s)) unless keys.empty?
nil
end

# Forces this widget to listen to all events
#
# @return [Boolean]
def handle_all_events
true
end

# Returns the list of added keys
#
# @return [Array<SSHPublicKey>] List of public keys
def keys
public_keys_list.keys
end

private

attr_writer :value
Expand Down Expand Up @@ -108,9 +124,7 @@ def read_key
Yast::Path.new(".target.mount"), [disk_selector.value, dir], "-o ro"
)
if mounted
new_key = read_key_from(dir)
self.value = new_key if new_key
refresh_fingerprint
read_key_from(dir)
else
report_mount_error(disk_selector.value)
end
Expand All @@ -120,23 +134,15 @@ def read_key
end
end

# Reads a the key from the given directory
# Reads a key from the given directory
#
# @note Asks the user to select a file and tries to read it.
def read_key_from(dir)
path = Yast::UI.AskForExistingFile(dir, "*", _("Select a public key"))
SSHPublicKey.new(File.read(path)) if path && File.exist?(path)
end

# Refreshes the fingerprint which is shown in the UI.
def refresh_fingerprint
content =
if value
Left(Label(value.fingerprint))
else
Empty()
end
Yast::UI::ReplaceWidget(Id(:fingerprint), content)
return unless path && File.exist?(path)
public_keys_list.add(SSHPublicKey.new(File.read(path)))
rescue SSHPublicKey::InvalidKey
report_invalid_key
end

# Displays an error about the device which failed to be mounted
Expand All @@ -147,6 +153,7 @@ def report_mount_error(device)
Yast2::Popup.show(message, headline: :error)
end

# Displays an error about an invalid SSH key
def report_invalid_key
Yast2::Popup.show(_("A valid key was not found"), headline: :error)
end
Expand Down
1 change: 0 additions & 1 deletion src/lib/users/widgets/public_keys_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def change_items(keys)
# @see CWM::AbstractWdiget
def handle(event)
remove_key(event) if event["ID"].to_s.start_with?("remove_")

nil
end

Expand Down
26 changes: 10 additions & 16 deletions test/lib/users/widgets/public_key_selector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
describe "#handle" do
let(:tmpdir) { TESTS_PATH.join("tmp") }
let(:event) { { "ID" => :browse } }
let(:value) { "/dev/sr0" }
let(:disk_selector) { instance_double(Y2Users::Widgets::DiskSelector, value: value) }
let(:disk) { "/dev/sr0" }
let(:disk_selector) { instance_double(Y2Users::Widgets::DiskSelector, value: disk) }
let(:mounted?) { true }

before do
Expand All @@ -48,11 +48,6 @@
FileUtils.mkdir(tmpdir)
end

# around do |example|
# example.run
# FileUtils.rm_r(tmpdir) if tmpdir.exist?
# end

context "when the user selects a key" do
let(:key_path) { FIXTURES_PATH.join("id_rsa.pub") }
let(:key_content) { File.read(key_path).strip }
Expand All @@ -64,7 +59,7 @@

it "reads the key" do
widget.handle(event)
expect(widget.value.to_s).to eq(key_content)
expect(widget.keys.map(&:to_s)).to eq([key_content])
end
end

Expand All @@ -73,7 +68,7 @@

it "does not import any value" do
widget.handle(event)
expect(widget.value).to be_nil
expect(widget.keys).to eq([])
end
end

Expand All @@ -89,24 +84,23 @@

describe "#store" do
before do
allow(widget).to receive(:value).and_return(value)
allow(widget).to receive(:keys).and_return(keys)
end

context "when a key was read" do
let(:value) do
Y2Users::SSHPublicKey.new(File.read(FIXTURES_PATH.join("id_rsa.pub")))
end
let(:key) { Y2Users::SSHPublicKey.new(File.read(FIXTURES_PATH.join("id_rsa.pub"))) }
let(:keys) { [key] }

it "imports the key" do
expect(Yast::SSHAuthorizedKeys).to receive(:import_keys).with("/root", [value.to_s])
expect(Yast::SSHAuthorizedKeys).to receive(:import_keys).with("/root", [key.to_s])
widget.store
end
end

context "when no key was read" do
let(:value) { nil }
let(:keys) { [] }

it "does not try to import the key" do
it "does not try to import any key" do
expect(Yast::SSHAuthorizedKeys).to_not receive(:import_keys)
widget.store
end
Expand Down

0 comments on commit 86b13df

Please sign in to comment.