Several small bugs and usability fixes

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2022-05-05 19:17:55 -04:00
parent cdb7cc5bdb
commit de87080987
5 changed files with 17 additions and 6 deletions

View file

@ -323,6 +323,9 @@ module Inspec
def pretty_handle_exception(exception) def pretty_handle_exception(exception)
case exception case exception
when Inspec::InvalidProfileSignature
$stderr.puts exception.message
Inspec::UI.new.exit(:bad_signature)
when Inspec::Error when Inspec::Error
$stderr.puts exception.message $stderr.puts exception.message
exit(1) exit(1)

View file

@ -23,5 +23,5 @@ module Inspec
attr_accessor :version attr_accessor :version
end end
class InvalidProfile < Error; end class InvalidProfileSignature < Error; end
end end

View file

@ -20,7 +20,7 @@ module Inspec
if iaf_file.valid? if iaf_file.valid?
IafProvider.new(path) IafProvider.new(path)
else else
raise Inspec::InvalidProfile, "Profile is invalid." raise Inspec::InvalidProfileSignature, "Profile signature is invalid."
end end
elsif File.exist?(path) elsif File.exist?(path)
DirProvider.new(path) DirProvider.new(path)
@ -239,7 +239,7 @@ module Inspec
f.close f.close
content = content.slice(490, content.length).lstrip content = content.slice(490, content.length).lstrip
else else
raise Inspec::InvalidProfile, "Profile is invalid." raise Inspec::InvalidProfileSignature, "Unrecognized IAF version."
end end
tmpfile = nil tmpfile = nil

View file

@ -56,15 +56,18 @@ module Inspec
false false
end end
attr_reader :key_name, :version
def initialize(path) def initialize(path)
@path = path @path = path
@key_name = nil
end end
def valid? def valid?
header = [] header = []
valid = true valid = true
f = File.open(@path, "rb") f = File.open(@path, "rb")
version = f.readline.strip! @version = f.readline.strip!
if version == INSPEC_PROFILE_VERSION_1 if version == INSPEC_PROFILE_VERSION_1
header << version header << version
header << f.readline.strip! header << f.readline.strip!
@ -96,6 +99,7 @@ module Inspec
valid = false valid = false
end end
@key_name = header[1]
validation_key_path = Inspec::IafFile.find_validation_key(header[1]) validation_key_path = Inspec::IafFile.find_validation_key(header[1])
unless valid_header?(header) unless valid_header?(header)

View file

@ -36,11 +36,11 @@ module InspecPlugins
FileUtils.mkdir_p(path) FileUtils.mkdir_p(path)
puts "Generating signing key in #{path}/#{options["keyname"]}.pem.key" puts "Generating signing key in #{path}/#{options["keyname"]}.pem.key"
open "#{options["keyname"]}.pem.key", "w" do |io| open "#{path}/#{options["keyname"]}.pem.key", "w" do |io|
io.write key.to_pem io.write key.to_pem
end end
puts "Generating validation key in #{path}/#{options["keyname"]}.pem.pub" puts "Generating validation key in #{path}/#{options["keyname"]}.pem.pub"
open "#{options["keyname"]}.pem.pub", "w" do |io| open "#{path}/#{options["keyname"]}.pem.pub", "w" do |io|
io.write key.public_key.to_pem io.write key.public_key.to_pem
end end
end end
@ -89,9 +89,13 @@ module InspecPlugins
iaf_file = Inspec::IafFile.new(file_to_verify) iaf_file = Inspec::IafFile.new(file_to_verify)
if iaf_file.valid? if iaf_file.valid?
puts "Detected format version '#{iaf_file.version}'"
puts "Attempting to verify using key '#{iaf_file.key_name}'"
puts "Profile is valid." puts "Profile is valid."
Inspec::UI.new.exit(:normal) Inspec::UI.new.exit(:normal)
else else
puts "Detected format version '#{iaf_file.version}'"
puts "Attempting to verify using key '#{iaf_file.key_name}'" if iaf_file.key_name
puts "Profile is invalid" puts "Profile is invalid"
Inspec::UI.new.exit(:bad_signature) Inspec::UI.new.exit(:bad_signature)
end end