-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #597 from alpineriveredge/add-wafv2-ip-set
Add wafv2_ip_set resource
- Loading branch information
Showing
15 changed files
with
246 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
### exist | ||
|
||
You can set `scope` to CLOUDFRONT or REGIONAL ( default: `REGIONAL` ). | ||
|
||
```ruby | ||
describe wafv2_ip_set('my-ip-set'), scope: 'REGIONAL' do | ||
it { should exist } | ||
end | ||
``` | ||
|
||
### have_ip_address | ||
|
||
```ruby | ||
describe wafv2_ip_set('my-ip-set'), scope: 'REGIONAL' do | ||
it { should have_ip_address('10.0.0.0/32') } | ||
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
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Awspec::Generator | ||
module Doc | ||
module Type | ||
class Wafv2IpSet < Base | ||
def initialize | ||
super | ||
@type_name = 'Wafv2IpSet' | ||
@type = Awspec::Type::Wafv2IpSet.new('my-ip-set') | ||
@ret = @type.resource_via_client | ||
@matchers = [] | ||
@ignore_matchers = [] | ||
@describes = [] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Awspec::Generator | ||
module Spec | ||
class Wafv2IpSet | ||
include Awspec::Helper::Finder | ||
def generate_by_scope(scope) | ||
ip_sets = select_all_ip_sets(scope) | ||
raise 'Not Found WAFV2 IP sets' if ip_sets.empty? | ||
|
||
specs = ip_sets.map do |i| | ||
ip_set = get_ip_set(scope, i.name, i.id) | ||
ERB.new(wafv2_ip_set_spec_template, nil, '-').result(binding).gsub(/^\n/, '') | ||
end | ||
specs.join("\n") | ||
end | ||
|
||
def wafv2_ip_set_spec_template | ||
<<-'EOF' | ||
describe wafv2_ip_set('<%= ip_set.name %>'), scope: '<%= scope %>' do | ||
it { should exist } | ||
its(:name) { should eq '<%= ip_set.name %>' } | ||
its(:id) { should eq '<%= ip_set.id %>' } | ||
its(:arn) { should eq '<%= ip_set.arn %>' } | ||
its(:description) { should eq '<%= ip_set.description %>' } | ||
its(:ip_address_version) { should eq '<%= ip_set.ip_address_version %>' } | ||
<% ip_set.addresses.each do |address| %> | ||
it { should have_ip_address('<%= address %>') } | ||
<% end %> | ||
end | ||
EOF | ||
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Awspec::Helper | ||
module Finder | ||
module Wafv2 | ||
def find_ip_set(scope, name) | ||
ip_sets = select_all_ip_sets(scope) | ||
ip_set = ip_sets.find do |i| | ||
i.name == name | ||
end | ||
return false unless ip_set | ||
|
||
get_ip_set(scope, name, ip_set.id) | ||
end | ||
|
||
def select_all_ip_sets(scope) | ||
res = wafv2_client.list_ip_sets({ scope: scope }) | ||
res.ip_sets | ||
end | ||
|
||
def get_ip_set(scope, name, id) | ||
res = wafv2_client.get_ip_set({ name: name, scope: scope, id: id }) | ||
res.ip_set | ||
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
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
Aws.config[:wafv2] = { | ||
stub_responses: { | ||
get_ip_set: { | ||
ip_set: { | ||
name: 'my-ip-set', | ||
id: '01234567-89ab-cdef-0123-456789abcdef', | ||
arn: 'arn:aws:wafv2:ap-northeast-1:123456789012:regional/ipset/my-ip-set/01234567-89ab-cdef-0123-456789abcdef', | ||
description: 'dev ips', | ||
ip_address_version: 'IPV4', | ||
addresses: [ | ||
'10.0.0.0/32', | ||
'10.0.0.1/32' | ||
] | ||
}, | ||
lock_token: '01234567-89ab-cdef-0123456789abcdef0' | ||
}, | ||
list_ip_sets: { | ||
next_marker: 'my-ip-set', | ||
ip_sets: [ | ||
{ | ||
name: 'my-ip-set', | ||
id: '01234567-89ab-cdef-0123-456789abcdef', | ||
description: 'dev ips', | ||
lock_token: '01234567-89ab-cdef-0123456789abcdef0', | ||
arn: 'arn:aws:wafv2:ap-northeast-1:123456789012:regional/ipset/my-ip-set/01234567-89ab-cdef-0123-456789abcdef' | ||
} | ||
] | ||
} | ||
} | ||
} |
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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Awspec::Type | ||
class Wafv2IpSet < ResourceBase | ||
attr_accessor :scope | ||
|
||
def resource_via_client | ||
@resource_via_client ||= find_ip_set(scope, @display_name) | ||
end | ||
|
||
def id | ||
@id ||= resource_via_client.id if resource_via_client | ||
end | ||
|
||
def scope | ||
@scope || 'REGIONAL' | ||
end | ||
|
||
def has_ip_address?(ip_address) | ||
resource_via_client.addresses.include?(ip_address) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'Awspec::Generator::Spec::Wafv2IpSet' do | ||
before do | ||
Awspec::Stub.load 'wafv2_ip_set' | ||
end | ||
let(:wafv2_ip_set) { Awspec::Generator::Spec::Wafv2IpSet.new } | ||
it 'generate_by_scope generate spec' do | ||
spec = <<-'EOF' | ||
describe wafv2_ip_set('my-ip-set'), scope: 'REGIONAL' do | ||
it { should exist } | ||
its(:name) { should eq 'my-ip-set' } | ||
its(:id) { should eq '01234567-89ab-cdef-0123-456789abcdef' } | ||
its(:arn) { should eq 'arn:aws:wafv2:ap-northeast-1:123456789012:regional/ipset/my-ip-set/01234567-89ab-cdef-0123-456789abcdef' } | ||
its(:description) { should eq 'dev ips' } | ||
its(:ip_address_version) { should eq 'IPV4' } | ||
it { should have_ip_address('10.0.0.0/32') } | ||
it { should have_ip_address('10.0.0.1/32') } | ||
end | ||
EOF | ||
expect(wafv2_ip_set.generate_by_scope('REGIONAL').to_s.gsub(/\n/, "\n")).to eq spec | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
Awspec::Stub.load 'wafv2_ip_set' | ||
|
||
describe wafv2_ip_set('my-ip-set'), scope: 'REGIONAL' do | ||
it { should exist } | ||
its(:name) { should eq 'my-ip-set' } | ||
its(:id) { should eq '01234567-89ab-cdef-0123-456789abcdef' } | ||
its(:arn) do | ||
should eq 'arn:aws:wafv2:ap-northeast-1:123456789012:regional/ipset/'\ | ||
'my-ip-set/01234567-89ab-cdef-0123-456789abcdef' | ||
end | ||
its(:description) { should eq 'dev ips' } | ||
its(:ip_address_version) { should eq 'IPV4' } | ||
it { should have_ip_address('10.0.0.0/32') } | ||
it { should have_ip_address('10.0.0.1/32') } | ||
end |