2016-02-21 03:00:52 +00:00
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
|
|
|
|
require 'uri'
|
2017-06-03 00:14:55 +00:00
|
|
|
require 'openssl'
|
2016-02-21 03:00:52 +00:00
|
|
|
require 'tempfile'
|
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
module Fetchers
|
2017-12-07 19:22:55 +00:00
|
|
|
class Url < Inspec.fetcher(1)
|
2016-09-08 09:11:44 +00:00
|
|
|
MIME_TYPES = {
|
|
|
|
'application/x-zip-compressed' => '.zip',
|
|
|
|
'application/zip' => '.zip',
|
|
|
|
'application/x-gzip' => '.tar.gz',
|
|
|
|
'application/gzip' => '.tar.gz',
|
|
|
|
}.freeze
|
|
|
|
|
2016-02-21 03:00:52 +00:00
|
|
|
name 'url'
|
|
|
|
priority 200
|
|
|
|
|
|
|
|
def self.resolve(target, opts = {})
|
2016-09-08 09:11:44 +00:00
|
|
|
if target.is_a?(Hash) && target.key?(:url)
|
2018-08-30 16:57:50 +00:00
|
|
|
resolve_from_string(target[:url], opts, target[:username], target[:password])
|
2016-09-08 09:11:44 +00:00
|
|
|
elsif target.is_a?(String)
|
|
|
|
resolve_from_string(target, opts)
|
2018-12-12 18:05:51 +00:00
|
|
|
elsif target.is_a?(URI)
|
|
|
|
resolve_from_string(target.to_s, opts)
|
2016-09-08 09:11:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-30 16:57:50 +00:00
|
|
|
def self.resolve_from_string(target, opts, username = nil, password = nil)
|
2016-02-21 03:00:52 +00:00
|
|
|
uri = URI.parse(target)
|
|
|
|
return nil if uri.nil? or uri.scheme.nil?
|
|
|
|
return nil unless %{ http https }.include? uri.scheme
|
|
|
|
target = transform(target)
|
2018-08-30 16:57:50 +00:00
|
|
|
opts[:username] = username if username
|
|
|
|
opts[:password] = password if password
|
2016-09-08 09:11:44 +00:00
|
|
|
new(target, opts)
|
|
|
|
rescue URI::Error
|
2016-02-21 03:00:52 +00:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2017-06-05 14:02:56 +00:00
|
|
|
# Transforms a browser github/bitbucket url to github/bitbucket tar url
|
|
|
|
# We distinguish between three different Github/Bitbucket URL types:
|
2016-02-21 03:00:52 +00:00
|
|
|
# - Master URL
|
|
|
|
# - Branch URL
|
|
|
|
# - Commit URL
|
|
|
|
#
|
|
|
|
# master url:
|
|
|
|
# https://github.com/nathenharvey/tmp_compliance_profile/ is transformed to
|
|
|
|
# https://github.com/nathenharvey/tmp_compliance_profile/archive/master.tar.gz
|
2017-06-05 14:02:56 +00:00
|
|
|
# https://bitbucket.org/username/repo is transformed to
|
|
|
|
# https://bitbucket.org/username/repo/get/master.tar.gz
|
2016-02-21 03:00:52 +00:00
|
|
|
#
|
2017-06-05 14:02:56 +00:00
|
|
|
# branch:
|
2016-02-21 03:00:52 +00:00
|
|
|
# https://github.com/hardening-io/tests-os-hardening/tree/2.0 is transformed to
|
|
|
|
# https://github.com/hardening-io/tests-os-hardening/archive/2.0.tar.gz
|
2017-06-05 14:02:56 +00:00
|
|
|
# https://bitbucket.org/username/repo/branch/branchname is transformed to
|
|
|
|
# https://bitbucket.org/username/repo/get/newbranch.tar.gz
|
2016-02-21 03:00:52 +00:00
|
|
|
#
|
2017-06-05 14:02:56 +00:00
|
|
|
# commit:
|
2016-02-21 03:00:52 +00:00
|
|
|
# https://github.com/hardening-io/tests-os-hardening/tree/48bd4388ddffde68badd83aefa654e7af3231876
|
|
|
|
# is transformed to
|
|
|
|
# https://github.com/hardening-io/tests-os-hardening/archive/48bd4388ddffde68badd83aefa654e7af3231876.tar.gz
|
2017-06-05 14:02:56 +00:00
|
|
|
# https://bitbucket.org/username/repo/commits/95ce1f83d5bbe9eec34c5973f6894617e8d6d8cc is transformed to
|
|
|
|
# https://bitbucket.org/username/repo/get/95ce1f83d5bbe9eec34c5973f6894617e8d6d8cc.tar.gz
|
|
|
|
|
2016-09-08 09:11:44 +00:00
|
|
|
GITHUB_URL_REGEX = %r{^https?://(www\.)?github\.com/(?<user>[\w-]+)/(?<repo>[\w-]+)(\.git)?(/)?$}
|
|
|
|
GITHUB_URL_WITH_TREE_REGEX = %r{^https?://(www\.)?github\.com/(?<user>[\w-]+)/(?<repo>[\w-]+)/tree/(?<commit>[\w\.]+)(/)?$}
|
2017-06-05 14:02:56 +00:00
|
|
|
BITBUCKET_URL_REGEX = %r{^https?://(www\.)?bitbucket\.org/(?<user>[\w-]+)/(?<repo>[\w-]+)(\.git)?(/)?$}
|
|
|
|
BITBUCKET_URL_BRANCH_REGEX = %r{^https?://(www\.)?bitbucket\.org/(?<user>[\w-]+)/(?<repo>[\w-]+)/branch/(?<branch>[\w\.]+)(/)?$}
|
|
|
|
BITBUCKET_URL_COMMIT_REGEX = %r{^https?://(www\.)?bitbucket\.org/(?<user>[\w-]+)/(?<repo>[\w-]+)/commits/(?<commit>[\w\.]+)(/)?$}
|
|
|
|
|
2016-02-21 03:00:52 +00:00
|
|
|
def self.transform(target)
|
2016-09-08 09:11:44 +00:00
|
|
|
transformed_target = if m = GITHUB_URL_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition
|
|
|
|
"https://github.com/#{m[:user]}/#{m[:repo]}/archive/master.tar.gz"
|
|
|
|
elsif m = GITHUB_URL_WITH_TREE_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition
|
|
|
|
"https://github.com/#{m[:user]}/#{m[:repo]}/archive/#{m[:commit]}.tar.gz"
|
2017-06-05 14:02:56 +00:00
|
|
|
elsif m = BITBUCKET_URL_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition
|
|
|
|
"https://bitbucket.org/#{m[:user]}/#{m[:repo]}/get/master.tar.gz"
|
|
|
|
elsif m = BITBUCKET_URL_BRANCH_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition
|
|
|
|
"https://bitbucket.org/#{m[:user]}/#{m[:repo]}/get/#{m[:branch]}.tar.gz"
|
|
|
|
elsif m = BITBUCKET_URL_COMMIT_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition
|
|
|
|
"https://bitbucket.org/#{m[:user]}/#{m[:repo]}/get/#{m[:commit]}.tar.gz"
|
2016-09-08 09:11:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if transformed_target
|
|
|
|
Inspec::Log.warn("URL target #{target} transformed to #{transformed_target}. Consider using the git fetcher")
|
|
|
|
transformed_target
|
|
|
|
else
|
|
|
|
target
|
|
|
|
end
|
|
|
|
end
|
2016-02-21 03:00:52 +00:00
|
|
|
|
2016-09-08 09:11:44 +00:00
|
|
|
attr_reader :files, :archive_path
|
2016-02-21 03:00:52 +00:00
|
|
|
|
2016-09-08 09:11:44 +00:00
|
|
|
def initialize(url, opts)
|
2018-12-12 18:05:51 +00:00
|
|
|
@target = url.to_s
|
|
|
|
@target_uri = url.is_a?(URI) ? url : parse_uri(url)
|
2016-09-08 09:11:44 +00:00
|
|
|
@insecure = opts['insecure']
|
|
|
|
@token = opts['token']
|
2016-09-09 09:21:54 +00:00
|
|
|
@config = opts
|
2017-06-15 16:10:47 +00:00
|
|
|
@archive_path = nil
|
|
|
|
@temp_archive_path = nil
|
2016-02-21 03:00:52 +00:00
|
|
|
end
|
|
|
|
|
2016-09-08 09:11:44 +00:00
|
|
|
def fetch(path)
|
2016-09-20 10:36:23 +00:00
|
|
|
@archive_path ||= download_archive(path)
|
|
|
|
end
|
|
|
|
|
2016-09-08 09:11:44 +00:00
|
|
|
def resolved_source
|
2016-09-21 09:15:00 +00:00
|
|
|
@resolved_source ||= { url: @target, sha256: sha256 }
|
2016-09-20 10:36:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key
|
2016-09-22 10:23:32 +00:00
|
|
|
@archive_shasum ||= sha256
|
2016-09-20 10:36:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
@target
|
2016-09-08 09:11:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-02-21 03:00:52 +00:00
|
|
|
|
2018-11-08 20:55:15 +00:00
|
|
|
def parse_uri(target)
|
|
|
|
return URI.parse(target) if target.is_a?(String)
|
|
|
|
URI.parse(target[:url])
|
|
|
|
end
|
|
|
|
|
2016-09-22 10:23:32 +00:00
|
|
|
def sha256
|
|
|
|
file = @archive_path || temp_archive_path
|
2017-06-03 00:14:55 +00:00
|
|
|
OpenSSL::Digest::SHA256.digest(File.read(file)).unpack('H*')[0]
|
2016-09-20 10:36:23 +00:00
|
|
|
end
|
2016-02-21 03:00:52 +00:00
|
|
|
|
2016-09-20 10:36:23 +00:00
|
|
|
def file_type_from_remote(remote)
|
2016-02-21 03:00:52 +00:00
|
|
|
content_type = remote.meta['content-type']
|
2016-09-20 10:36:23 +00:00
|
|
|
file_type = MIME_TYPES[content_type]
|
2016-02-21 03:00:52 +00:00
|
|
|
|
|
|
|
if file_type.nil?
|
2016-09-20 10:36:23 +00:00
|
|
|
Inspec::Log.warn("Unrecognized content type: #{content_type}. Assuming tar.gz")
|
|
|
|
file_type = '.tar.gz'
|
2016-02-21 03:00:52 +00:00
|
|
|
end
|
2016-09-20 10:36:23 +00:00
|
|
|
|
|
|
|
file_type
|
|
|
|
end
|
|
|
|
|
2016-09-22 10:23:32 +00:00
|
|
|
def temp_archive_path
|
2018-04-19 17:01:54 +00:00
|
|
|
@temp_archive_path ||= if @config['server_type'] == 'automate2'
|
|
|
|
download_automate2_archive_to_temp
|
|
|
|
else
|
|
|
|
download_archive_to_temp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def download_automate2_archive_to_temp
|
|
|
|
return @temp_archive_path if !@temp_archive_path.nil?
|
|
|
|
|
|
|
|
Inspec::Log.debug("Fetching URL: #{@target}")
|
|
|
|
json = {
|
|
|
|
owner: @config['profile'][0],
|
|
|
|
name: @config['profile'][1],
|
|
|
|
version: @config['profile'][2],
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
opts = http_opts
|
2018-11-08 20:55:15 +00:00
|
|
|
opts[:use_ssl] = @target_uri.scheme == 'https'
|
2018-04-19 17:01:54 +00:00
|
|
|
|
2018-05-03 18:07:38 +00:00
|
|
|
if @insecure
|
|
|
|
opts[:verify_mode] = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
else
|
|
|
|
opts[:verify_mode] = OpenSSL::SSL::VERIFY_PEER
|
|
|
|
end
|
|
|
|
|
2018-11-08 20:55:15 +00:00
|
|
|
req = Net::HTTP::Post.new(@target_uri)
|
2018-04-19 17:01:54 +00:00
|
|
|
opts.each do |key, value|
|
|
|
|
req.add_field(key, value)
|
|
|
|
end
|
|
|
|
req.body = json
|
2018-11-08 20:55:15 +00:00
|
|
|
res = Net::HTTP.start(@target_uri.host, @target_uri.port, opts) { |http|
|
2018-04-19 17:01:54 +00:00
|
|
|
http.request(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
@archive_type = '.tar.gz'
|
|
|
|
archive = Tempfile.new(['inspec-dl-', @archive_type])
|
|
|
|
archive.binmode
|
|
|
|
archive.write(res.body)
|
|
|
|
archive.rewind
|
|
|
|
archive.close
|
|
|
|
Inspec::Log.debug("Archive stored at temporary location: #{archive.path}")
|
|
|
|
@temp_archive_path = archive.path
|
2016-09-22 10:23:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Downloads archive to temporary file with side effect :( of setting @archive_type
|
|
|
|
def download_archive_to_temp
|
2017-11-21 07:49:41 +00:00
|
|
|
return @temp_archive_path if !@temp_archive_path.nil?
|
2016-09-22 10:23:32 +00:00
|
|
|
Inspec::Log.debug("Fetching URL: #{@target}")
|
2018-11-08 20:55:15 +00:00
|
|
|
remote = open_via_uri(@target)
|
2016-09-22 10:23:32 +00:00
|
|
|
@archive_type = file_type_from_remote(remote) # side effect :(
|
|
|
|
archive = Tempfile.new(['inspec-dl-', @archive_type])
|
2016-02-21 03:00:52 +00:00
|
|
|
archive.binmode
|
|
|
|
archive.write(remote.read)
|
|
|
|
archive.rewind
|
|
|
|
archive.close
|
2016-09-22 10:23:32 +00:00
|
|
|
Inspec::Log.debug("Archive stored at temporary location: #{archive.path}")
|
|
|
|
@temp_archive_path = archive.path
|
|
|
|
end
|
|
|
|
|
2018-11-08 20:55:15 +00:00
|
|
|
def open_via_uri(target)
|
|
|
|
opts = http_opts
|
|
|
|
|
|
|
|
if opts[:http_basic_authentication]
|
|
|
|
# OpenURI does not support userinfo so we need to remove it
|
|
|
|
open(target.sub("#{@target_uri.userinfo}@", ''), opts)
|
|
|
|
else
|
|
|
|
open(target, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-22 10:23:32 +00:00
|
|
|
def download_archive(path)
|
2018-04-19 17:01:54 +00:00
|
|
|
temp_archive_path
|
2016-09-22 10:23:32 +00:00
|
|
|
final_path = "#{path}#{@archive_type}"
|
2017-03-24 20:28:00 +00:00
|
|
|
FileUtils.mkdir_p(File.dirname(final_path))
|
2016-09-22 10:23:32 +00:00
|
|
|
FileUtils.mv(temp_archive_path, final_path)
|
2016-09-08 09:11:44 +00:00
|
|
|
Inspec::Log.debug("Fetched archive moved to: #{final_path}")
|
2016-09-22 10:23:32 +00:00
|
|
|
@temp_archive_path = nil
|
2016-09-08 09:11:44 +00:00
|
|
|
final_path
|
2016-08-23 10:47:53 +00:00
|
|
|
end
|
2017-06-22 00:09:13 +00:00
|
|
|
|
|
|
|
def http_opts
|
|
|
|
opts = {}
|
|
|
|
opts[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE if @insecure
|
|
|
|
|
2018-04-19 17:01:54 +00:00
|
|
|
if @config['server_type'] =~ /automate/
|
2017-06-22 00:09:13 +00:00
|
|
|
opts['chef-delivery-enterprise'] = @config['automate']['ent']
|
|
|
|
if @config['automate']['token_type'] == 'dctoken'
|
|
|
|
opts['x-data-collector-token'] = @config['token']
|
|
|
|
else
|
|
|
|
opts['chef-delivery-user'] = @config['user']
|
|
|
|
opts['chef-delivery-token'] = @config['token']
|
|
|
|
end
|
|
|
|
elsif @token
|
|
|
|
opts['Authorization'] = "Bearer #{@token}"
|
|
|
|
end
|
|
|
|
|
2018-11-08 20:55:15 +00:00
|
|
|
username = @config[:username] || @target_uri.user
|
|
|
|
password = @config[:password] || @target_uri.password
|
|
|
|
opts[:http_basic_authentication] = [username, password] if username
|
2018-08-30 16:57:50 +00:00
|
|
|
|
2017-06-22 00:09:13 +00:00
|
|
|
# Do not send any headers that have nil values.
|
|
|
|
# Net::HTTP does not gracefully handle this situation.
|
|
|
|
check_for_missing_values!(opts)
|
|
|
|
|
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_for_missing_values!(opts)
|
|
|
|
keys_missing_values = opts.keys.delete_if do |k|
|
|
|
|
if opts[k].nil?
|
|
|
|
false
|
|
|
|
elsif opts[k].respond_to?(:empty?) && opts[k].empty?
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
raise 'Unable to fetch profile - the following HTTP headers have no value: ' \
|
|
|
|
"#{keys_missing_values.join(', ')}" unless keys_missing_values.empty?
|
|
|
|
end
|
2016-02-21 03:00:52 +00:00
|
|
|
end
|
|
|
|
end
|