Skip to content

Commit a75a81b

Browse files
(maint) Merge up 7631a54 to main
Generated by CI * commit '7631a54b1c217957c8a0bf808fab3e9d1e605bee': (PUP-10928) Fix missing : (packaging) Updating manpage file for 7.x (PUP-10928) Add allow_pson_serialization setting
2 parents f7d2e26 + 7631a54 commit a75a81b

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

lib/puppet/defaults.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,15 @@ def self.initialize_default_settings!(settings)
17171717
end
17181718
}
17191719
},
1720+
:allow_pson_serialization => {
1721+
:default => true,
1722+
:type => :boolean,
1723+
:desc => "Whether when unable to serialize to JSON or other formats,
1724+
Puppet falls back to PSON. This option affects both puppetserver's
1725+
configuration management service responses and when the agent saves its
1726+
cached catalog. This option is useful in preventing the loss of data because
1727+
rich data cannot be serialized via PSON.",
1728+
},
17201729
:agent_catalog_run_lockfile => {
17211730
:default => "$statedir/agent_catalog_run.lock",
17221731
:type => :string, # (#2888) Ensure this file is not added to the settings catalog.

lib/puppet/indirector/catalog/json.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ def from_json(text)
1818
def to_json(object)
1919
object.render(json_format)
2020
rescue Puppet::Network::FormatHandler::FormatError => err
21-
Puppet.info(_("Unable to serialize catalog to json, retrying with pson"))
22-
Puppet.log_exception(err, err.message, level: :debug)
23-
object.render('pson').force_encoding(Encoding::BINARY)
21+
if Puppet[:allow_pson_serialization]
22+
Puppet.info(_("Unable to serialize catalog to json, retrying with pson. PSON is deprecated and will be removed in a future release"))
23+
Puppet.log_exception(err, err.message, level: :debug)
24+
object.render('pson').force_encoding(Encoding::BINARY)
25+
else
26+
Puppet.info(_("Unable to serialize catalog to json, no other acceptable format"))
27+
Puppet.log_exception(err, err.message, level: :err)
28+
end
2429
end
2530

2631
private

lib/puppet/network/http/api/indirected_routes.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,13 @@ def first_response_formatter_for(model, request, key, &block)
192192
yield format
193193
true
194194
rescue Puppet::Network::FormatHandler::FormatError => err
195-
Puppet.warning(_("Failed to serialize %{model} for '%{key}': %{detail}") %
196-
{model: model, key: key, detail: err})
195+
msg = _("Failed to serialize %{model} for '%{key}': %{detail}") %
196+
{model: model, key: key, detail: err}
197+
if Puppet[:allow_pson_serialization]
198+
Puppet.warning(msg)
199+
else
200+
raise Puppet::Network::FormatHandler::FormatError.new(msg)
201+
end
197202
false
198203
end
199204
end

spec/unit/network/http/api/indirected_routes_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@
210210
handler.call(request, response)
211211
}.to raise_error(not_found_error)
212212
end
213+
214+
it "should raise FormatError if tries to fallback and pson serialization is not allowed" do
215+
Puppet[:allow_pson_serialization] = false
216+
data = Puppet::IndirectorTesting.new("my data")
217+
indirection.save(data, "my data")
218+
request = a_request_that_finds(data, :accept_header => "unknown, text/pson")
219+
allow(data).to receive(:to_pson).and_raise(Puppet::Network::FormatHandler::FormatError, 'Could not render to Puppet::Network::Format[pson]: source sequence is illegal/malformed utf-8')
220+
221+
expect {
222+
handler.call(request, response)
223+
}.to raise_error(Puppet::Network::FormatHandler::FormatError,
224+
%r{Failed to serialize Puppet::IndirectorTesting for 'my data': Could not render to Puppet::Network::Format\[pson\]})
225+
end
213226
end
214227

215228
describe "when searching for model instances" do
@@ -249,6 +262,19 @@
249262
%r{No supported formats are acceptable \(Accept: application/json\)})
250263
end
251264

265+
it "raises FormatError if tries to fallback and pson serialization is not allowed" do
266+
Puppet[:allow_pson_serialization] = false
267+
data = Puppet::IndirectorTesting.new("my data")
268+
indirection.save(data, "my data")
269+
request = a_request_that_searches(Puppet::IndirectorTesting.new("my"), :accept_header => "unknown, text/pson")
270+
allow(data).to receive(:to_pson).and_raise(Puppet::Network::FormatHandler::FormatError, 'Could not render to Puppet::Network::Format[pson]: source sequence is illegal/malformed utf-8')
271+
272+
expect {
273+
handler.call(request, response)
274+
}.to raise_error(Puppet::Network::FormatHandler::FormatError,
275+
%r{Failed to serialize Puppet::IndirectorTesting for 'my': Could not render_multiple to Puppet::Network::Format\[pson\]})
276+
end
277+
252278
it "should return [] when searching returns an empty array" do
253279
request = a_request_that_searches(Puppet::IndirectorTesting.new("nothing"), :accept_header => "unknown, application/json")
254280

0 commit comments

Comments
 (0)