Skip to content

Commit

Permalink
Require a key attribute for the key_rsa resource (#2891)
Browse files Browse the repository at this point in the history
Bug Fix #2865
* Defining an attribute without a default value generates a stacktrace
* Fix string quotes
* Moved logic out of the initilize method.
* Refactoring for better clarity.
* Fixing trailing white spaces
Signed-off-by: Omar J Irizarry <[email protected]>
  • Loading branch information
omar-irizarry authored and jquick committed Apr 12, 2018
1 parent 0c3bec2 commit a278ae9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/resources/key_rsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'openssl'
require 'hashie/mash'
require 'utils/file_reader'
require 'utils/pkey_reader'

module Inspec::Resources
class RsaKey < Inspec.resource(1)
Expand All @@ -22,11 +23,12 @@ class RsaKey < Inspec.resource(1)
"

include FileReader
include PkeyReader

def initialize(keypath, passphrase = nil)
@key_path = keypath
@passphrase = passphrase
@key = OpenSSL::PKey.read(read_file_content(@key_path, allow_empty: true), @passphrase)
@key = read_pkey(read_file_content(@key_path, allow_empty: true), @passphrase)
end

def public?
Expand Down
15 changes: 15 additions & 0 deletions lib/utils/pkey_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module PkeyReader
def read_pkey(filecontent, passphrase)
raise_if_default(passphrase)

OpenSSL::PKey.read(filecontent, passphrase)
rescue OpenSSL::PKey::PKeyError
raise Inspec::Exceptions::ResourceFailed, 'passphrase error'
end

def raise_if_default(passphrase)
if passphrase.is_a? Inspec::Attribute::DEFAULT_ATTRIBUTE
raise Inspec::Exceptions::ResourceFailed, 'Please provide default value for attribute'
end
end
end

0 comments on commit a278ae9

Please sign in to comment.