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,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../schema"
|
|
4
|
+
require_relative "rule"
|
|
5
|
+
require_relative "template"
|
|
6
|
+
|
|
7
|
+
module RubyCdk
|
|
8
|
+
module Validation
|
|
9
|
+
# Everything one validation of one template found, in one place: every issue, with
|
|
10
|
+
# the suggestions its rule made and, for an issue about a resource, the page AWS
|
|
11
|
+
# documents the type on. Architecture §6 names this report; the pipeline raises its
|
|
12
|
+
# own ValidationError from the issues, and a command that reports rather than fails
|
|
13
|
+
# prints this.
|
|
14
|
+
class Report
|
|
15
|
+
attr_reader :issues, :errors, :warnings, :template
|
|
16
|
+
|
|
17
|
+
# issues: every Issue and Warning of every rule, in order. template: the template
|
|
18
|
+
# they are about.
|
|
19
|
+
def initialize(issues, template)
|
|
20
|
+
@issues = issues.freeze
|
|
21
|
+
@template = template
|
|
22
|
+
@errors = issues.reject { |issue| Validation.warning?(issue) }.freeze
|
|
23
|
+
@warnings = issues.select { |issue| Validation.warning?(issue) }.freeze
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# True when nothing keeps the template from being written.
|
|
27
|
+
def ok?
|
|
28
|
+
errors.empty?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def empty?
|
|
32
|
+
issues.empty?
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# The issues of one rule, by its id.
|
|
36
|
+
def of_rule(id)
|
|
37
|
+
issues.select { |issue| issue.rule == id }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# [ [ rule id, issues ], ... ] in the order of the template.
|
|
41
|
+
def by_rule
|
|
42
|
+
RULES.keys.filter_map { |id| found = of_rule(id); [ id, found ] unless found.empty? }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# [ rule, path, message, hint, documentation_url ], for a command that prints its own.
|
|
46
|
+
def rows
|
|
47
|
+
issues.map do |issue|
|
|
48
|
+
[ issue.rule, issue.path, issue.message, issue.hint, documentation_url(issue) ]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The page AWS documents the type of the resource an issue is about, or nil.
|
|
53
|
+
def documentation_url(issue)
|
|
54
|
+
name = issue.path.match(/\AResources\.([A-Za-z0-9]+)\./)&.captures&.first
|
|
55
|
+
type = Template.resource_type(Template.section(template, "Resources")[name])
|
|
56
|
+
type && Schema.type(type)&.documentation_url
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# The whole report, one issue after another:
|
|
60
|
+
#
|
|
61
|
+
# Validation failed with 2 errors and 1 warning:
|
|
62
|
+
# V3 Resources.Table.Properties.KeySchem: "KeySchem" is not a property of AWS::DynamoDB::Table; did you mean KeySchema?
|
|
63
|
+
# write the CloudFormation name, or in snake_case: key_schema
|
|
64
|
+
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html
|
|
65
|
+
def to_s
|
|
66
|
+
return "Validation passed: nothing to report.\n" if empty?
|
|
67
|
+
|
|
68
|
+
[ summary, *issues.flat_map { |issue| lines_of(issue) } ].join("\n") + "\n"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def summary
|
|
74
|
+
return "Validation passed with #{count(warnings.size, "warning")}:" if errors.empty?
|
|
75
|
+
|
|
76
|
+
"Validation failed with #{count(errors.size, "error")}#{warnings.empty? ? "" : " and #{count(warnings.size, "warning")}"}:"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def count(number, word)
|
|
80
|
+
"#{number} #{word}#{number == 1 ? "" : "s"}"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def lines_of(issue)
|
|
84
|
+
url = documentation_url(issue)
|
|
85
|
+
lines = [ "#{issue.rule} #{issue.path}: #{issue.message}#{Validation.warning?(issue) ? " (warning)" : ""}" ]
|
|
86
|
+
lines << " #{issue.hint}" if issue.hint
|
|
87
|
+
lines << " #{url}" if url && !issue.hint.to_s.include?(url)
|
|
88
|
+
lines
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../errors"
|
|
4
|
+
require_relative "template"
|
|
5
|
+
|
|
6
|
+
module RubyCdk
|
|
7
|
+
module Validation
|
|
8
|
+
# One warning. The pipeline fails a synthesis on every Issue a validator answers, so
|
|
9
|
+
# a warning has to be told apart from an error: it is an Issue (everything that reads
|
|
10
|
+
# one reads the other) that also answers warning? with true.
|
|
11
|
+
class Warning < Issue
|
|
12
|
+
def warning?
|
|
13
|
+
true
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# :warning here, :error for every other Issue.
|
|
17
|
+
def severity
|
|
18
|
+
:warning
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# True for a Warning, false for every other value: the one place that decides.
|
|
23
|
+
def self.warning?(issue)
|
|
24
|
+
issue.is_a?(Warning)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# What every rule is: an id and call. A rule never raises for a bad template: it
|
|
28
|
+
# answers the issues it found, every one of them.
|
|
29
|
+
class Rule
|
|
30
|
+
class << self
|
|
31
|
+
# The id of the rule, "V3". The class body sets it once, with `rule "V3"`.
|
|
32
|
+
attr_reader :id
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def rule(id)
|
|
37
|
+
@id = id
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def id
|
|
42
|
+
self.class.id
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The issues the rule finds in context.template: an Array of Issue and Warning, in the order of the template.
|
|
46
|
+
def call(_context)
|
|
47
|
+
raise NotImplementedError, "#{self.class} finds nothing"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# path is an Array of segments, or the path already as text.
|
|
53
|
+
def error(path, message, hint: nil)
|
|
54
|
+
Issue.new(rule: id, path: at(path), message:, hint:)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def warning(path, message, hint: nil)
|
|
58
|
+
Warning.new(rule: id, path: at(path), message:, hint:)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def at(path)
|
|
62
|
+
path.is_a?(Array) ? Template.path(path) : path
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Words for a message: the names closest to one that is wrong, and how lists read.
|
|
67
|
+
module Text
|
|
68
|
+
module_function
|
|
69
|
+
|
|
70
|
+
# "a, b and c", for a message that lists.
|
|
71
|
+
def list(items)
|
|
72
|
+
items = items.map(&:to_s)
|
|
73
|
+
case items.size
|
|
74
|
+
when 0 then ""
|
|
75
|
+
when 1 then items.first
|
|
76
|
+
else "#{items[0..-2].join(', ')} and #{items.last}"
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# How far a name may be from another to be offered in its place.
|
|
81
|
+
def near?(distance, length)
|
|
82
|
+
distance <= (length < 8 ? 2 : 3)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The names nearest to the one given, nearest first: those it is a suffix of, then
|
|
86
|
+
# those within a small edit distance inside the same service ("AWS::S3"). At most
|
|
87
|
+
# limit names, and none when nothing is close.
|
|
88
|
+
def closest(given, names, limit: 3)
|
|
89
|
+
service = given.include?("::") ? given[0, given.rindex("::")] : ""
|
|
90
|
+
scored = names.reject { |name| name == given }.filter_map do |name|
|
|
91
|
+
scope = service.empty? || name.start_with?("#{service}::") ? 0 : 1
|
|
92
|
+
# The distance of a name of another length entirely is not worth computing.
|
|
93
|
+
next if scope == 1 && (name.size - given.size).abs > 3
|
|
94
|
+
|
|
95
|
+
[ name.end_with?(given) ? 0 : 1, scope, distance(Template.fold(given), Template.fold(name)), name ]
|
|
96
|
+
end
|
|
97
|
+
suffixes, others = scored.partition { |suffix, _, _, _| suffix.zero? }
|
|
98
|
+
near = others.select { |_suffix, scope, edit, name| scope.zero? && near?(edit, name.size) }
|
|
99
|
+
near = others.select { |_suffix, _scope, edit, name| near?(edit, name.size) } if near.empty?
|
|
100
|
+
(suffixes.map(&:last) + near.sort_by { |_suffix, _scope, edit, name| [ edit, name ] }.map(&:last)).uniq.first(limit)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# The smallest number of single-character edits that turns one string into the other.
|
|
104
|
+
def distance(left, right)
|
|
105
|
+
row = (0..right.size).to_a
|
|
106
|
+
left.each_char.with_index(1) do |char, i|
|
|
107
|
+
previous = row[0]
|
|
108
|
+
row[0] = i
|
|
109
|
+
right.each_char.with_index(1) do |other, j|
|
|
110
|
+
current = row[j]
|
|
111
|
+
row[j] = [ row[j] + 1, row[j - 1] + 1, previous + (char == other ? 0 : 1) ].min
|
|
112
|
+
previous = current
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
row.last
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../schema"
|
|
4
|
+
require_relative "../normalizer"
|
|
5
|
+
require_relative "template"
|
|
6
|
+
require_relative "walk"
|
|
7
|
+
|
|
8
|
+
module RubyCdk
|
|
9
|
+
module Validation
|
|
10
|
+
# The scans more than one rule of the schema needs: every resource, every key the
|
|
11
|
+
# normalizer could not resolve, every leaf value, and what kind of value a kind of
|
|
12
|
+
# property takes. Nothing here decides anything: the rules do.
|
|
13
|
+
module Scans
|
|
14
|
+
# The resources of a template, with what the model knows of them.
|
|
15
|
+
module Resources
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
# Yields [ name, body, path ] for every resource.
|
|
19
|
+
def each(context)
|
|
20
|
+
Template.resources(context.template).each { |name, body| yield name, body, [ "Resources", name.to_s ] }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Yields [ name, body, path, type, properties ] for every resource whose type the
|
|
24
|
+
# model knows and whose Properties is an object. path is that of the Properties.
|
|
25
|
+
#
|
|
26
|
+
def typed(context)
|
|
27
|
+
each(context) do |name, body, path|
|
|
28
|
+
type = Schema.type(Template.resource_type(body).to_s)
|
|
29
|
+
properties = body["Properties"]
|
|
30
|
+
next unless type && properties.is_a?(Hash)
|
|
31
|
+
|
|
32
|
+
yield name, body, path + [ "Properties" ], type, properties
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Every key under a typed resource's properties that is not a property's exact name:
|
|
38
|
+
# the one scan rules V3 and V4 share.
|
|
39
|
+
module Keys
|
|
40
|
+
module_function
|
|
41
|
+
|
|
42
|
+
# Yields [ key, outcome, names, segments, owner ]: the key as written, what the
|
|
43
|
+
# normalizer made of it (:unique, :none or :ambiguous), every property of what it
|
|
44
|
+
# sits on, where it is, and what it sits on.
|
|
45
|
+
def unresolved(context)
|
|
46
|
+
Resources.typed(context) do |_name, _body, path, type, properties|
|
|
47
|
+
next if type.additional_properties?
|
|
48
|
+
|
|
49
|
+
SchemaWalk.each_node(type, properties, path) do |walk_type, node, hash, at|
|
|
50
|
+
hash.each_key do |key|
|
|
51
|
+
outcome, = Normalizer.resolve(key.to_s, node)
|
|
52
|
+
next if outcome == :exact
|
|
53
|
+
|
|
54
|
+
yield key.to_s, outcome, node.properties.keys, at + [ key.to_s ], Template.node_name(walk_type, node)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Every value the model gives a kind: the value itself, then each literal element of
|
|
62
|
+
# a list or a map whose elements have a scalar kind. The one walk rules V6, V7, V17,
|
|
63
|
+
# V18 and V19 share.
|
|
64
|
+
module Leaves
|
|
65
|
+
SCALARS = %i[string integer long double boolean timestamp].freeze
|
|
66
|
+
|
|
67
|
+
module_function
|
|
68
|
+
|
|
69
|
+
# Yields [ type, node, property, kind, value, segments ]
|
|
70
|
+
def each(context)
|
|
71
|
+
Resources.typed(context) do |_name, _body, path, type, properties|
|
|
72
|
+
SchemaWalk.each_value(type, properties, path) do |walk_type, node, property, value, at|
|
|
73
|
+
yield walk_type, node, property, property.kind, value, at
|
|
74
|
+
next unless SCALARS.include?(property.item_kind)
|
|
75
|
+
|
|
76
|
+
if property.kind == :list && value.is_a?(Array)
|
|
77
|
+
value.each_with_index do |item, index|
|
|
78
|
+
yield walk_type, node, property, property.item_kind, item, at + [ "[#{index}]" ] if Template.literal?(item)
|
|
79
|
+
end
|
|
80
|
+
elsif property.kind == :map && value.is_a?(Hash)
|
|
81
|
+
value.each do |key, item|
|
|
82
|
+
yield walk_type, node, property, property.item_kind, item, at + [ key.to_s ] if Template.literal?(item)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# What kind of value each kind of property takes (V6), and what is written in its
|
|
91
|
+
# place wrongly: a number or a boolean in quotes (V17).
|
|
92
|
+
module Kind
|
|
93
|
+
WHOLE = /\A\s*-?\d+\s*\z/
|
|
94
|
+
DECIMAL = /\A\s*-?\d+(\.\d+)?([eE][-+]?\d+)?\s*\z/
|
|
95
|
+
ARTICLES = { string: "a string", integer: "a whole number", long: "a whole number", double: "a number", boolean: "a boolean",
|
|
96
|
+
timestamp: "a timestamp", json: "an object", nested: "an object", map: "an object", list: "a list" }.freeze
|
|
97
|
+
HINTS = { string: "write it in quotes", integer: "write it without quotes and without decimals",
|
|
98
|
+
long: "write it without quotes and without decimals", double: "write it without quotes",
|
|
99
|
+
boolean: "write true or false, without quotes", timestamp: 'write an ISO 8601 string, as "2026-01-01T00:00:00Z"',
|
|
100
|
+
json: "write a Hash", nested: "write a Hash", map: "write a Hash" }.freeze
|
|
101
|
+
|
|
102
|
+
module_function
|
|
103
|
+
|
|
104
|
+
# :ok; :wrong for a value of another kind (V6); :string_for_number or
|
|
105
|
+
# :string_for_boolean for the right value in quotes (V17).
|
|
106
|
+
#
|
|
107
|
+
# A number or a boolean where a string belongs is :ok, as the CDK takes it
|
|
108
|
+
# (cloudformation-include/test/valid-templates.test.ts: "accepts numbers for
|
|
109
|
+
# properties with type string", "accepts booleans for properties with type
|
|
110
|
+
# string"), and CloudFormation does too.
|
|
111
|
+
def check(kind, value)
|
|
112
|
+
case kind
|
|
113
|
+
when :string
|
|
114
|
+
scalar?(value) ? :ok : :wrong
|
|
115
|
+
when :integer, :long
|
|
116
|
+
value.is_a?(Integer) ? :ok : value.is_a?(String) && value.match?(WHOLE) ? :string_for_number : :wrong
|
|
117
|
+
when :double
|
|
118
|
+
value.is_a?(Numeric) ? :ok : value.is_a?(String) && value.match?(DECIMAL) ? :string_for_number : :wrong
|
|
119
|
+
when :boolean
|
|
120
|
+
value == true || value == false ? :ok : value == "true" || value == "false" ? :string_for_boolean : :wrong
|
|
121
|
+
when :timestamp
|
|
122
|
+
value.is_a?(String) ? :ok : :wrong
|
|
123
|
+
when :json
|
|
124
|
+
value.is_a?(Hash) || value.is_a?(Array) ? :ok : :wrong
|
|
125
|
+
when :nested, :map
|
|
126
|
+
value.is_a?(Hash) ? :ok : :wrong
|
|
127
|
+
else
|
|
128
|
+
:ok
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def scalar?(value)
|
|
133
|
+
value.is_a?(String) || value.is_a?(Numeric) || value == true || value == false
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def article(kind)
|
|
137
|
+
ARTICLES[kind]
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def hint(kind)
|
|
141
|
+
HINTS[kind]
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|