Class: Puppet::Forge::Repository

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/puppet/forge/repository.rb

Overview

Repository

This class is a file for accessing remote repositories with modules.

Constant Summary collapse

NET_HTTP_EXCEPTIONS =

List of Net::HTTP exceptions to catch

[
  EOFError,
  Errno::ECONNABORTED,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EINVAL,
  Errno::ETIMEDOUT,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ProtocolError,
  SocketError,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, for_agent) ⇒ Repository

Instantiate a new repository instance rooted at the url. The library will report for_agent in the User-Agent to the repository.



38
39
40
41
42
43
# File 'lib/puppet/forge/repository.rb', line 38

def initialize(host, for_agent)
  @host  = host
  @agent = for_agent
  @cache = Cache.new(self)
  @uri   = URI.parse(host)
end

Instance Attribute Details

#cacheObject (readonly)



16
17
18
# File 'lib/puppet/forge/repository.rb', line 16

def cache
  @cache
end

#uriObject (readonly)



16
17
18
# File 'lib/puppet/forge/repository.rb', line 16

def uri
  @uri
end

Instance Method Details

#cache_keyObject

Return the cache key for this repository, this a hashed string based on the URI.



168
169
170
171
172
173
# File 'lib/puppet/forge/repository.rb', line 168

def cache_key
  return @cache_key ||= [
    @host.to_s.gsub(/[^[:alnum:]]+/, '_').sub(/_$/, ''),
    Digest::SHA1.hexdigest(@host.to_s)
  ].join('-').freeze
end

#forge_authorizationObject



52
53
54
55
56
57
58
# File 'lib/puppet/forge/repository.rb', line 52

def forge_authorization
  if Puppet[:forge_authorization]
    Puppet[:forge_authorization]
  elsif Puppet.features.pe_license?
    PELicense.load_license_key.authorization_token
  end
end

#get_http_objectNet::HTTP::Proxy

Return a Net::HTTP::Proxy object constructed from the settings provided accessing the repository.

This method optionally configures SSL correctly if the URI scheme is ‘https’, including setting up the root certificate store so remote server SSL certificates can be validated.

Returns:

  • (Net::HTTP::Proxy)

    object constructed from repo settings



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/puppet/forge/repository.rb', line 132

def get_http_object
  proxy = Puppet::Util::HttpProxy.proxy(@uri)

  if @uri.scheme == 'https'
    cert_store = OpenSSL::X509::Store.new
    cert_store.set_default_paths

    proxy.use_ssl = true
    proxy.verify_mode = OpenSSL::SSL::VERIFY_PEER
    proxy.cert_store = cert_store
  end

  if Puppet[:http_debug]
    proxy.set_debug_output($stderr)
  end

  proxy.open_timeout = Puppet[:http_connect_timeout]
  proxy.read_timeout = Puppet[:http_read_timeout]

  proxy
end

#get_request_object(path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet/forge/repository.rb', line 60

def get_request_object(path)
  headers = {
    "User-Agent" => user_agent,
  }

  if Puppet.features.zlib?
    headers = headers.merge({
      "Accept-Encoding" => Puppet::Network::HTTP::Compression::ACCEPT_ENCODING
    })
  end

  if forge_authorization
    headers = headers.merge({"Authorization" => forge_authorization})
  end

  request = Net::HTTP::Get.new(URI.escape(path), headers)

  unless @uri.user.nil? || @uri.password.nil? || forge_authorization
    request.basic_auth(@uri.user, @uri.password)
  end

  return request
end

#make_http_request(path, io = nil) ⇒ Object

Return a Net::HTTPResponse read for this path.



46
47
48
49
50
# File 'lib/puppet/forge/repository.rb', line 46

def make_http_request(path, io = nil)
  Puppet.debug "HTTP GET #{@host}#{path}"
  request = get_request_object(path)
  return read_response(request, io)
end

#read_response(request, io = nil) ⇒ Net::HTTPResponse

Return a Net::HTTPResponse read from this HTTPRequest request.

Parameters:

  • request (Net::HTTPRequest)

    request to make

Returns:

  • (Net::HTTPResponse)

    response from request

Raises:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/puppet/forge/repository.rb', line 92

def read_response(request, io = nil)
  http_object = get_http_object

  http_object.start do |http|
    response = http.request(request)

    if Puppet.features.zlib?
      if response && response.key?("content-encoding")
        case response["content-encoding"]
        when "gzip"
          response.body = Zlib::GzipReader.new(StringIO.new(response.read_body), :encoding => "ASCII-8BIT").read
          response.delete("content-encoding")
        when "deflate"
          response.body = Zlib::Inflate.inflate(response.read_body)
          response.delete("content-encoding")
        end
      end
    end

    io.write(response.body) if io.respond_to? :write
    response
  end
rescue *NET_HTTP_EXCEPTIONS => e
  raise CommunicationError.new(:uri => @uri.to_s, :original => e)
rescue OpenSSL::SSL::SSLError => e
  if e.message =~ /certificate verify failed/
    raise SSLVerifyError.new(:uri => @uri.to_s, :original => e)
  else
    raise e
  end
end

#retrieve(release) ⇒ Object

Return the local file name containing the data downloaded from the repository at release (e.g. “myuser-mymodule”).



156
157
158
159
# File 'lib/puppet/forge/repository.rb', line 156

def retrieve(release)
  path = @host.chomp('/') + release
  return cache.retrieve(path)
end

#to_sObject

Return the URI string for this repository.



162
163
164
# File 'lib/puppet/forge/repository.rb', line 162

def to_s
  "#<#{self.class} #{@host}>"
end