-
Notifications
You must be signed in to change notification settings - Fork 83
Introduce a cidr list to exclude from connection rate limiting #883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
b1tamara
wants to merge
1
commit into
cloudfoundry:master
Choose a base branch
from
sap-contributions:natgaway
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+124
−5
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
25 changes: 25 additions & 0 deletions
25
jobs/haproxy/templates/cidrs_to_exclude_from_blocking.txt.erb
This file contains hidden or 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,25 @@ | ||
| # generated from cidrs_to_exclude_from_blocking.txt.erb | ||
| <% | ||
| require "base64" | ||
| require 'zlib' | ||
| require 'stringio' | ||
|
|
||
| if_p("ha_proxy.connections_rate_limit.cidrs_to_exclude") do |cidrs| | ||
| uncompressed = '' | ||
| if cidrs.is_a?(Array) | ||
| uncompressed << "\# detected cidrs provided as array in cleartext format\n" | ||
| cidrs.each do |cidr| | ||
| uncompressed << cidr << "\n" | ||
| end | ||
| else | ||
| gzplain = Base64.decode64(cidrs) | ||
| gz = Zlib::GzipReader.new(StringIO.new(gzplain)) | ||
| uncompressed = gz.read | ||
| end | ||
| %> | ||
| # BEGIN cidrs to exclude from tcp rejection because of connection rate limiting | ||
| <%= uncompressed %> | ||
| # END cidrs to exclude from tcp rejection because of connection rate limiting | ||
| <% | ||
| end | ||
| %> | ||
This file contains hidden or 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
63 changes: 63 additions & 0 deletions
63
spec/haproxy/templates/cidrs_to_exclude_from_blocking.txt_spec.rb
This file contains hidden or 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,63 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rspec' | ||
|
|
||
| describe 'config/cidrs_to_exclude_from_blocking.txt' do | ||
| let(:template) { haproxy_job.template('config/cidrs_to_exclude_from_blocking.txt') } | ||
|
|
||
| context 'when ha_proxy.connections_rate_limit.cidrs_to_exclude is provided' do | ||
| context 'when an array of cidrs is provided' do | ||
| it 'has the correct contents' do | ||
| expect(template.render({ | ||
| 'ha_proxy' => { | ||
| 'connections_rate_limit' => { | ||
| 'window_size' => '10s', | ||
| 'table_size' => '10m', | ||
| 'cidrs_to_exclude' => ['10.0.0.0/8', '3.22.12.3/32'] | ||
| } | ||
| } | ||
| })).to eq(<<~EXPECTED) | ||
| # generated from cidrs_to_exclude_from_blocking.txt.erb | ||
|
|
||
| # BEGIN cidrs to exclude from tcp rejection because of connection rate limiting | ||
| # detected cidrs provided as array in cleartext format | ||
| 10.0.0.0/8 | ||
| 3.22.12.3/32 | ||
|
|
||
| # END cidrs to exclude from tcp rejection because of connection rate limiting | ||
|
|
||
| EXPECTED | ||
| end | ||
| end | ||
|
|
||
| context 'when a base64-encoded, gzipped config is provided' do | ||
| it 'has the correct contents' do | ||
| expect(template.render({ | ||
| 'ha_proxy' => { | ||
| 'connections_rate_limit' => { | ||
| 'cidrs_to_exclude' => gzip_and_b64_encode(<<~INPUT) | ||
| 10.0.0.0/8 | ||
| 3.22.12.3/32 | ||
| INPUT | ||
| } | ||
| } | ||
| })).to eq(<<~EXPECTED) | ||
| # generated from cidrs_to_exclude_from_blocking.txt.erb | ||
|
|
||
| # BEGIN cidrs to exclude from tcp rejection because of connection rate limiting | ||
| 10.0.0.0/8 | ||
| 3.22.12.3/32 | ||
|
|
||
| # END cidrs to exclude from tcp rejection because of connection rate limiting | ||
|
|
||
| EXPECTED | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'when ha_proxy.connections_rate_limit.cidrs_to_exclude is not provided' do | ||
| it 'is empty' do | ||
| expect(template.render({})).to be_a_blank_string | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -93,10 +93,31 @@ | |
| temp_properties.deep_merge({ 'connections_rate_limit' => { 'connections' => '5', 'block' => 'true' } }) | ||
| end | ||
|
|
||
| it 'adds http-request deny condition to http-in and https-in frontends' do | ||
| expect(frontend_http).to include('tcp-request connection reject if { sc_conn_rate(0) gt 5 }') | ||
| it 'adds tcp-request connection reject condition to http-in and https-in frontends' do | ||
| expect(frontend_http).to include('tcp-request connection reject if { sc_conn_rate(0) gt 5 } !cidr_list_to_exclude') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The acl check is missing here as the acl will exist but the file will be empty
|
||
| expect(frontend_http).to include('tcp-request connection track-sc0 src table st_tcp_conn_rate') | ||
| expect(frontend_https).to include('tcp-request connection reject if { sc_conn_rate(0) gt 5 } !cidr_list_to_exclude') | ||
| expect(frontend_https).to include('tcp-request connection track-sc0 src table st_tcp_conn_rate') | ||
| end | ||
| end | ||
| context 'when "connections", "block" and "cidrs_to_exclude" are also provided' do | ||
| let(:properties) do | ||
| temp_properties.deep_merge( | ||
| { 'connections_rate_limit' => | ||
| { | ||
| 'connections' => '5', | ||
| 'block' => 'true', | ||
| 'cidrs_to_exclude' => ['10.0.0.0/8', '3.22.12.3/32'], | ||
| } | ||
| }) | ||
| end | ||
|
|
||
| it 'adds tcp-request connection reject condition to http-in and https-in frontends' do | ||
| expect(frontend_http).to include('acl cidr_list_to_exclude src -f /var/vcap/jobs/haproxy/config/cidrs_to_exclude_from_blocking.txt') | ||
| expect(frontend_http).to include('tcp-request connection reject if { sc_conn_rate(0) gt 5 } !cidr_list_to_exclude') | ||
| expect(frontend_http).to include('tcp-request connection track-sc0 src table st_tcp_conn_rate') | ||
| expect(frontend_https).to include('tcp-request connection reject if { sc_conn_rate(0) gt 5 }') | ||
| expect(frontend_https).to include('acl cidr_list_to_exclude src -f /var/vcap/jobs/haproxy/config/cidrs_to_exclude_from_blocking.txt') | ||
| expect(frontend_https).to include('tcp-request connection reject if { sc_conn_rate(0) gt 5 } !cidr_list_to_exclude') | ||
| expect(frontend_https).to include('tcp-request connection track-sc0 src table st_tcp_conn_rate') | ||
| end | ||
| end | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to rename the file to cidrs_to_exclude_from_rate_limiting.txt to show that it is connected to rate limiting.