ruby-cdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +87 -0
  3. data/LICENSE +21 -0
  4. data/README.md +303 -0
  5. data/THIRD_PARTY_NOTICES.md +327 -0
  6. data/data/spec/META.json +33 -0
  7. data/data/spec/model.json.gz +0 -0
  8. data/exe/ruby-cdk +9 -0
  9. data/lib/ruby-cdk.rb +4 -0
  10. data/lib/ruby_cdk/assets/builder.rb +122 -0
  11. data/lib/ruby_cdk/assets/hasher.rb +175 -0
  12. data/lib/ruby_cdk/assets/record.rb +49 -0
  13. data/lib/ruby_cdk/assets/uploader.rb +139 -0
  14. data/lib/ruby_cdk/assets/zipper.rb +108 -0
  15. data/lib/ruby_cdk/assets.rb +116 -0
  16. data/lib/ruby_cdk/bootstrap.rb +289 -0
  17. data/lib/ruby_cdk/cfn/resource.rb +93 -0
  18. data/lib/ruby_cdk/cfn.rb +10 -0
  19. data/lib/ruby_cdk/cli/arguments.rb +41 -0
  20. data/lib/ruby_cdk/cli/aws.rb +35 -0
  21. data/lib/ruby_cdk/cli/bootstrap.rb +63 -0
  22. data/lib/ruby_cdk/cli/diff.rb +98 -0
  23. data/lib/ruby_cdk/cli/failure.rb +80 -0
  24. data/lib/ruby_cdk/cli/io.rb +70 -0
  25. data/lib/ruby_cdk/cli/list.rb +46 -0
  26. data/lib/ruby_cdk/cli/new.rb +313 -0
  27. data/lib/ruby_cdk/cli/operation.rb +124 -0
  28. data/lib/ruby_cdk/cli/pairs.rb +32 -0
  29. data/lib/ruby_cdk/cli/report.rb +118 -0
  30. data/lib/ruby_cdk/cli/selection.rb +134 -0
  31. data/lib/ruby_cdk/cli/session.rb +290 -0
  32. data/lib/ruby_cdk/cli/shell.rb +30 -0
  33. data/lib/ruby_cdk/cli/synth.rb +58 -0
  34. data/lib/ruby_cdk/cli/validate.rb +101 -0
  35. data/lib/ruby_cdk/cli.rb +242 -0
  36. data/lib/ruby_cdk/constructs/bucket.rb +191 -0
  37. data/lib/ruby_cdk/constructs/function.rb +163 -0
  38. data/lib/ruby_cdk/constructs/grant.rb +90 -0
  39. data/lib/ruby_cdk/constructs/log_group.rb +115 -0
  40. data/lib/ruby_cdk/constructs/policy_statement.rb +161 -0
  41. data/lib/ruby_cdk/constructs/queue.rb +118 -0
  42. data/lib/ruby_cdk/constructs/role.rb +282 -0
  43. data/lib/ruby_cdk/constructs/table.rb +160 -0
  44. data/lib/ruby_cdk/constructs/topic.rb +88 -0
  45. data/lib/ruby_cdk/constructs.rb +56 -0
  46. data/lib/ruby_cdk/core/app.rb +146 -0
  47. data/lib/ruby_cdk/core/construct.rb +105 -0
  48. data/lib/ruby_cdk/core/elements.rb +107 -0
  49. data/lib/ruby_cdk/core/ids.rb +117 -0
  50. data/lib/ruby_cdk/core/stack.rb +291 -0
  51. data/lib/ruby_cdk/core/stateful.rb +156 -0
  52. data/lib/ruby_cdk/core.rb +15 -0
  53. data/lib/ruby_cdk/deploy/capabilities.rb +44 -0
  54. data/lib/ruby_cdk/deploy/deployer.rb +805 -0
  55. data/lib/ruby_cdk/deploy/errors.rb +72 -0
  56. data/lib/ruby_cdk/deploy/fakes.rb +546 -0
  57. data/lib/ruby_cdk/deploy/output/base.rb +167 -0
  58. data/lib/ruby_cdk/deploy/output/human.rb +37 -0
  59. data/lib/ruby_cdk/deploy/output/json.rb +80 -0
  60. data/lib/ruby_cdk/deploy/output/quiet.rb +20 -0
  61. data/lib/ruby_cdk/deploy/parameters.rb +86 -0
  62. data/lib/ruby_cdk/deploy/poller.rb +72 -0
  63. data/lib/ruby_cdk/deploy/ports.rb +205 -0
  64. data/lib/ruby_cdk/deploy/sdk_adapters.rb +608 -0
  65. data/lib/ruby_cdk/deploy/states.rb +62 -0
  66. data/lib/ruby_cdk/deploy.rb +25 -0
  67. data/lib/ruby_cdk/diff/compare.rb +141 -0
  68. data/lib/ruby_cdk/diff/normalizer.rb +83 -0
  69. data/lib/ruby_cdk/diff/render.rb +242 -0
  70. data/lib/ruby_cdk/diff/security.rb +322 -0
  71. data/lib/ruby_cdk/diff/statement.rb +256 -0
  72. data/lib/ruby_cdk/diff/template.rb +524 -0
  73. data/lib/ruby_cdk/diff.rb +62 -0
  74. data/lib/ruby_cdk/env.rb +42 -0
  75. data/lib/ruby_cdk/errors.rb +38 -0
  76. data/lib/ruby_cdk/fn.rb +409 -0
  77. data/lib/ruby_cdk/lazy.rb +55 -0
  78. data/lib/ruby_cdk/nag/config.rb +303 -0
  79. data/lib/ruby_cdk/nag/engine.rb +152 -0
  80. data/lib/ruby_cdk/nag/report.rb +86 -0
  81. data/lib/ruby_cdk/nag/rule.rb +171 -0
  82. data/lib/ruby_cdk/nag/rules/api_gateway_authorization.rb +91 -0
  83. data/lib/ruby_cdk/nag/rules/dynamodb_point_in_time_recovery.rb +45 -0
  84. data/lib/ruby_cdk/nag/rules/ebs_volume_encrypted.rb +41 -0
  85. data/lib/ruby_cdk/nag/rules/ec2_ingress_http.rb +48 -0
  86. data/lib/ruby_cdk/nag/rules/ec2_ingress_other_ports.rb +46 -0
  87. data/lib/ruby_cdk/nag/rules/iam_policy_admin_access.rb +60 -0
  88. data/lib/ruby_cdk/nag/rules/iam_policy_full_access.rb +55 -0
  89. data/lib/ruby_cdk/nag/rules/iam_user_credentials.rb +61 -0
  90. data/lib/ruby_cdk/nag/rules/lambda_runtime_supported.rb +88 -0
  91. data/lib/ruby_cdk/nag/rules/lambda_secret_environment.rb +61 -0
  92. data/lib/ruby_cdk/nag/rules/log_group_retention.rb +40 -0
  93. data/lib/ruby_cdk/nag/rules/policy_statements.rb +128 -0
  94. data/lib/ruby_cdk/nag/rules/rds_storage_encrypted.rb +65 -0
  95. data/lib/ruby_cdk/nag/rules/resource_policy_wildcard_principal.rb +58 -0
  96. data/lib/ruby_cdk/nag/rules/s3_public_access_block.rb +56 -0
  97. data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +136 -0
  98. data/lib/ruby_cdk/nag/rules/stateful_deletion_policy.rb +46 -0
  99. data/lib/ruby_cdk/nag/rules.rb +51 -0
  100. data/lib/ruby_cdk/nag/values.rb +305 -0
  101. data/lib/ruby_cdk/nag.rb +60 -0
  102. data/lib/ruby_cdk/schema/normalizer.rb +164 -0
  103. data/lib/ruby_cdk/schema/property_schema.rb +22 -0
  104. data/lib/ruby_cdk/schema/schema.rb +195 -0
  105. data/lib/ruby_cdk/schema/sources/compiler.rb +56 -0
  106. data/lib/ruby_cdk/schema/sources/descriptor.rb +21 -0
  107. data/lib/ruby_cdk/schema/sources/download.rb +86 -0
  108. data/lib/ruby_cdk/schema/sources/provider_schemas.rb +47 -0
  109. data/lib/ruby_cdk/schema/sources/provider_type.rb +630 -0
  110. data/lib/ruby_cdk/schema/sources/regions.rb +86 -0
  111. data/lib/ruby_cdk/schema/sources/resource_specification.rb +94 -0
  112. data/lib/ruby_cdk/schema/sources/specification_type.rb +159 -0
  113. data/lib/ruby_cdk/schema/sources/update.rb +178 -0
  114. data/lib/ruby_cdk/schema/sources.rb +20 -0
  115. data/lib/ruby_cdk/schema/type_schema.rb +109 -0
  116. data/lib/ruby_cdk/schema/validator/corpus_check.rb +81 -0
  117. data/lib/ruby_cdk/schema/validator/policies.rb +48 -0
  118. data/lib/ruby_cdk/schema/validator/refs.rb +489 -0
  119. data/lib/ruby_cdk/schema/validator/report.rb +92 -0
  120. data/lib/ruby_cdk/schema/validator/rule.rb +119 -0
  121. data/lib/ruby_cdk/schema/validator/scans.rb +146 -0
  122. data/lib/ruby_cdk/schema/validator/structure.rb +469 -0
  123. data/lib/ruby_cdk/schema/validator/template.rb +203 -0
  124. data/lib/ruby_cdk/schema/validator/template_rules.rb +70 -0
  125. data/lib/ruby_cdk/schema/validator/walk.rb +122 -0
  126. data/lib/ruby_cdk/schema/validator.rb +159 -0
  127. data/lib/ruby_cdk/schema.rb +19 -0
  128. data/lib/ruby_cdk/synth/manifest.rb +128 -0
  129. data/lib/ruby_cdk/synth/resolver.rb +326 -0
  130. data/lib/ruby_cdk/synth/rules.rb +83 -0
  131. data/lib/ruby_cdk/synth/serializer.rb +120 -0
  132. data/lib/ruby_cdk/synth/writer.rb +31 -0
  133. data/lib/ruby_cdk/synth.rb +255 -0
  134. data/lib/ruby_cdk/test/capture.rb +133 -0
  135. data/lib/ruby_cdk/test/match.rb +426 -0
  136. data/lib/ruby_cdk/test/matcher.rb +290 -0
  137. data/lib/ruby_cdk/test/private/absent.rb +34 -0
  138. data/lib/ruby_cdk/test/private/conditions.rb +39 -0
  139. data/lib/ruby_cdk/test/private/cyclic.rb +160 -0
  140. data/lib/ruby_cdk/test/private/error.rb +48 -0
  141. data/lib/ruby_cdk/test/private/js.rb +326 -0
  142. data/lib/ruby_cdk/test/private/mappings.rb +39 -0
  143. data/lib/ruby_cdk/test/private/outputs.rb +39 -0
  144. data/lib/ruby_cdk/test/private/parameters.rb +39 -0
  145. data/lib/ruby_cdk/test/private/resources.rb +116 -0
  146. data/lib/ruby_cdk/test/private/section.rb +98 -0
  147. data/lib/ruby_cdk/test/private/sorting.rb +40 -0
  148. data/lib/ruby_cdk/test/private/sparse_matrix.rb +35 -0
  149. data/lib/ruby_cdk/test/private/type.rb +23 -0
  150. data/lib/ruby_cdk/test/template.rb +364 -0
  151. data/lib/ruby_cdk/test.rb +116 -0
  152. data/lib/ruby_cdk/values.rb +8 -0
  153. data/lib/ruby_cdk/version.rb +5 -0
  154. data/lib/ruby_cdk.rb +47 -0
  155. metadata +256 -0
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "report"
4
+ require_relative "values"
5
+
6
+ module RubyCdk
7
+ module Nag
8
+ # A rule whose definition does not hold together: an id of the wrong shape,
9
+ # a severity that does not exist. It is a mistake in this gem, never in a template.
10
+ class DefinitionError < RubyCdk::Error; end
11
+
12
+ # A rule: an id, a description, a severity, the resource types it applies
13
+ # to, and the audit that returns violations for one resource.
14
+ #
15
+ # class EbsVolumeEncrypted < Rule
16
+ # id "RC-EBS-001"
17
+ # description "EBS volume is encrypted"
18
+ # severity :high
19
+ # applies_to "AWS::EC2::Volume"
20
+ #
21
+ # def audit
22
+ # check("Properties.Encrypted") do
23
+ # violate("Properties.Encrypted", "is not encrypted: set Encrypted to true") unless values.boolean("Properties.Encrypted") == true
24
+ # end
25
+ # end
26
+ # end
27
+ #
28
+ # An instance is one rule applied to one resource. Its audit reads values
29
+ # through values (a Reader), inside check blocks. A check that meets a
30
+ # dynamic value ends there and is counted as skipped; the other checks of
31
+ # the same rule on the same resource still run.
32
+ class Rule
33
+ # Every rule id has this shape.
34
+ ID_PATTERN = /\ARC-[A-Z0-9]+-\d{3}\z/
35
+ RESOURCE_TYPE_PATTERN = /\AAWS::[A-Za-z0-9]+::[A-Za-z0-9]+\z/
36
+ UNSET = Object.new.freeze
37
+
38
+ # What one rule found on one resource.
39
+ Outcome = Data.define(:violations, :skipped)
40
+
41
+ class << self
42
+ # --- the definition, written once in the body of a rule's class.
43
+
44
+ # The id, "RC-<AREA>-<NNN>". With an argument, sets it.
45
+ def id(value = UNSET)
46
+ @id = value unless value.equal?(UNSET)
47
+ @id
48
+ end
49
+
50
+ # What the rule demands of a resource, in one line. With an argument, sets it.
51
+ def description(value = UNSET)
52
+ @description = value unless value.equal?(UNSET)
53
+ @description
54
+ end
55
+
56
+ # :high, :medium or :low. With an argument, sets it.
57
+ def severity(value = UNSET)
58
+ @severity = value unless value.equal?(UNSET)
59
+ @severity
60
+ end
61
+
62
+ # The resource types the rule applies to, as written with applies_to.
63
+ attr_reader :resource_types
64
+
65
+ # Sets the resource types the rule applies to.
66
+ def applies_to(*types)
67
+ @resource_types = types.freeze unless types.empty?
68
+ end
69
+
70
+ # True when the rule is written for this resource type.
71
+ def applies_to?(type)
72
+ Array(resource_types).include?(type)
73
+ end
74
+
75
+ # Everything wrong with the definition. Empty when it holds together.
76
+ def definition_problems
77
+ problems = []
78
+ problems << "id #{id.inspect} does not have the shape RC-<AREA>-<NNN>" unless ID_PATTERN.match?(id.to_s)
79
+ problems << "has no description" if description.to_s.strip.empty?
80
+ problems << "severity #{severity.inspect} is not one of #{SEVERITIES.inspect}" unless SEVERITIES.include?(severity)
81
+ unless Array(resource_types).any? { |type| RESOURCE_TYPE_PATTERN.match?(type) } || decides_applicability_itself?
82
+ problems << "names no resource type (applies_to) and does not decide applicability itself (applicable?)"
83
+ end
84
+ problems
85
+ end
86
+
87
+ private
88
+
89
+ def decides_applicability_itself?
90
+ instance_method(:applicable?).owner != Rule
91
+ end
92
+ end
93
+
94
+ attr_reader :resource, :config
95
+
96
+ # resource: the resource as written in the template, a Hash. logical_id:
97
+ # its key under Resources. config: the Config of this run, for the rules
98
+ # that take a list from it.
99
+ def initialize(resource, logical_id:, config:)
100
+ @resource = resource
101
+ @logical_id = logical_id
102
+ @config = config
103
+ @values = Reader.new(resource)
104
+ @violations = []
105
+ @skipped = []
106
+ end
107
+
108
+ # The resource's Type.
109
+ def type
110
+ @resource.is_a?(Hash) ? @resource["Type"] : nil
111
+ end
112
+
113
+ # False when the rule has nothing to say about this resource.
114
+ def applicable?
115
+ self.class.applies_to?(type)
116
+ end
117
+
118
+ # Runs the rule over its resource. Never raises: a value that is not a
119
+ # literal, and a fault inside the rule, both end as a Skipped record.
120
+ def run
121
+ begin
122
+ audit
123
+ rescue Undecided => e
124
+ @skipped << skipped_record(e.path, e.reason)
125
+ rescue StandardError => e
126
+ @skipped << skipped_record("", "the rule failed: #{e.class}: #{e.message}")
127
+ end
128
+ Outcome.new(violations: @violations, skipped: @skipped)
129
+ end
130
+
131
+ # The rule itself. Reads through values, inside check blocks.
132
+ def audit
133
+ raise NotImplementedError, "#{self.class} does not audit anything"
134
+ end
135
+
136
+ protected
137
+
138
+ # The Reader over the resource.
139
+ def values
140
+ @values
141
+ end
142
+
143
+ # One check, at one path. Runs the block. When a value the block needs is
144
+ # not a literal, the block ends, a Skipped is recorded for path, and
145
+ # check returns nil. Otherwise it returns what the block returned.
146
+ def check(path)
147
+ yield
148
+ rescue Undecided => e
149
+ @skipped << skipped_record(e.path, e.reason)
150
+ nil
151
+ end
152
+
153
+ # Records a violation at path with a message that says what to change.
154
+ def violate(path, message)
155
+ @violations << Violation.new(rule: self.class.id, severity: self.class.severity, logical_id: @logical_id, resource_type: type, path: path,
156
+ message: message)
157
+ end
158
+
159
+ # Ends the current check as undecided, with the reason.
160
+ def undecided(path, reason)
161
+ raise Undecided.new(path, reason)
162
+ end
163
+
164
+ private
165
+
166
+ def skipped_record(path, reason)
167
+ Skipped.new(rule: self.class.id, logical_id: @logical_id, resource_type: type, path: path, reason: reason)
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+
5
+ module RubyCdk
6
+ module Nag
7
+ module Rules
8
+ # RC-APIGW-001. A method, or a v2 route, is compliant when its
9
+ # AuthorizationType is something other than NONE. A method with no
10
+ # AuthorizationType counts as NONE: that is what API Gateway applies.
11
+ # An OPTIONS method whose every response is the CORS preflight response
12
+ # (status 204 with an Access-Control-Allow-* header) is left alone: a
13
+ # preflight is never authorized.
14
+ #
15
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
16
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0),
17
+ # src/rules/apigw/APIGWAuthorization.ts, AwsSolutions-APIG4 in
18
+ # src/packs/aws-solutions.ts: "The API does not implement
19
+ # authorization." It reads CfnMethod and CfnRoute, exempts an OPTIONS
20
+ # method only when checkCORSMethodResponses holds, and is
21
+ # non-compliant when the authorization type is undefined or NONE.
22
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
23
+ # SPDX-License-Identifier: Apache-2.0
24
+ #
25
+ # What changed: "NONE" is read in any case, as cfn-nag's W59,
26
+ # ApiGatewayMethodAuthorizationTypeRule (stelligent/cfn_nag, commit
27
+ # 8b5f03da74202ba323a145e9d037ddce6cab9dec, MIT) reads it. Where the
28
+ # source reports an error for an intrinsic inside MethodResponses and
29
+ # then still judges the method, an intrinsic there ends the exemption as
30
+ # not evaluated and the method is judged. AWS::ApiGatewayV2::Route is
31
+ # ported with the method, as the source has it; the wave card names only
32
+ # the method.
33
+ class ApiGatewayAuthorization < Rule
34
+ id "RC-APIGW-001"
35
+ description "API Gateway method and route have an authorization type other than NONE"
36
+ severity :high
37
+ applies_to "AWS::ApiGateway::Method", "AWS::ApiGatewayV2::Route"
38
+
39
+ AUTHORIZATION_TYPE = "Properties.AuthorizationType"
40
+ METHOD = "AWS::ApiGateway::Method"
41
+ HTTP_METHOD = "Properties.HttpMethod"
42
+ METHOD_RESPONSES = "Properties.MethodResponses"
43
+ RESPONSE_STATUS = "StatusCode"
44
+ RESPONSE_PARAMETERS = "ResponseParameters"
45
+ CORS_HEADER = "method.response.header.Access-Control-Allow-"
46
+ PREFLIGHT = "OPTIONS"
47
+ PREFLIGHT_STATUS = "204"
48
+ NONE = "NONE"
49
+
50
+ def audit
51
+ v = values
52
+ return if cors_preflight?(v)
53
+
54
+ check(AUTHORIZATION_TYPE) do
55
+ written = v.string(AUTHORIZATION_TYPE)
56
+ next unless written.nil? || written.casecmp(NONE).zero?
57
+
58
+ violate(AUTHORIZATION_TYPE,
59
+ written.nil? ? "has no AuthorizationType, which API Gateway reads as NONE: set it to AWS_IAM, COGNITO_USER_POOLS or CUSTOM" \
60
+ : "has AuthorizationType NONE: set it to AWS_IAM, COGNITO_USER_POOLS or CUSTOM")
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ # The source's checkCORSMethodResponses: every method response is a
67
+ # 204 with an Access-Control-Allow-* response parameter set to true.
68
+ # Undecided when one of them cannot be read: then it is no exemption.
69
+ def cors_preflight?(v)
70
+ return false unless type == METHOD
71
+
72
+ preflight = check(METHOD_RESPONSES) do
73
+ next false unless v.string(HTTP_METHOD) == PREFLIGHT
74
+
75
+ responses = v.item_paths(METHOD_RESPONSES)
76
+ next false if responses.empty?
77
+
78
+ responses.all? do |response|
79
+ v.string("#{response}.#{RESPONSE_STATUS}") == PREFLIGHT_STATUS && cors_header?(v.object("#{response}.#{RESPONSE_PARAMETERS}"))
80
+ end
81
+ end
82
+ preflight == true
83
+ end
84
+
85
+ def cors_header?(parameters)
86
+ parameters.nil? ? false : parameters.any? { |name, value| name.start_with?(CORS_HEADER) && value == true }
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+
5
+ module RubyCdk
6
+ module Nag
7
+ module Rules
8
+ # RC-DYNAMODB-001. A table is compliant when it enables point-in-time
9
+ # recovery.
10
+ #
11
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
12
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0),
13
+ # src/rules/dynamodb/DynamoDBPITREnabled.ts, AwsSolutions-DDB3 in
14
+ # src/packs/aws-solutions.ts: "The DynamoDB table does not have
15
+ # Point-in-time Recovery enabled." Non-compliant when
16
+ # PointInTimeRecoverySpecification is absent or its
17
+ # PointInTimeRecoveryEnabled is not true. The same reading is cfn-nag's
18
+ # W78, DynamoDBBackupRule (stelligent/cfn_nag, commit
19
+ # 8b5f03da74202ba323a145e9d037ddce6cab9dec, MIT).
20
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
21
+ # SPDX-License-Identifier: Apache-2.0
22
+ #
23
+ # What changed: the severity is the wave card's, medium, where the
24
+ # source pack has WARN for DDB3.
25
+ class DynamodbPointInTimeRecovery < Rule
26
+ id "RC-DYNAMODB-001"
27
+ description "DynamoDB table has point-in-time recovery enabled"
28
+ severity :medium
29
+ applies_to "AWS::DynamoDB::Table"
30
+
31
+ SPECIFICATION = "Properties.PointInTimeRecoverySpecification"
32
+ ENABLED = "#{SPECIFICATION}.PointInTimeRecoveryEnabled"
33
+
34
+ def audit
35
+ v = values
36
+ check(SPECIFICATION) do
37
+ next if v.boolean(ENABLED) == true
38
+
39
+ violate(SPECIFICATION, "does not enable point-in-time recovery: set PointInTimeRecoverySpecification.PointInTimeRecoveryEnabled to true")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+
5
+ module RubyCdk
6
+ module Nag
7
+ module Rules
8
+ # RC-EBS-001. A volume is compliant when it is encrypted.
9
+ #
10
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
11
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0),
12
+ # src/rules/ec2/EC2EBSVolumeEncrypted.ts, AwsSolutions-EC26 in
13
+ # src/packs/aws-solutions.ts: "The resource creates one or more EBS
14
+ # volumes that have encryption disabled." For a CfnVolume, non-compliant
15
+ # when Encrypted is not true. The same reading is cfn-nag's F1,
16
+ # EbsVolumeHasSseRule (stelligent/cfn_nag, commit
17
+ # 8b5f03da74202ba323a145e9d037ddce6cab9dec, MIT).
18
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
19
+ # SPDX-License-Identifier: Apache-2.0
20
+ #
21
+ # What changed: only the volume resource is ported, which is the wave
22
+ # card's rule. The source also reads the block device mappings of an
23
+ # instance, of a launch configuration, of a launch template and of an
24
+ # auto scaling group; those are not ported.
25
+ class EbsVolumeEncrypted < Rule
26
+ id "RC-EBS-001"
27
+ description "EBS volume is encrypted"
28
+ severity :high
29
+ applies_to "AWS::EC2::Volume"
30
+
31
+ ENCRYPTED = "Properties.Encrypted"
32
+
33
+ def audit
34
+ check(ENCRYPTED) do
35
+ violate(ENCRYPTED, "is not encrypted: set Encrypted to true") unless values.boolean(ENCRYPTED) == true
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+ require_relative "security_group_ingress"
5
+
6
+ module RubyCdk
7
+ module Nag
8
+ module Rules
9
+ # RC-EC2-002. A security group is compliant when none of its ingress
10
+ # rules is open to the world (0.0.0.0/0 or ::/0) on port 80 or 443: a
11
+ # range that holds only 80, only 443, or both. A range that holds any
12
+ # other port is RC-EC2-001's finding, not this one's.
13
+ #
14
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
15
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0):
16
+ # - src/rules/ec2/EC2RestrictedInbound.ts, AwsSolutions-EC23 in
17
+ # src/packs/aws-solutions.ts: "The Security Group allows for
18
+ # 0.0.0.0/0 or ::/0 inbound access." Its explanation names the two
19
+ # ports: "your web servers may only require 80 and 443 ports to be
20
+ # open, but not all".
21
+ # - src/rules/ec2/EC2RestrictedSSH.ts: the port test.
22
+ # The reading of the rules is in SecurityGroupIngress, which says what
23
+ # changed there.
24
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
25
+ # SPDX-License-Identifier: Apache-2.0
26
+ #
27
+ # What changed here: the wave card gives the web ports their own rule,
28
+ # at medium, where the source reports them under EC23 at ERROR.
29
+ class Ec2IngressHttp < SecurityGroupRule
30
+ id "RC-EC2-002"
31
+ description "Security group ingress is not open to the world on port 80 or 443"
32
+ severity :medium
33
+ applies_to "AWS::EC2::SecurityGroup", "AWS::EC2::SecurityGroupIngress"
34
+
35
+ private
36
+
37
+ def flags_ports?(ports)
38
+ ports != SecurityGroupRule::ALL_PORTS && !SecurityGroupRule.covers_other_port?(ports)
39
+ end
40
+
41
+ def message(cidr, ports)
42
+ "allows ingress from #{cidr} on #{ports}: the web ports are open to everyone; " \
43
+ "put a load balancer or a CloudFront distribution in front, or restrict the source"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+ require_relative "security_group_ingress"
5
+
6
+ module RubyCdk
7
+ module Nag
8
+ module Rules
9
+ # RC-EC2-001. A security group is compliant when none of its ingress
10
+ # rules is open to the world (0.0.0.0/0 or ::/0) on a port other than
11
+ # 80 and 443, on all ports, or on a protocol that is neither icmp nor
12
+ # icmpv6.
13
+ #
14
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
15
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0):
16
+ # - src/rules/ec2/EC2RestrictedInbound.ts, AwsSolutions-EC23 in
17
+ # src/packs/aws-solutions.ts: "The Security Group allows for
18
+ # 0.0.0.0/0 or ::/0 inbound access."
19
+ # - src/rules/ec2/EC2RestrictedSSH.ts, in the HIPAA, NIST 800-53 and
20
+ # PCI DSS packs: the port test, here for every port but 80 and 443.
21
+ # The reading of the rules is in SecurityGroupIngress, which says what
22
+ # changed there.
23
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
24
+ # SPDX-License-Identifier: Apache-2.0
25
+ #
26
+ # What changed here: the wave card's severity is high for this half of
27
+ # the findings; the source has ERROR for EC23 and for SSH.
28
+ class Ec2IngressOtherPorts < SecurityGroupRule
29
+ id "RC-EC2-001"
30
+ description "Security group ingress is not open to the world on a port other than 80 or 443"
31
+ severity :high
32
+ applies_to "AWS::EC2::SecurityGroup", "AWS::EC2::SecurityGroupIngress"
33
+
34
+ private
35
+
36
+ def flags_ports?(ports)
37
+ ports == SecurityGroupRule::ALL_PORTS || SecurityGroupRule.covers_other_port?(ports)
38
+ end
39
+
40
+ def message(cidr, ports)
41
+ "allows ingress from #{cidr} on #{ports}: restrict the source to a known CIDR range or a security group"
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+ require_relative "policy_statements"
5
+
6
+ module RubyCdk
7
+ module Nag
8
+ module Rules
9
+ # RC-IAM-001. An IAM policy is compliant when no Allow statement has
10
+ # Action "*" together with a Resource that is "*" or an ARN ending in
11
+ # "*": that statement lets its principal do anything, anywhere.
12
+ #
13
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
14
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0),
15
+ # src/rules/iam/IAMPolicyNoStatementsWithAdminAccess.ts, in the
16
+ # HIPAA, NIST 800-53 and PCI DSS packs of src/packs/: "The IAM policy
17
+ # grants admin access, meaning the policy allows a principal to perform
18
+ # all actions on all resources". The source finds the statement with a
19
+ # regular expression over the serialized document: Action holds "*",
20
+ # Effect is "Allow", and Resource holds "*" or an ARN that ends in "*".
21
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
+ # SPDX-License-Identifier: Apache-2.0
23
+ #
24
+ # What changed: the statement is read structurally, so the order in
25
+ # which its keys are written does not matter; the regular expression
26
+ # finds only a statement written Action, Effect, Resource in that
27
+ # order. AWS::IAM::User is checked as well: the source leaves users
28
+ # out of this rule, while IAMNoWildcardPermissions, from which it was
29
+ # split, checks them. There is one violation per statement, where the
30
+ # source gives one NON_COMPLIANT per resource.
31
+ class IamPolicyAdminAccess < PolicyStatements
32
+ id "RC-IAM-001"
33
+ description "IAM policy does not grant admin access: no Allow statement with Action \"*\" on Resource \"*\""
34
+ severity :high
35
+ applies_to "AWS::IAM::Policy", "AWS::IAM::ManagedPolicy", "AWS::IAM::Role", "AWS::IAM::User", "AWS::IAM::Group"
36
+
37
+ # From the source's regular expression: "*", or an ARN that ends in "*".
38
+ ALL_ACTIONS = "*"
39
+ ALL_RESOURCES = /\A(\*|arn:.*\*)\z/.freeze
40
+
41
+ def audit
42
+ each_identity_policy_document do |document|
43
+ each_statement(document) do |path, statement|
44
+ check(path) do
45
+ next unless statement.string("Effect") == "Allow"
46
+ next unless statement.strings("Action").include?(ALL_ACTIONS)
47
+
48
+ resources = statement.strings("Resource").grep(ALL_RESOURCES)
49
+ unless resources.empty?
50
+ violate("#{path}.Resource",
51
+ "grants admin access: Action \"*\" on #{resources.join(", ")}; name the actions and the resources the principal needs")
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+ require_relative "policy_statements"
5
+
6
+ module RubyCdk
7
+ module Nag
8
+ module Rules
9
+ # RC-IAM-002. An IAM policy is compliant when no Allow statement has an
10
+ # Action that is "*", or a service with ":*" after it such as "s3:*":
11
+ # the statement lets its principal do everything to what it names.
12
+ #
13
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
14
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0),
15
+ # src/rules/iam/IAMPolicyNoStatementsWithFullAccess.ts, in the HIPAA,
16
+ # NIST 800-53 and PCI DSS packs of src/packs/: "The IAM policy grants
17
+ # full access, meaning the policy allows a principal to perform all
18
+ # actions on individual resources". The source finds the statement with
19
+ # a regular expression over the serialized document: an Action that is
20
+ # "*" or "word:*", then Effect "Allow".
21
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
+ # SPDX-License-Identifier: Apache-2.0
23
+ #
24
+ # What changed: the statement is read structurally, so the order in
25
+ # which its keys are written does not matter. AWS::IAM::User is checked
26
+ # as well, as in RC-IAM-001. There is one violation per statement, where
27
+ # the source gives one NON_COMPLIANT per resource. A statement with
28
+ # Action "*" and Resource "*" is reported here and by RC-IAM-001.
29
+ class IamPolicyFullAccess < PolicyStatements
30
+ id "RC-IAM-002"
31
+ description "IAM policy does not grant full access: no Allow statement with Action \"*\""
32
+ severity :medium
33
+ applies_to "AWS::IAM::Policy", "AWS::IAM::ManagedPolicy", "AWS::IAM::Role", "AWS::IAM::User", "AWS::IAM::Group"
34
+
35
+ # From the source's regular expression: "*", or a word and ":*".
36
+ FULL_ACCESS_ACTION = /\A(\*|\w+:\*)\z/.freeze
37
+
38
+ def audit
39
+ each_identity_policy_document do |document|
40
+ each_statement(document) do |path, statement|
41
+ check(path) do
42
+ next unless statement.string("Effect") == "Allow"
43
+
44
+ actions = statement.strings("Action").grep(FULL_ACCESS_ACTION)
45
+ unless actions.empty?
46
+ violate("#{path}.Action", "grants full access: Action #{actions.join(", ")}; name the actions the principal needs")
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+
5
+ module RubyCdk
6
+ module Nag
7
+ module Rules
8
+ # RC-IAM-003. An IAM user is compliant when nothing gives it credentials
9
+ # of its own: no AWS::IAM::AccessKey names it, and it carries no inline
10
+ # policy.
11
+ #
12
+ # The inline policy half is ported from cdk-nag (cdklabs/cdk-nag, commit
13
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0),
14
+ # src/rules/iam/IAMNoInlinePolicy.ts, in the HIPAA, NIST 800-53 and PCI
15
+ # DSS packs of src/packs/: "The IAM Group, User, or Role contains an
16
+ # inline policy", and from cfn-nag (stelligent/cfn_nag, commit
17
+ # 8b5f03da74202ba323a145e9d037ddce6cab9dec, MIT, Copyright (c)
18
+ # 2016-2021 Stelligent), lib/cfn-nag/custom_rules/UserHasInlinePolicyRule.rb,
19
+ # F10: "IAM user should not have any inline policies".
20
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
21
+ # SPDX-License-Identifier: Apache-2.0
22
+ #
23
+ # What changed: the wave card's rule is about users, so the groups and
24
+ # roles of IAMNoInlinePolicy are not ported, nor is its finding on every
25
+ # AWS::IAM::Policy. An empty Policies list is not a finding (the source
26
+ # reports any Policies at all; cfn-nag reports a non-empty one, as here).
27
+ # The access key half is the wave card's own: neither cdk-nag at
28
+ # da5bed77 nor cfn-nag has a rule on AWS::IAM::AccessKey.
29
+ class IamUserCredentials < Rule
30
+ id "RC-IAM-003"
31
+ description "IAM user has no access key and no inline policy"
32
+ severity :medium
33
+ applies_to "AWS::IAM::User", "AWS::IAM::AccessKey"
34
+
35
+ INLINE_POLICIES = "Properties.Policies"
36
+ ACCESS_KEY = "AWS::IAM::AccessKey"
37
+
38
+ def audit
39
+ if type == ACCESS_KEY
40
+ user = Values.preview(values.peek("Properties.UserName"))
41
+ violate("Properties.UserName", "is an access key, a long-lived credential, for IAM user #{user}: give the workload a role instead")
42
+ else
43
+ audit_inline_policies
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def audit_inline_policies
50
+ check(INLINE_POLICIES) do
51
+ policies = values.item_paths(INLINE_POLICIES)
52
+ next if policies.empty?
53
+
54
+ violate(INLINE_POLICIES,
55
+ "has #{policies.length} inline #{policies.one? ? "policy" : "policies"}: attach managed policies to a group the user belongs to instead")
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end