-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add system properties to configure Jackson's stream read constraints (#…
…15720) This commit added a few jvm.options properties to configure the Jackson read constraints defaults (Maximum Number value length, Maximum String value length, and Maximum Nesting depth).
- Loading branch information
Showing
5 changed files
with
239 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Licensed to Elasticsearch B.V. under one or more contributor | ||
# license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright | ||
# ownership. Elasticsearch B.V. licenses this file to you under | ||
# the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
module LogStash | ||
module Util | ||
module Jackson | ||
def self.set_jackson_defaults(logger) | ||
JacksonStreamReadConstraintsDefaults.new(logger).configure | ||
end | ||
|
||
class JacksonStreamReadConstraintsDefaults | ||
|
||
java_import com.fasterxml.jackson.core.StreamReadConstraints | ||
|
||
PROPERTY_MAX_STRING_LENGTH = 'logstash.jackson.stream-read-constraints.max-string-length'.freeze | ||
PROPERTY_MAX_NUMBER_LENGTH = 'logstash.jackson.stream-read-constraints.max-number-length'.freeze | ||
PROPERTY_MAX_NESTING_DEPTH = 'logstash.jackson.stream-read-constraints.max-nesting-depth'.freeze | ||
|
||
def initialize(logger) | ||
@logger = logger | ||
end | ||
|
||
public | ||
|
||
def configure | ||
max_string_len = get_default_value_override!(PROPERTY_MAX_STRING_LENGTH) | ||
max_num_len = get_default_value_override!(PROPERTY_MAX_NUMBER_LENGTH) | ||
max_nesting_depth = get_default_value_override!(PROPERTY_MAX_NESTING_DEPTH) | ||
|
||
if max_string_len || max_num_len || max_nesting_depth | ||
begin | ||
override_default_stream_read_constraints(max_string_len, max_num_len, max_nesting_depth) | ||
rescue java.lang.IllegalArgumentException => e | ||
raise LogStash::ConfigurationError, "Invalid `logstash.jackson.*` system properties configuration: #{e.message}" | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def get_default_value_override!(property) | ||
value = get_property_value(property) | ||
return if value.nil? | ||
|
||
begin | ||
int_value = java.lang.Integer.parseInt(value) | ||
|
||
if int_value < 1 | ||
raise LogStash::ConfigurationError, "System property '#{property}' must be bigger than zero. Received: #{int_value}" | ||
end | ||
|
||
@logger.info("Jackson default value override `#{property}` configured to `#{int_value}`") | ||
|
||
int_value | ||
rescue java.lang.NumberFormatException => _e | ||
raise LogStash::ConfigurationError, "System property '#{property}' must be a positive integer value. Received: #{value}" | ||
end | ||
end | ||
|
||
def get_property_value(name) | ||
java.lang.System.getProperty(name) | ||
end | ||
|
||
def override_default_stream_read_constraints(max_string_len, max_num_len, max_nesting_depth) | ||
builder = new_stream_read_constraints_builder | ||
builder.maxStringLength(max_string_len) if max_string_len | ||
builder.maxNumberLength(max_num_len) if max_num_len | ||
builder.maxNestingDepth(max_nesting_depth) if max_nesting_depth | ||
|
||
StreamReadConstraints.overrideDefaultStreamReadConstraints(builder.build) | ||
end | ||
|
||
def new_stream_read_constraints_builder | ||
StreamReadConstraints::builder | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Licensed to Elasticsearch B.V. under one or more contributor | ||
# license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright | ||
# ownership. Elasticsearch B.V. licenses this file to you under | ||
# the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
require 'spec_helper' | ||
|
||
describe LogStash::Util::Jackson do | ||
it 'configures the read constraints defaults' do | ||
read_constraints_defaults = double('read_constraints_defaults') | ||
expect(read_constraints_defaults).to receive(:configure) | ||
|
||
expect(LogStash::Util::Jackson::JacksonStreamReadConstraintsDefaults).to receive(:new).and_return(read_constraints_defaults) | ||
|
||
LogStash::Util::Jackson.set_jackson_defaults(double('logger').as_null_object) | ||
end | ||
end | ||
|
||
describe LogStash::Util::Jackson::JacksonStreamReadConstraintsDefaults do | ||
let(:logger) { double('logger') } | ||
|
||
subject { described_class.new(logger) } | ||
|
||
shared_examples 'stream read constraint property' do |property| | ||
let(:property) { property } | ||
let(:value) { nil } | ||
let(:builder) { double('builder') } | ||
let(:builder_set_value_method) { expected_builder_set_value_method(property) } | ||
|
||
before(:each) do | ||
allow(logger).to receive(:info) | ||
|
||
allow(builder).to receive(:build).and_return(com.fasterxml.jackson.core.StreamReadConstraints::builder.build) | ||
allow(builder).to receive(builder_set_value_method).with(value.to_i) | ||
|
||
allow(subject).to receive(:new_stream_read_constraints_builder).and_return(builder) | ||
allow(subject).to receive(:get_property_value) do |name| | ||
if name == property | ||
value.to_s | ||
else | ||
nil | ||
end | ||
end | ||
end | ||
|
||
context 'with valid number' do | ||
let(:value) { '10' } | ||
it 'does not raises an error and set value' do | ||
expect { subject.configure }.to_not raise_error | ||
expect(builder).to have_received(builder_set_value_method).with(value.to_i) | ||
end | ||
end | ||
|
||
context 'with non-number value' do | ||
let(:value) { 'foo' } | ||
it 'raises an error and does not set value' do | ||
expect { subject.configure }.to raise_error(LogStash::ConfigurationError, /System property '#{property}' must be a positive integer value. Received: #{value}/) | ||
expect(builder).to_not have_received(builder_set_value_method) | ||
end | ||
end | ||
|
||
context 'with zeroed value' do | ||
let(:value) { '0' } | ||
it 'raises an error and does not set value' do | ||
expect { subject.configure }.to raise_error(LogStash::ConfigurationError, /System property '#{property}' must be bigger than zero. Received: #{value}/) | ||
expect(builder).to_not have_received(builder_set_value_method) | ||
end | ||
end | ||
|
||
context 'with zeroed value' do | ||
let(:value) { '-1' } | ||
it 'raises an error and does not set value' do | ||
expect { subject.configure }.to raise_error(LogStash::ConfigurationError, /System property '#{property}' must be bigger than zero. Received: #{value}/) | ||
expect(builder).to_not have_received(builder_set_value_method) | ||
end | ||
end | ||
|
||
def expected_builder_set_value_method(property) | ||
case property | ||
when LogStash::Util::Jackson::JacksonStreamReadConstraintsDefaults::PROPERTY_MAX_STRING_LENGTH | ||
return :maxStringLength | ||
when LogStash::Util::Jackson::JacksonStreamReadConstraintsDefaults::PROPERTY_MAX_NUMBER_LENGTH | ||
return :maxNumberLength | ||
when LogStash::Util::Jackson::JacksonStreamReadConstraintsDefaults::PROPERTY_MAX_NESTING_DEPTH | ||
return :maxNestingDepth | ||
else | ||
raise 'Invalid system property value' | ||
end | ||
end | ||
end | ||
|
||
[ | ||
LogStash::Util::Jackson::JacksonStreamReadConstraintsDefaults::PROPERTY_MAX_STRING_LENGTH, | ||
LogStash::Util::Jackson::JacksonStreamReadConstraintsDefaults::PROPERTY_MAX_NUMBER_LENGTH, | ||
LogStash::Util::Jackson::JacksonStreamReadConstraintsDefaults::PROPERTY_MAX_NESTING_DEPTH, | ||
].each { |system_property| | ||
context "#{system_property}" do | ||
it_behaves_like "stream read constraint property", system_property | ||
end | ||
} | ||
end |