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,326 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
require_relative "../fn"
|
|
5
|
+
require_relative "../lazy"
|
|
6
|
+
require_relative "../core/elements"
|
|
7
|
+
require_relative "../core/stateful"
|
|
8
|
+
require_relative "serializer"
|
|
9
|
+
|
|
10
|
+
module RubyCdk
|
|
11
|
+
module Synth
|
|
12
|
+
# The resolve phase of architecture §5. The live tree of one Stack becomes
|
|
13
|
+
# a plain template: every Lazy is evaluated here, once, and nothing the user
|
|
14
|
+
# wrote is changed, because the template is a parallel structure of new
|
|
15
|
+
# arrays and hashes.
|
|
16
|
+
#
|
|
17
|
+
# A value the model of §4 cannot write does not stop the phase. It is
|
|
18
|
+
# reported as an issue under VALUE_RULE and a placeholder stands in its
|
|
19
|
+
# place, so that one synthesis reports every problem at once.
|
|
20
|
+
class Resolver
|
|
21
|
+
# The rule id of an issue from the value model. The rules of §6 are numbered from V1.
|
|
22
|
+
VALUE_RULE = "V0"
|
|
23
|
+
|
|
24
|
+
# What a nil property leaves behind: nothing.
|
|
25
|
+
OMIT = Object.new.freeze
|
|
26
|
+
|
|
27
|
+
# What stands in the template for a value that was rejected. A template
|
|
28
|
+
# that holds one is never written.
|
|
29
|
+
PLACEHOLDER = "<not a value>".freeze
|
|
30
|
+
|
|
31
|
+
ISO_HINT = "for example time.utc.iso8601".freeze
|
|
32
|
+
|
|
33
|
+
# The transform Fn::ToJsonString and Fn::Length need.
|
|
34
|
+
LANGUAGE_EXTENSIONS = "AWS::LanguageExtensions".freeze
|
|
35
|
+
|
|
36
|
+
def initialize(stack, issues: [])
|
|
37
|
+
@stack = stack
|
|
38
|
+
@issues = issues
|
|
39
|
+
@location = []
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The whole template of the stack.
|
|
43
|
+
#
|
|
44
|
+
# Transform comes last, whatever the order of resolution: a value deep in
|
|
45
|
+
# the tree can ask for AWS::LanguageExtensions while it is resolved. The
|
|
46
|
+
# serializer puts the section in its place.
|
|
47
|
+
def call
|
|
48
|
+
template = { "AWSTemplateFormatVersion" => Serializer::FORMAT_VERSION }
|
|
49
|
+
template["Description"] = @stack.description if @stack.description
|
|
50
|
+
template["Parameters"] = section("Parameters", @stack.parameters) { |parameter| parameter_body(parameter) }
|
|
51
|
+
template["Mappings"] = section("Mappings", @stack.mappings) { |mapping| pairs(mapping.hash) }
|
|
52
|
+
template["Conditions"] = section("Conditions", @stack.conditions) { |condition| value(condition.fn) }
|
|
53
|
+
template["Resources"] = resources
|
|
54
|
+
template["Outputs"] = section("Outputs", @stack.outputs) { |output| output_body(output) }
|
|
55
|
+
template["Metadata"] = at("Metadata") { pairs(@stack.metadata) } unless @stack.metadata.empty?
|
|
56
|
+
declared = @stack.transforms
|
|
57
|
+
template["Transform"] = declared.one? ? declared.first : declared unless declared.empty?
|
|
58
|
+
template.compact
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# One value, the way the template holds it. Raises ValidationError when
|
|
62
|
+
# it cannot be written.
|
|
63
|
+
def resolve(item)
|
|
64
|
+
resolved = value(item)
|
|
65
|
+
raise ValidationError.new(@issues) unless @issues.empty?
|
|
66
|
+
|
|
67
|
+
resolved.equal?(OMIT) ? nil : resolved
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
# --- the sections
|
|
73
|
+
#
|
|
74
|
+
# The attributes of a parameter and of an output follow the order in
|
|
75
|
+
# which the contract's signature names them; the serializer orders the
|
|
76
|
+
# sections and the attributes of a resource.
|
|
77
|
+
|
|
78
|
+
# A section built from what the stack declared, in declaration order.
|
|
79
|
+
# nil when it declared nothing.
|
|
80
|
+
def section(name, declarations)
|
|
81
|
+
return nil if declarations.empty?
|
|
82
|
+
|
|
83
|
+
at(name) { declarations.to_h { |key, element| [ key, at(key) { yield(element) } ] } }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def resources
|
|
87
|
+
declared = @stack.resources
|
|
88
|
+
return nil if declared.empty?
|
|
89
|
+
|
|
90
|
+
at("Resources") { declared.to_h { |resource| [ resource.logical_id, at(resource.logical_id) { resource_body(resource) } ] } }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def parameter_body(parameter)
|
|
94
|
+
body = { "Type" => parameter.type }
|
|
95
|
+
unless parameter.default.nil?
|
|
96
|
+
at("Default") do
|
|
97
|
+
resolved = value(parameter.default)
|
|
98
|
+
body["Default"] = resolved unless resolved.equal?(OMIT)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
body["Description"] = parameter.description if parameter.description
|
|
102
|
+
at("AllowedValues") { body["AllowedValues"] = value(parameter.allowed_values) } if parameter.allowed_values
|
|
103
|
+
body["NoEcho"] = true if parameter.no_echo
|
|
104
|
+
body
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def output_body(output)
|
|
108
|
+
body = {}
|
|
109
|
+
at("Value") do
|
|
110
|
+
resolved = value(output.value)
|
|
111
|
+
if resolved.equal?(OMIT)
|
|
112
|
+
report("the output has no value", hint: "value: is nil, or resolves to nil")
|
|
113
|
+
else
|
|
114
|
+
body["Value"] = resolved
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
body["Description"] = output.description if output.description
|
|
118
|
+
body["Export"] = { "Name" => @stack.export_name(output.id) } if output.export
|
|
119
|
+
body["Condition"] = output.condition if output.condition
|
|
120
|
+
body
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def resource_body(resource)
|
|
124
|
+
body = { "Type" => resource.type }
|
|
125
|
+
body["DependsOn"] = resource.depends_on unless resource.depends_on.empty?
|
|
126
|
+
body["Condition"] = resource.condition if resource.condition
|
|
127
|
+
body["Metadata"] = at("Metadata") { pairs(resource.metadata) } unless resource.metadata.empty?
|
|
128
|
+
deletion_policy, update_replace_policy = policies(resource)
|
|
129
|
+
body["DeletionPolicy"] = deletion_policy if deletion_policy
|
|
130
|
+
body["UpdateReplacePolicy"] = update_replace_policy if update_replace_policy
|
|
131
|
+
unless resource.properties.empty?
|
|
132
|
+
at("Properties") do
|
|
133
|
+
properties = pairs(resource.properties)
|
|
134
|
+
body["Properties"] = properties unless properties.empty?
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
body
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# [DeletionPolicy, UpdateReplacePolicy].
|
|
141
|
+
#
|
|
142
|
+
# deletion_policy: is the CDK's removal policy: it sets both, as
|
|
143
|
+
# applyRemovalPolicy does. update_replace_policy: replaces the second
|
|
144
|
+
# alone. With neither, a stateful type gets what architecture §7 gives it
|
|
145
|
+
# under the stack's default, and any other type gets nothing.
|
|
146
|
+
def policies(resource)
|
|
147
|
+
defaults = Core::Stateful.policies_for(resource.type, default: @stack.stateful_default)
|
|
148
|
+
given = resource.deletion_policy ? Core::Stateful.pair(resource.deletion_policy) : [ nil, nil ]
|
|
149
|
+
|
|
150
|
+
[
|
|
151
|
+
given[0] || defaults&.first,
|
|
152
|
+
resource.update_replace_policy || given[1] || defaults&.last
|
|
153
|
+
]
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# --- the value model (architecture §4)
|
|
157
|
+
|
|
158
|
+
# A value by the rules of the model, or OMIT for a nil.
|
|
159
|
+
def value(item)
|
|
160
|
+
case item
|
|
161
|
+
when String, Integer
|
|
162
|
+
item
|
|
163
|
+
when Float
|
|
164
|
+
item.finite? ? item : reject("Infinity and NaN are not values")
|
|
165
|
+
when true, false
|
|
166
|
+
item
|
|
167
|
+
when nil
|
|
168
|
+
OMIT
|
|
169
|
+
when Symbol
|
|
170
|
+
item.name
|
|
171
|
+
when Hash
|
|
172
|
+
hash_value(item)
|
|
173
|
+
when Array
|
|
174
|
+
array_value(item)
|
|
175
|
+
when Lazy
|
|
176
|
+
lazy_value(item)
|
|
177
|
+
when Time
|
|
178
|
+
reject("a Time is not a value: write an ISO 8601 string", hint: ISO_HINT)
|
|
179
|
+
when Proc
|
|
180
|
+
reject("a Proc is not a value: a Proc is never called for you", hint: "wrap it in RubyCdk::Lazy.new { ... } when it should run at synthesis")
|
|
181
|
+
when Construct
|
|
182
|
+
construct_value(item)
|
|
183
|
+
when Core::Element
|
|
184
|
+
reject("the #{kind_of(item)} #{item.id} of the stack is not a value", hint: "Fn.ref takes a parameter, Fn.if a condition and Fn.find_in_map a mapping")
|
|
185
|
+
else
|
|
186
|
+
other_value(item)
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def hash_value(hash)
|
|
191
|
+
# Fn::ToJsonString and Fn::Length are CloudFormation's only functions
|
|
192
|
+
# that need the AWS::LanguageExtensions transform (its reference says so
|
|
193
|
+
# on both pages). Declaring it here, as a stack of the CDK does with
|
|
194
|
+
# addTransform, is what makes the template valid.
|
|
195
|
+
@stack.transform(LANGUAGE_EXTENSIONS) if needs_language_extensions?(hash)
|
|
196
|
+
simplify_join(pairs(hash))
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# A Hash with one key, Fn::ToJsonString or Fn::Length.
|
|
200
|
+
def needs_language_extensions?(hash)
|
|
201
|
+
hash.size == 1 && (hash.key?("Fn::ToJsonString") || hash.key?("Fn::Length"))
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def array_value(array)
|
|
205
|
+
array.map.with_index do |item, index|
|
|
206
|
+
at("[#{index}]") do
|
|
207
|
+
resolved = value(item)
|
|
208
|
+
next resolved unless resolved.equal?(OMIT)
|
|
209
|
+
|
|
210
|
+
reject("nil is not a value inside an array", hint: "remove the nil entries with compact")
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# The Lazy's value. A RubyCdk::Error raised while the block runs, as an
|
|
216
|
+
# asset whose directory is missing, is an issue like any other, so that
|
|
217
|
+
# one synthesis reports every problem at once. Anything else is a bug in
|
|
218
|
+
# the block and leaves with its backtrace.
|
|
219
|
+
def lazy_value(lazy)
|
|
220
|
+
resolved =
|
|
221
|
+
begin
|
|
222
|
+
lazy.resolve(@stack)
|
|
223
|
+
rescue RubyCdk::Error => e
|
|
224
|
+
return reject("#{e.message} (a Lazy at this place could not be evaluated)")
|
|
225
|
+
end
|
|
226
|
+
value(resolved)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def construct_value(construct)
|
|
230
|
+
if construct.respond_to?(:logical_id)
|
|
231
|
+
reject("the resource #{construct.path} is not a value: did you mean `Fn.ref(...)`?",
|
|
232
|
+
hint: "Fn.ref(resource) writes its Ref, and Fn.get_att(resource, \"Arn\") an attribute")
|
|
233
|
+
else
|
|
234
|
+
reject("#{construct.path} is a construct, and a construct is not a value")
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def other_value(item)
|
|
239
|
+
if date?(item)
|
|
240
|
+
reject("a #{item.class} is not a value: write an ISO 8601 string", hint: ISO_HINT)
|
|
241
|
+
elsif asset?(item)
|
|
242
|
+
reject("a zipped asset is not a value: use `.s3_location`, `.bucket` or `.key`")
|
|
243
|
+
else
|
|
244
|
+
reject("#{article(item.class)} #{item.class} is not a value: a template holds strings, numbers, booleans, arrays and hashes")
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def date?(item)
|
|
249
|
+
defined?(::Date) && item.is_a?(::Date)
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# Anything that answers what a zipped asset answers. Which component
|
|
253
|
+
# defines that class is not this component's business.
|
|
254
|
+
def asset?(item)
|
|
255
|
+
item.respond_to?(:s3_location) && item.respond_to?(:bucket) && item.respond_to?(:key)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def kind_of(element)
|
|
259
|
+
element.class.name.split("::").last.downcase
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def article(klass)
|
|
263
|
+
klass.name.start_with?("A", "E", "I", "O", "U") ? "an" : "a"
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# --- helpers
|
|
267
|
+
|
|
268
|
+
# A Hash whose keys are Strings and whose values are resolved. A nil
|
|
269
|
+
# value leaves its key out.
|
|
270
|
+
def pairs(hash)
|
|
271
|
+
hash.each_with_object({}) do |(key, item), out|
|
|
272
|
+
name = key_name(key)
|
|
273
|
+
resolved = at(name) { value(item) }
|
|
274
|
+
out[name] = resolved unless resolved.equal?(OMIT)
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def key_name(key)
|
|
279
|
+
return key if key.is_a?(String)
|
|
280
|
+
return key.name if key.is_a?(Symbol)
|
|
281
|
+
|
|
282
|
+
report("a Hash key must be a String or a Symbol, got #{key.inspect}")
|
|
283
|
+
key.to_s
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# An Fn::Join whose list is wholly known becomes the shortest expression
|
|
287
|
+
# that says the same, as it does when the CDK resolves a template.
|
|
288
|
+
def simplify_join(hash)
|
|
289
|
+
return hash unless hash.size == 1 && hash.key?("Fn::Join")
|
|
290
|
+
|
|
291
|
+
delimiter, list = hash["Fn::Join"]
|
|
292
|
+
return hash unless list.is_a?(Array)
|
|
293
|
+
|
|
294
|
+
values = Fn.minimal_join(delimiter, list)
|
|
295
|
+
values.one? ? values.first : { "Fn::Join" => [ delimiter, values ] }
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Reports an issue and answers the placeholder that stands in the template.
|
|
299
|
+
def reject(message, hint: nil)
|
|
300
|
+
report(message, hint:)
|
|
301
|
+
PLACEHOLDER
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def report(message, hint: nil)
|
|
305
|
+
@issues << Issue.new(rule: VALUE_RULE, path: location, message:, hint:)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def at(segment)
|
|
309
|
+
@location << segment
|
|
310
|
+
yield
|
|
311
|
+
ensure
|
|
312
|
+
@location.pop
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
# Where we are in the template, as "Resources.Table.Properties.KeySchema[0].KeyType".
|
|
316
|
+
def location
|
|
317
|
+
@location.each_with_object(+"") do |segment, text|
|
|
318
|
+
next text << segment if segment.start_with?("[")
|
|
319
|
+
|
|
320
|
+
text << "." unless text.empty?
|
|
321
|
+
text << segment
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
require_relative "../core/ids"
|
|
5
|
+
require_relative "../core/stack"
|
|
6
|
+
|
|
7
|
+
module RubyCdk
|
|
8
|
+
module Synth
|
|
9
|
+
# The two rules that need no knowledge of AWS's resources, and so live with
|
|
10
|
+
# the pipeline. They are registered like every other validator, in synth.rb,
|
|
11
|
+
# and take a Context and return issues.
|
|
12
|
+
module Rules
|
|
13
|
+
# V2: a logical ID holds letters and digits only, and names one thing in
|
|
14
|
+
# the whole stack.
|
|
15
|
+
#
|
|
16
|
+
# A parameter, an output, a mapping, a condition and a resource are
|
|
17
|
+
# refused when they are declared with any other id, so what reaches this
|
|
18
|
+
# rule is what a normalizer wrote. Core::Ids has the words.
|
|
19
|
+
class LogicalIds
|
|
20
|
+
RULE = "V2"
|
|
21
|
+
SECTIONS = %w[Parameters Mappings Conditions Resources Outputs].freeze
|
|
22
|
+
|
|
23
|
+
def call(context)
|
|
24
|
+
issues = []
|
|
25
|
+
first_seen = {}
|
|
26
|
+
template = context.template
|
|
27
|
+
SECTIONS.each do |section|
|
|
28
|
+
names = template[section].is_a?(Hash) ? template[section].keys : []
|
|
29
|
+
names.each do |name|
|
|
30
|
+
path = "#{section}.#{name}"
|
|
31
|
+
fault = Core::Ids.logical_id_fault(name)
|
|
32
|
+
if fault
|
|
33
|
+
issues << Issue.new(rule: RULE, path:, message: fault, hint: Core::Ids.logical_id_way_out(name))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
if (earlier = first_seen[name])
|
|
37
|
+
issues << Issue.new(
|
|
38
|
+
rule: RULE, path:,
|
|
39
|
+
message: "the logical ID #{name.inspect} is declared more than once in the stack: in #{earlier} and here",
|
|
40
|
+
hint: "a logical ID names one thing in the whole stack"
|
|
41
|
+
)
|
|
42
|
+
else
|
|
43
|
+
first_seen[name] = path
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Two resources with one logical ID leave one key in the template, so the tree is where the second one is.
|
|
49
|
+
context.stack.resources.group_by(&:logical_id).each do |name, resources|
|
|
50
|
+
next if resources.one?
|
|
51
|
+
|
|
52
|
+
issues << Issue.new(
|
|
53
|
+
rule: RULE, path: "Resources.#{name}",
|
|
54
|
+
message: "the logical ID #{name.inspect} is that of #{resources.length} resources: #{resources.map(&:path).join(", ")}",
|
|
55
|
+
hint: "a resource inside a construct takes the ids above it in front of its own: give one of them another id"
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
issues
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# V13: a stack name starts with a letter, goes on with letters, digits
|
|
63
|
+
# and hyphens, and is at most 128 characters long.
|
|
64
|
+
class StackName
|
|
65
|
+
RULE = "V13"
|
|
66
|
+
|
|
67
|
+
def call(context)
|
|
68
|
+
name = context.stack.stack_name
|
|
69
|
+
return [] if Stack::NAME_PATTERN.match?(name) && name.length <= Stack::MAX_NAME_LENGTH
|
|
70
|
+
|
|
71
|
+
[
|
|
72
|
+
Issue.new(
|
|
73
|
+
rule: RULE, path: "Stack",
|
|
74
|
+
message: "the stack name #{name.inspect} is not valid: it must start with a letter, go on with letters, digits and hyphens, " \
|
|
75
|
+
"and be at most #{Stack::MAX_NAME_LENGTH} characters long",
|
|
76
|
+
hint: "a stack's name is the prefix of App.new(stack_name_prefix:) followed by its id"
|
|
77
|
+
)
|
|
78
|
+
]
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "../errors"
|
|
5
|
+
|
|
6
|
+
module RubyCdk
|
|
7
|
+
module Synth
|
|
8
|
+
# Raised when the template is not plain JSON data by the time it is serialized.
|
|
9
|
+
class SerializationError < RubyCdk::Error; end
|
|
10
|
+
|
|
11
|
+
# The serialization contract (architecture §5, D-028). Every byte of a
|
|
12
|
+
# template comes from here.
|
|
13
|
+
module Serializer
|
|
14
|
+
FORMAT_VERSION = "2010-09-09"
|
|
15
|
+
FILE_SUFFIX = ".template.json"
|
|
16
|
+
|
|
17
|
+
# Top-level sections, in this order, each present only when it has something in it.
|
|
18
|
+
SECTION_ORDER = %w[AWSTemplateFormatVersion Transform Description Parameters Mappings Conditions Resources Outputs Metadata].freeze
|
|
19
|
+
|
|
20
|
+
# The attributes of a resource, in this order.
|
|
21
|
+
RESOURCE_ATTRIBUTE_ORDER = %w[Type DependsOn Condition Metadata DeletionPolicy UpdateReplacePolicy Properties].freeze
|
|
22
|
+
|
|
23
|
+
# JSON.generate with these options writes two-space indentation, one space
|
|
24
|
+
# after each colon and one value per line.
|
|
25
|
+
OPTIONS = { indent: " ", space: " ", object_nl: "\n", array_nl: "\n" }.freeze
|
|
26
|
+
|
|
27
|
+
module_function
|
|
28
|
+
|
|
29
|
+
# The bytes of a template: UTF-8 JSON with LF line endings and one trailing newline.
|
|
30
|
+
def call(template)
|
|
31
|
+
render(ordered(template))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The same bytes for any plain Hash: the manifest uses them too.
|
|
35
|
+
#
|
|
36
|
+
# JSON.generate would write anything else through its to_s, so the Hash is
|
|
37
|
+
# checked first: it holds strings, numbers, booleans and nil, in arrays and
|
|
38
|
+
# in hashes whose keys are strings, and nothing else.
|
|
39
|
+
def render(hash)
|
|
40
|
+
check(hash, [])
|
|
41
|
+
JSON.generate(hash, **OPTIONS) + "\n"
|
|
42
|
+
rescue JSON::JSONError => e
|
|
43
|
+
Kernel.raise SerializationError, "a template holds only strings, numbers, booleans, arrays and hashes: #{e.message}"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Where a stack's template is written.
|
|
47
|
+
def path(out_dir, stack_name)
|
|
48
|
+
File.join(out_dir, "#{stack_name}#{FILE_SUFFIX}")
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The template as it is written: top-level sections in SECTION_ORDER,
|
|
52
|
+
# every resource with its attributes in RESOURCE_ATTRIBUTE_ORDER, and
|
|
53
|
+
# anything else in the order it was given. A section or an attribute with
|
|
54
|
+
# nothing in it is left out.
|
|
55
|
+
def ordered(template)
|
|
56
|
+
(SECTION_ORDER | template.keys).each_with_object({}) do |name, done|
|
|
57
|
+
section = template[name]
|
|
58
|
+
next if empty?(section)
|
|
59
|
+
|
|
60
|
+
done[name] = name == "Resources" && section.is_a?(Hash) ? order_resources(section) : section
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def order_resources(resources)
|
|
65
|
+
resources.to_h { |name, body| [ name, body.is_a?(Hash) ? order_resource(body) : body ] }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def order_resource(body)
|
|
69
|
+
(RESOURCE_ATTRIBUTE_ORDER | body.keys).each_with_object({}) do |name, ordered|
|
|
70
|
+
ordered[name] = body[name] unless empty?(body[name])
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def empty?(value)
|
|
75
|
+
value.nil? || (value.respond_to?(:empty?) && value.empty?)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Raises SerializationError naming the place of the first value that is not plain data.
|
|
79
|
+
def check(value, place)
|
|
80
|
+
case value
|
|
81
|
+
when String, Integer, true, false, nil
|
|
82
|
+
nil
|
|
83
|
+
when Float
|
|
84
|
+
not_plain(value, place) unless value.finite?
|
|
85
|
+
when Array
|
|
86
|
+
value.each_with_index { |item, index| check(item, place + [ "[#{index}]" ]) }
|
|
87
|
+
when Hash
|
|
88
|
+
value.each do |key, item|
|
|
89
|
+
not_plain(key, place, "a key") unless key.is_a?(String)
|
|
90
|
+
check(item, place + [ key ])
|
|
91
|
+
end
|
|
92
|
+
else
|
|
93
|
+
not_plain(value, place)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def not_plain(value, place, what = nil)
|
|
98
|
+
kind = what || "a #{value.class}"
|
|
99
|
+
Kernel.raise SerializationError, "#{where(place)}: #{kind} is not plain data: #{short(value)} cannot be written"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# A place in the template, as "Resources.A.Properties.Tags[0]".
|
|
103
|
+
def where(place)
|
|
104
|
+
return "the template" if place.empty?
|
|
105
|
+
|
|
106
|
+
place.each_with_object(+"") do |segment, text|
|
|
107
|
+
next text << segment if segment.start_with?("[")
|
|
108
|
+
|
|
109
|
+
text << "." unless text.empty?
|
|
110
|
+
text << segment
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def short(value)
|
|
115
|
+
value.to_s.size > 60 ? "#{value.to_s[0, 60]}..." : value.to_s
|
|
116
|
+
end
|
|
117
|
+
private_class_method :order_resources, :order_resource, :empty?, :check, :not_plain, :where, :short
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require_relative "../errors"
|
|
5
|
+
|
|
6
|
+
module RubyCdk
|
|
7
|
+
module Synth
|
|
8
|
+
# The write phase: a file is written only when its bytes changed.
|
|
9
|
+
module Writer
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
# Writes bytes to path unless the file already holds them. Returns true
|
|
13
|
+
# when the file was written.
|
|
14
|
+
def write_if_changed(path, bytes)
|
|
15
|
+
return false if File.file?(path) && File.binread(path).b == bytes.b
|
|
16
|
+
|
|
17
|
+
File.binwrite(path, bytes)
|
|
18
|
+
true
|
|
19
|
+
rescue SystemCallError => e
|
|
20
|
+
Kernel.raise UsageError, "Cannot write #{path}: #{e.message}"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Makes path, and every directory above it that is missing.
|
|
24
|
+
def mkdir(path)
|
|
25
|
+
FileUtils.mkdir_p(path)
|
|
26
|
+
rescue SystemCallError => e
|
|
27
|
+
Kernel.raise UsageError, "Cannot create the directory #{path}: #{e.message}"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|