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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +87 -0
- data/LICENSE +21 -0
- data/README.md +303 -0
- data/THIRD_PARTY_NOTICES.md +327 -0
- data/data/spec/META.json +33 -0
- data/data/spec/model.json.gz +0 -0
- data/exe/ruby-cdk +9 -0
- data/lib/ruby-cdk.rb +4 -0
- data/lib/ruby_cdk/assets/builder.rb +122 -0
- data/lib/ruby_cdk/assets/hasher.rb +175 -0
- data/lib/ruby_cdk/assets/record.rb +49 -0
- data/lib/ruby_cdk/assets/uploader.rb +139 -0
- data/lib/ruby_cdk/assets/zipper.rb +108 -0
- data/lib/ruby_cdk/assets.rb +116 -0
- data/lib/ruby_cdk/bootstrap.rb +289 -0
- data/lib/ruby_cdk/cfn/resource.rb +93 -0
- data/lib/ruby_cdk/cfn.rb +10 -0
- data/lib/ruby_cdk/cli/arguments.rb +41 -0
- data/lib/ruby_cdk/cli/aws.rb +35 -0
- data/lib/ruby_cdk/cli/bootstrap.rb +63 -0
- data/lib/ruby_cdk/cli/diff.rb +98 -0
- data/lib/ruby_cdk/cli/failure.rb +80 -0
- data/lib/ruby_cdk/cli/io.rb +70 -0
- data/lib/ruby_cdk/cli/list.rb +46 -0
- data/lib/ruby_cdk/cli/new.rb +313 -0
- data/lib/ruby_cdk/cli/operation.rb +124 -0
- data/lib/ruby_cdk/cli/pairs.rb +32 -0
- data/lib/ruby_cdk/cli/report.rb +118 -0
- data/lib/ruby_cdk/cli/selection.rb +134 -0
- data/lib/ruby_cdk/cli/session.rb +290 -0
- data/lib/ruby_cdk/cli/shell.rb +30 -0
- data/lib/ruby_cdk/cli/synth.rb +58 -0
- data/lib/ruby_cdk/cli/validate.rb +101 -0
- data/lib/ruby_cdk/cli.rb +242 -0
- data/lib/ruby_cdk/constructs/bucket.rb +191 -0
- data/lib/ruby_cdk/constructs/function.rb +163 -0
- data/lib/ruby_cdk/constructs/grant.rb +90 -0
- data/lib/ruby_cdk/constructs/log_group.rb +115 -0
- data/lib/ruby_cdk/constructs/policy_statement.rb +161 -0
- data/lib/ruby_cdk/constructs/queue.rb +118 -0
- data/lib/ruby_cdk/constructs/role.rb +282 -0
- data/lib/ruby_cdk/constructs/table.rb +160 -0
- data/lib/ruby_cdk/constructs/topic.rb +88 -0
- data/lib/ruby_cdk/constructs.rb +56 -0
- data/lib/ruby_cdk/core/app.rb +146 -0
- data/lib/ruby_cdk/core/construct.rb +105 -0
- data/lib/ruby_cdk/core/elements.rb +107 -0
- data/lib/ruby_cdk/core/ids.rb +117 -0
- data/lib/ruby_cdk/core/stack.rb +291 -0
- data/lib/ruby_cdk/core/stateful.rb +156 -0
- data/lib/ruby_cdk/core.rb +15 -0
- data/lib/ruby_cdk/deploy/capabilities.rb +44 -0
- data/lib/ruby_cdk/deploy/deployer.rb +805 -0
- data/lib/ruby_cdk/deploy/errors.rb +72 -0
- data/lib/ruby_cdk/deploy/fakes.rb +546 -0
- data/lib/ruby_cdk/deploy/output/base.rb +167 -0
- data/lib/ruby_cdk/deploy/output/human.rb +37 -0
- data/lib/ruby_cdk/deploy/output/json.rb +80 -0
- data/lib/ruby_cdk/deploy/output/quiet.rb +20 -0
- data/lib/ruby_cdk/deploy/parameters.rb +86 -0
- data/lib/ruby_cdk/deploy/poller.rb +72 -0
- data/lib/ruby_cdk/deploy/ports.rb +205 -0
- data/lib/ruby_cdk/deploy/sdk_adapters.rb +608 -0
- data/lib/ruby_cdk/deploy/states.rb +62 -0
- data/lib/ruby_cdk/deploy.rb +25 -0
- data/lib/ruby_cdk/diff/compare.rb +141 -0
- data/lib/ruby_cdk/diff/normalizer.rb +83 -0
- data/lib/ruby_cdk/diff/render.rb +242 -0
- data/lib/ruby_cdk/diff/security.rb +322 -0
- data/lib/ruby_cdk/diff/statement.rb +256 -0
- data/lib/ruby_cdk/diff/template.rb +524 -0
- data/lib/ruby_cdk/diff.rb +62 -0
- data/lib/ruby_cdk/env.rb +42 -0
- data/lib/ruby_cdk/errors.rb +38 -0
- data/lib/ruby_cdk/fn.rb +409 -0
- data/lib/ruby_cdk/lazy.rb +55 -0
- data/lib/ruby_cdk/nag/config.rb +303 -0
- data/lib/ruby_cdk/nag/engine.rb +152 -0
- data/lib/ruby_cdk/nag/report.rb +86 -0
- data/lib/ruby_cdk/nag/rule.rb +171 -0
- data/lib/ruby_cdk/nag/rules/api_gateway_authorization.rb +91 -0
- data/lib/ruby_cdk/nag/rules/dynamodb_point_in_time_recovery.rb +45 -0
- data/lib/ruby_cdk/nag/rules/ebs_volume_encrypted.rb +41 -0
- data/lib/ruby_cdk/nag/rules/ec2_ingress_http.rb +48 -0
- data/lib/ruby_cdk/nag/rules/ec2_ingress_other_ports.rb +46 -0
- data/lib/ruby_cdk/nag/rules/iam_policy_admin_access.rb +60 -0
- data/lib/ruby_cdk/nag/rules/iam_policy_full_access.rb +55 -0
- data/lib/ruby_cdk/nag/rules/iam_user_credentials.rb +61 -0
- data/lib/ruby_cdk/nag/rules/lambda_runtime_supported.rb +88 -0
- data/lib/ruby_cdk/nag/rules/lambda_secret_environment.rb +61 -0
- data/lib/ruby_cdk/nag/rules/log_group_retention.rb +40 -0
- data/lib/ruby_cdk/nag/rules/policy_statements.rb +128 -0
- data/lib/ruby_cdk/nag/rules/rds_storage_encrypted.rb +65 -0
- data/lib/ruby_cdk/nag/rules/resource_policy_wildcard_principal.rb +58 -0
- data/lib/ruby_cdk/nag/rules/s3_public_access_block.rb +56 -0
- data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +136 -0
- data/lib/ruby_cdk/nag/rules/stateful_deletion_policy.rb +46 -0
- data/lib/ruby_cdk/nag/rules.rb +51 -0
- data/lib/ruby_cdk/nag/values.rb +305 -0
- data/lib/ruby_cdk/nag.rb +60 -0
- data/lib/ruby_cdk/schema/normalizer.rb +164 -0
- data/lib/ruby_cdk/schema/property_schema.rb +22 -0
- data/lib/ruby_cdk/schema/schema.rb +195 -0
- data/lib/ruby_cdk/schema/sources/compiler.rb +56 -0
- data/lib/ruby_cdk/schema/sources/descriptor.rb +21 -0
- data/lib/ruby_cdk/schema/sources/download.rb +86 -0
- data/lib/ruby_cdk/schema/sources/provider_schemas.rb +47 -0
- data/lib/ruby_cdk/schema/sources/provider_type.rb +630 -0
- data/lib/ruby_cdk/schema/sources/regions.rb +86 -0
- data/lib/ruby_cdk/schema/sources/resource_specification.rb +94 -0
- data/lib/ruby_cdk/schema/sources/specification_type.rb +159 -0
- data/lib/ruby_cdk/schema/sources/update.rb +178 -0
- data/lib/ruby_cdk/schema/sources.rb +20 -0
- data/lib/ruby_cdk/schema/type_schema.rb +109 -0
- data/lib/ruby_cdk/schema/validator/corpus_check.rb +81 -0
- data/lib/ruby_cdk/schema/validator/policies.rb +48 -0
- data/lib/ruby_cdk/schema/validator/refs.rb +489 -0
- data/lib/ruby_cdk/schema/validator/report.rb +92 -0
- data/lib/ruby_cdk/schema/validator/rule.rb +119 -0
- data/lib/ruby_cdk/schema/validator/scans.rb +146 -0
- data/lib/ruby_cdk/schema/validator/structure.rb +469 -0
- data/lib/ruby_cdk/schema/validator/template.rb +203 -0
- data/lib/ruby_cdk/schema/validator/template_rules.rb +70 -0
- data/lib/ruby_cdk/schema/validator/walk.rb +122 -0
- data/lib/ruby_cdk/schema/validator.rb +159 -0
- data/lib/ruby_cdk/schema.rb +19 -0
- data/lib/ruby_cdk/synth/manifest.rb +128 -0
- data/lib/ruby_cdk/synth/resolver.rb +326 -0
- data/lib/ruby_cdk/synth/rules.rb +83 -0
- data/lib/ruby_cdk/synth/serializer.rb +120 -0
- data/lib/ruby_cdk/synth/writer.rb +31 -0
- data/lib/ruby_cdk/synth.rb +255 -0
- data/lib/ruby_cdk/test/capture.rb +133 -0
- data/lib/ruby_cdk/test/match.rb +426 -0
- data/lib/ruby_cdk/test/matcher.rb +290 -0
- data/lib/ruby_cdk/test/private/absent.rb +34 -0
- data/lib/ruby_cdk/test/private/conditions.rb +39 -0
- data/lib/ruby_cdk/test/private/cyclic.rb +160 -0
- data/lib/ruby_cdk/test/private/error.rb +48 -0
- data/lib/ruby_cdk/test/private/js.rb +326 -0
- data/lib/ruby_cdk/test/private/mappings.rb +39 -0
- data/lib/ruby_cdk/test/private/outputs.rb +39 -0
- data/lib/ruby_cdk/test/private/parameters.rb +39 -0
- data/lib/ruby_cdk/test/private/resources.rb +116 -0
- data/lib/ruby_cdk/test/private/section.rb +98 -0
- data/lib/ruby_cdk/test/private/sorting.rb +40 -0
- data/lib/ruby_cdk/test/private/sparse_matrix.rb +35 -0
- data/lib/ruby_cdk/test/private/type.rb +23 -0
- data/lib/ruby_cdk/test/template.rb +364 -0
- data/lib/ruby_cdk/test.rb +116 -0
- data/lib/ruby_cdk/values.rb +8 -0
- data/lib/ruby_cdk/version.rb +5 -0
- data/lib/ruby_cdk.rb +47 -0
- metadata +256 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../rule"
|
|
4
|
+
|
|
5
|
+
module RubyCdk
|
|
6
|
+
module Nag
|
|
7
|
+
module Rules
|
|
8
|
+
# RC-STATE-001. A stateful resource is compliant when its DeletionPolicy
|
|
9
|
+
# is not Delete: deleting the stack must not destroy its data.
|
|
10
|
+
#
|
|
11
|
+
# This rule is this gem's own. Neither cdk-nag
|
|
12
|
+
# (da5bed77c1b08e584e3de2d67534c76871af957f) nor cfn-nag
|
|
13
|
+
# (8b5f03da74202ba323a145e9d037ddce6cab9dec) reads DeletionPolicy. It
|
|
14
|
+
# states, for a template as it is written, the safe default of
|
|
15
|
+
# architecture section 7 (D-007), which synthesis applies for its own
|
|
16
|
+
# templates.
|
|
17
|
+
#
|
|
18
|
+
# The resource types are the stateful ones, given by the configuration
|
|
19
|
+
# (Config#stateful_types), so that the list is the same everywhere in the
|
|
20
|
+
# tool. applies_to is therefore not declared.
|
|
21
|
+
#
|
|
22
|
+
# What is not reported: a stateful resource with no DeletionPolicy at
|
|
23
|
+
# all, which CloudFormation also deletes. The wave card's rule is the
|
|
24
|
+
# written policy "Delete"; synthesis never leaves a stateful resource
|
|
25
|
+
# without one.
|
|
26
|
+
class StatefulDeletionPolicy < Rule
|
|
27
|
+
id "RC-STATE-001"
|
|
28
|
+
description "Stateful resource is not deleted with its stack"
|
|
29
|
+
severity :high
|
|
30
|
+
|
|
31
|
+
DELETION_POLICY = "DeletionPolicy"
|
|
32
|
+
DELETE = "Delete"
|
|
33
|
+
|
|
34
|
+
def applicable?
|
|
35
|
+
config.stateful_types.include?(type)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def audit
|
|
39
|
+
check(DELETION_POLICY) do
|
|
40
|
+
violate(DELETION_POLICY, "is a stateful resource with DeletionPolicy Delete: retaining it keeps its data when the stack is deleted") if values.string(DELETION_POLICY) == DELETE
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "rule"
|
|
4
|
+
require_relative "rules/api_gateway_authorization"
|
|
5
|
+
require_relative "rules/dynamodb_point_in_time_recovery"
|
|
6
|
+
require_relative "rules/ebs_volume_encrypted"
|
|
7
|
+
require_relative "rules/ec2_ingress_http"
|
|
8
|
+
require_relative "rules/ec2_ingress_other_ports"
|
|
9
|
+
require_relative "rules/iam_policy_admin_access"
|
|
10
|
+
require_relative "rules/iam_policy_full_access"
|
|
11
|
+
require_relative "rules/iam_user_credentials"
|
|
12
|
+
require_relative "rules/lambda_runtime_supported"
|
|
13
|
+
require_relative "rules/lambda_secret_environment"
|
|
14
|
+
require_relative "rules/log_group_retention"
|
|
15
|
+
require_relative "rules/rds_storage_encrypted"
|
|
16
|
+
require_relative "rules/resource_policy_wildcard_principal"
|
|
17
|
+
require_relative "rules/s3_public_access_block"
|
|
18
|
+
require_relative "rules/stateful_deletion_policy"
|
|
19
|
+
|
|
20
|
+
module RubyCdk
|
|
21
|
+
module Nag
|
|
22
|
+
# The rule pack: v0.1, one class per rule.
|
|
23
|
+
module Rules
|
|
24
|
+
# Every rule, in id order. A rule that is not in this list does not run.
|
|
25
|
+
ALL = [
|
|
26
|
+
BucketPublicAccessBlock,
|
|
27
|
+
ResourcePolicyWildcardPrincipal,
|
|
28
|
+
IamPolicyAdminAccess,
|
|
29
|
+
IamPolicyFullAccess,
|
|
30
|
+
IamUserCredentials,
|
|
31
|
+
Ec2IngressOtherPorts,
|
|
32
|
+
Ec2IngressHttp,
|
|
33
|
+
LambdaRuntimeSupported,
|
|
34
|
+
LambdaSecretEnvironment,
|
|
35
|
+
DynamodbPointInTimeRecovery,
|
|
36
|
+
EbsVolumeEncrypted,
|
|
37
|
+
RdsStorageEncrypted,
|
|
38
|
+
ApiGatewayAuthorization,
|
|
39
|
+
StatefulDeletionPolicy,
|
|
40
|
+
LogGroupRetention
|
|
41
|
+
].sort_by(&:id).freeze
|
|
42
|
+
|
|
43
|
+
class << self
|
|
44
|
+
# Every rule id, in ALL's order.
|
|
45
|
+
def ids
|
|
46
|
+
ALL.map(&:id)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyCdk
|
|
4
|
+
module Nag
|
|
5
|
+
# Raised inside a rule, by a value reader, when the value a check needs is
|
|
6
|
+
# not a literal. It never leaves the rule: Rule#check turns it into a Skipped.
|
|
7
|
+
class Undecided < StandardError
|
|
8
|
+
attr_reader :path, :reason
|
|
9
|
+
|
|
10
|
+
# reason completes the sentence that starts with path: "is a Ref", "is not a string".
|
|
11
|
+
def initialize(path, reason)
|
|
12
|
+
@path = path
|
|
13
|
+
@reason = reason
|
|
14
|
+
super("#{path} #{reason}")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The value helper. It tells a literal from a dynamic value the same way for
|
|
19
|
+
# every rule, so that no rule decides on its own what a Ref is.
|
|
20
|
+
#
|
|
21
|
+
# A dynamic value is an intrinsic function: a Hash with a "Ref" key, or with
|
|
22
|
+
# any key that starts with "Fn::", and anything that contains one. A rule
|
|
23
|
+
# that needs a literal and meets one skips that check and counts it.
|
|
24
|
+
#
|
|
25
|
+
# One intrinsic is the exception. {"Ref" => "AWS::NoValue"} removes the
|
|
26
|
+
# property it is the value of, so it is read as "absent", never as dynamic:
|
|
27
|
+
# the same reading cfn-nag's BooleanBaseRule gives it (spec/test_templates
|
|
28
|
+
# use it for exactly that).
|
|
29
|
+
#
|
|
30
|
+
# So is one Fn::Sub. An Fn::Sub whose string holds no ${...} variable
|
|
31
|
+
# resolves to that string, so it is read as the literal it is; only an
|
|
32
|
+
# Fn::Sub with variables is dynamic.
|
|
33
|
+
module Values
|
|
34
|
+
# The property this is the value of is left out of the resource.
|
|
35
|
+
NO_VALUE = { "Ref" => "AWS::NoValue" }.freeze
|
|
36
|
+
|
|
37
|
+
# The dynamic references CloudFormation resolves to a secret at deploy
|
|
38
|
+
# time. cfn-nag's enforce_string_or_dynamic_reference.rb exempts exactly
|
|
39
|
+
# these two, and nothing else.
|
|
40
|
+
SECRET_DYNAMIC_REFERENCE_PREFIXES = [ "{{resolve:secretsmanager:", "{{resolve:ssm-secure:" ].freeze
|
|
41
|
+
|
|
42
|
+
# A ${...} in an Fn::Sub string that stands for a variable. ${!Word} is
|
|
43
|
+
# how a literal ${Word} is written, and is not one.
|
|
44
|
+
SUB_VARIABLE = /\$\{(?!!)[^}]*\}/.freeze
|
|
45
|
+
|
|
46
|
+
# Longest form of a value in a message or a reason.
|
|
47
|
+
PREVIEW_LENGTH = 60
|
|
48
|
+
|
|
49
|
+
module_function
|
|
50
|
+
|
|
51
|
+
# True only for an intrinsic function itself: {"Ref" => ...} or {"Fn::..." => ...}.
|
|
52
|
+
# One is the exception: an Fn::Sub whose string holds no variable is
|
|
53
|
+
# the String it holds, and is not dynamic.
|
|
54
|
+
def intrinsic?(value)
|
|
55
|
+
return false if literal_sub?(value)
|
|
56
|
+
|
|
57
|
+
value.is_a?(Hash) && (value.key?("Ref") || value.keys.any? { |key| key.is_a?(String) && key.start_with?("Fn::") })
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# True when value, or anything inside it, is an intrinsic function.
|
|
61
|
+
def dynamic?(value)
|
|
62
|
+
return false if literal_sub?(value)
|
|
63
|
+
|
|
64
|
+
case value
|
|
65
|
+
when Array
|
|
66
|
+
value.any? { |item| dynamic?(item) }
|
|
67
|
+
when Hash
|
|
68
|
+
intrinsic?(value) || value.values.any? { |item| dynamic?(item) }
|
|
69
|
+
else
|
|
70
|
+
false
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The String an Fn::Sub without variables resolves to.
|
|
75
|
+
def literal_sub_string(value)
|
|
76
|
+
written = value["Fn::Sub"]
|
|
77
|
+
written.is_a?(Array) ? written.first : written
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# {"Fn::Sub" => "x"} or {"Fn::Sub" => ["x", ...]} where "x" holds no
|
|
81
|
+
# ${...} variable: CloudFormation leaves the string as it is.
|
|
82
|
+
def literal_sub?(value)
|
|
83
|
+
return false unless value.is_a?(Hash) && value.keys == [ "Fn::Sub" ]
|
|
84
|
+
|
|
85
|
+
text = value["Fn::Sub"].is_a?(Array) ? value["Fn::Sub"].first : value["Fn::Sub"]
|
|
86
|
+
text.is_a?(String) && !SUB_VARIABLE.match?(text)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# True when value removes the property it is written in.
|
|
90
|
+
def no_value?(value)
|
|
91
|
+
value == NO_VALUE
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# True for a String that CloudFormation replaces with the value of a
|
|
95
|
+
# secret: not a literal in the clear, whatever the template says.
|
|
96
|
+
def secret_dynamic_reference?(value)
|
|
97
|
+
value.is_a?(String) && SECRET_DYNAMIC_REFERENCE_PREFIXES.any? { |prefix| value.start_with?(prefix) } && value.end_with?("}}")
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# A short form of a value for a message. Never raises.
|
|
101
|
+
def preview(value)
|
|
102
|
+
text = value.inspect
|
|
103
|
+
text.length > PREVIEW_LENGTH ? "#{text[0, PREVIEW_LENGTH]}..." : text
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Reads the literal values of one resource, or of one part of it, by path.
|
|
108
|
+
#
|
|
109
|
+
# A path is written with the template's own keys: "Properties.Encrypted",
|
|
110
|
+
# "Properties.SecurityGroupIngress[1].CidrIp". A reader that was positioned
|
|
111
|
+
# at a path (at, sub) reads relative to it.
|
|
112
|
+
#
|
|
113
|
+
# Every reader method returns nil for a value that is absent or is
|
|
114
|
+
# AWS::NoValue, and raises Undecided for one that is dynamic or is not of
|
|
115
|
+
# the kind asked for. A rule calls them inside Rule#check, which records
|
|
116
|
+
# the Undecided and carries on.
|
|
117
|
+
class Reader
|
|
118
|
+
# One step of a path: a key, and the index of a list item when the key ends in "[n]".
|
|
119
|
+
SEGMENT = /\A([^.\[\]]+)(?:\[(\d+)\])?\z/
|
|
120
|
+
INTEGER = /\A\s*-?\d+\s*\z/
|
|
121
|
+
|
|
122
|
+
# root: the Hash (or nil) the paths are read from. prefix: the path root stands for.
|
|
123
|
+
def initialize(root, prefix = "")
|
|
124
|
+
@root = root
|
|
125
|
+
@prefix = prefix
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# A reader for the Hash or the list item at path, with path as its prefix.
|
|
129
|
+
def at(path)
|
|
130
|
+
Reader.new(raw(path), join(@prefix, path))
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# A reader for a document that was parsed out of the template, such as a
|
|
134
|
+
# policy document written as a JSON string, filed under path.
|
|
135
|
+
def sub(path, document)
|
|
136
|
+
Reader.new(document, join(@prefix, path))
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# The path a reader at this position reads for path, written in the resource's own terms.
|
|
140
|
+
def full_path(path)
|
|
141
|
+
join(@prefix, path)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# The value at path as it is written: nil when absent or AWS::NoValue.
|
|
145
|
+
def raw(path)
|
|
146
|
+
walk(path, dynamic: :raise)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# The value at path without ever raising: an intrinsic comes back as it
|
|
150
|
+
# is written. For describing a finding, not for deciding one.
|
|
151
|
+
def peek(path)
|
|
152
|
+
walk(path, dynamic: :keep)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# True when a value other than AWS::NoValue is written at path.
|
|
156
|
+
def present?(path)
|
|
157
|
+
!raw(path).nil?
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# The String at path.
|
|
161
|
+
def string(path)
|
|
162
|
+
value = raw(path)
|
|
163
|
+
return nil if value.nil?
|
|
164
|
+
return value if value.is_a?(String)
|
|
165
|
+
|
|
166
|
+
raise Undecided.new(join(@prefix, path), "is #{Values.preview(value)}, not a string")
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# The Integer at path. Templates written before the schema was strict
|
|
170
|
+
# carry numbers as strings, so "22" reads as 22.
|
|
171
|
+
def integer(path)
|
|
172
|
+
value = raw(path)
|
|
173
|
+
return nil if value.nil?
|
|
174
|
+
return value if value.is_a?(Integer)
|
|
175
|
+
return Integer(value, 10) if value.is_a?(String) && INTEGER.match?(value)
|
|
176
|
+
|
|
177
|
+
raise Undecided.new(join(@prefix, path), "is #{Values.preview(value)}, not an integer")
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# true or false at path. "true" and "false", in any case, read as the
|
|
181
|
+
# booleans they name, as they do in cfn-nag's truthy?.
|
|
182
|
+
def boolean(path)
|
|
183
|
+
value = raw(path)
|
|
184
|
+
return nil if value.nil?
|
|
185
|
+
return value if value == true || value == false
|
|
186
|
+
if value.is_a?(String)
|
|
187
|
+
return true if value.strip.casecmp?("true")
|
|
188
|
+
return false if value.strip.casecmp?("false")
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
raise Undecided.new(join(@prefix, path), "is #{Values.preview(value)}, not a boolean")
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# The object (a Hash) at path.
|
|
195
|
+
def object(path)
|
|
196
|
+
value = raw(path)
|
|
197
|
+
return nil if value.nil?
|
|
198
|
+
return value if value.is_a?(Hash)
|
|
199
|
+
|
|
200
|
+
raise Undecided.new(join(@prefix, path), "is #{Values.preview(value)}, not an object")
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# The String at path, or every String of the list at path.
|
|
204
|
+
def strings(path)
|
|
205
|
+
value = raw(path)
|
|
206
|
+
return [] if value.nil?
|
|
207
|
+
return [ value ] if value.is_a?(String)
|
|
208
|
+
if value.is_a?(Array)
|
|
209
|
+
return value.map do |item|
|
|
210
|
+
item.is_a?(String) ? item : raise(Undecided.new(join(@prefix, path), "holds #{Values.preview(item)}, not a string"))
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
raise Undecided.new(join(@prefix, path), "is #{Values.preview(value)}, neither a string nor a list")
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# Every key of the Hash at path. An intrinsic among the values does not
|
|
218
|
+
# matter here: only the key is returned.
|
|
219
|
+
def keys(path)
|
|
220
|
+
value = raw(path)
|
|
221
|
+
return [] if value.nil?
|
|
222
|
+
return value.keys if value.is_a?(Hash) && value.keys.all?(String)
|
|
223
|
+
|
|
224
|
+
raise Undecided.new(join(@prefix, path), "is #{Values.preview(value)}, not an object with string keys")
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# The value written under key inside the Hash at path. For maps whose keys
|
|
228
|
+
# are the user's, such as a function's environment variables.
|
|
229
|
+
def entry(path, key)
|
|
230
|
+
container = object(path)
|
|
231
|
+
return nil if container.nil?
|
|
232
|
+
|
|
233
|
+
where = join(join(@prefix, path), key.to_s)
|
|
234
|
+
value = container[key]
|
|
235
|
+
return nil if value.nil? || Values.no_value?(value)
|
|
236
|
+
raise Undecided.new(where, "is #{Values.preview(value)}, a dynamic value") if Values.intrinsic?(value)
|
|
237
|
+
|
|
238
|
+
value
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# The paths of the items of the list at path, as this reader reads them.
|
|
242
|
+
# CloudFormation lets a list be written as one object when it holds one
|
|
243
|
+
# item, as a policy's Statement often is, so an object at path counts as
|
|
244
|
+
# a list of one. Absent: no paths.
|
|
245
|
+
def item_paths(path)
|
|
246
|
+
value = raw(path)
|
|
247
|
+
return [] if value.nil?
|
|
248
|
+
return value.each_index.map { |index| "#{path}[#{index}]" } if value.is_a?(Array)
|
|
249
|
+
return [ path ] if value.is_a?(Hash)
|
|
250
|
+
|
|
251
|
+
raise Undecided.new(full_path(path), "is #{Values.preview(value)}, neither a list nor an object")
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
private
|
|
255
|
+
|
|
256
|
+
# The value at path as written, or nil when a container on the way is
|
|
257
|
+
# missing. Each step reads a key, then the item of a list when the key
|
|
258
|
+
# The value at path as written, or nil when a container on the way is
|
|
259
|
+
# missing. Each step reads a key, then the item of a list when the key
|
|
260
|
+
# is written with "[n]". When a step meets an intrinsic function, :raise
|
|
261
|
+
# ends the read as undecided, and :keep returns the intrinsic as it is
|
|
262
|
+
# written, AWS::NoValue included.
|
|
263
|
+
def walk(path, dynamic:)
|
|
264
|
+
value = @root
|
|
265
|
+
where = @prefix
|
|
266
|
+
path.split(".").each do |part|
|
|
267
|
+
match = SEGMENT.match(part)
|
|
268
|
+
raise Undecided.new(full_path(path), "is not a path a reader understands") unless match
|
|
269
|
+
|
|
270
|
+
where = join(where, match[1])
|
|
271
|
+
value = read(value, match[1], where, dynamic, Hash)
|
|
272
|
+
if match[2]
|
|
273
|
+
where = "#{where}[#{match[2]}]"
|
|
274
|
+
value = read(value, Integer(match[2], 10), where, dynamic, Array)
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
value
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# The item under key inside container, which must be of the given class.
|
|
281
|
+
# AWS::NoValue reads as absent, except for :keep, which returns it.
|
|
282
|
+
def read(container, key, where, dynamic, klass)
|
|
283
|
+
return nil if container.nil?
|
|
284
|
+
return nil unless container.is_a?(klass)
|
|
285
|
+
|
|
286
|
+
value = container[key]
|
|
287
|
+
return nil if Values.no_value?(value) && dynamic != :keep
|
|
288
|
+
return Values.literal_sub_string(value) if Values.literal_sub?(value)
|
|
289
|
+
if Values.intrinsic?(value)
|
|
290
|
+
return value if dynamic == :keep
|
|
291
|
+
|
|
292
|
+
raise Undecided.new(where, "is #{Values.preview(value)}, a dynamic value")
|
|
293
|
+
end
|
|
294
|
+
value
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def join(prefix, path)
|
|
298
|
+
return path if prefix.empty?
|
|
299
|
+
return prefix if path.empty?
|
|
300
|
+
|
|
301
|
+
"#{prefix}.#{path}"
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
end
|
data/lib/ruby_cdk/nag.rb
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The security rules. This file is the component's entry: requiring it loads
|
|
4
|
+
# the whole rule pack, and nothing else of the gem but the errors.
|
|
5
|
+
|
|
6
|
+
require_relative "errors"
|
|
7
|
+
require_relative "nag/config"
|
|
8
|
+
require_relative "nag/engine"
|
|
9
|
+
|
|
10
|
+
module RubyCdk
|
|
11
|
+
module Nag
|
|
12
|
+
# The rules are v0.1: their ids are fixed, and the pack is listed in Rules::ALL.
|
|
13
|
+
RULES_VERSION = "0.1"
|
|
14
|
+
|
|
15
|
+
# The stateful resource types when the configuration names none: the list
|
|
16
|
+
# of architecture section 7 (D-007).
|
|
17
|
+
STATEFUL_TYPES = Config::DEFAULT_STATEFUL_TYPES
|
|
18
|
+
|
|
19
|
+
class << self
|
|
20
|
+
# Checks one template, a plain Hash in CloudFormation syntax with the
|
|
21
|
+
# keys as the serializer writes them, and returns a Report.
|
|
22
|
+
#
|
|
23
|
+
# config: a Config, or a Hash written like .ruby-cdk/nag.yml, or nil for
|
|
24
|
+
# the default. path: where the template is, which paths_to_suppress is
|
|
25
|
+
# matched against; a check of one template has no stack name.
|
|
26
|
+
def check(template, config: nil, path: nil)
|
|
27
|
+
Engine.new.check(template, config: config, locations: [ path ])
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Checks every stack and returns { stack name => Report }, in the order
|
|
31
|
+
# given. templates_by_stack_name maps a stack name to its template.
|
|
32
|
+
#
|
|
33
|
+
# paths optionally maps a stack name to where its template is. The globs
|
|
34
|
+
# of paths_to_suppress are matched against each stack's name and its
|
|
35
|
+
# path.
|
|
36
|
+
def check_all(templates_by_stack_name, config: nil, paths: {})
|
|
37
|
+
unless templates_by_stack_name.is_a?(Hash)
|
|
38
|
+
raise ArgumentError, "templates_by_stack_name must map a stack name to its template, not a #{templates_by_stack_name.class}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
engine = Engine.new
|
|
42
|
+
templates_by_stack_name.to_h do |stack_name, template|
|
|
43
|
+
[ stack_name, engine.check(template, config: config, locations: [ stack_name, paths[stack_name] ]) ]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The configuration of the project whose root is root: its
|
|
48
|
+
# .ruby-cdk/nag.yml, or the default when there is none. Raises
|
|
49
|
+
# ConfigError when the file cannot be understood.
|
|
50
|
+
def load_config(root = ".")
|
|
51
|
+
Config.load(root)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Every rule class of the pack, in id order.
|
|
55
|
+
def rules
|
|
56
|
+
Rules::ALL
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../schema"
|
|
4
|
+
require_relative "validator/template"
|
|
5
|
+
|
|
6
|
+
module RubyCdk
|
|
7
|
+
module Validation
|
|
8
|
+
# The normalizer of architecture §6: the template with every key CloudFormation
|
|
9
|
+
# knows written under the name CloudFormation knows it by, and every other key exactly
|
|
10
|
+
# as it was written. It answers a new Hash and changes nothing it was given.
|
|
11
|
+
#
|
|
12
|
+
# What the CDK does was read first, at commit 68b2516c09791d78b199f30e455e4670dc9cde6.
|
|
13
|
+
# It resolves no key: the fromCloudFormation of its generated classes match a
|
|
14
|
+
# CloudFormation name exactly, and a property they do not know is kept as written
|
|
15
|
+
# (packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts,
|
|
16
|
+
# FromCloudFormationPropertyObject.addUnrecognizedPropertiesAsExtra), which is why
|
|
17
|
+
# its own test accepts properties that are not in the specification
|
|
18
|
+
# (cloudformation-include/test/test-templates/properties-not-in-cfn-spec.json). The
|
|
19
|
+
# resolution of snake_case is this tool's own, and architecture §6 is its rule:
|
|
20
|
+
#
|
|
21
|
+
# 1. An exact match with a property name is used as is.
|
|
22
|
+
# 2. Otherwise the key is matched without regard to case and to underscores. Exactly
|
|
23
|
+
# one match: that property's name is written.
|
|
24
|
+
# 3. No match, or several: the key is left as written, for rule V3 or V4 to report.
|
|
25
|
+
#
|
|
26
|
+
# Verbatim zones, where nothing is resolved and a symbol becomes a string: a property of
|
|
27
|
+
# kind :json (the policy documents), the Metadata of a resource, and everything under
|
|
28
|
+
# a type the model does not know (rule V1), since nothing can say what its keys mean.
|
|
29
|
+
module Normalizer
|
|
30
|
+
# The outcomes of key resolution, as resolve answers them.
|
|
31
|
+
OUTCOMES = %i[exact unique none ambiguous].freeze
|
|
32
|
+
|
|
33
|
+
# How a node's property names fold, kept per node. The nodes are the model's own
|
|
34
|
+
# frozen objects, which live as long as the model does.
|
|
35
|
+
FOLDED = {}.compare_by_identity
|
|
36
|
+
|
|
37
|
+
module_function
|
|
38
|
+
|
|
39
|
+
# What the pipeline calls: the normalized template of the context.
|
|
40
|
+
def call(context)
|
|
41
|
+
normalize(context.template)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The template with the Properties of every resource normalized.
|
|
45
|
+
def normalize(template)
|
|
46
|
+
return template unless template.is_a?(Hash)
|
|
47
|
+
|
|
48
|
+
template.to_h do |section, value|
|
|
49
|
+
[ section, section == "Resources" ? resources(value) : value ]
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# How a key names a property of a node: the outcome (one of OUTCOMES) and the
|
|
54
|
+
# property it names, or nil when it names none or cannot be told from another.
|
|
55
|
+
def resolve(key, node)
|
|
56
|
+
property = node.properties[key]
|
|
57
|
+
return [ :exact, property ] if property
|
|
58
|
+
|
|
59
|
+
matches = folded(node)[Template.fold(key)]
|
|
60
|
+
case matches&.size
|
|
61
|
+
when 1 then [ :unique, node.properties[matches.first] ]
|
|
62
|
+
when nil, 0 then [ :none, nil ]
|
|
63
|
+
else [ :ambiguous, nil ]
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The property a key names, written exactly or in snake_case, or nil.
|
|
68
|
+
def lookup(node, key)
|
|
69
|
+
node.properties[key] || resolve(key, node)[1]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def folded(node)
|
|
73
|
+
FOLDED[node] ||= node.properties.keys.group_by { |name| Template.fold(name) }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def resources(section)
|
|
77
|
+
return section unless section.is_a?(Hash)
|
|
78
|
+
|
|
79
|
+
section.to_h do |name, body|
|
|
80
|
+
[ name, body.is_a?(Hash) ? resource(body) : body ]
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def resource(body)
|
|
85
|
+
body.to_h do |attribute, value|
|
|
86
|
+
case attribute
|
|
87
|
+
when "Properties" then [ attribute, properties(value, Template.resource_type(body)) ]
|
|
88
|
+
when "Metadata" then [ attribute, verbatim(value) ]
|
|
89
|
+
else [ attribute, value ]
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# A type the model does not know, or knows no property of, is written verbatim.
|
|
95
|
+
def properties(value, type)
|
|
96
|
+
schema = type && Schema.type(type)
|
|
97
|
+
schema && !schema.properties.empty? ? node(schema, schema, value) : verbatim(value)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# The keys of an object under a node the model describes.
|
|
101
|
+
def node(type, current, value)
|
|
102
|
+
return verbatim(value) unless value.is_a?(Hash) && !Template.intrinsic?(value)
|
|
103
|
+
|
|
104
|
+
value.to_h do |key, item|
|
|
105
|
+
outcome, property = resolve(key.to_s, current)
|
|
106
|
+
named = outcome == :exact || outcome == :unique ? property.name : key.to_s
|
|
107
|
+
[ named, property ? descend(type, property, item) : verbatim(item) ]
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# A property's value: verbatim under :json, normalized under :nested, a list or a map
|
|
112
|
+
# of objects, and as it is everywhere else.
|
|
113
|
+
def descend(type, property, value)
|
|
114
|
+
return verbatim(value) if property.kind == :json
|
|
115
|
+
|
|
116
|
+
in_branches(value) do |item|
|
|
117
|
+
next item if Template.intrinsic?(item)
|
|
118
|
+
next item unless item.is_a?(Hash) || item.is_a?(Array)
|
|
119
|
+
|
|
120
|
+
definition = type.definitions[property.definition]
|
|
121
|
+
next item unless definition
|
|
122
|
+
|
|
123
|
+
case [ property.kind, property.item_kind ]
|
|
124
|
+
when [ :nested, nil ]
|
|
125
|
+
item.is_a?(Hash) ? node(type, definition, item) : item
|
|
126
|
+
when [ :list, :nested ]
|
|
127
|
+
item.is_a?(Array) ? item.map { |element| object(type, definition, element) } : item
|
|
128
|
+
when [ :map, :nested ]
|
|
129
|
+
item.is_a?(Hash) ? item.to_h { |key, element| [ key.to_s, object(type, definition, element) ] } : item
|
|
130
|
+
else item
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def object(type, definition, value)
|
|
136
|
+
in_branches(value) { |item| item.is_a?(Hash) && !Template.intrinsic?(item) ? node(type, definition, item) : item }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# The value with each branch of its Fn::If replaced by what the block makes of it,
|
|
140
|
+
# branches of branches included. The value itself, block untouched, when it is no Fn::If
|
|
141
|
+
# of three parts.
|
|
142
|
+
def in_branches(value)
|
|
143
|
+
return yield(value) unless Template.function?(value, "Fn::If")
|
|
144
|
+
|
|
145
|
+
list = value["Fn::If"]
|
|
146
|
+
return value unless list.is_a?(Array) && list.size == 3
|
|
147
|
+
|
|
148
|
+
branches = list[1, 2]
|
|
149
|
+
return value unless branches.is_a?(Array)
|
|
150
|
+
|
|
151
|
+
value.merge("Fn::If" => [ list[0], *branches.map { |branch| in_branches(branch) { |inner| yield inner } } ])
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Keys exactly as written, symbols as strings, all the way down.
|
|
155
|
+
def verbatim(value)
|
|
156
|
+
case value
|
|
157
|
+
when Hash then value.to_h { |key, item| [ key.to_s, verbatim(item) ] }
|
|
158
|
+
when Array then value.map { |item| verbatim(item) }
|
|
159
|
+
else value
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyCdk
|
|
4
|
+
module Schema
|
|
5
|
+
# One property of a resource type or of a nested property type. A value object:
|
|
6
|
+
# frozen, and equal to another with the same members.
|
|
7
|
+
#
|
|
8
|
+
# name the CloudFormation name, "KmsKeyId"
|
|
9
|
+
# kind :string, :integer, :long, :double, :boolean, :timestamp, :json, :list, :map or :nested
|
|
10
|
+
# item_kind for :list and :map, the kind of each element; nil for the other kinds
|
|
11
|
+
# definition the key of TypeSchema#definitions that describes the value. Set when kind
|
|
12
|
+
# is :nested, and when item_kind is :nested (then it describes each element)
|
|
13
|
+
# required true when the enclosing type lists the property as required
|
|
14
|
+
# update_behaviour :mutable, :immutable, :conditional, or :unknown when no source states it
|
|
15
|
+
# allowed_values the literals the source allows, or nil when it lists none or leaves the choice open
|
|
16
|
+
# pattern what a literal string must match, or nil when the source has no single pattern
|
|
17
|
+
# unique_items for :list: whether duplicates are allowed, or nil when no source says
|
|
18
|
+
#
|
|
19
|
+
# For a :list or a :map, allowed_values and pattern apply to each element.
|
|
20
|
+
PropertySchema = Data.define(:name, :kind, :item_kind, :definition, :required, :update_behaviour, :allowed_values, :pattern, :unique_items)
|
|
21
|
+
end
|
|
22
|
+
end
|