2016-02-21 03:00:52 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
|
|
|
|
require 'uri'
|
2016-09-20 10:36:23 +00:00
|
|
|
require 'digest'
|
2016-02-21 03:00:52 +00:00
|
|
|
require 'tempfile'
|
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
module Fetchers
|
|
|
|
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)
|
|
|
|
resolve_from_string(target[:url], opts)
|
|
|
|
elsif target.is_a?(String)
|
|
|
|
resolve_from_string(target, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.resolve_from_string(target, opts)
|
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)
|
2016-09-08 09:11:44 +00:00
|
|
|
new(target, opts)
|
|
|
|
rescue URI::Error
|
2016-02-21 03:00:52 +00:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Transforms a browser github url to github tar url
|
|
|
|
# We distinguish between three different Github URL types:
|
|
|
|
# - 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
|
|
|
|
#
|
|
|
|
# github branch:
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
# github commit:
|
|
|
|
# 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
|
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\.]+)(/)?$}
|
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"
|
|
|
|
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)
|
|
|
|
@target = url
|
|
|
|
@insecure = opts['insecure']
|
|
|
|
@token = opts['token']
|
2016-09-09 09:21:54 +00:00
|
|
|
@config = opts
|
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-21 09:15:00 +00:00
|
|
|
def sha256
|
|
|
|
c = if @archive_path
|
|
|
|
File.read(@archive_path)
|
|
|
|
else
|
|
|
|
content
|
|
|
|
end
|
|
|
|
Digest::SHA256.hexdigest c
|
2016-09-08 09:11:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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-21 09:15:00 +00:00
|
|
|
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
|
|
|
|
2016-09-20 10:36:23 +00:00
|
|
|
def open_target
|
|
|
|
Inspec::Log.debug("Fetching URL: #{@target}")
|
2016-04-26 21:07:57 +00:00
|
|
|
http_opts = {}
|
2016-09-08 09:11:44 +00:00
|
|
|
http_opts['ssl_verify_mode'.to_sym] = OpenSSL::SSL::VERIFY_NONE if @insecure
|
|
|
|
http_opts['Authorization'] = "Bearer #{@token}" if @token
|
2016-09-20 10:36:23 +00:00
|
|
|
open(@target, http_opts)
|
|
|
|
end
|
2016-04-26 21:07:57 +00:00
|
|
|
|
2016-09-21 09:15:00 +00:00
|
|
|
def content
|
2016-09-20 10:36:23 +00:00
|
|
|
open_target.read
|
|
|
|
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
|
|
|
|
|
|
|
|
# download url into archive using opts,
|
|
|
|
# returns File object and content-type from HTTP headers
|
|
|
|
def download_archive(path)
|
|
|
|
remote = open_target
|
|
|
|
file_type = file_type_from_remote(remote)
|
2016-09-08 09:11:44 +00:00
|
|
|
final_path = "#{path}#{file_type}"
|
2016-02-21 03:00:52 +00:00
|
|
|
# download content
|
|
|
|
archive = Tempfile.new(['inspec-dl-', file_type])
|
|
|
|
archive.binmode
|
|
|
|
archive.write(remote.read)
|
|
|
|
archive.rewind
|
|
|
|
archive.close
|
2016-09-08 09:11:44 +00:00
|
|
|
FileUtils.mv(archive.path, final_path)
|
|
|
|
Inspec::Log.debug("Fetched archive moved to: #{final_path}")
|
|
|
|
final_path
|
2016-08-23 10:47:53 +00:00
|
|
|
end
|
2016-02-21 03:00:52 +00:00
|
|
|
end
|
|
|
|
end
|