Class: Inspec::Targets::UrlHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/targets/url.rb

Instance Method Summary collapse

Instance Method Details

#handles?(target) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/inspec/targets/url.rb', line 12

def handles?(target)
  uri = URI.parse(target)
  return false if uri.nil? or uri.scheme.nil?
  %{ http https }.include? uri.scheme
end

#resolve(target, opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/inspec/targets/url.rb', line 18

def resolve(target, opts = {})
  # abort if the target does not start with http or https
  return nil unless target.start_with? 'https://' or target.start_with? 'http://'

  # support for github https url
  if target.start_with? 'https://github.com' and target.end_with? '.git'
    url = target.sub(/.git$/, '') + '/archive/master.tar.gz'
  else
    url = target
  end

  resolve_zip(url, opts)
end

#resolve_zip(url, opts) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/inspec/targets/url.rb', line 32

def resolve_zip(url, opts)
  archive = Tempfile.new(['inspec-dl-', '.tar.gz'])
  archive.binmode

  remote = open(
    url,
    http_basic_authentication: [opts['user'] || '', opts['password'] || ''],
  )

  # download content
  archive.write(remote.read)
  archive.rewind
  archive.close

  content_type = remote.meta['content-type']
  # replace extension with zip if we detected a zip content type
  if ['application/x-zip-compressed', 'application/zip'].include?(content_type)
    # rename file for proper detection in DirHelper
    pn = Pathname.new(archive.path)
    new_path = pn.dirname.join(pn.basename.to_s.gsub('tar.gz', 'zip'))
    File.rename(pn.to_s, new_path.to_s)

    content = ZipHelper.new.resolve(new_path)
    File.unlink(new_path)
  # use tar helper as default
  elsif ['application/x-gzip', 'application/gzip'].include?(content_type)
    content = TarHelper.new.resolve(archive.path)
    archive.unlink
  end

  content
end

#to_sObject



65
66
67
# File 'lib/inspec/targets/url.rb', line 65

def to_s
  'URL Loader'
end