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,524 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
require_relative "compare"
|
|
5
|
+
|
|
6
|
+
module RubyCdk
|
|
7
|
+
module Diff
|
|
8
|
+
# One value of the template, from one side of the diff to the other. A nil
|
|
9
|
+
# old_value means the value is new to the template; a nil new_value means
|
|
10
|
+
# it is gone. A Difference whose sides compare equal is not built: the
|
|
11
|
+
# sections keep only what changed.
|
|
12
|
+
Difference = Data.define(:old_value, :new_value) do
|
|
13
|
+
def different?
|
|
14
|
+
!Compare.equal?(old_value, new_value)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def addition?
|
|
18
|
+
old_value.nil? && !new_value.nil?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def removal?
|
|
22
|
+
!old_value.nil? && new_value.nil?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def update?
|
|
26
|
+
!old_value.nil? && !new_value.nil?
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# One changed property path of a resource. name is the property at the top
|
|
31
|
+
# of the resource's Properties (the name CloudFormation names in a change
|
|
32
|
+
# set's detail); path is the way down to the change, segments being
|
|
33
|
+
# property names with integers for list indexes; impact says what changing
|
|
34
|
+
# the path costs, from the schema model's update behaviours:
|
|
35
|
+
#
|
|
36
|
+
# :update the resource is updated in place
|
|
37
|
+
# :replace the path sits inside a create-only property: the resource is replaced
|
|
38
|
+
# :may_replace the behaviour is conditional: the resource may be replaced
|
|
39
|
+
PropertyChange = Data.define(:name, :path, :old_value, :new_value, :impact) do
|
|
40
|
+
# The annotation the CDK prints after the property's name.
|
|
41
|
+
ANNOTATIONS = { replace: "requires replacement", may_replace: "may cause replacement" }.freeze
|
|
42
|
+
|
|
43
|
+
# The marker the CDK prints before a changed value: [+], [-] or [~].
|
|
44
|
+
def tag
|
|
45
|
+
return "~" if update?
|
|
46
|
+
return "-" if removal?
|
|
47
|
+
|
|
48
|
+
"+"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def addition?
|
|
52
|
+
old_value.nil? && !new_value.nil?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def removal?
|
|
56
|
+
!old_value.nil? && new_value.nil?
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def update?
|
|
60
|
+
!old_value.nil? && !new_value.nil?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def annotation
|
|
64
|
+
ANNOTATIONS[impact]
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The replacement the change set reports for the property: "True",
|
|
68
|
+
# "Conditional" or "False", as CloudFormation names them.
|
|
69
|
+
def replacement_field
|
|
70
|
+
case impact
|
|
71
|
+
when :replace then "True"
|
|
72
|
+
when :may_replace then "Conditional"
|
|
73
|
+
else "False"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def to_h
|
|
78
|
+
{ "path" => path.join("."), "old" => old_value, "new" => new_value, "replacement" => replacement_field }
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The change of one resource, as a change set would report it: the action
|
|
83
|
+
# (Add, Modify or Remove), whether the resource is replaced, and the
|
|
84
|
+
# changed property paths. Carries both sides' whole resource definition for
|
|
85
|
+
# the renderer and for the security view.
|
|
86
|
+
class ResourceChange
|
|
87
|
+
# How badly a change hits the resource that exists, costliest last. The
|
|
88
|
+
# order is the CDK's (ResourceImpact and worstImpact in diff/types.ts).
|
|
89
|
+
BADNESS = { none: 0, import: 0, update: 1, create: 2, orphan: 3, may_replace: 4, replace: 5, destroy: 6 }.freeze
|
|
90
|
+
|
|
91
|
+
# What an impact reads as, for a person. The words are the CDK's; the
|
|
92
|
+
# impacts it prints nothing for stay nil here.
|
|
93
|
+
WORDS = {
|
|
94
|
+
none: nil, update: nil, create: nil, import: "import",
|
|
95
|
+
may_replace: "may be replaced", replace: "replace", destroy: "destroy", orphan: "orphan"
|
|
96
|
+
}.freeze
|
|
97
|
+
|
|
98
|
+
attr_reader :logical_id, :old_value, :new_value, :old_type, :new_type, :properties, :others
|
|
99
|
+
|
|
100
|
+
def initialize(logical_id:, old_value: nil, new_value: nil, old_type: nil, new_type: nil, properties: [], others: {})
|
|
101
|
+
@logical_id = logical_id
|
|
102
|
+
@old_value = old_value
|
|
103
|
+
@new_value = new_value
|
|
104
|
+
@old_type = old_type
|
|
105
|
+
@new_type = new_type
|
|
106
|
+
@properties = properties
|
|
107
|
+
@others = others
|
|
108
|
+
@import = false
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Internal: the replacement propagation puts a replaced reference's
|
|
112
|
+
# property changes on the resources that reference what is replaced.
|
|
113
|
+
def with_properties(properties)
|
|
114
|
+
change = self.class.new(logical_id:, old_value:, new_value:, old_type:, new_type:, properties:, others:)
|
|
115
|
+
change.import! if @import
|
|
116
|
+
change
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def import!
|
|
120
|
+
@import = true
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def import?
|
|
124
|
+
@import
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def type_changed?
|
|
128
|
+
!old_type.nil? && !new_type.nil? && old_type != new_type
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def addition?
|
|
132
|
+
old_value.nil? && !new_value.nil?
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def removal?
|
|
136
|
+
!old_value.nil? && new_value.nil?
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def update?
|
|
140
|
+
different? && !addition? && !removal?
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def different?
|
|
144
|
+
# A resource that only appears on one side, and a resource whose Type
|
|
145
|
+
# changed, differ even with nothing else to show: the CDK's
|
|
146
|
+
# isDifferent (oldResourceType !== newResourceType, its differenceCount
|
|
147
|
+
# over the changes).
|
|
148
|
+
old_type != new_type || properties.any? || others.any? { |_name, difference| difference.different? }
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# The action CloudFormation's change set reports: "Add", "Modify" or
|
|
152
|
+
# "Remove". A replacement is reported as a Modify that replaces, which is
|
|
153
|
+
# why the replacement is its own field.
|
|
154
|
+
def action
|
|
155
|
+
return "Add" if addition?
|
|
156
|
+
return "Remove" if removal?
|
|
157
|
+
|
|
158
|
+
"Modify"
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# What the change set's Replacement field reads: "True" when a
|
|
162
|
+
# create-only path changed, "Conditional" when it may.
|
|
163
|
+
def replacement_field
|
|
164
|
+
return "True" if impact == :replace
|
|
165
|
+
return "Conditional" if impact == :may_replace
|
|
166
|
+
|
|
167
|
+
"False"
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# The costliest consequence of the change. A removal is a destroy, or an
|
|
171
|
+
# orphan when the resource's DeletionPolicy keeps it (Retain and
|
|
172
|
+
# RetainExceptOnCreate both do, as the CDK decides). A change of the
|
|
173
|
+
# resource's Type replaces it: CloudFormation has no such update.
|
|
174
|
+
def impact
|
|
175
|
+
return :import if @import
|
|
176
|
+
|
|
177
|
+
if old_type != new_type
|
|
178
|
+
return :create if old_type.nil?
|
|
179
|
+
return :replace unless new_type.nil?
|
|
180
|
+
return retained? ? :orphan : :destroy
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
base = others.any? { |_name, difference| difference.different? } ? :update : :none
|
|
184
|
+
properties.map(&:impact).reduce(base) { |worst, candidate| costlier(worst, candidate) }
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# The impact, for a person.
|
|
188
|
+
def word
|
|
189
|
+
WORDS.fetch(impact)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def to_h(security:)
|
|
193
|
+
changed_others = others.select { |_name, difference| difference.different? }
|
|
194
|
+
{
|
|
195
|
+
"logical_id" => logical_id,
|
|
196
|
+
"action" => action,
|
|
197
|
+
"type" => old_type || new_type,
|
|
198
|
+
"old_type" => old_type,
|
|
199
|
+
"new_type" => new_type,
|
|
200
|
+
"replacement" => replacement_field,
|
|
201
|
+
"impact" => impact.to_s,
|
|
202
|
+
"security" => security,
|
|
203
|
+
"properties" => properties.map(&:to_h),
|
|
204
|
+
"other" => changed_others.to_h { |name, difference| [ name, { "old" => difference.old_value, "new" => difference.new_value } ] }
|
|
205
|
+
}
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
private
|
|
209
|
+
|
|
210
|
+
# True when the resource as it is deployed keeps its physical self after
|
|
211
|
+
# the stack: its DeletionPolicy retains it.
|
|
212
|
+
def retained?
|
|
213
|
+
policy = old_value.is_a?(Hash) ? old_value["DeletionPolicy"] : nil
|
|
214
|
+
%w[Retain RetainExceptOnCreate].include?(policy)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def costlier(worst, candidate)
|
|
218
|
+
BADNESS.fetch(candidate, 0) > BADNESS.fetch(worst, 0) ? candidate : worst
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# The change set of two templates: what would change between what is
|
|
223
|
+
# deployed and what was synthesized. Sections are compared entry by entry,
|
|
224
|
+
# resources by logical ID with the schema model's update behaviours, and
|
|
225
|
+
# the security view is built from what both templates carry (architecture
|
|
226
|
+
# section 10).
|
|
227
|
+
#
|
|
228
|
+
# A replacement is propagated the way the CDK's diffTemplate propagates it:
|
|
229
|
+
# a resource that is replaced changes what every reference to it resolves
|
|
230
|
+
# to, so a resource whose properties reference it is shown with the
|
|
231
|
+
# property that uses the reference marked as replacing it too.
|
|
232
|
+
class TemplateDiff
|
|
233
|
+
# The template's own keys that are sections of their own. Everything else
|
|
234
|
+
# that differs goes under "Other Changes", as the CDK does.
|
|
235
|
+
SECTIONS = %w[Parameters Mappings Conditions Metadata Outputs].freeze
|
|
236
|
+
|
|
237
|
+
attr_reader :stack_name, :format_version, :description, :transform, :unknown
|
|
238
|
+
|
|
239
|
+
# old_template and new_template are normalized Hashes; nil answers an
|
|
240
|
+
# empty template, which is how a stack that does not exist yet diffs
|
|
241
|
+
# against nothing. stack_name is carried for the renderer and the JSON.
|
|
242
|
+
def self.build(old_template, new_template, stack_name: nil)
|
|
243
|
+
diff = new(stack_name:)
|
|
244
|
+
diff.send(:compute, old_template || {}, new_template || {})
|
|
245
|
+
diff
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def initialize(stack_name: nil)
|
|
249
|
+
@stack_name = stack_name
|
|
250
|
+
@format_version = nil
|
|
251
|
+
@description = nil
|
|
252
|
+
@transform = nil
|
|
253
|
+
@unknown = {}
|
|
254
|
+
@sections = SECTIONS.to_h { |section| [ section, {} ] }
|
|
255
|
+
@resources = {}
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# { "Parameters" => { name => Difference }, ... }, only what changed.
|
|
259
|
+
attr_reader :sections
|
|
260
|
+
|
|
261
|
+
# Every resource of both templates, by logical ID: the changed ones and
|
|
262
|
+
# also the ones that did not, because a reference into an unchanged
|
|
263
|
+
# resource can still become a change through a replaced target.
|
|
264
|
+
attr_reader :resources
|
|
265
|
+
|
|
266
|
+
# The resources that change, in the order the CDK renders them: removals,
|
|
267
|
+
# then additions, then updates.
|
|
268
|
+
def changes
|
|
269
|
+
removed = []
|
|
270
|
+
added = []
|
|
271
|
+
updated = []
|
|
272
|
+
@resources.each_value do |change|
|
|
273
|
+
next unless change.different?
|
|
274
|
+
|
|
275
|
+
if change.removal?
|
|
276
|
+
removed << change
|
|
277
|
+
elsif change.addition?
|
|
278
|
+
added << change
|
|
279
|
+
else
|
|
280
|
+
updated << change
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
removed + added + updated
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# How many things the template changes: the CDK's differenceCount.
|
|
287
|
+
def change_count
|
|
288
|
+
template_attributes.length + @sections.sum { |_name, entries| entries.length } + changes.length + @unknown.length
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def empty?
|
|
292
|
+
change_count.zero?
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def security
|
|
296
|
+
@security ||= Security.build(self)
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
# The whole diff as one machine object (--json, section 10).
|
|
300
|
+
def to_h
|
|
301
|
+
{
|
|
302
|
+
"name" => stack_name,
|
|
303
|
+
"has_changes" => !empty?,
|
|
304
|
+
"description" => attribute_to_h(description),
|
|
305
|
+
"format_version" => attribute_to_h(format_version),
|
|
306
|
+
"transform" => attribute_to_h(transform),
|
|
307
|
+
"sections" => @sections.to_h do |section, entries|
|
|
308
|
+
[ section.downcase, entries.to_h { |name, difference| [ name, { "old" => difference.old_value, "new" => difference.new_value } ] } ]
|
|
309
|
+
end,
|
|
310
|
+
"resources" => changes.map { |change| change.to_h(security: security.security_change?(change)) },
|
|
311
|
+
"other" => @unknown.to_h { |key, difference| [ key, { "old" => difference.old_value, "new" => difference.new_value } ] },
|
|
312
|
+
"security" => security.to_h
|
|
313
|
+
}.compact
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
private
|
|
317
|
+
|
|
318
|
+
def attribute_to_h(attribute)
|
|
319
|
+
return nil unless attribute
|
|
320
|
+
|
|
321
|
+
{ "old" => attribute.old_value, "new" => attribute.new_value }
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def template_attributes
|
|
325
|
+
[ format_version, description, transform ].compact
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
# The whole computation, the CDK's calculateTemplateDiff plus its
|
|
329
|
+
# replacement propagation.
|
|
330
|
+
def compute(old_template, new_template)
|
|
331
|
+
compute_template_attributes(old_template, new_template)
|
|
332
|
+
compute_sections(old_template, new_template)
|
|
333
|
+
compute_resources(old_template, new_template)
|
|
334
|
+
propagate_replacements(old_template, new_template)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def compute_template_attributes(old_template, new_template)
|
|
338
|
+
@format_version = difference_for(old_template, new_template, "AWSTemplateFormatVersion")
|
|
339
|
+
@description = difference_for(old_template, new_template, "Description")
|
|
340
|
+
@transform = difference_for(old_template, new_template, "Transform")
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def difference_for(old_template, new_template, key)
|
|
344
|
+
old_value = old_template[key]
|
|
345
|
+
new_value = new_template[key]
|
|
346
|
+
return nil if Compare.equal?(old_value, new_value)
|
|
347
|
+
|
|
348
|
+
Difference.new(old_value, new_value)
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def compute_sections(old_template, new_template)
|
|
352
|
+
SECTIONS.each do |section|
|
|
353
|
+
entries = {}
|
|
354
|
+
Compare.keyed(old_template[section], new_template[section]) do |name, old_value, new_value|
|
|
355
|
+
next if Compare.equal?(old_value, new_value)
|
|
356
|
+
|
|
357
|
+
entries[name] = Difference.new(old_value, new_value)
|
|
358
|
+
end
|
|
359
|
+
@sections[section] = entries
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
@unknown = {}
|
|
363
|
+
Compare.keyed(old_template, new_template) do |key, old_value, new_value|
|
|
364
|
+
next if key == "Resources" || key == "AWSTemplateFormatVersion" || key == "Description" || key == "Transform" || SECTIONS.include?(key)
|
|
365
|
+
next if Compare.equal?(old_value, new_value)
|
|
366
|
+
|
|
367
|
+
@unknown[key] = Difference.new(old_value, new_value)
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
def compute_resources(old_template, new_template)
|
|
372
|
+
Compare.keyed(old_template["Resources"], new_template["Resources"]) do |logical_id, old_value, new_value|
|
|
373
|
+
@resources[logical_id] = build_resource(logical_id, old_value, new_value)
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
def build_resource(logical_id, old_value, new_value)
|
|
378
|
+
old_type = old_value.is_a?(Hash) ? old_value["Type"] : nil
|
|
379
|
+
new_type = new_value.is_a?(Hash) ? new_value["Type"] : nil
|
|
380
|
+
same_type = !old_type.nil? && old_type == new_type
|
|
381
|
+
|
|
382
|
+
properties = same_type ? property_changes(old_type, old_value, new_value) : []
|
|
383
|
+
others = {}
|
|
384
|
+
if same_type
|
|
385
|
+
Compare.keyed(old_value, new_value) do |key, old_side, new_side|
|
|
386
|
+
next if key == "Properties"
|
|
387
|
+
|
|
388
|
+
others[key] = Difference.new(old_side, new_side)
|
|
389
|
+
end
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
ResourceChange.new(logical_id:, old_value:, new_value:, old_type:, new_type:, properties:, others:)
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
# The changed property paths of one resource, each with the impact the
|
|
396
|
+
# schema model gives the path. A property of an unknown type, or one the
|
|
397
|
+
# model does not describe, costs an update: this tool claims a
|
|
398
|
+
# replacement only where AWS's published model says one.
|
|
399
|
+
def property_changes(resource_type, old_value, new_value)
|
|
400
|
+
old_properties = old_value.is_a?(Hash) ? old_value["Properties"] : nil
|
|
401
|
+
new_properties = new_value.is_a?(Hash) ? new_value["Properties"] : nil
|
|
402
|
+
changes = []
|
|
403
|
+
Compare.keyed(old_properties, new_properties) do |name, old_property, new_property|
|
|
404
|
+
walk_property(resource_type, name, [ name ], old_property, new_property, changes)
|
|
405
|
+
end
|
|
406
|
+
changes
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
# The changed leaves under one property: objects are walked by key, lists
|
|
410
|
+
# by index, so the path names the exact place the template changes at.
|
|
411
|
+
# Values of two different kinds, and a property set against one taken
|
|
412
|
+
# away, are one change for the whole value.
|
|
413
|
+
def walk_property(resource_type, name, path, old_value, new_value, out)
|
|
414
|
+
return if Compare.equal?(old_value, new_value)
|
|
415
|
+
|
|
416
|
+
if old_value.is_a?(Hash) && new_value.is_a?(Hash)
|
|
417
|
+
keys = old_value.keys + (new_value.keys - old_value.keys)
|
|
418
|
+
keys.each { |key| walk_property(resource_type, name, path + [ key ], old_value[key], new_value[key], out) }
|
|
419
|
+
return
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
if old_value.is_a?(Array) && new_value.is_a?(Array)
|
|
423
|
+
length = [ old_value.length, new_value.length ].max
|
|
424
|
+
length.times do |index|
|
|
425
|
+
walk_property(resource_type, name, path + [ index ], old_value[index], new_value[index], out)
|
|
426
|
+
end
|
|
427
|
+
return
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
out << PropertyChange.new(name:, path:, old_value:, new_value:, impact: impact_of(resource_type, path))
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
# The cost of changing one path, straight from the model
|
|
434
|
+
# (Schema.update_type): a create-only path is a replacement, a
|
|
435
|
+
# conditional one may be.
|
|
436
|
+
def impact_of(resource_type, path)
|
|
437
|
+
case RubyCdk::Schema.update_type(resource_type, path)
|
|
438
|
+
when :immutable then :replace
|
|
439
|
+
when :conditional then :may_replace
|
|
440
|
+
else :update
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
# The CDK's reference propagation (diff-template.ts, diffTemplate): while
|
|
445
|
+
# some resource is replaced, rewrite what the new template resolves each
|
|
446
|
+
# reference into, and re-diff, until the rewrite settles. Then a resource
|
|
447
|
+
# that references a replaced target through a property whose change
|
|
448
|
+
# replaces it takes that replacement on.
|
|
449
|
+
def propagate_replacements(old_template, new_template)
|
|
450
|
+
copy = Normalizer.normalize(new_template)
|
|
451
|
+
recompute = recompute_resources(old_template, copy)
|
|
452
|
+
loop do
|
|
453
|
+
propagated = !!(recompute.each_value
|
|
454
|
+
.select { |change| change.different? && change.impact == :replace }
|
|
455
|
+
.any? { |change| replace_references(copy, change.logical_id) })
|
|
456
|
+
recompute = recompute_resources(old_template, copy) if propagated
|
|
457
|
+
break unless propagated
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
recompute.each_value
|
|
461
|
+
.select { |change| change.different? && %i[replace may_replace].include?(change.impact) }
|
|
462
|
+
.each do |downstream|
|
|
463
|
+
resource = @resources[downstream.logical_id]
|
|
464
|
+
next if resource.impact == downstream.impact
|
|
465
|
+
|
|
466
|
+
replacements = downstream.properties.select { |property| property.impact == :replace }
|
|
467
|
+
@resources[downstream.logical_id] = resource.with_properties(merge_properties(resource.properties, replacements))
|
|
468
|
+
end
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
# The resources as they differ between the old template and the copy.
|
|
472
|
+
def recompute_resources(old_template, copy)
|
|
473
|
+
recompute = {}
|
|
474
|
+
Compare.keyed(old_template["Resources"], copy["Resources"]) do |logical_id, old_value, new_value|
|
|
475
|
+
recompute[logical_id] = build_resource(logical_id, old_value, new_value)
|
|
476
|
+
end
|
|
477
|
+
recompute
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
# Replaces every reference to the logical ID in the template with the
|
|
481
|
+
# marked form the CDK writes ("LogicalId (replaced)"), so that the next
|
|
482
|
+
# comparison sees what the reference resolves to now. A reference is a
|
|
483
|
+
# one-key object: {"Ref": id}, or an Fn:: intrinsic whose first argument
|
|
484
|
+
# is the ID, as Fn::GetAtt is.
|
|
485
|
+
def replace_references(value, logical_id)
|
|
486
|
+
case value
|
|
487
|
+
when Array
|
|
488
|
+
value.any? { |entry| replace_references(entry, logical_id) }
|
|
489
|
+
when Hash
|
|
490
|
+
replace_in_object(value, logical_id) ||
|
|
491
|
+
value.any? { |entry| replace_references(entry, logical_id) }
|
|
492
|
+
else
|
|
493
|
+
false
|
|
494
|
+
end
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
def replace_in_object(object, logical_id)
|
|
498
|
+
keys = object.keys
|
|
499
|
+
return false unless keys.length == 1
|
|
500
|
+
|
|
501
|
+
key = keys.first
|
|
502
|
+
if key == "Ref" && object[key] == logical_id
|
|
503
|
+
object[key] = "#{logical_id} (replaced)"
|
|
504
|
+
return true
|
|
505
|
+
end
|
|
506
|
+
return false unless key.start_with?("Fn::")
|
|
507
|
+
|
|
508
|
+
arguments = object[key]
|
|
509
|
+
return false unless arguments.is_a?(Array) && arguments.first == logical_id
|
|
510
|
+
|
|
511
|
+
arguments[0] = "#{logical_id} (replaced)"
|
|
512
|
+
true
|
|
513
|
+
end
|
|
514
|
+
|
|
515
|
+
# The downstream replacement's property changes, over what the resource
|
|
516
|
+
# already carries, by property name: an entry the propagation replaces
|
|
517
|
+
# takes the propagated values, as the CDK's setPropertyChange does.
|
|
518
|
+
def merge_properties(existing, replacements)
|
|
519
|
+
names = replacements.map(&:name)
|
|
520
|
+
existing.reject { |property| names.include?(property.name) } + replacements
|
|
521
|
+
end
|
|
522
|
+
end
|
|
523
|
+
end
|
|
524
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "errors"
|
|
4
|
+
require_relative "deploy/ports"
|
|
5
|
+
require_relative "schema"
|
|
6
|
+
require_relative "diff/normalizer"
|
|
7
|
+
require_relative "diff/compare"
|
|
8
|
+
require_relative "diff/statement"
|
|
9
|
+
require_relative "diff/security"
|
|
10
|
+
require_relative "diff/template"
|
|
11
|
+
require_relative "diff/render"
|
|
12
|
+
|
|
13
|
+
module RubyCdk
|
|
14
|
+
# The change preview of two templates (architecture section 10): what would
|
|
15
|
+
# change between the template that is deployed and the one that was
|
|
16
|
+
# synthesized, per resource with what replaces it, and the security view in
|
|
17
|
+
# a table of its own. `ruby-cdk diff` renders it; this module computes it.
|
|
18
|
+
module Diff
|
|
19
|
+
# The diff of two templates. Either side may be nil -- a stack that does
|
|
20
|
+
# not exist yet diffs against nothing, and everything is an Add -- a Hash,
|
|
21
|
+
# or the text of a template (JSON, or YAML when another tool deployed it
|
|
22
|
+
# in that form). Templates are normalized on copies; the caller's objects
|
|
23
|
+
# are never touched. stack_name is carried onto the diff for the renderer
|
|
24
|
+
# and the JSON.
|
|
25
|
+
def self.of(old_template, new_template, stack_name: nil)
|
|
26
|
+
TemplateDiff.build(normal_side(old_template), normal_side(new_template), stack_name:)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# The diff of one stack: the template it deploys against the template the
|
|
30
|
+
# deployed stack answers with, read through the port's `get_template`
|
|
31
|
+
# (module RubyCdk::Deploy::CfnClient) and no other call. A client that
|
|
32
|
+
# answers nil -- the stack does not exist, or was deleted -- diffs against
|
|
33
|
+
# nothing.
|
|
34
|
+
def self.for_stack(client, name, template:, stack_name: nil)
|
|
35
|
+
deployed = client.get_template(name)
|
|
36
|
+
old_template = deployed.nil? ? {} : deployed_template(deployed, name)
|
|
37
|
+
of(old_template, template, stack_name: stack_name || name)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# The text of a diff, for a person (Render.call).
|
|
41
|
+
def self.render(diff, quiet: false, security_only: false)
|
|
42
|
+
Render.call(diff, quiet: quiet, security_only: security_only)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.normal_side(template)
|
|
46
|
+
return nil if template.nil?
|
|
47
|
+
|
|
48
|
+
body = template.is_a?(String) ? Normalizer.parse(template) : template
|
|
49
|
+
raise Error, "a template is a JSON object, not #{body.class}, so it cannot be diffed" unless body.is_a?(Hash)
|
|
50
|
+
|
|
51
|
+
Normalizer.normalize(body)
|
|
52
|
+
end
|
|
53
|
+
private_class_method :normal_side
|
|
54
|
+
|
|
55
|
+
def self.deployed_template(text, name)
|
|
56
|
+
Normalizer.parse(text)
|
|
57
|
+
rescue Error => e
|
|
58
|
+
raise Error, "The deployed template of stack #{name} cannot be read: #{e.message}"
|
|
59
|
+
end
|
|
60
|
+
private_class_method :deployed_template
|
|
61
|
+
end
|
|
62
|
+
end
|
data/lib/ruby_cdk/env.rb
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyCdk
|
|
4
|
+
# A concrete target: an account and a region.
|
|
5
|
+
class Env
|
|
6
|
+
ACCOUNT_PATTERN = /\A\d{12}\z/
|
|
7
|
+
REGION_PATTERN = /\A[a-z]{2}(-[a-z0-9]+)+-\d\z/
|
|
8
|
+
|
|
9
|
+
attr_reader :account, :region
|
|
10
|
+
|
|
11
|
+
def initialize(account:, region:)
|
|
12
|
+
# The account must be a String, as the CDK refuses a non-string one
|
|
13
|
+
# ("Account id of stack environment must be a 'string'", core/lib/stack.ts
|
|
14
|
+
# parseEnvironment). An Integer's to_s would look fine and slip through.
|
|
15
|
+
unless account.is_a?(String) && ACCOUNT_PATTERN.match?(account)
|
|
16
|
+
raise ArgumentError, "account must be a 12-digit account ID as a String, got #{account.inspect}"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
unless region.is_a?(String) && REGION_PATTERN.match?(region)
|
|
20
|
+
raise ArgumentError, "region must look like eu-west-1, got #{region.inspect}"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
@account = account.freeze
|
|
24
|
+
@region = region.freeze
|
|
25
|
+
freeze
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def name
|
|
29
|
+
"#{account}/#{region}"
|
|
30
|
+
end
|
|
31
|
+
alias to_s name
|
|
32
|
+
|
|
33
|
+
def ==(other)
|
|
34
|
+
other.is_a?(Env) && other.account == account && other.region == region
|
|
35
|
+
end
|
|
36
|
+
alias eql? ==
|
|
37
|
+
|
|
38
|
+
def hash
|
|
39
|
+
[ self.class, account, region ].hash
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyCdk
|
|
4
|
+
# Base class of every error this gem raises on purpose.
|
|
5
|
+
class Error < StandardError; end
|
|
6
|
+
|
|
7
|
+
# Exit 1: the command was asked to do something it cannot.
|
|
8
|
+
class UsageError < Error; end
|
|
9
|
+
|
|
10
|
+
# Exit 1: a precondition failed, such as an account mismatch or a busy stack.
|
|
11
|
+
class PreconditionError < Error; end
|
|
12
|
+
|
|
13
|
+
# Exit 3: CloudFormation rejected the change set.
|
|
14
|
+
class ChangeSetRejected < Error; end
|
|
15
|
+
|
|
16
|
+
# Exit 4: approval was refused, or needed and not given.
|
|
17
|
+
class ApprovalRefused < Error; end
|
|
18
|
+
|
|
19
|
+
# Exit 5: the stack operation failed or rolled back.
|
|
20
|
+
class DeploymentFailed < Error; end
|
|
21
|
+
|
|
22
|
+
# Exit 6: security rules above the threshold failed.
|
|
23
|
+
class RulesFailed < Error; end
|
|
24
|
+
|
|
25
|
+
# One problem found by a validation rule. path is the location inside the stack.
|
|
26
|
+
Issue = Data.define(:rule, :path, :message, :hint)
|
|
27
|
+
|
|
28
|
+
# Exit 2: local validation failed. Every issue is reported at once.
|
|
29
|
+
class ValidationError < Error
|
|
30
|
+
attr_reader :issues
|
|
31
|
+
|
|
32
|
+
# issues: an Array of Issue, or of strings.
|
|
33
|
+
def initialize(issues)
|
|
34
|
+
@issues = issues.freeze
|
|
35
|
+
super(issues.map { |issue| issue.is_a?(Issue) ? "#{issue.rule} #{issue.path}: #{issue.message}" : issue.to_s }.join("\n"))
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|