Skip to content

Commit

Permalink
Merge pull request #8431 from joshcooper/dont_accept_msgpack_10772
Browse files Browse the repository at this point in the history
(PUP-10772) Only accept msgpack if gem is installed
  • Loading branch information
GabrielNagy authored Nov 3, 2020
2 parents bfd2220 + af00807 commit 23968a9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/puppet/network/formats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ def render(instance)
end

def supported?(klass)
klass == Puppet::Resource::Catalog &&
suitable? &&
klass == Puppet::Resource::Catalog &&
Puppet.lookup(:current_environment).rich_data?
end
end
49 changes: 49 additions & 0 deletions spec/unit/http/service/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@
subject.post_catalog(certname, environment: 'production', facts: facts, checksum_type: %w[sha256 sha384])
end

it 'does not accept msgpack by default' do
stub_request(:post, uri)
.with(headers: {'Accept' => 'application/vnd.puppet.rich+json, application/json, text/pson'})
.to_return(**catalog_response)

allow(Puppet.features).to receive(:msgpack?).and_return(false)

subject.post_catalog(certname, environment: environment, facts: facts)
end

it 'accepts msgpack & rich_json_msgpack if the gem is present' do
stub_request(:post, uri)
.with(headers: {'Accept' => 'application/vnd.puppet.rich+json, application/json, application/vnd.puppet.rich+msgpack, application/x-msgpack, text/pson'})
.to_return(**catalog_response)

allow(Puppet.features).to receive(:msgpack?).and_return(true)

subject.post_catalog(certname, environment: environment, facts: facts)
end

it 'returns a deserialized catalog' do
stub_request(:post, uri)
.to_return(**catalog_response)
Expand All @@ -140,6 +160,35 @@
expect(cat.name).to eq(certname)
end

it 'deserializes the catalog from msgpack', if: Puppet.features.msgpack? do
body = catalog.to_msgpack
formatter = Puppet::Network::FormatHandler.format(:msgpack)
catalog_response = { body: body, headers: {'Content-Type' => formatter.mime }}

stub_request(:post, uri)
.to_return(**catalog_response)

_, cat = subject.post_catalog(certname, environment: 'production', facts: facts)
expect(cat).to be_a(Puppet::Resource::Catalog)
expect(cat.name).to eq(certname)
end

it 'deserializes the catalog from rich msgpack', if: Puppet.features.msgpack? do
body = Puppet.override(rich_data: true) do
catalog.to_msgpack
end

formatter = Puppet::Network::FormatHandler.format(:rich_data_msgpack)
catalog_response = { body: body, headers: {'Content-Type' => formatter.mime }}

stub_request(:post, uri)
.to_return(**catalog_response)

_, cat = subject.post_catalog(certname, environment: 'production', facts: facts)
expect(cat).to be_a(Puppet::Resource::Catalog)
expect(cat.name).to eq(certname)
end

it 'returns the request response' do
stub_request(:post, uri)
.to_return(**catalog_response)
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/http/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def mime_types(model)
catalog_mimes = if Puppet.features.msgpack?
%w[application/vnd.puppet.rich+json application/json application/vnd.puppet.rich+msgpack application/x-msgpack text/pson]
else
%w[application/vnd.puppet.rich+json application/json application/vnd.puppet.rich+msgpack text/pson]
%w[application/vnd.puppet.rich+json application/json text/pson]
end
expect(service.mime_types(Puppet::Resource::Catalog)).to eq(catalog_mimes)
end
Expand Down

0 comments on commit 23968a9

Please sign in to comment.