Skip to content

Commit a9e8aad

Browse files
authored
Merge pull request #597 from alpineriveredge/add-wafv2-ip-set
Add wafv2_ip_set resource
2 parents 6493b44 + 4632882 commit a9e8aad

File tree

15 files changed

+246
-2
lines changed

15 files changed

+246
-2
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Lint/DuplicateMethods:
2020
- 'lib/awspec/type/ecs_service.rb'
2121
- 'lib/awspec/type/eks_nodegroup.rb'
2222
- 'lib/awspec/type/resource_base.rb'
23+
- 'lib/awspec/type/wafv2_ip_set.rb'
2324

2425
Lint/ErbNewArguments:
2526
Enabled: false

doc/_resource_types/wafv2_ip_set.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### exist
2+
3+
You can set `scope` to CLOUDFRONT or REGIONAL ( default: `REGIONAL` ).
4+
5+
```ruby
6+
describe wafv2_ip_set('my-ip-set'), scope: 'REGIONAL' do
7+
it { should exist }
8+
end
9+
```
10+
11+
### have_ip_address
12+
13+
```ruby
14+
describe wafv2_ip_set('my-ip-set'), scope: 'REGIONAL' do
15+
it { should have_ip_address('10.0.0.0/32') }
16+
end
17+
```

doc/resource_types.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
| [vpn_gateway](#vpn_gateway)
9090
| [waf_web_acl](#waf_web_acl)
9191
| [wafregional_web_acl](#wafregional_web_acl)
92+
| [wafv2_ip_set](#wafv2_ip_set)
9293
| [account](#account)
9394

9495
## <a name="acm">acm</a>
@@ -4300,6 +4301,30 @@ end
43004301

43014302

43024303
### its(:default_action), its(:web_acl_id), its(:name), its(:metric_name), its(:web_acl_arn)
4304+
## <a name="wafv2_ip_set">wafv2_ip_set</a>
4305+
4306+
Wafv2IpSet resource type.
4307+
4308+
### exist
4309+
4310+
You can set `scope` to CLOUDFRONT or REGIONAL ( default: `REGIONAL` ).
4311+
4312+
```ruby
4313+
describe wafv2_ip_set('my-ip-set'), scope: 'REGIONAL' do
4314+
it { should exist }
4315+
end
4316+
```
4317+
4318+
4319+
### have_ip_address
4320+
4321+
```ruby
4322+
describe wafv2_ip_set('my-ip-set'), scope: 'REGIONAL' do
4323+
it { should have_ip_address('10.0.0.0/32') }
4324+
end
4325+
```
4326+
4327+
### its(:name), its(:id), its(:arn), its(:description), its(:ip_address_version), its(:addresses)
43034328
# Account and Attributes
43044329

43054330
## <a name="account">account</a>

lib/awspec/command/generate.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ def s3_bucket(bucket_name = nil)
6262
end
6363
end
6464

65+
types = %w[
66+
wafv2_ip_set
67+
]
68+
69+
types.each do |type|
70+
desc "#{type} [scope]", "Generate #{type} spec from scope: (CLOUDFRONT or REGIONAL)."
71+
define_method type do |_scope|
72+
Awsecrets.load(profile: options[:profile], region: options[:region], secrets_path: options[:secrets_path])
73+
eval "puts Awspec::Generator::Spec::#{type.camelize}.new.generate_by_scope(_scope)"
74+
end
75+
end
76+
6577
types_for_generate_all = %w[
6678
cloudwatch_alarm cloudwatch_event directconnect ebs efs
6779
elasticsearch iam_group iam_policy iam_role iam_user kms lambda

lib/awspec/generator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
require 'awspec/generator/spec/rds_global_cluster'
4646
require 'awspec/generator/spec/managed_prefix_list'
4747
require 'awspec/generator/spec/codepipeline'
48+
require 'awspec/generator/spec/wafv2_ip_set'
4849

4950
# Doc
5051
require 'awspec/generator/doc/type'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module Awspec::Generator
4+
module Doc
5+
module Type
6+
class Wafv2IpSet < Base
7+
def initialize
8+
super
9+
@type_name = 'Wafv2IpSet'
10+
@type = Awspec::Type::Wafv2IpSet.new('my-ip-set')
11+
@ret = @type.resource_via_client
12+
@matchers = []
13+
@ignore_matchers = []
14+
@describes = []
15+
end
16+
end
17+
end
18+
end
19+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
module Awspec::Generator
4+
module Spec
5+
class Wafv2IpSet
6+
include Awspec::Helper::Finder
7+
def generate_by_scope(scope)
8+
ip_sets = select_all_ip_sets(scope)
9+
raise 'Not Found WAFV2 IP sets' if ip_sets.empty?
10+
11+
specs = ip_sets.map do |i|
12+
ip_set = get_ip_set(scope, i.name, i.id)
13+
ERB.new(wafv2_ip_set_spec_template, nil, '-').result(binding).gsub(/^\n/, '')
14+
end
15+
specs.join("\n")
16+
end
17+
18+
def wafv2_ip_set_spec_template
19+
<<-'EOF'
20+
describe wafv2_ip_set('<%= ip_set.name %>'), scope: '<%= scope %>' do
21+
it { should exist }
22+
its(:name) { should eq '<%= ip_set.name %>' }
23+
its(:id) { should eq '<%= ip_set.id %>' }
24+
its(:arn) { should eq '<%= ip_set.arn %>' }
25+
its(:description) { should eq '<%= ip_set.description %>' }
26+
its(:ip_address_version) { should eq '<%= ip_set.ip_address_version %>' }
27+
<% ip_set.addresses.each do |address| %>
28+
it { should have_ip_address('<%= address %>') }
29+
<% end %>
30+
end
31+
EOF
32+
end
33+
end
34+
end
35+
end

lib/awspec/helper/finder.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
require 'awspec/helper/finder/cognito_identity_pool'
5757
require 'awspec/helper/finder/transfer'
5858
require 'awspec/helper/finder/codepipeline'
59+
require 'awspec/helper/finder/wafv2'
5960

6061
require 'awspec/helper/finder/account_attributes'
6162

@@ -119,6 +120,7 @@ module Finder
119120
include Awspec::Helper::Finder::CognitoIdentityPool
120121
include Awspec::Helper::Finder::Transfer
121122
include Awspec::Helper::Finder::Codepipeline
123+
include Awspec::Helper::Finder::Wafv2
122124

123125
CLIENTS = {
124126
ec2_client: Aws::EC2::Client,
@@ -168,7 +170,8 @@ module Finder
168170
cognito_identity_client: Aws::CognitoIdentity::Client,
169171
cognito_identity_provider_client: Aws::CognitoIdentityProvider::Client,
170172
transfer_client: Aws::Transfer::Client,
171-
codepipeline_client: Aws::CodePipeline::Client
173+
codepipeline_client: Aws::CodePipeline::Client,
174+
wafv2_client: Aws::WAFV2::Client
172175
}
173176

174177
CLIENT_OPTIONS = {

lib/awspec/helper/finder/wafv2.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module Awspec::Helper
4+
module Finder
5+
module Wafv2
6+
def find_ip_set(scope, name)
7+
ip_sets = select_all_ip_sets(scope)
8+
ip_set = ip_sets.find do |i|
9+
i.name == name
10+
end
11+
return false unless ip_set
12+
13+
get_ip_set(scope, name, ip_set.id)
14+
end
15+
16+
def select_all_ip_sets(scope)
17+
res = wafv2_client.list_ip_sets({ scope: scope })
18+
res.ip_sets
19+
end
20+
21+
def get_ip_set(scope, name, id)
22+
res = wafv2_client.get_ip_set({ name: name, scope: scope, id: id })
23+
res.ip_set
24+
end
25+
end
26+
end
27+
end

lib/awspec/helper/type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module Type
2424
internet_gateway acm cloudwatch_logs dynamodb_table eip sqs ssm_parameter cloudformation_stack
2525
codebuild sns_topic redshift redshift_cluster_parameter_group codedeploy codedeploy_deployment_group
2626
secretsmanager msk transit_gateway cognito_identity_pool cognito_user_pool vpc_endpoints
27-
transfer_server managed_prefix_list codepipeline
27+
transfer_server managed_prefix_list codepipeline wafv2_ip_set
2828
]
2929

3030
ACCOUNT_ATTRIBUTES = %w[

0 commit comments

Comments
 (0)