File: curl.rb

package info (click to toggle)
puppet-module-voxpupuli-archive 7.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 744 kB
  • sloc: ruby: 2,483; sh: 10; makefile: 4
file content (82 lines) | stat: -rw-r--r-- 2,062 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

require 'uri'
require 'tempfile'

Puppet::Type.type(:archive).provide(:curl, parent: :ruby) do
  commands curl: 'curl'
  defaultfor feature: :posix

  def curl_params(params)
    params_ordered = []
    params_ordered += optional_switch(resource[:headers], ['--header', '%s']) if resource[:headers]
    params_ordered += params

    if resource[:username]
      if resource[:username] =~ %r{\s} || resource[:password] =~ %r{\s}
        Puppet.warning('Username or password contains a space. Unable to use netrc file to hide credentials')
        account = [resource[:username], resource[:password]].compact.join(':')
        params_ordered += optional_switch(account, ['--user', '%s'])
      else
        create_netrcfile
        params_ordered += ['--netrc-file', @netrc_file.path]
      end
    end
    params_ordered += optional_switch(resource[:proxy_server], ['--proxy', '%s'])
    params_ordered += ['--insecure'] if resource[:allow_insecure]
    params_ordered += resource[:download_options] if resource[:download_options]
    params_ordered += optional_switch(resource[:cookie], ['--cookie', '%s'])

    params_ordered
  end

  def create_netrcfile
    @netrc_file = Tempfile.new('.puppet_archive_curl')
    machine = URI.parse(resource[:source]).host
    @netrc_file.write("machine #{machine}\nlogin #{resource[:username]}\npassword #{resource[:password]}\n")
    @netrc_file.close
  end

  def delete_netrcfile
    return if @netrc_file.nil?

    @netrc_file.unlink
    @netrc_file = nil
  end

  def download(filepath)
    params = curl_params(
      [
        resource[:source],
        '-o',
        filepath,
        '-fsSLg',
        '--max-redirs',
        5
      ]
    )

    begin
      curl(params)
    ensure
      delete_netrcfile
    end
  end

  def remote_checksum
    params = curl_params(
      [
        resource[:checksum_url],
        '-fsSLg',
        '--max-redirs',
        5
      ]
    )

    begin
      curl(params)[%r{\b[\da-f]{32,128}\b}i]
    ensure
      delete_netrcfile
    end
  end
end