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.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +87 -0
  3. data/LICENSE +21 -0
  4. data/README.md +303 -0
  5. data/THIRD_PARTY_NOTICES.md +327 -0
  6. data/data/spec/META.json +33 -0
  7. data/data/spec/model.json.gz +0 -0
  8. data/exe/ruby-cdk +9 -0
  9. data/lib/ruby-cdk.rb +4 -0
  10. data/lib/ruby_cdk/assets/builder.rb +122 -0
  11. data/lib/ruby_cdk/assets/hasher.rb +175 -0
  12. data/lib/ruby_cdk/assets/record.rb +49 -0
  13. data/lib/ruby_cdk/assets/uploader.rb +139 -0
  14. data/lib/ruby_cdk/assets/zipper.rb +108 -0
  15. data/lib/ruby_cdk/assets.rb +116 -0
  16. data/lib/ruby_cdk/bootstrap.rb +289 -0
  17. data/lib/ruby_cdk/cfn/resource.rb +93 -0
  18. data/lib/ruby_cdk/cfn.rb +10 -0
  19. data/lib/ruby_cdk/cli/arguments.rb +41 -0
  20. data/lib/ruby_cdk/cli/aws.rb +35 -0
  21. data/lib/ruby_cdk/cli/bootstrap.rb +63 -0
  22. data/lib/ruby_cdk/cli/diff.rb +98 -0
  23. data/lib/ruby_cdk/cli/failure.rb +80 -0
  24. data/lib/ruby_cdk/cli/io.rb +70 -0
  25. data/lib/ruby_cdk/cli/list.rb +46 -0
  26. data/lib/ruby_cdk/cli/new.rb +313 -0
  27. data/lib/ruby_cdk/cli/operation.rb +124 -0
  28. data/lib/ruby_cdk/cli/pairs.rb +32 -0
  29. data/lib/ruby_cdk/cli/report.rb +118 -0
  30. data/lib/ruby_cdk/cli/selection.rb +134 -0
  31. data/lib/ruby_cdk/cli/session.rb +290 -0
  32. data/lib/ruby_cdk/cli/shell.rb +30 -0
  33. data/lib/ruby_cdk/cli/synth.rb +58 -0
  34. data/lib/ruby_cdk/cli/validate.rb +101 -0
  35. data/lib/ruby_cdk/cli.rb +242 -0
  36. data/lib/ruby_cdk/constructs/bucket.rb +191 -0
  37. data/lib/ruby_cdk/constructs/function.rb +163 -0
  38. data/lib/ruby_cdk/constructs/grant.rb +90 -0
  39. data/lib/ruby_cdk/constructs/log_group.rb +115 -0
  40. data/lib/ruby_cdk/constructs/policy_statement.rb +161 -0
  41. data/lib/ruby_cdk/constructs/queue.rb +118 -0
  42. data/lib/ruby_cdk/constructs/role.rb +282 -0
  43. data/lib/ruby_cdk/constructs/table.rb +160 -0
  44. data/lib/ruby_cdk/constructs/topic.rb +88 -0
  45. data/lib/ruby_cdk/constructs.rb +56 -0
  46. data/lib/ruby_cdk/core/app.rb +146 -0
  47. data/lib/ruby_cdk/core/construct.rb +105 -0
  48. data/lib/ruby_cdk/core/elements.rb +107 -0
  49. data/lib/ruby_cdk/core/ids.rb +117 -0
  50. data/lib/ruby_cdk/core/stack.rb +291 -0
  51. data/lib/ruby_cdk/core/stateful.rb +156 -0
  52. data/lib/ruby_cdk/core.rb +15 -0
  53. data/lib/ruby_cdk/deploy/capabilities.rb +44 -0
  54. data/lib/ruby_cdk/deploy/deployer.rb +805 -0
  55. data/lib/ruby_cdk/deploy/errors.rb +72 -0
  56. data/lib/ruby_cdk/deploy/fakes.rb +546 -0
  57. data/lib/ruby_cdk/deploy/output/base.rb +167 -0
  58. data/lib/ruby_cdk/deploy/output/human.rb +37 -0
  59. data/lib/ruby_cdk/deploy/output/json.rb +80 -0
  60. data/lib/ruby_cdk/deploy/output/quiet.rb +20 -0
  61. data/lib/ruby_cdk/deploy/parameters.rb +86 -0
  62. data/lib/ruby_cdk/deploy/poller.rb +72 -0
  63. data/lib/ruby_cdk/deploy/ports.rb +205 -0
  64. data/lib/ruby_cdk/deploy/sdk_adapters.rb +608 -0
  65. data/lib/ruby_cdk/deploy/states.rb +62 -0
  66. data/lib/ruby_cdk/deploy.rb +25 -0
  67. data/lib/ruby_cdk/diff/compare.rb +141 -0
  68. data/lib/ruby_cdk/diff/normalizer.rb +83 -0
  69. data/lib/ruby_cdk/diff/render.rb +242 -0
  70. data/lib/ruby_cdk/diff/security.rb +322 -0
  71. data/lib/ruby_cdk/diff/statement.rb +256 -0
  72. data/lib/ruby_cdk/diff/template.rb +524 -0
  73. data/lib/ruby_cdk/diff.rb +62 -0
  74. data/lib/ruby_cdk/env.rb +42 -0
  75. data/lib/ruby_cdk/errors.rb +38 -0
  76. data/lib/ruby_cdk/fn.rb +409 -0
  77. data/lib/ruby_cdk/lazy.rb +55 -0
  78. data/lib/ruby_cdk/nag/config.rb +303 -0
  79. data/lib/ruby_cdk/nag/engine.rb +152 -0
  80. data/lib/ruby_cdk/nag/report.rb +86 -0
  81. data/lib/ruby_cdk/nag/rule.rb +171 -0
  82. data/lib/ruby_cdk/nag/rules/api_gateway_authorization.rb +91 -0
  83. data/lib/ruby_cdk/nag/rules/dynamodb_point_in_time_recovery.rb +45 -0
  84. data/lib/ruby_cdk/nag/rules/ebs_volume_encrypted.rb +41 -0
  85. data/lib/ruby_cdk/nag/rules/ec2_ingress_http.rb +48 -0
  86. data/lib/ruby_cdk/nag/rules/ec2_ingress_other_ports.rb +46 -0
  87. data/lib/ruby_cdk/nag/rules/iam_policy_admin_access.rb +60 -0
  88. data/lib/ruby_cdk/nag/rules/iam_policy_full_access.rb +55 -0
  89. data/lib/ruby_cdk/nag/rules/iam_user_credentials.rb +61 -0
  90. data/lib/ruby_cdk/nag/rules/lambda_runtime_supported.rb +88 -0
  91. data/lib/ruby_cdk/nag/rules/lambda_secret_environment.rb +61 -0
  92. data/lib/ruby_cdk/nag/rules/log_group_retention.rb +40 -0
  93. data/lib/ruby_cdk/nag/rules/policy_statements.rb +128 -0
  94. data/lib/ruby_cdk/nag/rules/rds_storage_encrypted.rb +65 -0
  95. data/lib/ruby_cdk/nag/rules/resource_policy_wildcard_principal.rb +58 -0
  96. data/lib/ruby_cdk/nag/rules/s3_public_access_block.rb +56 -0
  97. data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +136 -0
  98. data/lib/ruby_cdk/nag/rules/stateful_deletion_policy.rb +46 -0
  99. data/lib/ruby_cdk/nag/rules.rb +51 -0
  100. data/lib/ruby_cdk/nag/values.rb +305 -0
  101. data/lib/ruby_cdk/nag.rb +60 -0
  102. data/lib/ruby_cdk/schema/normalizer.rb +164 -0
  103. data/lib/ruby_cdk/schema/property_schema.rb +22 -0
  104. data/lib/ruby_cdk/schema/schema.rb +195 -0
  105. data/lib/ruby_cdk/schema/sources/compiler.rb +56 -0
  106. data/lib/ruby_cdk/schema/sources/descriptor.rb +21 -0
  107. data/lib/ruby_cdk/schema/sources/download.rb +86 -0
  108. data/lib/ruby_cdk/schema/sources/provider_schemas.rb +47 -0
  109. data/lib/ruby_cdk/schema/sources/provider_type.rb +630 -0
  110. data/lib/ruby_cdk/schema/sources/regions.rb +86 -0
  111. data/lib/ruby_cdk/schema/sources/resource_specification.rb +94 -0
  112. data/lib/ruby_cdk/schema/sources/specification_type.rb +159 -0
  113. data/lib/ruby_cdk/schema/sources/update.rb +178 -0
  114. data/lib/ruby_cdk/schema/sources.rb +20 -0
  115. data/lib/ruby_cdk/schema/type_schema.rb +109 -0
  116. data/lib/ruby_cdk/schema/validator/corpus_check.rb +81 -0
  117. data/lib/ruby_cdk/schema/validator/policies.rb +48 -0
  118. data/lib/ruby_cdk/schema/validator/refs.rb +489 -0
  119. data/lib/ruby_cdk/schema/validator/report.rb +92 -0
  120. data/lib/ruby_cdk/schema/validator/rule.rb +119 -0
  121. data/lib/ruby_cdk/schema/validator/scans.rb +146 -0
  122. data/lib/ruby_cdk/schema/validator/structure.rb +469 -0
  123. data/lib/ruby_cdk/schema/validator/template.rb +203 -0
  124. data/lib/ruby_cdk/schema/validator/template_rules.rb +70 -0
  125. data/lib/ruby_cdk/schema/validator/walk.rb +122 -0
  126. data/lib/ruby_cdk/schema/validator.rb +159 -0
  127. data/lib/ruby_cdk/schema.rb +19 -0
  128. data/lib/ruby_cdk/synth/manifest.rb +128 -0
  129. data/lib/ruby_cdk/synth/resolver.rb +326 -0
  130. data/lib/ruby_cdk/synth/rules.rb +83 -0
  131. data/lib/ruby_cdk/synth/serializer.rb +120 -0
  132. data/lib/ruby_cdk/synth/writer.rb +31 -0
  133. data/lib/ruby_cdk/synth.rb +255 -0
  134. data/lib/ruby_cdk/test/capture.rb +133 -0
  135. data/lib/ruby_cdk/test/match.rb +426 -0
  136. data/lib/ruby_cdk/test/matcher.rb +290 -0
  137. data/lib/ruby_cdk/test/private/absent.rb +34 -0
  138. data/lib/ruby_cdk/test/private/conditions.rb +39 -0
  139. data/lib/ruby_cdk/test/private/cyclic.rb +160 -0
  140. data/lib/ruby_cdk/test/private/error.rb +48 -0
  141. data/lib/ruby_cdk/test/private/js.rb +326 -0
  142. data/lib/ruby_cdk/test/private/mappings.rb +39 -0
  143. data/lib/ruby_cdk/test/private/outputs.rb +39 -0
  144. data/lib/ruby_cdk/test/private/parameters.rb +39 -0
  145. data/lib/ruby_cdk/test/private/resources.rb +116 -0
  146. data/lib/ruby_cdk/test/private/section.rb +98 -0
  147. data/lib/ruby_cdk/test/private/sorting.rb +40 -0
  148. data/lib/ruby_cdk/test/private/sparse_matrix.rb +35 -0
  149. data/lib/ruby_cdk/test/private/type.rb +23 -0
  150. data/lib/ruby_cdk/test/template.rb +364 -0
  151. data/lib/ruby_cdk/test.rb +116 -0
  152. data/lib/ruby_cdk/values.rb +8 -0
  153. data/lib/ruby_cdk/version.rb +5 -0
  154. data/lib/ruby_cdk.rb +47 -0
  155. metadata +256 -0
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyCdk
4
+ module Diff
5
+ # Whether two template values mean the same thing, and which entries of two
6
+ # keyed sections differ.
7
+ #
8
+ # equal? is the CDK's deepEqual (cloudformation-diff, lib/diff/util.ts at
9
+ # eca1b73d682d7763955bec30ab5dd52bbc4327b6), which is not a plain deep
10
+ # equality on purpose, because CloudFormation is not strict either:
11
+ #
12
+ # - a numeric 10 and the literal "10" are equal (safeParseFloat there,
13
+ # number here, with JavaScript's Number rules: a String that names no
14
+ # number is NaN, and NaN equals nothing, which is why versions like
15
+ # "0.0.1" against "0.0.2" still count as a change);
16
+ # - a boolean and its stringified counterpart are equal
17
+ # ("true" against true), for the same reason;
18
+ # - a DependsOn list compares equal to the single value it wraps, and two
19
+ # lists compare equal whatever their order, elements matched one to one
20
+ # so duplicates count by multiplicity.
21
+ module Compare
22
+ module_function
23
+
24
+ # True when the two values are equivalent, as above.
25
+ def equal?(lvalue, rvalue)
26
+ return true if lvalue == rvalue
27
+
28
+ if (lvalue.is_a?(String) && boolean?(rvalue)) || (boolean?(lvalue) && rvalue.is_a?(String))
29
+ return true if lvalue.to_s == rvalue.to_s
30
+ end
31
+
32
+ if lvalue.is_a?(String) || rvalue.is_a?(String)
33
+ return true if number(lvalue) == number(rvalue)
34
+ end
35
+
36
+ return false unless same_kind?(lvalue, rvalue)
37
+
38
+ if lvalue.is_a?(Array)
39
+ return false unless lvalue.length == rvalue.length
40
+
41
+ lvalue.each_with_index { |entry, index| return false unless equal?(entry, rvalue[index]) }
42
+ return true
43
+ end
44
+
45
+ if lvalue.is_a?(Hash)
46
+ return false unless lvalue.keys.length == rvalue.keys.length
47
+
48
+ lvalue.each do |key, value|
49
+ return false unless rvalue.key?(key)
50
+ return false unless key == "DependsOn" ? depends_on_equal?(value, rvalue[key]) : equal?(value, rvalue[key])
51
+ end
52
+ return true
53
+ end
54
+
55
+ # Same kind, not equal under ==: two strings that differ, two numbers
56
+ # that differ, nil against nil was taken by == at the top.
57
+ false
58
+ end
59
+
60
+ # The DependsOn comparison of the CDK's dependsOnEqual: a one-element
61
+ # list equals the bare value, two lists equal each other irrespective of
62
+ # order, and two values that are not lists never equal each other here
63
+ # (the normalizer makes lists of DependsOn before the comparison runs).
64
+ def depends_on_equal?(lvalue, rvalue)
65
+ if lvalue.is_a?(Array) != rvalue.is_a?(Array)
66
+ list, other = lvalue.is_a?(Array) ? [ lvalue, rvalue ] : [ rvalue, lvalue ]
67
+ return list.length == 1 && equal?(list.first, other)
68
+ end
69
+
70
+ return false unless lvalue.is_a?(Array)
71
+ return false unless lvalue.length == rvalue.length
72
+
73
+ unmatched = (0...rvalue.length).to_a
74
+ lvalue.each do |entry|
75
+ at = unmatched.find_index { |index| equal?(entry, rvalue[index]) }
76
+ return false if at.nil?
77
+
78
+ unmatched.delete_at(at)
79
+ end
80
+ true
81
+ end
82
+
83
+ # JavaScript's Number, for the numeric equivalence above. A String that
84
+ # names no number, and any other kind of value, is NaN: equal to nothing,
85
+ # not even to itself.
86
+ def number(value)
87
+ case value
88
+ when Integer, Float then value.to_f
89
+ when String
90
+ stripped = value.strip
91
+ return 0.0 if stripped.empty?
92
+
93
+ parsed = begin
94
+ Kernel.Float(stripped)
95
+ rescue ArgumentError, TypeError
96
+ nil
97
+ end
98
+ parsed || Float::NAN
99
+ when nil then 0.0
100
+ when true then 1.0
101
+ when false then 0.0
102
+ when Array
103
+ elements = value.map { |entry| entry.is_a?(String) ? entry : (entry.is_a?(Array) ? entry.compact.join(",") : "") }
104
+ number(elements.join(","))
105
+ else Float::NAN
106
+ end
107
+ end
108
+
109
+ # Every entry of both keyed maps, in the order the old one holds them and
110
+ # then the new one's, so the comparison walks the template as it was
111
+ # written. Yields the key and both values (nil for the side that lacks it).
112
+ def keyed(old_map, new_map)
113
+ old_map = {} unless old_map.is_a?(Hash)
114
+ new_map = {} unless new_map.is_a?(Hash)
115
+ keys = old_map.keys + (new_map.keys - old_map.keys)
116
+ keys.each { |key| yield key, old_map[key], new_map[key] }
117
+ end
118
+
119
+ # True when the two values are of the same kind, in the sense the CDK's
120
+ # typeof and Array.isArray checks make: strings with strings, numbers
121
+ # with numbers, booleans with booleans, lists with lists, objects with
122
+ # objects.
123
+ def same_kind?(lvalue, rvalue)
124
+ case [ lvalue.class, rvalue.class ]
125
+ in [ String, String ] then true
126
+ in [ TrueClass | FalseClass, TrueClass | FalseClass ] then true
127
+ in [ Integer | Float, Integer | Float ] then true
128
+ in [ Array, Array ] then true
129
+ in [ Hash, Hash ] then true
130
+ in [ NilClass, NilClass ] then true
131
+ else false
132
+ end
133
+ end
134
+
135
+ # True for true and false, and for nothing else.
136
+ def boolean?(value)
137
+ value == true || value == false
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "yaml"
5
+ require_relative "../errors"
6
+
7
+ module RubyCdk
8
+ module Diff
9
+ # Reads a template from text, and prepares both templates of a diff for the
10
+ # comparison.
11
+ #
12
+ # The deployed template comes back from the port's `get_template` as text:
13
+ # JSON, or YAML when another tool deployed it in that form. A template that
14
+ # is neither is refused, because guessing at how CloudFormation would read
15
+ # it is how a wrong diff gets printed (the deploy engine refuses a YAML
16
+ # template outright, architecture section 9; the diff still works when the
17
+ # text parses as YAML).
18
+ #
19
+ # Normalization never touches the caller's objects: it works on deep copies
20
+ # (the CDK does the same in cloudformation-diff's
21
+ # diffNormalizedTemplates, so that a reordered DependsOn in a template the
22
+ # caller still holds is never seen as a change). What it does is what the
23
+ # CDK's normalize does (lib/diff-template.ts at eca1b73d):
24
+ #
25
+ # - `DependsOn` written as one String becomes a one-element list, and a
26
+ # list is sorted, so order and shape carry no meaning;
27
+ # - `Fn::GetAtt` written in the short form "LogicalId.Attribute" becomes
28
+ # the list form, so both spellings compare equal;
29
+ # - symbols become strings, because a Hash built in Ruby has symbol keys
30
+ # and a template read from JSON does not.
31
+ module Normalizer
32
+ # A template as text, parsed: JSON first, then YAML. Raises
33
+ # RubyCdk::Error with the way out when neither parses. A Hash passes
34
+ # through: the port's get_template answers text, and a test's fake may
35
+ # answer the template as the Hash it registered.
36
+ def self.parse(text)
37
+ return text if text.is_a?(Hash)
38
+
39
+ body = JSON.parse(text)
40
+ return body if body.is_a?(Hash)
41
+
42
+ raise Error, "the template is a JSON document but not an object, so it cannot be a template"
43
+ rescue JSON::ParserError, TypeError
44
+ begin
45
+ body = YAML.safe_load(text, aliases: true)
46
+ rescue Psych::SyntaxError, Psych::DisallowedClass, ArgumentError
47
+ raise Error, "the template is neither JSON nor YAML a reader of this tool understands: " \
48
+ "deploy it as JSON, or check what `get_template` answers for the stack"
49
+ end
50
+ return body if body.is_a?(Hash)
51
+
52
+ raise Error, "the template parses, but not into an object, so it cannot be a template"
53
+ end
54
+
55
+ # A normalized deep copy of the template.
56
+ def self.normalize(template)
57
+ case template
58
+ when Hash then normalize_hash(template)
59
+ when Array then template.map { |item| normalize(item) }
60
+ else template
61
+ end
62
+ end
63
+
64
+ # The DependsOn value, as the sorted list the comparison expects.
65
+ def self.depends_on(value)
66
+ list = value.is_a?(Array) ? value : [ value ]
67
+ list.map { |entry| entry.is_a?(String) ? entry : entry.to_s }.sort
68
+ end
69
+
70
+ def self.normalize_hash(template)
71
+ template.each_with_object({}) do |(key, value), normalized|
72
+ key = key.to_s unless key.is_a?(String)
73
+ normalized[key] = case key
74
+ when "DependsOn" then depends_on(value)
75
+ when "Fn::GetAtt" then value.is_a?(String) ? value.split(".") : normalize(value)
76
+ else normalize(value)
77
+ end
78
+ end
79
+ end
80
+ private_class_method :normalize_hash
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,242 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module RubyCdk
6
+ module Diff
7
+ # The diff, as text for a person. What is printed is the CDK's
8
+ # formatDifferences (cloudformation-diff, lib/format.ts at eca1b73d):
9
+ # the stack's name, then the sections in its order -- the template's own
10
+ # attributes, the security tables, Parameters, Metadata, Mappings,
11
+ # Conditions, Resources with the property changes under each resource, and
12
+ # Outputs, with anything else under Other Changes. A resource carries one
13
+ # of the markers [+], [-] and [~], the words "replace", "may be replaced",
14
+ # "destroy" and "orphan" for what happens to it, and a line per changed
15
+ # property path with the values on both sides.
16
+ #
17
+ # Two departures from the CDK, both recorded (architecture section 21):
18
+ # its tables are drawn with unicode box characters and colour; this
19
+ # rendering is plain text, so that a golden test can hold it and a pipe
20
+ # carries it whole. And it does not print the CDK's warning that its
21
+ # security view may miss changes, because this one reads every property a
22
+ # template carries policy statements in and misses none.
23
+ module Render
24
+ module_function
25
+
26
+ # The text of one stack's diff. quiet leaves out the stack's name and
27
+ # the nothing-to-do lines, as the CDK's --quiet does; security_only
28
+ # shows the security view alone, as its --security-only does.
29
+ def call(diff, quiet: false, security_only: false)
30
+ lines = [ header(diff, quiet, security_only) ]
31
+ lines.concat(security_only ? security_only_view(diff, quiet) : full_view(diff, quiet))
32
+ lines.compact.join("\n").chomp
33
+ end
34
+
35
+ # The stack's name, printed whenever there is something to read, even
36
+ # under --quiet with changes, and even over an empty diff without quiet
37
+ # -- the CDK's rule (stackName && (!options.quiet || !diff.isEmpty)).
38
+ def header(diff, quiet, security_only)
39
+ changed = security_only ? diff.security.has_changes? : !diff.empty?
40
+ return nil if diff.stack_name.nil? || (quiet && !changed)
41
+
42
+ "Stack #{diff.stack_name}"
43
+ end
44
+
45
+ def full_view(diff, quiet)
46
+ return [] if quiet && diff.empty?
47
+ return [ NO_CHANGES ] if diff.empty?
48
+
49
+ [ *template_section(diff), *security_section(diff), *simple_sections(diff), *resources_section(diff) ]
50
+ end
51
+
52
+ def security_only_view(diff, quiet)
53
+ return [] if quiet && !diff.security.has_changes?
54
+ return [ NO_SECURITY_CHANGES ] unless diff.security.has_changes?
55
+
56
+ [ *warning(diff.security.broadening?), *tables(diff.security) ]
57
+ end
58
+
59
+ def template_section(diff)
60
+ lines = []
61
+ lines << value_line(diff.format_version, "AWSTemplateFormatVersion") if diff.format_version
62
+ lines << value_line(diff.transform, "Transform") if diff.transform
63
+ lines << value_line(diff.description, "Description") if diff.description
64
+ section("Template", lines)
65
+ end
66
+
67
+ # The security view: the warning when the deployment broadens what the
68
+ # deployed template may do, then the statement table, then the managed
69
+ # policy table, then the capabilities that appeared.
70
+ def security_section(diff)
71
+ security = diff.security
72
+ return [] unless security.has_changes?
73
+
74
+ [ *warning(security.broadening?), *tables(security), "" ]
75
+ end
76
+
77
+ def warning(broadening)
78
+ return [] unless broadening
79
+
80
+ [ "This deployment will make potentially sensitive changes according to your current security approval level.",
81
+ "Please confirm you intend to make the following modifications:" ]
82
+ end
83
+
84
+ def tables(security)
85
+ lines = []
86
+ if security.statement_additions.any? || security.statement_removals.any?
87
+ rows = security.statement_additions.map { |statement| row("+", statement) } +
88
+ security.statement_removals.map { |statement| row("-", statement) }
89
+ rows.sort_by! { |cells| cells[1].to_s }
90
+ lines << "IAM Statement Changes"
91
+ lines.concat(table([ "", "Resource", "Effect", "Action", "Principal", "Condition" ], rows))
92
+ lines << ""
93
+ end
94
+ if security.managed_policy_additions.any? || security.managed_policy_removals.any?
95
+ rows = security.managed_policy_additions.map { |attachment| [ "+", attachment.identity, attachment.arn ] } +
96
+ security.managed_policy_removals.map { |attachment| [ "-", attachment.identity, attachment.arn ] }
97
+ rows.sort_by! { |cells| cells[1].to_s }
98
+ lines << "IAM Policy Changes"
99
+ lines.concat(table([ "", "Resource", "Managed Policy ARN" ], rows))
100
+ lines << ""
101
+ end
102
+ if security.capabilities_added.any?
103
+ reason = if security.capabilities_added.include?("CAPABILITY_NAMED_IAM")
104
+ "the new template creates IAM resources, some with explicit names"
105
+ else
106
+ "the new template creates IAM resources"
107
+ end
108
+ lines << "New capabilities needed: #{security.capabilities_added.join(", ")}, because #{reason}."
109
+ end
110
+ lines
111
+ end
112
+
113
+ # One row of the statement table, the CDK's summarizeStatements: the
114
+ # marker, then what the statement says, rendered for a person.
115
+ def row(marker, statement)
116
+ rendered = statement.render
117
+ [ marker, rendered[:resource], rendered[:effect], rendered[:action], rendered[:principal], rendered[:condition] ]
118
+ end
119
+
120
+ def simple_sections(diff)
121
+ lines = []
122
+ {
123
+ "Parameters" => "Parameter",
124
+ "Metadata" => "Metadata",
125
+ "Mappings" => "Mapping",
126
+ "Conditions" => "Condition",
127
+ "Outputs" => "Output"
128
+ }.each do |title, entry_type|
129
+ entries = diff.sections[title]
130
+ next if entries.empty?
131
+
132
+ lines.concat(section(title, entries.map { |name, difference| value_line(difference, entry_type, name) }))
133
+ end
134
+ lines.concat(section("Other Changes", diff.unknown.map { |key, difference| value_line(difference, "Unknown", key) }))
135
+ lines
136
+ end
137
+
138
+ def resources_section(diff)
139
+ changes = diff.changes
140
+ return [] if changes.empty?
141
+
142
+ lines = [ "Resources" ]
143
+ changes.each do |change|
144
+ lines << resource_line(change)
145
+ lines.concat(property_lines(change.properties))
146
+ end
147
+ lines << ""
148
+ lines
149
+ end
150
+
151
+ # A titled section, printed only when it has lines to show, closed by an
152
+ # empty line as the CDK's printSectionFooter is.
153
+ def section(title, lines)
154
+ return [] if lines.empty?
155
+
156
+ [ title, *lines, "" ]
157
+ end
158
+
159
+ # `[~] AWS::S3::Bucket Bucket replace` -- the marker, the type, the
160
+ # logical ID, and what happens to the resource when it is more than an
161
+ # update.
162
+ def resource_line(change)
163
+ parts = [ "[#{marker_for(change)}] #{change.old_type || change.new_type} #{change.logical_id}" ]
164
+ parts << change.word if change.word
165
+ parts.join(" ")
166
+ end
167
+
168
+ def marker_for(change)
169
+ return "-" if change.removal?
170
+ return "+" if change.addition?
171
+ return "+" if change.import?
172
+
173
+ "~"
174
+ end
175
+
176
+ # One line per changed property path, values on both sides under it. The
177
+ # last property closes its tree with └─, the others with ├─.
178
+ def property_lines(properties)
179
+ properties.each_with_index.flat_map do |property, index|
180
+ last = index == properties.length - 1
181
+ branch = last ? "└" : "├"
182
+ stem = last ? " " : "│ "
183
+ annotation = property.annotation ? " (#{property.annotation})" : ""
184
+ [ " #{branch}─ [#{property.tag}] #{property.path.join(".")}#{annotation}" ] +
185
+ values_of(property).map { |line| " #{stem}#{line}" }
186
+ end
187
+ end
188
+
189
+ # The two sides of a property change, as the CDK's formatObjectDiff
190
+ # prints them for a scalar: the old value struck out with [-], the new
191
+ # one added with [+]. A value that is not a string is its JSON.
192
+ def values_of(property)
193
+ if property.update?
194
+ [ "├─ [-] #{shown(property.old_value)}", "└─ [+] #{shown(property.new_value)}" ]
195
+ elsif property.removal?
196
+ [ "└─ [-] #{shown(property.old_value)}" ]
197
+ else
198
+ [ "└─ [+] #{shown(property.new_value)}" ]
199
+ end
200
+ end
201
+
202
+ def shown(value)
203
+ value.is_a?(String) ? value : JSON.generate(value)
204
+ end
205
+
206
+ # `[+] Parameter EnvType: prod`, `[~] Parameter EnvType: prod to
207
+ # staging`, `[-] Parameter EnvType` -- the CDK's formatDifference.
208
+ def value_line(difference, entry_type, name = nil)
209
+ values = if difference.update?
210
+ "#{shown(difference.old_value)} to #{shown(difference.new_value)}"
211
+ elsif difference.removal?
212
+ shown(difference.old_value)
213
+ else
214
+ shown(difference.new_value)
215
+ end
216
+ marker = difference.update? ? "~" : (difference.removal? ? "-" : "+")
217
+ "[#{marker}] #{entry_type}#{name ? " #{name}" : ""}: #{values}"
218
+ end
219
+
220
+ # A plain-text table: every column as wide as its widest line, two
221
+ # spaces between columns, a cell that wraps onto several lines keeping
222
+ # its column. The CDK draws a box with unicode rules and colour; this is
223
+ # the same rows without them.
224
+ def table(header, rows)
225
+ all_rows = [ header ] + rows
226
+ widths = header.length.times.map do |column|
227
+ all_rows.flat_map { |cells| (cells[column] || "").to_s.split("\n", -1) }.map(&:length).max || 0
228
+ end
229
+ all_rows.flat_map do |cells|
230
+ cell_lines = cells.map { |cell| (cell || "").to_s.split("\n", -1) }
231
+ height = cell_lines.map(&:length).max
232
+ height.times.map do |line|
233
+ cell_lines.each_with_index.map { |lines, column| (lines[line] || "").ljust(widths[column]) }.join(" ").rstrip
234
+ end
235
+ end
236
+ end
237
+
238
+ NO_CHANGES = "There were no differences"
239
+ NO_SECURITY_CHANGES = "There were no security-related changes"
240
+ end
241
+ end
242
+ end