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,630 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../errors"
|
|
4
|
+
require_relative "descriptor"
|
|
5
|
+
require_relative "specification_type"
|
|
6
|
+
|
|
7
|
+
module RubyCdk
|
|
8
|
+
module Schema
|
|
9
|
+
module Sources
|
|
10
|
+
# Compiles one resource type out of its provider schema, then merges in what the
|
|
11
|
+
# resource specification adds: the update behaviour of every property, and the odd
|
|
12
|
+
# property only it knows.
|
|
13
|
+
#
|
|
14
|
+
# The provider schema is a JSON Schema with CloudFormation's extensions. This is how
|
|
15
|
+
# a node becomes a kind:
|
|
16
|
+
#
|
|
17
|
+
# $ref resolved to its definition. A definition can be anything,
|
|
18
|
+
# a string with a pattern included, so it decides the kind.
|
|
19
|
+
# When a property and the definition it points to disagree
|
|
20
|
+
# on `type`, the definition wins.
|
|
21
|
+
# type string :string, or :timestamp with format date-time
|
|
22
|
+
# type integer :integer, or :long with format int64
|
|
23
|
+
# type number :double
|
|
24
|
+
# type boolean :boolean
|
|
25
|
+
# type [..] (a union) :json: every alternative is accepted and none is enforced
|
|
26
|
+
# type array :list; the element from `items`, its duplicates rule from uniqueItems
|
|
27
|
+
# patternProperties :map of what the patterns describe
|
|
28
|
+
# properties, or oneOf/anyOf of objects with properties
|
|
29
|
+
# :nested, with a definition that lists every property the
|
|
30
|
+
# node and its alternatives name
|
|
31
|
+
# any other object, an empty schema, a mix of an object with a scalar, a list or a map
|
|
32
|
+
# :json
|
|
33
|
+
#
|
|
34
|
+
# A definition keeps the name the provider gave it. An object defined inline, without
|
|
35
|
+
# a name, takes the name of the property that holds it, under the one that holds that:
|
|
36
|
+
# "RecoveryPointSelection.DateRange".
|
|
37
|
+
#
|
|
38
|
+
# Allowed values and patterns come from the node, from the definition it points to,
|
|
39
|
+
# and from a list's or a map's elements. Across oneOf and anyOf they survive only when
|
|
40
|
+
# every alternative states the same closed list, because the union of an enum with
|
|
41
|
+
# anything else is open.
|
|
42
|
+
class ProviderType
|
|
43
|
+
# An object with named properties before it becomes a definition. properties maps a
|
|
44
|
+
# name to every node that describes it: the alternatives of a oneOf may each
|
|
45
|
+
# describe the same property. name is the definition the provider gave it, if any.
|
|
46
|
+
Shape = Data.define(:name, :properties, :required)
|
|
47
|
+
|
|
48
|
+
# The type itself, in the tables below. A definition is there under its name.
|
|
49
|
+
ROOT = ""
|
|
50
|
+
|
|
51
|
+
COMPOSITION = %w[allOf oneOf anyOf].freeze
|
|
52
|
+
|
|
53
|
+
# What a path under /properties means for the property it ends at.
|
|
54
|
+
FLAGS = {
|
|
55
|
+
"createOnlyProperties" => :create_only,
|
|
56
|
+
"conditionalCreateOnlyProperties" => :conditional,
|
|
57
|
+
"readOnlyProperties" => :read_only
|
|
58
|
+
}.freeze
|
|
59
|
+
|
|
60
|
+
# The numeric kinds, narrowest first.
|
|
61
|
+
NUMERIC = %i[integer long double].freeze
|
|
62
|
+
|
|
63
|
+
# How a specification UpdateType is written in the model.
|
|
64
|
+
UPDATES = { "Immutable" => "immutable", "Conditional" => "conditional", "Mutable" => "mutable" }.freeze
|
|
65
|
+
|
|
66
|
+
MAX_REFERENCE_HOPS = 20
|
|
67
|
+
MAX_DEPTH = 100
|
|
68
|
+
|
|
69
|
+
def initialize(schema, specification)
|
|
70
|
+
@schema = schema
|
|
71
|
+
@specification = specification
|
|
72
|
+
@type_name = schema.fetch("typeName")
|
|
73
|
+
@shapes = {} # definition name => Shape
|
|
74
|
+
@origins = {} # definition name => :definition when the provider named it, :inline when this compiler did
|
|
75
|
+
@descriptors = {} # definition name (ROOT for the type) => { property name => Descriptor }
|
|
76
|
+
@pending = [] # definitions whose properties are not described yet
|
|
77
|
+
@flags = Hash.new { |hash, key| hash[key] = [] } # [definition name, property name] => the flags on that path
|
|
78
|
+
@specified = {} # [definition name, property name] => what the specification says about the property
|
|
79
|
+
@extra = nil # what only the specification knows, as SpecificationType::Extra
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The type as the model stores it.
|
|
83
|
+
def call
|
|
84
|
+
describe_all
|
|
85
|
+
read_flags
|
|
86
|
+
read_specification
|
|
87
|
+
build
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
# --- what every node is
|
|
93
|
+
|
|
94
|
+
def describe_all
|
|
95
|
+
register(root_shape, ROOT, :root)
|
|
96
|
+
describe_properties(@pending.shift) until @pending.empty?
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def root_shape
|
|
100
|
+
Shape.new(name: nil, properties: nodes_by_name(@schema["properties"]), required: Array(@schema["required"]))
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def nodes_by_name(properties)
|
|
104
|
+
properties.is_a?(Hash) ? properties.to_h { |name, node| [ name, [ node ] ] } : {}
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def describe_properties(name)
|
|
108
|
+
@descriptors[name] = @shapes.fetch(name).properties.to_h do |property, nodes|
|
|
109
|
+
described = nodes.map { |node| descriptor(node, hint_for(name, property)) }
|
|
110
|
+
[ property, described.reduce { |left, right| or_(left, right) } ]
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def hint_for(name, property)
|
|
115
|
+
name == ROOT ? property : "#{name}.#{property}"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Gives an object with named properties its definition: the name the provider gave
|
|
119
|
+
# it, or else the hint, which names the property that holds it. Returns the name of
|
|
120
|
+
# the definition. An object reached again is left alone unless it adds a property
|
|
121
|
+
# or drops a requirement, in which case both are merged and it is described again.
|
|
122
|
+
def register(shape, hint, origin)
|
|
123
|
+
name = shape.name || hint
|
|
124
|
+
existing = @shapes[name]
|
|
125
|
+
if existing.nil?
|
|
126
|
+
@shapes[name] = shape
|
|
127
|
+
@origins[name] = origin
|
|
128
|
+
@pending << name
|
|
129
|
+
elsif !same_shape?(existing, shape)
|
|
130
|
+
if @origins[name] != origin
|
|
131
|
+
raise RubyCdk::Error, "#{@type_name}: the provider schema has a definition called #{name} and an inline object would take the same name"
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
merged = or_shape(existing, shape)
|
|
135
|
+
unless same_shape?(existing, merged)
|
|
136
|
+
@shapes[name] = merged
|
|
137
|
+
@descriptors.delete(name)
|
|
138
|
+
@pending << name unless @pending.include?(name)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
name
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def same_shape?(left, right)
|
|
145
|
+
left.properties == right.properties && left.required == right.required
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# The object that satisfies either: every property, and only what both require.
|
|
149
|
+
def or_shape(left, right)
|
|
150
|
+
Shape.new(name: left.name == right.name ? left.name : nil,
|
|
151
|
+
properties: left.properties.merge(right.properties) { |_property, a, b| (a + b).uniq },
|
|
152
|
+
required: left.required & right.required)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# The object that satisfies both.
|
|
156
|
+
def and_shape(left, right)
|
|
157
|
+
Shape.new(name: left.name == right.name ? left.name : nil,
|
|
158
|
+
properties: left.properties.merge(right.properties) { |_property, a, b| (a + b).uniq },
|
|
159
|
+
required: (left.required + right.required).uniq)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def descriptor(node, hint, depth = 0)
|
|
163
|
+
to_descriptor(term_of(node, hint, depth), hint)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def to_descriptor(term, hint)
|
|
167
|
+
case term
|
|
168
|
+
when Shape
|
|
169
|
+
Descriptor.new(kind: :nested, item: nil, of: register(term, hint, term.name ? :definition : :inline), values: nil, pattern: nil, unique: nil)
|
|
170
|
+
when Descriptor
|
|
171
|
+
term
|
|
172
|
+
else
|
|
173
|
+
ANY_VALUE
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# What a node accepts, as one of: :any, when it states nothing; :any_object, an
|
|
178
|
+
# object with no named properties; a Shape; or a Descriptor.
|
|
179
|
+
def term_of(node, hint, depth = 0)
|
|
180
|
+
raise RubyCdk::Error, "#{@type_name}: schema nested deeper than #{MAX_DEPTH} at #{hint}" if depth > MAX_DEPTH
|
|
181
|
+
return :any unless node.is_a?(Hash)
|
|
182
|
+
|
|
183
|
+
node, name = dereference(node)
|
|
184
|
+
hint = name || hint
|
|
185
|
+
base = simple_term(node, hint, depth)
|
|
186
|
+
base = base.with(name:) if name && base.is_a?(Shape)
|
|
187
|
+
|
|
188
|
+
compositions = COMPOSITION.filter_map { |keyword| [ keyword, node[keyword] ] if node[keyword].is_a?(Array) && node[keyword].any? }
|
|
189
|
+
return base || :any if compositions.empty?
|
|
190
|
+
|
|
191
|
+
# allOf is a conjunction; oneOf and anyOf are, for a kind, the same disjunction.
|
|
192
|
+
compositions.reduce(base || :any) do |term, (keyword, alternatives)|
|
|
193
|
+
combined = alternatives.map { |alternative| and_(term, term_of(alternative, hint, depth + 1)) }
|
|
194
|
+
combined.reduce { |left, right| keyword == "allOf" ? and_(left, right) : or_(left, right) }
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def simple_term(node, hint, depth)
|
|
199
|
+
case node["type"]
|
|
200
|
+
when Array then Descriptor.new(kind: :json, item: nil, of: nil, values: node["enum"], pattern: node["pattern"], unique: nil)
|
|
201
|
+
when "array" then list_term(node, hint, depth)
|
|
202
|
+
when "object" then object_term(node, hint, depth)
|
|
203
|
+
when nil then untyped_term(node, hint, depth)
|
|
204
|
+
else scalar_term(node)
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def scalar_term(node)
|
|
209
|
+
kind = case node["type"]
|
|
210
|
+
when "string" then node["format"] == "date-time" ? :timestamp : :string
|
|
211
|
+
when "integer" then node["format"] == "int64" ? :long : :integer
|
|
212
|
+
when "number" then :double
|
|
213
|
+
when "boolean" then :boolean
|
|
214
|
+
end
|
|
215
|
+
return ANY_VALUE unless kind
|
|
216
|
+
|
|
217
|
+
Descriptor.new(kind:, item: nil, of: nil, values: node["enum"], pattern: node["pattern"], unique: nil)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# A node with no `type`, or with annotations only.
|
|
221
|
+
def untyped_term(node, hint, depth)
|
|
222
|
+
if node.key?("patternProperties")
|
|
223
|
+
map_term(node, node["patternProperties"].to_h.values, hint, depth)
|
|
224
|
+
elsif node.key?("properties")
|
|
225
|
+
object_term(node, hint, depth)
|
|
226
|
+
elsif node.key?("items")
|
|
227
|
+
list_term(node, hint, depth)
|
|
228
|
+
elsif node.key?("enum")
|
|
229
|
+
Descriptor.new(kind: kind_of_values(node["enum"]), item: nil, of: nil, values: node["enum"], pattern: node["pattern"], unique: nil)
|
|
230
|
+
elsif node["format"] == "date-time"
|
|
231
|
+
Descriptor.new(kind: :timestamp, item: nil, of: nil, values: nil, pattern: node["pattern"], unique: nil)
|
|
232
|
+
elsif node.key?("pattern")
|
|
233
|
+
Descriptor.new(kind: :string, item: nil, of: nil, values: nil, pattern: node["pattern"], unique: nil)
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def kind_of_values(values)
|
|
238
|
+
return :json unless values.is_a?(Array) && values.any?
|
|
239
|
+
|
|
240
|
+
values.all?(String) ? :string : values.all?(Integer) ? :integer : :json
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def list_term(node, hint, depth)
|
|
244
|
+
items = node["items"]
|
|
245
|
+
element = items.is_a?(Hash) ? descriptor(items, hint, depth + 1) : ANY_VALUE
|
|
246
|
+
Descriptor.new(kind: :list, item: element.kind, of: element.kind == :nested ? element.of : nil,
|
|
247
|
+
values: node["enum"] || element.values, pattern: node["pattern"] || element.pattern,
|
|
248
|
+
unique: node["uniqueItems"] == true ? true : node["uniqueItems"] == false ? false : nil)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def map_term(node, schemas, hint, depth)
|
|
252
|
+
elements = schemas.map { |schema| descriptor(schema, hint, depth + 1) }
|
|
253
|
+
element = elements.first || ANY_VALUE
|
|
254
|
+
return Descriptor.new(kind: :map, item: nil, of: nil, values: nil, pattern: nil, unique: nil) unless elements.all? { |other| other.kind == element.kind && other.of == element.of }
|
|
255
|
+
|
|
256
|
+
Descriptor.new(kind: :map, item: element.kind, of: element.of, values: node["enum"] || element.values, pattern: node["pattern"] || element.pattern, unique: nil)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def object_term(node, hint, depth)
|
|
260
|
+
return map_term(node, [ node["additionalProperties"] ], hint, depth) if node["additionalProperties"].is_a?(Hash)
|
|
261
|
+
return map_term(node, node["patternProperties"].to_h.values, hint, depth) if node.key?("patternProperties")
|
|
262
|
+
return map_term(node, [ node["items"] ], hint, depth) if node.key?("items") # one schema describes an object's elements under `items`
|
|
263
|
+
|
|
264
|
+
properties = node["properties"]
|
|
265
|
+
return :any_object unless properties.is_a?(Hash) && properties.any?
|
|
266
|
+
|
|
267
|
+
Shape.new(name: nil, properties: nodes_by_name(properties), required: Array(node["required"]))
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# Follows a chain of $ref to what it describes. Returns the node reached, with the
|
|
271
|
+
# last definition merged in, and the name of the last definition on the way (the one
|
|
272
|
+
# that describes the node), or nil when the chain names none.
|
|
273
|
+
def dereference(node, hops = 0)
|
|
274
|
+
reference = node["$ref"]
|
|
275
|
+
return [ node, nil ] unless reference.is_a?(String)
|
|
276
|
+
|
|
277
|
+
raise RubyCdk::Error, "#{@type_name}: a chain of more than #{MAX_REFERENCE_HOPS} references at #{reference}" if hops >= MAX_REFERENCE_HOPS
|
|
278
|
+
|
|
279
|
+
target = target_of(reference)
|
|
280
|
+
rest = node.except("$ref")
|
|
281
|
+
reached, deeper = dereference(rest.empty? ? target : merge(target, rest), hops + 1)
|
|
282
|
+
[ reached, deeper || (reference.start_with?("#/definitions/") ? reference.split("/").last : nil) ]
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# A node next to its $ref, with the definition under it: the definition's `type`
|
|
286
|
+
# wins when the two disagree, since it is written once and referenced many times.
|
|
287
|
+
def merge(target, rest)
|
|
288
|
+
merged = target.merge(rest)
|
|
289
|
+
merged["type"] = target["type"] if rest.key?("type") && target["type"] != rest["type"]
|
|
290
|
+
merged
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def target_of(reference)
|
|
294
|
+
path = reference.delete_prefix("#/").split("/")
|
|
295
|
+
target = path.reduce(@schema) { |cursor, part| cursor.is_a?(Hash) ? cursor[part] : nil }
|
|
296
|
+
raise RubyCdk::Error, "#{@type_name}: #{reference} points at nothing" unless target.is_a?(Hash)
|
|
297
|
+
|
|
298
|
+
target
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
# --- combining terms
|
|
302
|
+
|
|
303
|
+
def json?(term)
|
|
304
|
+
term.is_a?(Descriptor) && term.kind == :json
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# The union of what two terms accept. A constraint survives only when both state
|
|
308
|
+
# the same one, so the result is never stricter than the truth, with one exception:
|
|
309
|
+
# a bare object ("type": "object" and nothing else) gives way to the other term.
|
|
310
|
+
# It occurs once in us-east-1 on 2026-11-24, at AWS::DynamoDB::Table KeySchema
|
|
311
|
+
# (a list of KeySchema, or a bare object), and keeping it would make the property
|
|
312
|
+
# :json, where keys are emitted verbatim.
|
|
313
|
+
def or_(left, right)
|
|
314
|
+
return :any if left == :any || right == :any
|
|
315
|
+
return ANY_VALUE if json?(left) || json?(right)
|
|
316
|
+
|
|
317
|
+
if left == :any_object || right == :any_object
|
|
318
|
+
other = left == :any_object ? right : left
|
|
319
|
+
return other if other.is_a?(Shape) || (other.is_a?(Descriptor) && other.kind != :json)
|
|
320
|
+
|
|
321
|
+
return ANY_VALUE
|
|
322
|
+
end
|
|
323
|
+
return or_shape(left, right) if left.is_a?(Shape) && right.is_a?(Shape)
|
|
324
|
+
return ANY_VALUE unless left.is_a?(Descriptor) && right.is_a?(Descriptor)
|
|
325
|
+
return numeric(left, right, widest: true) unless left.kind == right.kind
|
|
326
|
+
|
|
327
|
+
case left.kind
|
|
328
|
+
when :nested
|
|
329
|
+
left.of == right.of ? left : ANY_VALUE
|
|
330
|
+
when :list, :map
|
|
331
|
+
element = union_element(left, right)
|
|
332
|
+
element ? left.with(item: element[0], of: element[1], values: joined(left.values, right.values), pattern: shared_pattern(left.pattern, right.pattern), unique: unique_or(left.unique, right.unique)) : ANY_VALUE
|
|
333
|
+
else
|
|
334
|
+
left.with(values: joined(left.values, right.values), pattern: shared_pattern(left.pattern, right.pattern))
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
# What two terms both accept.
|
|
339
|
+
def and_(left, right)
|
|
340
|
+
return right if left == :any || json?(left)
|
|
341
|
+
return left if right == :any || json?(right)
|
|
342
|
+
|
|
343
|
+
if left == :any_object || right == :any_object
|
|
344
|
+
other = left == :any_object ? right : left
|
|
345
|
+
return other.is_a?(Shape) || (other.is_a?(Descriptor) && other.kind == :map) ? other : ANY_VALUE
|
|
346
|
+
end
|
|
347
|
+
return and_shape(left, right) if left.is_a?(Shape) && right.is_a?(Shape)
|
|
348
|
+
return ANY_VALUE unless left.is_a?(Descriptor) && right.is_a?(Descriptor)
|
|
349
|
+
return numeric(left, right, widest: false) unless left.kind == right.kind
|
|
350
|
+
|
|
351
|
+
case left.kind
|
|
352
|
+
when :nested
|
|
353
|
+
left.of == right.of ? left : ANY_VALUE
|
|
354
|
+
when :list, :map
|
|
355
|
+
element = common_element(left, right)
|
|
356
|
+
element ? left.with(item: element[0], of: element[1], values: shared_values(left.values, right.values), pattern: shared_pattern(left.pattern, right.pattern), unique: unique_and(left.unique, right.unique)) : ANY_VALUE
|
|
357
|
+
else
|
|
358
|
+
left.with(values: shared_values(left.values, right.values), pattern: shared_pattern(left.pattern, right.pattern))
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
# Two numeric kinds as one: the widest in a union, the narrowest in an intersection.
|
|
363
|
+
# nil when they cannot both be satisfied, which is left to ANY_VALUE by the caller.
|
|
364
|
+
def numeric(left, right, widest:)
|
|
365
|
+
a = NUMERIC.index(left.kind)
|
|
366
|
+
b = NUMERIC.index(right.kind)
|
|
367
|
+
return ANY_VALUE unless a && b
|
|
368
|
+
|
|
369
|
+
Descriptor.new(kind: NUMERIC[widest ? [ a, b ].max : [ a, b ].min], item: nil, of: nil, values: nil, pattern: nil, unique: nil)
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
# [element kind, definition] for the union of two lists or two maps; nil when nothing
|
|
373
|
+
# precise can be said.
|
|
374
|
+
def union_element(left, right)
|
|
375
|
+
return [ left.item, left.of ] if left.item == right.item && left.of == right.of
|
|
376
|
+
return nil unless left.of.nil? && right.of.nil?
|
|
377
|
+
|
|
378
|
+
index = NUMERIC.index(left.item)
|
|
379
|
+
other = NUMERIC.index(right.item)
|
|
380
|
+
index && other ? [ NUMERIC[[ index, other ].max], nil ] : [ nil, nil ]
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
# The same for the intersection. A nil element kind means any JSON value.
|
|
384
|
+
def common_element(left, right)
|
|
385
|
+
return [ right.item, right.of ] if left.item.nil?
|
|
386
|
+
return [ left.item, left.of ] if right.item.nil?
|
|
387
|
+
return [ left.item, left.of ] if left.item == right.item && left.of == right.of
|
|
388
|
+
return nil unless left.of.nil? && right.of.nil?
|
|
389
|
+
|
|
390
|
+
index = NUMERIC.index(left.item)
|
|
391
|
+
other = NUMERIC.index(right.item)
|
|
392
|
+
index && other ? [ NUMERIC[[ index, other ].min], nil ] : nil
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
# Every literal either allows; nil when either leaves the choice open.
|
|
396
|
+
def joined(left, right)
|
|
397
|
+
left && right ? (left + right).uniq : nil
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
# Every literal both allow; nil when either leaves the choice open.
|
|
401
|
+
def shared_values(left, right)
|
|
402
|
+
left && right ? left & right : nil
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
# A pattern both state, or nil: two patterns joined cannot be written as one.
|
|
406
|
+
def shared_pattern(left, right)
|
|
407
|
+
left && right && left == right ? left : nil
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def unique_or(left, right)
|
|
411
|
+
left.nil? || right.nil? ? nil : left && right
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
def unique_and(left, right)
|
|
415
|
+
return false if left == false || right == false
|
|
416
|
+
return true if left == true || right == true
|
|
417
|
+
|
|
418
|
+
nil
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
# --- what the two sources say about updating
|
|
422
|
+
|
|
423
|
+
def read_flags
|
|
424
|
+
FLAGS.each do |key, flag|
|
|
425
|
+
Array(@schema[key]).each do |path|
|
|
426
|
+
segments = segments_of(path)
|
|
427
|
+
if segments.include?("*")
|
|
428
|
+
cover_wildcard(segments, flag)
|
|
429
|
+
else
|
|
430
|
+
located = locate(segments)
|
|
431
|
+
@flags[located] << flag if located
|
|
432
|
+
end
|
|
433
|
+
end
|
|
434
|
+
end
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
# A wildcard flag path ("/properties/Parts/*/Role", or deeper:
|
|
438
|
+
# "/properties/Rules/*/Outcomes/*/Arn") states the flag for a property of the
|
|
439
|
+
# elements the wildcards stand in front of; the wildcard is the list index or map
|
|
440
|
+
# key. AWS writes 183 such paths in us-east-1, with one to three stars, and every
|
|
441
|
+
# one of them is of this middle shape. Resolved here, once, by walking the
|
|
442
|
+
# descriptor tree: the literals up to each star locate a property, the star descends
|
|
443
|
+
# into what that property holds, and the literals after the last star name the
|
|
444
|
+
# flagged property.
|
|
445
|
+
def cover_wildcard(segments, flag, container = ROOT)
|
|
446
|
+
star = segments.index("*") or return finish_wildcard(segments, flag, container)
|
|
447
|
+
located = locate(segments[0...star], container) or return
|
|
448
|
+
nested = container_of(@descriptors.dig(located[0], located[1])) or return
|
|
449
|
+
cover_wildcard(segments[(star + 1)..], flag, nested)
|
|
450
|
+
end
|
|
451
|
+
|
|
452
|
+
def finish_wildcard(rest, flag, container)
|
|
453
|
+
located = locate(rest, container) if rest.any?
|
|
454
|
+
@flags[located] << flag if located
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
def segments_of(path)
|
|
458
|
+
path.is_a?(String) ? path.delete_prefix("/properties/").split("/") : []
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
# [definition name, property name] for a path under /properties, or nil when the
|
|
462
|
+
# path does not lead to a property.
|
|
463
|
+
def locate(segments, container = ROOT)
|
|
464
|
+
return if segments.empty?
|
|
465
|
+
|
|
466
|
+
found = @descriptors.dig(container, segments.first)
|
|
467
|
+
return unless found
|
|
468
|
+
return [ container, segments.first ] if segments.one?
|
|
469
|
+
|
|
470
|
+
nested = container_of(found)
|
|
471
|
+
locate(segments.drop(1), nested) if nested
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
# The definition describing what a property holds, when it holds objects.
|
|
475
|
+
def container_of(descriptor)
|
|
476
|
+
return nil unless descriptor.of && (descriptor.kind == :nested || descriptor.item == :nested)
|
|
477
|
+
|
|
478
|
+
@descriptors.key?(descriptor.of) ? descriptor.of : nil
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
def read_specification
|
|
482
|
+
resource = @specification.resource_type(@type_name) or return
|
|
483
|
+
walk(ROOT, resource["Properties"], {})
|
|
484
|
+
add_specification_only_properties(resource)
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
# The specification states an update type for every property, nested ones included,
|
|
488
|
+
# under the names the provider schema uses. Both trees are walked together. At the
|
|
489
|
+
# top level every property is recorded, the ones only the specification knows too,
|
|
490
|
+
# so that create_only sees which of them it marks Immutable.
|
|
491
|
+
def walk(container, properties, seen)
|
|
492
|
+
return unless properties.is_a?(Hash)
|
|
493
|
+
|
|
494
|
+
if container == ROOT
|
|
495
|
+
properties.each do |property, specified|
|
|
496
|
+
@specified[[ container, property ]] = said(specified) unless @descriptors.fetch(ROOT).key?(property)
|
|
497
|
+
end
|
|
498
|
+
end
|
|
499
|
+
@descriptors.fetch(container).each do |property, descriptor|
|
|
500
|
+
specified = properties[property] or next
|
|
501
|
+
@specified[[ container, property ]] = said(specified)
|
|
502
|
+
|
|
503
|
+
nested = container_of(descriptor) or next
|
|
504
|
+
deeper = @specification.properties(@type_name, specified) or next
|
|
505
|
+
next if seen[[ nested, deeper.object_id ]]
|
|
506
|
+
|
|
507
|
+
seen[[ nested, deeper.object_id ]] = true
|
|
508
|
+
walk(nested, deeper, seen)
|
|
509
|
+
end
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
def said(specified)
|
|
513
|
+
{ "update" => UPDATES[specified["UpdateType"]], "duplicates" => specified["DuplicatesAllowed"] }
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
# A property only the specification knows is added after the provider's, as it describes it.
|
|
517
|
+
def add_specification_only_properties(resource)
|
|
518
|
+
unknown_to_provider = resource["Properties"].reject { |property,| @descriptors.fetch(ROOT).key?(property) }
|
|
519
|
+
@extra = SpecificationType.new(@specification, @type_name).extra(unknown_to_provider)
|
|
520
|
+
return if @extra.entries.empty?
|
|
521
|
+
|
|
522
|
+
taken = @extra.definitions.keys & (@shapes.keys - [ ROOT ])
|
|
523
|
+
raise RubyCdk::Error, "#{@type_name}: the provider schema and the specification both define #{taken.first.inspect}, and they may differ" if taken.any?
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
# --- the type
|
|
527
|
+
|
|
528
|
+
def build
|
|
529
|
+
type = {
|
|
530
|
+
"source" => "provider",
|
|
531
|
+
"documentation_url" => documentation_url,
|
|
532
|
+
"required" => @shapes.fetch(ROOT).required.uniq.sort,
|
|
533
|
+
"create_only" => create_only,
|
|
534
|
+
"read_only" => read_only,
|
|
535
|
+
"properties" => entries(ROOT),
|
|
536
|
+
"definitions" => (@shapes.keys - [ ROOT ]).sort.to_h { |name| [ name, definition(name) ] }
|
|
537
|
+
}
|
|
538
|
+
type["additional_properties"] = true if additional_properties?
|
|
539
|
+
if @extra && !@extra.entries.empty?
|
|
540
|
+
type["properties"].concat(@extra.entries)
|
|
541
|
+
type["definitions"].merge!(@extra.definitions)
|
|
542
|
+
end
|
|
543
|
+
type
|
|
544
|
+
end
|
|
545
|
+
|
|
546
|
+
# A type may take properties neither source lists when either says so. The provider
|
|
547
|
+
# schema says `additionalProperties: false` of every type, which carries no
|
|
548
|
+
# information; the one type the specification opens is AWS::CloudFormation::CustomResource.
|
|
549
|
+
def additional_properties?
|
|
550
|
+
return true if @schema["additionalProperties"] == true
|
|
551
|
+
|
|
552
|
+
@specification.resource_type(@type_name)&.fetch("AdditionalProperties", nil) == true
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
def documentation_url
|
|
556
|
+
url = @specification.resource_type(@type_name)&.fetch("Documentation", nil) || @schema["documentationUrl"]
|
|
557
|
+
url&.sub(%r{\Ahttp://docs\.aws\.amazon\.com/}, "https://docs.aws.amazon.com/")
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
def create_only
|
|
561
|
+
from_provider = Array(@schema["createOnlyProperties"]).map { |path| segments_of(path).join(".") }
|
|
562
|
+
from_specification = @specified.filter_map { |(container, property), said| property if container == ROOT && said["update"] == "immutable" }
|
|
563
|
+
(from_provider + from_specification).uniq.sort
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
def read_only
|
|
567
|
+
attributes = @specification.resource_type(@type_name)&.fetch("Attributes", nil)
|
|
568
|
+
from_specification = attributes.is_a?(Hash) ? attributes.keys : []
|
|
569
|
+
(Array(@schema["readOnlyProperties"]).map { |path| segments_of(path).join(".") } + from_specification).uniq.sort
|
|
570
|
+
end
|
|
571
|
+
|
|
572
|
+
# [name, entry] pairs, in the provider schema's order.
|
|
573
|
+
def entries(container)
|
|
574
|
+
required = @shapes.fetch(container).required
|
|
575
|
+
@descriptors.fetch(container).map { |property, descriptor| [ property, entry(container, property, descriptor, required.include?(property)) ] }
|
|
576
|
+
end
|
|
577
|
+
|
|
578
|
+
def definition(name)
|
|
579
|
+
{ "required" => @shapes.fetch(name).required.uniq.sort, "properties" => entries(name) }
|
|
580
|
+
end
|
|
581
|
+
|
|
582
|
+
def entry(container, property, descriptor, required)
|
|
583
|
+
key = [ container, property ]
|
|
584
|
+
entry = { "kind" => descriptor.kind.to_s }
|
|
585
|
+
entry["item"] = descriptor.item.to_s if descriptor.item
|
|
586
|
+
entry["of"] = descriptor.of if descriptor.of
|
|
587
|
+
update = update_of(container, property, @specified[key])
|
|
588
|
+
entry["update"] = update if update
|
|
589
|
+
entry["values"] = descriptor.values unless descriptor.values.nil?
|
|
590
|
+
entry["pattern"] = descriptor.pattern if descriptor.pattern
|
|
591
|
+
unique = unique_items(descriptor, @specified[key])
|
|
592
|
+
entry["unique"] = unique unless unique.nil?
|
|
593
|
+
entry
|
|
594
|
+
end
|
|
595
|
+
|
|
596
|
+
# The provider schema's create-only list decides first: a create-only property is
|
|
597
|
+
# immutable. Then comes the specification's UpdateType, which is where Mutable,
|
|
598
|
+
# Immutable and Conditional come from. Only when the specification says nothing,
|
|
599
|
+
# because it does not list the property or the type, does the provider schema's own
|
|
600
|
+
# conditionalCreateOnlyProperties make it conditional; and a writable property
|
|
601
|
+
# neither source mentions is mutable, since the provider schema passes it to the
|
|
602
|
+
# update handler. A read-only property has no behaviour, since nothing updates it.
|
|
603
|
+
#
|
|
604
|
+
# The flags are the ones the provider schema states for the property's own path,
|
|
605
|
+
# plus what wildcard flag paths cover it (see cover_wildcard): AWS::MSK::Replicator
|
|
606
|
+
# writes its replication info as createOnlyProperties "/properties/ReplicationInfoList/*",
|
|
607
|
+
# and 150 more properties travel only in such wildcard paths, all of which would
|
|
608
|
+
# otherwise look writable to the diff.
|
|
609
|
+
def update_of(container, property, said)
|
|
610
|
+
flags = @flags[[ container, property ]]
|
|
611
|
+
return "immutable" if flags.include?(:create_only)
|
|
612
|
+
return nil if flags.include?(:read_only)
|
|
613
|
+
return said["update"] if said && said["update"]
|
|
614
|
+
return "conditional" if flags.include?(:conditional)
|
|
615
|
+
|
|
616
|
+
"mutable"
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
# uniqueItems when the provider schema states it; otherwise what the specification's
|
|
620
|
+
# DuplicatesAllowed says; otherwise nothing.
|
|
621
|
+
def unique_items(descriptor, said)
|
|
622
|
+
return descriptor.unique unless descriptor.unique.nil?
|
|
623
|
+
return nil unless said && !said["duplicates"].nil?
|
|
624
|
+
|
|
625
|
+
!said["duplicates"]
|
|
626
|
+
end
|
|
627
|
+
end
|
|
628
|
+
end
|
|
629
|
+
end
|
|
630
|
+
end
|