Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sha256 checksum to json #1796

Merged
merged 1 commit into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/inspec/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ module Inspec
# Extract metadata.rb information
class Metadata # rubocop:disable Metrics/ClassLength
attr_reader :ref
attr_accessor :params
attr_accessor :params, :content
def initialize(ref, logger = nil)
@ref = ref
@logger = logger || Logger.new(nil)
@content = ''
@params = {}
@missing_methods = []
end
Expand Down Expand Up @@ -206,26 +207,28 @@ def self.finalize(metadata, profile_id, options, logger = nil)
metadata
end

def self.from_yaml(ref, contents, profile_id, logger = nil)
def self.from_yaml(ref, content, profile_id, logger = nil)
res = Metadata.new(ref, logger)
res.params = YAML.load(contents)
res.params = YAML.load(content)
res.content = content
finalize(res, profile_id, {}, logger)
end

def self.from_ruby(ref, contents, profile_id, logger = nil)
def self.from_ruby(ref, content, profile_id, logger = nil)
res = Metadata.new(ref, logger)
res.instance_eval(contents, ref, 1)
res.instance_eval(content, ref, 1)
res.content = content
finalize(res, profile_id, {}, logger)
end

def self.from_ref(ref, contents, profile_id, logger = nil)
def self.from_ref(ref, content, profile_id, logger = nil)
# NOTE there doesn't have to exist an actual file, it may come from an
# archive (i.e., contents)
# archive (i.e., content)
case File.basename(ref)
when 'inspec.yml'
from_yaml(ref, contents, profile_id, logger)
from_yaml(ref, content, profile_id, logger)
when 'metadata.rb'
from_ruby(ref, contents, profile_id, logger)
from_ruby(ref, content, profile_id, logger)
else
logger ||= Logger.new(nil)
logger.error "Don't know how to handle metadata in #{ref}"
Expand Down
22 changes: 22 additions & 0 deletions lib/inspec/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# author: Christoph Hartmann

require 'forwardable'
require 'digest'
require 'inspec/polyfill'
require 'inspec/cached_fetcher'
require 'inspec/file_provider'
Expand Down Expand Up @@ -208,6 +209,7 @@ def info(res = params.dup)

# add information about the required attributes
res[:attributes] = res[:attributes].map(&:to_hash) unless res[:attributes].nil? || res[:attributes].empty?
res[:sha256] = sha256
res
end

Expand Down Expand Up @@ -395,6 +397,26 @@ def load_dependencies
Inspec::DependencySet.from_lockfile(lockfile, cwd, @cache, @backend, { attributes: @attr_values })
end

# Calculate this profile's SHA256 checksum. Includes metadata, dependencies,
# libraries, data files, and controls.
#
# @return [Type] description of returned object
def sha256
# get all dependency checksums
deps = Hash[locked_dependencies.list.map { |k, v| [k, v.profile.sha256] }]

res = Digest::SHA256.new
files = source_reader.tests.to_a + source_reader.libraries.to_a +
source_reader.data_files.to_a +
[['inspec.yml', source_reader.metadata.content]] +
[['inspec.lock.deps', YAML.dump(deps)]]

files.sort { |a, b| a[0] <=> b[0] }
.map { |f| res << f[0] << "\0" << f[1] << "\0" }

res.hexdigest
end

private

# Create an archive name for this profile and an additional options
Expand Down
1 change: 1 addition & 0 deletions lib/inspec/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Schema # rubocop:disable Metrics/ClassLength
'properties' => {
'name' => { 'type' => 'string' },
'version' => { 'type' => 'string', 'optional' => true },
'sha256' => { 'type' => 'string', 'optional' => false },

'title' => { 'type' => 'string', 'optional' => true },
'maintainer' => { 'type' => 'string', 'optional' => true },
Expand Down
1 change: 1 addition & 0 deletions test/functional/inspec_exec_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"license" => "Apache 2 license",
"summary" => "Demonstrates the use of InSpec Compliance Profile",
"version" => "1.0.0",
"sha256" => "dc106310cc44b877b6ebf4fae786fb6d0b88bc2660e36b360e8ef28cae8fbfc5",
"supports" => [{"os-family" => "unix"}],
"attributes" => []
})
Expand Down
7 changes: 7 additions & 0 deletions test/unit/profiles/metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def supports_meta(params)
describe 'running on ubuntu 14.04' do
let (:backend) { MockLoader.new(:ubuntu1404).backend }

it 'provides all metadata content' do
s = "---\nname: hello #{rand}"
res = Inspec::Metadata.from_yaml('mock', s, nil)
Inspec::Metadata.finalize(res, 'mock', empty_options)
res.content.must_equal(s)
end

it 'finalizes a loaded metadata via Profile ID' do
res = Inspec::Metadata.from_yaml('mock', '---', nil)
Inspec::Metadata.finalize(res, 'mock', empty_options)
Expand Down
10 changes: 10 additions & 0 deletions test/unit/profiles/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
end
end

describe 'SHA256 sums' do
it 'works on an empty profile' do
MockLoader.load_profile('empty-metadata').sha256.must_equal 'ee95f4cf4258402604d4cc581a672bbd2f73d212b09cd4bcf1c5984e97e68963'
end

it 'works on a complete profile' do
MockLoader.load_profile('complete-profile').sha256.must_equal '41ce15e61b7e02aad5dade183f68c547e062f1390762d266c5c8cf96d49134c7'
end
end

describe 'when checking' do
describe 'an empty profile' do
let(:profile_id) { 'empty-metadata' }
Expand Down