Skip to content

Commit

Permalink
feature: add support for cidr in RemoteAddress strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed Dec 16, 2021
1 parent 1e9910d commit 9a4b333
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/unleash/strategy/remote_address.rb
Expand Up @@ -13,7 +13,15 @@ def is_enabled?(params = {}, context = nil)
return false unless params.fetch(PARAM, nil).is_a? String
return false unless context.class.name == 'Unleash::Context'

params[PARAM].split(',').map(&:strip).include?(context.remote_address)
remote_address = IPAddr.new(context.remote_address) rescue nil

params[PARAM]
.split(',')
.map(&:strip)
.map{ |ipblock| IPAddr.new(ipblock) rescue nil }
.compact
.map{ |ipb| ipb.include? remote_address }
.any?
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/unleash/strategy/remote_address_spec.rb
Expand Up @@ -14,6 +14,20 @@
expect(strategy.is_enabled?({ 'IPs' => '192.168.0.1,127.0.0.1,172.12.0.1' }, unleash_context2)).to be_truthy
end

it 'should be enabled with correct CIDR params' do
ips_and_cidrs = '192.168.0.0/24,127.0.0.1/32,172.12.0.1'
expect(strategy.is_enabled?({ 'IPs' => ips_and_cidrs }, unleash_context)).to be_truthy

expect(strategy.is_enabled?({ 'IPs' => ips_and_cidrs }, Unleash::Context.new(remote_address: '172.12.0.1'))).to be_truthy
expect(strategy.is_enabled?({ 'IPs' => ips_and_cidrs }, Unleash::Context.new(remote_address: '127.0.0.1'))).to be_truthy
expect(strategy.is_enabled?({ 'IPs' => ips_and_cidrs }, Unleash::Context.new(remote_address: '192.168.0.0'))).to be_truthy
expect(strategy.is_enabled?({ 'IPs' => ips_and_cidrs }, Unleash::Context.new(remote_address: '192.168.0.1'))).to be_truthy

expect(strategy.is_enabled?({ 'IPs' => ips_and_cidrs }, Unleash::Context.new(remote_address: '127.0.0.2'))).to be_falsey
expect(strategy.is_enabled?({ 'IPs' => ips_and_cidrs }, Unleash::Context.new(remote_address: '192.168.1.0'))).to be_falsey
expect(strategy.is_enabled?({ 'IPs' => ips_and_cidrs }, Unleash::Context.new(remote_address: '192.168.1.255'))).to be_falsey
end

it 'should be disabled with false params' do
expect(strategy.is_enabled?({ 'IPs' => '192.168.0.1,172.12.0.1' }, unleash_context)).to be_falsey
end
Expand All @@ -29,6 +43,10 @@
expect(strategy.is_enabled?({ 'IPs' => '192.168.0.1,127.0.0.1,172.12.0.1' }, Unleash::Context.new)).to be_falsey
expect(strategy.is_enabled?({ 'IPs' => '192.168.0.1,127.0.0.1,172.12.0.1' }, nil)).to be_falsey
expect(strategy.is_enabled?({ 'IPs' => '192.168.0.1,127.0.0.1,172.12.0.1' })).to be_falsey
expect(strategy.is_enabled?({ 'IPs' => '192.168.x.y,127.0.0.1,172.12.0.1' }, Unleash::Context.new(remote_address: 'abc123'))).to be_falsey
expect(strategy.is_enabled?({ 'IPs' => 'foobar,abc/32' }, Unleash::Context.new(remote_address: '192.168.1.0'))).to be_falsey
expect(strategy.is_enabled?({ 'IPs' => 'foobar,abc/32' }, nil)).to be_falsey
expect(strategy.is_enabled?({ 'IPs' => 'foobar,abc/32' })).to be_falsey
end
end
end

0 comments on commit 9a4b333

Please sign in to comment.