Skip to content

Commit

Permalink
Add an SSHPublicKey class
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Oct 30, 2018
1 parent a2d707a commit dfc98a0
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package/yast2-users.spec
Expand Up @@ -38,6 +38,7 @@ BuildRequires: yast2-perl-bindings
BuildRequires: yast2-security
BuildRequires: yast2-testsuite
BuildRequires: rubygem(%rb_default_ruby_abi:rspec)
BuildRequires: openssh

Requires: cracklib
Requires: perl-Digest-SHA1
Expand All @@ -63,6 +64,7 @@ Requires: yast2 >= 3.2.8
Requires: yast2-core >= 2.21.0

Requires: yast2-ruby-bindings >= 1.0.0
Requires: openssh

Summary: YaST2 - User and Group Configuration
License: GPL-2.0-only
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Expand Up @@ -70,6 +70,7 @@ ylib_DATA = \
lib/users/encryption_method.rb \
lib/users/leaf_blk_device.rb \
lib/users/proposal.rb \
lib/users/ssh_public_key.rb \
lib/users/encryption_proposal.rb \
lib/users/ssh_authorized_keys_file.rb \
lib/users/ssh_authorized_keyring.rb \
Expand Down
69 changes: 69 additions & 0 deletions src/lib/users/ssh_public_key.rb
@@ -0,0 +1,69 @@
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast2/execute"

module Y2Users
# This class is a simplified representation of a OpenSSH public key.
#
# @example Read a public key
# key = Y2Users::SSHPublicKey.new(File.read("id_rsa.pub"))
# key.fingerprint # => "SHA256:uadPyDQj9VlFZVjK8UNp57jOnWwzGgKQJpeJEhZyV0I"
class SSHPublicKey
# Not a valid SSH public key
class InvalidKey < StandardError; end

# @return [String] Key fingerprint
attr_reader :fingerprint

# Constructor
#
# @param raw [String] Public key content
#
# @raise InvalidKey
def initialize(raw)
@fingerprint = fingerprint_from(raw)
@raw = raw.strip
end

# Returns the key comment
#
# @return [String] Comment field
def comment
@comment ||= @raw.split(" ").last
end

private

# Gets the fingerprint for the given OpenSSH public key
#
# @return [String] Key fingerprint
# @raise InvalidKey
def fingerprint_from(raw)
output = Yast::Execute.locally!(
["echo", raw], ["ssh-keygen", "-l", "-f", "/dev/stdin"], stdout: :capture
)
output.split(" ")[1].to_s
rescue Cheetah::ExecutionFailed
raise InvalidKey
end
end
end
1 change: 1 addition & 0 deletions test/Makefile.am
Expand Up @@ -5,6 +5,7 @@ TESTS = \
lib/users/ssh_authorized_keys_file_test.rb \
lib/users/users_database_test.rb \
lib/users/leaf_blk_device_test.rb \
lib/users/ssh_public_key_test.rb \
lib/users/widgets/disk_selector_test.rb \
lib/users/widgets/public_key_selector_test.rb \
dialogs_test.rb \
Expand Down
51 changes: 51 additions & 0 deletions test/lib/users/ssh_public_key_test.rb
@@ -0,0 +1,51 @@
#!/usr/bin/env rspec
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../test_helper"
require "users/ssh_public_key"

describe Y2Users::SSHPublicKey do
subject(:key) { described_class.new(File.read(path)) }
let(:path) { FIXTURES_PATH.join("id_rsa.pub") }

describe ".new" do
context "when the key is not valid" do
let(:content) { "some-not-valid-key" }

it "raises an InvalidKey error" do
expect { described_class.new(content) }.to raise_error(Y2Users::SSHPublicKey::InvalidKey)
end
end
end

describe "#fingerprint" do
it "returns the key fingerprint" do
expect(subject.fingerprint).to eq("SHA256:uadPyDQj9VlFZVjK8UNp57jOnWwzGgKQJpeJEhZyV0I")
end
end

describe "#comment" do
it "returns the key comment" do
expect(key.comment).to eq("dummy1@example.net")
end
end
end

0 comments on commit dfc98a0

Please sign in to comment.