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,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+
5
+ module RubyCdk
6
+ module Nag
7
+ module Rules
8
+ # RC-LAMBDA-001. A function is compliant when its Runtime is one that
9
+ # Lambda supports, or when its package is a container image, which has
10
+ # no runtime of its own.
11
+ #
12
+ # No rule of cdk-nag (da5bed77c1b08e584e3de2d67534c76871af957f) and none
13
+ # of cfn-nag (8b5f03da74202ba323a145e9d037ddce6cab9dec) covers the
14
+ # runtime, so this rule is this gem's own. It ports the decision the CDK
15
+ # makes when it marks a runtime deprecated, and the list it marks by
16
+ # (aws/aws-cdk, packages/aws-cdk-lib/aws-lambda/lib/runtime.ts, at
17
+ # 78649cb7cf7399204ed63f629964779eeaa8b37a). Every identifier in
18
+ # SUPPORTED_RUNTIMES is a Runtime there that carries no @deprecated.
19
+ # The list was not read from the provider schema: on 2026-11-23 the
20
+ # schema of AWS::Lambda::Function gives Runtime the type string and no
21
+ # enum.
22
+ #
23
+ # What changed: the list is a constant in this file, read on 2026-11-23
24
+ # from the Lambda Developer Guide, "Supported runtimes"
25
+ # (https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html),
26
+ # kept to the four families the wave card names. It is stricter than
27
+ # the CDK's marks by two runtimes: on that day the CDK had not marked
28
+ # nodejs20.x and ruby3.2 deprecated, and the Lambda documentation
29
+ # already did (April 30 and March 31, 2026).
30
+ class LambdaRuntimeSupported < Rule
31
+ id "RC-LAMBDA-001"
32
+ description "Lambda function runs on a supported runtime"
33
+ severity :high
34
+ applies_to "AWS::Lambda::Function"
35
+
36
+ # The runtimes Lambda supported on 2026-11-23, newest first in each family.
37
+ SUPPORTED_RUNTIMES = %w[
38
+ ruby4.0 ruby3.4 ruby3.3
39
+ nodejs26.x nodejs24.x nodejs22.x
40
+ python3.15 python3.14 python3.13 python3.12 python3.11 python3.10
41
+ java25 java21 java17.al2023 java11.al2023 java8.al2023 java17 java11 java8.al2
42
+ ].freeze
43
+
44
+ # In public preview on 2026-11-23: supported, but not what a finding
45
+ # should tell someone to move to.
46
+ PREVIEW_RUNTIMES = %w[nodejs26.x python3.15].freeze
47
+
48
+ # The families the list covers, read from the list itself.
49
+ FAMILIES = SUPPORTED_RUNTIMES.map { |runtime| runtime[/\A[a-z]+/] }.uniq.freeze
50
+
51
+ PACKAGE_TYPE = "Properties.PackageType"
52
+ RUNTIME = "Properties.Runtime"
53
+
54
+ def audit
55
+ v = values
56
+ package_type = check(PACKAGE_TYPE) { v.string(PACKAGE_TYPE) || "Zip" }
57
+ return if package_type == "Image"
58
+
59
+ check(RUNTIME) do
60
+ runtime = v.string(RUNTIME)
61
+ if runtime.nil?
62
+ raise Undecided.new(RUNTIME, "is not set and PackageType is not a literal") if package_type.nil?
63
+
64
+ violate(RUNTIME, "has no Runtime: set it to a supported runtime such as #{newest(FAMILIES.first)}")
65
+ next
66
+ end
67
+
68
+ family = FAMILIES.find { |candidate| runtime.start_with?(candidate) }
69
+ unless family
70
+ raise Undecided.new(RUNTIME, "is #{runtime.inspect}, of a family this rule does not cover: it keeps #{FAMILIES.join(", ")}")
71
+ end
72
+
73
+ unless SUPPORTED_RUNTIMES.include?(runtime)
74
+ violate(RUNTIME, "runs on #{runtime}, which Lambda no longer supports: move it to #{newest(family)}")
75
+ end
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ # The newest supported runtime of a family that is not in preview.
82
+ def newest(family)
83
+ SUPPORTED_RUNTIMES.find { |runtime| runtime.start_with?(family) && !PREVIEW_RUNTIMES.include?(runtime) }
84
+ end
85
+ end
86
+ end
87
+ end
88
+ 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-LAMBDA-002. A function is compliant when no environment variable
9
+ # whose name looks like a secret holds a value that is written out in
10
+ # the template. A variable that takes its value from somewhere else, a
11
+ # Ref or an intrinsic, is not evaluated: the rule cannot see it.
12
+ #
13
+ # No rule of cdk-nag (da5bed77c1b08e584e3de2d67534c76871af957f) and
14
+ # none of cfn-nag (8b5f03da74202ba323a145e9d037ddce6cab9dec) reads a
15
+ # function's environment, so the rule is the wave card's own. From cfn-nag
16
+ # (MIT, Copyright (c) 2016-2021 Stelligent), lib/cfn-nag/util/
17
+ # enforce_string_or_dynamic_reference.rb, it ports the one kind of string
18
+ # that is not a literal in the clear: a dynamic reference to Secrets
19
+ # Manager or to a secure SSM parameter.
20
+ #
21
+ # What changed: cfn-nag's PasswordBaseRule, which uses that helper, also
22
+ # follows a Ref into the Parameters section, to report a parameter whose
23
+ # Default holds the secret. That is not ported: here every intrinsic is
24
+ # dynamic and is counted as not evaluated, as the wave card decides for
25
+ # the whole rule pack.
26
+ class LambdaSecretEnvironment < Rule
27
+ id "RC-LAMBDA-002"
28
+ description "Lambda environment holds no literal value in a variable named like a secret"
29
+ severity :high
30
+ applies_to "AWS::Lambda::Function"
31
+
32
+ VARIABLES = "Properties.Environment.Variables"
33
+
34
+ # The words the wave card names, anywhere in the variable's name.
35
+ SECRET_NAME = /KEY|SECRET|PASSWORD|TOKEN|CREDENTIAL/i
36
+
37
+ def audit
38
+ v = values
39
+ check(VARIABLES) { v.keys(VARIABLES) }.to_a.each do |name|
40
+ audit_variable(name) if SECRET_NAME.match?(name)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def audit_variable(name)
47
+ v = values
48
+ path = v.full_path("#{VARIABLES}.#{name}")
49
+ check(path) do
50
+ value = v.entry(VARIABLES, name)
51
+ next if Values.secret_dynamic_reference?(value)
52
+
53
+ violate(path,
54
+ "is named like a secret and its value is written in the template: pass it from Secrets Manager " \
55
+ "or from a secure SSM parameter instead")
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+
5
+ module RubyCdk
6
+ module Nag
7
+ module Rules
8
+ # RC-LOGS-001. A log group is compliant when it sets a retention period.
9
+ #
10
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
11
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0),
12
+ # src/rules/cloudwatch/CloudWatchLogGroupRetentionPeriod.ts, in the
13
+ # Serverless pack and in the HIPAA, NIST 800-53 and PCI DSS packs of
14
+ # src/packs/: "The CloudWatch Log Group does not have an explicit
15
+ # retention policy defined. By default, logs are kept indefinitely and
16
+ # never expire." Non-compliant when RetentionInDays is undefined. The
17
+ # same reading is cfn-nag's W86, LogsLogGroupRetentionRule
18
+ # (stelligent/cfn_nag, commit 8b5f03da74202ba323a145e9d037ddce6cab9dec, MIT).
19
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
20
+ # SPDX-License-Identifier: Apache-2.0
21
+ #
22
+ # What changed: the severity is the wave card's, low, where the source
23
+ # packs have WARN or ERROR.
24
+ class LogGroupRetention < Rule
25
+ id "RC-LOGS-001"
26
+ description "CloudWatch log group has a retention period"
27
+ severity :low
28
+ applies_to "AWS::Logs::LogGroup"
29
+
30
+ RETENTION_IN_DAYS = "Properties.RetentionInDays"
31
+
32
+ def audit
33
+ check(RETENTION_IN_DAYS) do
34
+ violate(RETENTION_IN_DAYS, "keeps its logs forever: set RetentionInDays, for example to 30 or 90") unless values.present?(RETENTION_IN_DAYS)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require_relative "../rule"
5
+
6
+ module RubyCdk
7
+ module Nag
8
+ module Rules
9
+ # The shared base of the rules that read IAM policy statements: where a
10
+ # policy document is written, how its statements are listed, and the
11
+ # tests a statement is put to. RC-POLICY-001, RC-IAM-001 and RC-IAM-002
12
+ # inherit from it.
13
+ #
14
+ # Ported from cfn-model 0.6.6, the model cfn-nag reads templates with
15
+ # (stelligent/cfn-model, commit 3ca846093639c1bfdf2069d33c8050ad66122e6b,
16
+ # MIT, Copyright (c) 2016-2020 Stelligent):
17
+ # - lib/cfn-model/parser/policy_document_parser.rb, streamline_array:
18
+ # a Statement, an Action or a Resource is a list, or one item.
19
+ # - lib/cfn-model/model/principal.rb, Principal.wildcard?
20
+ #
21
+ # And from cdk-nag (cdklabs/cdk-nag, commit
22
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0),
23
+ # src/rules/iam/IAMNoWildcardPermissions.ts and the two rules split from
24
+ # it: which resources hold identity policies, namely the Policies of a
25
+ # role, a user or a group, and the PolicyDocument of a policy resource.
26
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
27
+ # SPDX-License-Identifier: Apache-2.0
28
+ #
29
+ # What changed: written against a plain Hash instead of cfn-model's
30
+ # objects, which resolve Refs through the model; here a Ref is dynamic
31
+ # and the statement is left unevaluated. A document written as a JSON
32
+ # string, which the schemas of KeyPolicy and PolicyDocument allow, is
33
+ # parsed; cfn-model does not read one.
34
+ class PolicyStatements < Rule
35
+ STATEMENT = "Statement"
36
+ INLINE_POLICIES = "Properties.Policies"
37
+ INLINE_POLICY_TYPES = %w[AWS::IAM::Role AWS::IAM::User AWS::IAM::Group].freeze
38
+ STANDALONE_DOCUMENT = "Properties.PolicyDocument"
39
+
40
+ protected
41
+
42
+ # Yields the path of every identity policy document on the resource:
43
+ # the Policies that a role, a user or a group carries inline, or the
44
+ # PolicyDocument of a policy resource. The trust policy of a role is
45
+ # not among them: no cdk-nag rule reads one.
46
+ def each_identity_policy_document
47
+ unless INLINE_POLICY_TYPES.include?(type)
48
+ yield STANDALONE_DOCUMENT
49
+ return
50
+ end
51
+
52
+ policies = check(INLINE_POLICIES) { values.item_paths(INLINE_POLICIES) } || []
53
+ policies.each { |policy| yield "#{policy}.PolicyDocument" }
54
+ end
55
+
56
+ # Yields the path and a Reader for every statement of the policy
57
+ # document at path. A document that is dynamic, or that is not a
58
+ # document, ends the listing with one Skipped record at path.
59
+ def each_statement(document_path)
60
+ statements = check(document_path) { statements_in(document_path) }
61
+ statements&.each { |path, statement| yield path, statement }
62
+ end
63
+
64
+ # cfn-model's Principal.wildcard?: the principal is a String that holds
65
+ # a "*", or an object one of whose String values, or String list items,
66
+ # does. A literal "*" decides, whatever else the principal holds. An
67
+ # intrinsic anywhere in it, and no "*", leaves the question open:
68
+ # cfn-model reads such a principal as "not a wildcard"; here the check
69
+ # that asked is counted as not decided.
70
+ def wildcard_principal?(statement)
71
+ principal = statement.raw("Principal")
72
+ return false if principal.nil?
73
+
74
+ parts = principal.is_a?(Hash) ? principal.values.flat_map { |part| part.is_a?(Array) ? part : [ part ] } : [ principal ]
75
+ return true if parts.any? { |part| part.is_a?(String) && part.include?("*") }
76
+ if parts.any? { |part| Values.dynamic?(part) }
77
+ raise Undecided.new(statement.full_path("Principal"), "holds #{Values.preview(parts.find { |part| Values.dynamic?(part) })}, a dynamic value")
78
+ end
79
+
80
+ false
81
+ end
82
+
83
+ # True when the statement carries a Condition block with something in
84
+ # it. Any condition counts as a restriction: reading which ones really
85
+ # narrow the grant is beyond these rules.
86
+ def restricts?(statement)
87
+ condition = statement.raw("Condition")
88
+ return false if condition.nil?
89
+ return !condition.empty? if condition.is_a?(Hash) || condition.is_a?(Array)
90
+
91
+ undecided(statement.full_path("Condition"), "is #{Values.preview(condition)}, not a condition block")
92
+ end
93
+
94
+ private
95
+
96
+ # [[path, Reader], ...], one entry per statement of the document.
97
+ def statements_in(document_path)
98
+ v = values
99
+ written = v.raw(document_path)
100
+ document = case written
101
+ when nil
102
+ nil
103
+ when Hash
104
+ written
105
+ when String
106
+ parse_document(document_path, written)
107
+ else
108
+ raise Undecided.new(document_path, "is #{Values.preview(written)}, not a policy document")
109
+ end
110
+ return [] unless document
111
+
112
+ root = v.sub(document_path, document)
113
+ root.item_paths(STATEMENT).map do |path|
114
+ [ root.full_path(path), root.at(path) ]
115
+ end
116
+ end
117
+
118
+ # The provider schemas give KeyPolicy and PolicyDocument the types
119
+ # ["object", "string"], so a document may be written as JSON text.
120
+ def parse_document(path, text)
121
+ JSON.parse(text)
122
+ rescue JSON::ParserError
123
+ raise Undecided.new(path, "is a string that is not JSON, so its statements cannot be read")
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+
5
+ module RubyCdk
6
+ module Nag
7
+ module Rules
8
+ # RC-RDS-001. A DB instance or an Aurora DB cluster is compliant when
9
+ # its storage is encrypted.
10
+ #
11
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
12
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0),
13
+ # src/rules/rds/RDSStorageEncrypted.ts, AwsSolutions-RDS2 in
14
+ # src/packs/aws-solutions.ts: "The RDS instance or Aurora DB cluster
15
+ # does not have storage encryption enabled." For a cluster,
16
+ # non-compliant when StorageEncrypted is absent or false. For an
17
+ # instance, non-compliant when StorageEncrypted is absent or false and
18
+ # its Engine does not hold "aurora": such an instance takes its
19
+ # encryption from its cluster.
20
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
21
+ # SPDX-License-Identifier: Apache-2.0
22
+ #
23
+ # What changed: a second exemption is ported from cfn-nag's F27,
24
+ # RDSDBInstanceStorageEncryptedRule (stelligent/cfn_nag, commit
25
+ # 8b5f03da74202ba323a145e9d037ddce6cab9dec, MIT, Copyright (c)
26
+ # 2016-2021 Stelligent): an instance that names a DBClusterIdentifier is
27
+ # a member of that cluster, which also holds for the members of a Multi-AZ
28
+ # DB cluster, whose engine does not hold "aurora". Both exemptions are
29
+ # the instance's: the source requires the cluster itself to be encrypted
30
+ # whatever its engine. The cluster is still read by this rule. There is
31
+ # one violation per resource, as in the source; its path says where to look.
32
+ class RdsStorageEncrypted < Rule
33
+ id "RC-RDS-001"
34
+ description "RDS DB instance and Aurora DB cluster have storage encryption enabled"
35
+ severity :high
36
+ applies_to "AWS::RDS::DBInstance", "AWS::RDS::DBCluster"
37
+
38
+ STORAGE_ENCRYPTED = "Properties.StorageEncrypted"
39
+ ENGINE = "Properties.Engine"
40
+ CLUSTER_IDENTIFIER = "Properties.DBClusterIdentifier"
41
+ AURORA = "aurora"
42
+
43
+ def audit
44
+ v = values
45
+ check(STORAGE_ENCRYPTED) do
46
+ next if v.boolean(STORAGE_ENCRYPTED) == true
47
+ next if type == "AWS::RDS::DBInstance" && (aurora_engine?(v) || cluster_member?(v))
48
+
49
+ violate(STORAGE_ENCRYPTED, "does not encrypt its storage: set StorageEncrypted to true")
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def aurora_engine?(v)
56
+ check(ENGINE) { v.string(ENGINE)&.downcase&.include?(AURORA) }
57
+ end
58
+
59
+ def cluster_member?(v)
60
+ check(CLUSTER_IDENTIFIER) { v.present?(CLUSTER_IDENTIFIER) }
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,58 @@
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-POLICY-001. A resource policy is compliant when no Allow statement
10
+ # grants to a wildcard principal unless a Condition restricts it.
11
+ #
12
+ # Ported from cfn-nag (stelligent/cfn_nag, commit
13
+ # 8b5f03da74202ba323a145e9d037ddce6cab9dec, MIT, Copyright (c)
14
+ # 2016-2021 Stelligent), lib/cfn-nag/custom_rules/:
15
+ # - S3BucketPolicyWildcardPrincipalRule, F16
16
+ # - SqsQueuePolicyWildcardPrincipalRule, F21
17
+ # - SnsTopicPolicyWildcardPrincipalRule, F18
18
+ # - KMSKeyWildcardPrincipalRule, F76
19
+ # Each reports a resource whose policy holds an Allow statement whose
20
+ # principal is a wildcard, read as cfn-model's Principal.wildcard? reads
21
+ # it (see PolicyStatements).
22
+ #
23
+ # What changed: the wave card adds "without a restricting condition", so
24
+ # a statement that carries a Condition is not reported; cfn-nag reads no
25
+ # condition in these four rules. There is one violation per statement,
26
+ # where cfn-nag gives one per resource. cdk-nag at da5bed77 has no rule
27
+ # of its own for these policies, so none of it is ported here.
28
+ class ResourcePolicyWildcardPrincipal < PolicyStatements
29
+ id "RC-POLICY-001"
30
+ description "Resource policy does not grant access to a wildcard principal without a restricting condition"
31
+ severity :high
32
+ applies_to "AWS::S3::BucketPolicy", "AWS::SQS::QueuePolicy", "AWS::SNS::TopicPolicy", "AWS::KMS::Key"
33
+
34
+ # Where each resource type writes its policy.
35
+ DOCUMENT = {
36
+ "AWS::S3::BucketPolicy" => "Properties.PolicyDocument",
37
+ "AWS::SQS::QueuePolicy" => "Properties.PolicyDocument",
38
+ "AWS::SNS::TopicPolicy" => "Properties.PolicyDocument",
39
+ "AWS::KMS::Key" => "Properties.KeyPolicy"
40
+ }.freeze
41
+
42
+ def audit
43
+ each_statement(DOCUMENT.fetch(type)) do |path, statement|
44
+ check(path) do
45
+ next unless statement.string("Effect") == "Allow"
46
+ next unless wildcard_principal?(statement)
47
+ next if restricts?(statement)
48
+
49
+ violate("#{path}.Principal",
50
+ "grants to a wildcard principal (#{Values.preview(statement.peek("Principal"))}) with no restricting condition: " \
51
+ "name the accounts, users or services in Principal, or add a Condition that narrows the grant")
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+
5
+ module RubyCdk
6
+ module Nag
7
+ module Rules
8
+ # RC-S3-001. A bucket is compliant when all four public access block
9
+ # settings are true.
10
+ #
11
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
12
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0):
13
+ # - src/rules/s3/S3BucketLevelPublicAccessProhibited.ts, AwsSolutions-S2
14
+ # in src/packs/aws-solutions.ts: BlockPublicAcls, BlockPublicPolicy,
15
+ # IgnorePublicAcls and RestrictPublicBuckets must all be true.
16
+ # - src/rules/s3/S3BucketPublicReadProhibited.ts (the compliance packs),
17
+ # which is non-compliant when PublicAccessBlockConfiguration is absent.
18
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
19
+ # SPDX-License-Identifier: Apache-2.0
20
+ #
21
+ # What changed: a bucket without PublicAccessBlockConfiguration is a
22
+ # violation, as in S3BucketPublicReadProhibited; S2 alone passes it. The
23
+ # ACL half of S3BucketPublicReadProhibited is not ported: the wave card's
24
+ # rule is the four settings. The four settings are each read on their
25
+ # own, so one that is an intrinsic skips only itself.
26
+ class BucketPublicAccessBlock < Rule
27
+ id "RC-S3-001"
28
+ description "S3 bucket has all four public access block settings on"
29
+ severity :high
30
+ applies_to "AWS::S3::Bucket"
31
+
32
+ CONFIGURATION = "Properties.PublicAccessBlockConfiguration"
33
+ SETTINGS = %w[BlockPublicAcls IgnorePublicAcls BlockPublicPolicy RestrictPublicBuckets].freeze
34
+
35
+ def audit
36
+ v = values
37
+ check(CONFIGURATION) do
38
+ written = v.raw(CONFIGURATION)
39
+ if written.nil?
40
+ violate(CONFIGURATION, "has no PublicAccessBlockConfiguration: set #{SETTINGS.join(", ")} to true")
41
+ next
42
+ end
43
+
44
+ off = SETTINGS.filter_map do |setting|
45
+ path = "#{CONFIGURATION}.#{setting}"
46
+ check(path) { v.boolean(path) == true ? nil : setting }
47
+ end
48
+ if off.any?
49
+ violate(CONFIGURATION, "does not block public access: #{off.join(", ")} #{off.one? ? "is" : "are"} not true; set #{off.one? ? "it" : "them"} to true")
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rule"
4
+
5
+ module RubyCdk
6
+ module Nag
7
+ module Rules
8
+ # The shared base of the two ingress rules: it finds the ingress rules
9
+ # that a security group resource or an ingress resource declares, and
10
+ # says whether one is open to the world, and on which ports. Each rule
11
+ # answers which ports are its finding, and what its finding says.
12
+ #
13
+ # Ported from cdk-nag (cdklabs/cdk-nag, commit
14
+ # da5bed77c1b08e584e3de2d67534c76871af957f, Apache-2.0):
15
+ # - src/rules/ec2/EC2RestrictedInbound.ts, AwsSolutions-EC23 in
16
+ # src/packs/aws-solutions.ts: a rule is open when its CidrIp or
17
+ # CidrIpv6 holds "/0", read on the inline ingress list of
18
+ # AWS::EC2::SecurityGroup and on AWS::EC2::SecurityGroupIngress;
19
+ # - src/rules/ec2/EC2RestrictedSSH.ts and EC2RestrictedCommonPorts.ts:
20
+ # how a rule's port range is read.
21
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
+ # SPDX-License-Identifier: Apache-2.0
23
+ #
24
+ # What changed: the finding is split by port, as the wave card's two
25
+ # rules ask, where the source reports every open rule under EC23 and
26
+ # reads ports only for SSH and a list of common ports. A protocol other
27
+ # than tcp, udp, icmp and icmpv6 is read as opening every port, which is
28
+ # what the provider schema of AWS::EC2::SecurityGroupIngress says of
29
+ # IpProtocol; the source reads only "-1" that way. ICMP is not a port
30
+ # and raises no finding. A value that is an intrinsic ends the check as
31
+ # skipped, where the source reports an error.
32
+ class SecurityGroupRule < Rule
33
+ INLINE = "Properties.SecurityGroupIngress"
34
+ STANDALONE = "Properties"
35
+ SOURCES = %w[CidrIp CidrIpv6].freeze
36
+ ALL_PROTOCOLS = -1
37
+ # The protocols a port range belongs to. The provider schema of
38
+ # AWS::EC2::SecurityGroupIngress says of IpProtocol: specifying -1 "or a
39
+ # protocol number other than tcp, udp, icmp, and icmpv6 allows traffic
40
+ # on all ports, regardless of any port range you specify".
41
+ PORT_PROTOCOLS = [ 6, 17 ].freeze
42
+ ICMP_PROTOCOLS = [ 1, 58 ].freeze
43
+ PROTOCOL_NAMES = { "tcp" => 6, "udp" => 17, "icmp" => 1, "icmpv6" => 58 }.freeze
44
+ ALL_PORTS = :all_ports
45
+ WEB_PORTS = [ 80, 443 ].freeze
46
+
47
+ # True when the range holds at least one port other than 80 and 443.
48
+ def self.covers_other_port?(range)
49
+ range.size > WEB_PORTS.length || (range.to_a - WEB_PORTS).any?
50
+ end
51
+
52
+ def audit
53
+ if type == "AWS::EC2::SecurityGroupIngress"
54
+ audit_ingress(STANDALONE)
55
+ else
56
+ rules = check(INLINE) { values.item_paths(INLINE) } || []
57
+ rules.each { |path| audit_ingress(path) }
58
+ end
59
+ end
60
+
61
+ protected
62
+
63
+ # Which of the rule's open ports are this rule's finding. Each of the
64
+ # two rules answers; this body only holds the type together.
65
+ def flags_ports?(_ports)
66
+ false
67
+ end
68
+
69
+ # What the finding says. Each of the two rules answers; this body only
70
+ # holds the type together.
71
+ def message(_cidr, _ports)
72
+ ""
73
+ end
74
+
75
+ private
76
+
77
+ def audit_ingress(path)
78
+ v = values
79
+ check(path) do
80
+ cidr = SOURCES.find { |key| open_to_the_world?(v.string(full(path, key))) }
81
+ next unless cidr
82
+
83
+ protocol = protocol_number(v.raw(full(path, "IpProtocol")), path)
84
+ next if ICMP_PROTOCOLS.include?(protocol)
85
+
86
+ ports = (protocol == ALL_PROTOCOLS || !PORT_PROTOCOLS.include?(protocol)) ? ALL_PORTS : port_range(v, path, protocol)
87
+ next if ports.nil?
88
+ next unless flags_ports?(ports)
89
+
90
+ violate(full(path, cidr), message(v.string(full(path, cidr)), describe_ports(protocol, ports)))
91
+ end
92
+ end
93
+
94
+ # EC23: the source holds "/0", so it is the whole address space.
95
+ def open_to_the_world?(cidr)
96
+ !cidr.nil? && cidr.include?("/0")
97
+ end
98
+
99
+ # The number of the protocol: tcp is 6, udp 17, icmp 1, icmpv6 58, and
100
+ # -1 is all of them.
101
+ def protocol_number(written, path)
102
+ name = written.to_s.strip.downcase
103
+ return ALL_PROTOCOLS if name == "-1"
104
+ return PROTOCOL_NAMES.fetch(name) if PROTOCOL_NAMES.key?(name)
105
+ return Integer(name, 10) if /\A\d+\z/.match?(name)
106
+
107
+ raise Undecided.new(full(path, "IpProtocol"), "has IpProtocol #{written.inspect}, which names no protocol")
108
+ end
109
+
110
+ # The port range of a tcp or udp rule, or ALL_PORTS when it says -1.
111
+ def port_range(v, path, protocol)
112
+ from_port = v.integer(full(path, "FromPort"))
113
+ to_port = v.integer(full(path, "ToPort"))
114
+ return ALL_PORTS if from_port == -1 || to_port == -1
115
+ if from_port.nil? || to_port.nil?
116
+ raise Undecided.new(path, "is a #{PROTOCOL_NAMES.key(protocol)} rule without a port range, which CloudFormation requires")
117
+ end
118
+ raise Undecided.new(path, "has the inverted port range #{from_port}-#{to_port}") if from_port > to_port
119
+
120
+ Range.new(from_port, to_port)
121
+ end
122
+
123
+ # "every port", "tcp ports 22-22".
124
+ def describe_ports(protocol, ports)
125
+ return "every port" if ports == ALL_PORTS
126
+
127
+ "#{PROTOCOL_NAMES.key(protocol)} ports #{ports.begin}-#{ports.end}"
128
+ end
129
+
130
+ def full(path, key)
131
+ [ path, key ].reject(&:empty?).join(".")
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end