Skip to content

Commit

Permalink
Change :shasum key to :sha256 for future upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Danna <steve@chef.io>
  • Loading branch information
stevendanna committed Sep 21, 2016
1 parent 6814d6a commit 7b0347f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions lib/fetchers/local.rb
Expand Up @@ -56,17 +56,17 @@ def archive_path
end

def cache_key
archive_shasum.to_s
sha256.to_s
end

def archive_shasum
def sha256
return nil if File.directory?(@target)
@archive_shasum ||= Digest::SHA256.hexdigest File.read(@target)
end

def resolved_source
h = { path: @target }
h[:shasum] = archive_shasum if archive_shasum
h[:sha256] = sha256 if sha256
h
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/fetchers/url.rb
Expand Up @@ -85,21 +85,21 @@ def fetch(path)
@archive_path ||= download_archive(path)
end

def shasum
content = if @archive_path
File.read(@archive_path)
else
remote_archive_content
end
Digest::SHA256.hexdigest content
def sha256
c = if @archive_path
File.read(@archive_path)
else
content
end
Digest::SHA256.hexdigest c
end

def resolved_source
@resolved_source ||= { url: @target, shasum: shasum }
@resolved_source ||= { url: @target, sha256: sha256 }
end

def cache_key
shasum
sha256
end

def to_s
Expand All @@ -116,7 +116,7 @@ def open_target
open(@target, http_opts)
end

def remote_archive_content
def content
open_target.read
end

Expand Down
2 changes: 1 addition & 1 deletion lib/inspec/fetcher.rb
Expand Up @@ -16,7 +16,7 @@ def resolve(target)
end
end

NON_FETCHER_KEYS = [:name, :version_constraint, :cwd, :backend, :cache, :shasum].freeze
NON_FETCHER_KEYS = [:name, :version_constraint, :cwd, :backend, :cache, :shasum256].freeze
def fetcher_specified?(target)
# Only set a default for Hash-based (i.e. from
# inspec.yml/inspec.lock) targets
Expand Down
10 changes: 5 additions & 5 deletions lib/inspec/profile.rb
Expand Up @@ -33,7 +33,7 @@ def self.resolve_target(target, cache = nil)
end

cache_key = if target.is_a?(Hash)
target[:shasum] || target[:ref] || fetcher.cache_key
target[:sha256] || target[:ref] || fetcher.cache_key
else
fetcher.cache_key
end
Expand All @@ -43,13 +43,13 @@ def self.resolve_target(target, cache = nil)
cache.prefered_entry_for(cache_key)
else
fetcher.fetch(cache.base_path_for(fetcher.cache_key))
if target.respond_to?(:key?) && target.key?(:shasum)
if fetcher.resolved_source[:shasum] != target[:shasum]
if target.respond_to?(:key?) && target.key?(:sha256)
if fetcher.resolved_source[:sha256] != target[:sha256]
fail <<EOF
The remote source #{fetcher} no longer has the requested content:
Request Content Hash: #{target[:shasum]}
Actual Content Hash: #{fetcher.resolved_source[:shasum]}
Request Content Hash: #{target[:sha256]}
Actual Content Hash: #{fetcher.resolved_source[:sha256]}
For URL, supermarket, compliance, and other sources that do not
provide versioned artifacts, this likely means that the remote source
Expand Down
10 changes: 5 additions & 5 deletions test/unit/fetchers/url_test.rb
Expand Up @@ -38,15 +38,15 @@ def initialize(target, opts)
res = fetcher.resolve(url)
res.expects(:open).returns(mock_open)
_(res).must_be_kind_of Fetchers::Url
_(res.resolved_source).must_equal({url: 'http://chef.io/some.tar.gz', shasum: expected_shasum})
_(res.resolved_source).must_equal({url: 'http://chef.io/some.tar.gz', sha256: expected_shasum})
end

it 'handles a https url' do
url = 'https://chef.io/some.tar.gz'
res = fetcher.resolve(url)
res.expects(:open).returns(mock_open)
_(res).must_be_kind_of Fetchers::Url
_(res.resolved_source).must_equal({url: 'https://chef.io/some.tar.gz', shasum: expected_shasum})
_(res.resolved_source).must_equal({url: 'https://chef.io/some.tar.gz', sha256: expected_shasum})

end

Expand All @@ -68,7 +68,7 @@ def initialize(target, opts)
res = fetcher.resolve(github)
res.expects(:open).returns(mock_open)
_(res).wont_be_nil
_(res.resolved_source).must_equal({url: 'https://github.com/chef/inspec/archive/master.tar.gz', shasum: expected_shasum})
_(res.resolved_source).must_equal({url: 'https://github.com/chef/inspec/archive/master.tar.gz', sha256: expected_shasum})
end
end

Expand All @@ -77,7 +77,7 @@ def initialize(target, opts)
res = fetcher.resolve(github)
res.expects(:open).returns(mock_open)
_(res).wont_be_nil
_(res.resolved_source).must_equal({url: 'https://github.com/hardening-io/tests-os-hardening/archive/2.0.tar.gz', shasum: expected_shasum})
_(res.resolved_source).must_equal({url: 'https://github.com/hardening-io/tests-os-hardening/archive/2.0.tar.gz', sha256: expected_shasum})
end

it "resolves a github commit url" do
Expand All @@ -86,7 +86,7 @@ def initialize(target, opts)
res.expects(:open).returns(mock_open)
_(res).wont_be_nil
_(res.resolved_source).must_equal({url: 'https://github.com/hardening-io/tests-os-hardening/archive/48bd4388ddffde68badd83aefa654e7af3231876.tar.gz',
shasum: expected_shasum})
sha256: expected_shasum})
end
end

Expand Down

0 comments on commit 7b0347f

Please sign in to comment.