Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |