|
19 | 19 | require 'java_buildpack/util/cache/cached_file' |
20 | 20 | require 'java_buildpack/util/cache/inferred_network_failure' |
21 | 21 | require 'java_buildpack/util/cache/internet_availability' |
| 22 | +require 'java_buildpack/util/configuration_utils' |
22 | 23 | require 'java_buildpack/util/sanitizer' |
23 | 24 | require 'monitor' |
24 | 25 | require 'net/http' |
| 26 | +require 'openssl' |
25 | 27 | require 'pathname' |
26 | 28 | require 'tmpdir' |
27 | 29 | require 'uri' |
@@ -136,13 +138,19 @@ def attempt(http, request, cached_file) |
136 | 138 | elsif redirect?(response) |
137 | 139 | downloaded = update URI(response['Location']), cached_file |
138 | 140 | else |
139 | | - fail InferredNetworkFailure, "Bad response: #{response}" |
| 141 | + fail InferredNetworkFailure, "#{response.code} #{response.message}\n#{response.body}" |
140 | 142 | end |
141 | 143 | end |
142 | 144 |
|
143 | 145 | downloaded |
144 | 146 | end |
145 | 147 |
|
| 148 | + def ca_file(http_options) |
| 149 | + return unless CA_FILE.exist? |
| 150 | + http_options[:ca_file] = CA_FILE.to_s |
| 151 | + @logger.debug { "Adding additional CA certificates from #{CA_FILE}" } |
| 152 | + end |
| 153 | + |
146 | 154 | def cache_content(response, cached_file) |
147 | 155 | compressed = compressed?(response) |
148 | 156 |
|
@@ -185,6 +193,22 @@ def cache_last_modified(response, cached_file) |
185 | 193 | end |
186 | 194 | end |
187 | 195 |
|
| 196 | + def client_authentication(http_options) |
| 197 | + client_authentication = JavaBuildpack::Util::ConfigurationUtils.load('cache')['client_authentication'] |
| 198 | + |
| 199 | + certificate_location = client_authentication['certificate_location'] |
| 200 | + File.open(certificate_location) do |f| |
| 201 | + http_options[:cert] = OpenSSL::X509::Certificate.new f.read |
| 202 | + @logger.debug { "Adding client certificate from #{certificate_location}" } |
| 203 | + end if certificate_location |
| 204 | + |
| 205 | + private_key_location = client_authentication['private_key_location'] |
| 206 | + File.open(private_key_location) do |f| |
| 207 | + http_options[:key] = OpenSSL::PKey.read f.read, client_authentication['private_key_password'] |
| 208 | + @logger.debug { "Adding private key from #{private_key_location}" } |
| 209 | + end if private_key_location |
| 210 | + end |
| 211 | + |
188 | 212 | def compressed?(response) |
189 | 213 | %w(br compress deflate gzip x-gzip).include?(response['Content-Encoding']) |
190 | 214 | end |
@@ -230,10 +254,8 @@ def http_options(rich_uri) |
230 | 254 | http_options[:use_ssl] = true |
231 | 255 | @logger.debug { 'Adding HTTP options for secure connection' } |
232 | 256 |
|
233 | | - if CA_FILE.exist? |
234 | | - http_options[:ca_file] = CA_FILE.to_s |
235 | | - @logger.debug { "Adding additional certs from #{CA_FILE}" } |
236 | | - end |
| 257 | + ca_file http_options |
| 258 | + client_authentication http_options |
237 | 259 | end |
238 | 260 |
|
239 | 261 | http_options |
@@ -294,7 +316,7 @@ def attempt_update(cached_file, http, uri) |
294 | 316 | InternetAvailability.instance.available false, "Request failed: #{e.message}" |
295 | 317 | raise e |
296 | 318 | else |
297 | | - @logger.warn { "Request failure #{failures}, retrying: #{e.message}" } |
| 319 | + @logger.warn { "Request failure #{failures}, retrying. Failure: #{e.message}" } |
298 | 320 | retry |
299 | 321 | end |
300 | 322 | end |
|
0 commit comments