Skip to content

Commit e1a3cd0

Browse files
committed
Merge 116145431-client-certificate to master
[Completes #116145431]
2 parents 8c0b246 + b885b0f commit e1a3cd0

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ AllCops:
1111
Metrics/AbcSize:
1212
Max: 22
1313
Metrics/ClassLength:
14-
Max: 200
14+
Max: 250
1515
Metrics/CyclomaticComplexity:
1616
Max: 10
1717
Metrics/LineLength:

config/cache.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@
1616
# Download cache configuration
1717
---
1818
remote_downloads: enabled
19+
client_authentication:
20+
certificate_location:
21+
private_key_location:
22+
private_key_password:

docs/extending-caches.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ Caching can be configured by modifying the [`config/cache.yml`][] file in the bu
4646
| Name | Description
4747
| ---- | -----------
4848
| `remote_downloads` | This property can take the value `enabled` or `disabled`. <p>The default value of `enabled` means that the buildpack will check the internet connection and remember the result for the remainder of the buildpack invocation. If the internet is available, it will then be used to download files. If the internet is not available, cache will be consulted instead. <p>Alternatively, the property may be set to `disabled` which avoids the check for an internet connection, does not attempt downloads, and consults the cache instead.
49+
| `client_authentication.certificate_location` | The path to a PEM or DER encoded certificate to use for SSL client certificate authentication
50+
| `client_authentication.private_key_location` | The path to a PEM or DER encoded DSA or RSA private key to use for SSL client certificate authentication
51+
| `client_authentication.private_key_password` | The password for the private key to use for SSL client certificate authentication
4952

5053
## `JavaBuildpack::Util::Cache::DownloadCache`
5154
The [`DownloadCache`][] is the most generic of the two caches. It allows you to create a cache that persists files any that write access is available. The constructor signature looks the following:

lib/java_buildpack/util/cache/download_cache.rb

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
require 'java_buildpack/util/cache/cached_file'
2020
require 'java_buildpack/util/cache/inferred_network_failure'
2121
require 'java_buildpack/util/cache/internet_availability'
22+
require 'java_buildpack/util/configuration_utils'
2223
require 'java_buildpack/util/sanitizer'
2324
require 'monitor'
2425
require 'net/http'
26+
require 'openssl'
2527
require 'pathname'
2628
require 'tmpdir'
2729
require 'uri'
@@ -136,13 +138,19 @@ def attempt(http, request, cached_file)
136138
elsif redirect?(response)
137139
downloaded = update URI(response['Location']), cached_file
138140
else
139-
fail InferredNetworkFailure, "Bad response: #{response}"
141+
fail InferredNetworkFailure, "#{response.code} #{response.message}\n#{response.body}"
140142
end
141143
end
142144

143145
downloaded
144146
end
145147

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+
146154
def cache_content(response, cached_file)
147155
compressed = compressed?(response)
148156

@@ -185,6 +193,22 @@ def cache_last_modified(response, cached_file)
185193
end
186194
end
187195

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+
188212
def compressed?(response)
189213
%w(br compress deflate gzip x-gzip).include?(response['Content-Encoding'])
190214
end
@@ -230,10 +254,8 @@ def http_options(rich_uri)
230254
http_options[:use_ssl] = true
231255
@logger.debug { 'Adding HTTP options for secure connection' }
232256

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
237259
end
238260

239261
http_options
@@ -294,7 +316,7 @@ def attempt_update(cached_file, http, uri)
294316
InternetAvailability.instance.available false, "Request failed: #{e.message}"
295317
raise e
296318
else
297-
@logger.warn { "Request failure #{failures}, retrying: #{e.message}" }
319+
@logger.warn { "Request failure #{failures}, retrying. Failure: #{e.message}" }
298320
retry
299321
end
300322
end

0 commit comments

Comments
 (0)