Skip to content

Commit 9f08d1e

Browse files
committed
(PUP-10639) Accept optional If-Modified-Since
Allow the If-Modified-Since header to be specified when calling the /puppet-ca/v1/certificate endpoint. By default, it is omitted, such as when the agent receives its own certificate. If the header is included and it hasn't been modified (as determined by puppetserver), then the method will raise a ResponseError with HTTP 304.
1 parent 806554e commit 9f08d1e

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/puppet/http/service/ca.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,21 @@ def initialize(client, session, server, port)
2828
# Submit a GET request to retrieve the named certificate from the server.
2929
#
3030
# @param [String] name name of the certificate to request
31+
# @param [Time] if_modified_since If not nil, only download the cert if it has
32+
# been modified since the specified time.
3133
# @param [Puppet::SSL::SSLContext] ssl_context
3234
#
3335
# @return [Array<Puppet::HTTP::Response, String>] An array containing the
3436
# request response and the stringified body of the request response
3537
#
3638
# @api public
37-
def get_certificate(name, ssl_context: nil)
39+
def get_certificate(name, if_modified_since: nil, ssl_context: nil)
40+
headers = add_puppet_headers(HEADERS)
41+
headers['If-Modified-Since'] = if_modified_since.httpdate if if_modified_since
42+
3843
response = @client.get(
3944
with_base_url("/certificate/#{name}"),
40-
headers: add_puppet_headers(HEADERS),
45+
headers: headers,
4146
options: {ssl_context: ssl_context}
4247
)
4348

spec/unit/http/service/ca_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@
9595
expect(err.response.code).to eq(404)
9696
end
9797
end
98+
99+
it 'raises a 304 response error if it is unmodified' do
100+
stub_request(:get, url).to_return(status: [304, 'Not Modified'])
101+
102+
expect {
103+
subject.get_certificate('ca', if_modified_since: Time.now)
104+
}.to raise_error do |err|
105+
expect(err).to be_an_instance_of(Puppet::HTTP::ResponseError)
106+
expect(err.message).to eq("Not Modified")
107+
expect(err.response.code).to eq(304)
108+
end
109+
end
98110
end
99111

100112
context 'when getting CRLs' do

0 commit comments

Comments
 (0)