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,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rule"
4
+ require_relative "scans"
5
+ require_relative "template"
6
+
7
+ module RubyCdk
8
+ module Validation
9
+ module Rules
10
+ # V23: intrinsics in CreationPolicy and UpdatePolicy only where CloudFormation takes
11
+ # them, which is in the place of a string, a number or a boolean, and never in the
12
+ # place of an object.
13
+ #
14
+ # This is what the CDK refuses by default when it reads a template, behind the
15
+ # feature flag cfnIncludeRejectComplexResourceUpdateCreatePolicyIntrinsics
16
+ # (packages/aws-cdk-lib/cx-api/lib/features.ts, recommendedValue true, at
17
+ # aws/aws-cdk@68b2516c): "it uses an intrinsic in a resource update or deletion
18
+ # policy to represent a non-primitive value" (core/lib/helpers-internal/cfn-parse.ts,
19
+ # throwIfIsIntrinsic, over the policy itself and over each of its sections).
20
+ #
21
+ # Architecture §6 says "not in a resource-signal count". The CDK's own tests say
22
+ # otherwise: cloudformation-include/test/test-templates/intrinsics-create-policy-
23
+ # resource-signal.json and intrinsics-create-policy-complex.json are accepted, and
24
+ # hold a Ref and an Fn::If as the count of the signal. What it refuses is an
25
+ # intrinsic where an object belongs, so that is what this rule reports.
26
+ class PolicyIntrinsics < Rule
27
+ rule "V23"
28
+
29
+ # The sections of each policy that take an object, by the section they hang under.
30
+ CREATION = { "AutoScalingCreationPolicy" => {}, "ResourceSignal" => {} }.freeze
31
+ UPDATE = {
32
+ "AutoScalingReplacingUpdate" => {},
33
+ "AutoScalingRollingUpdate" => {},
34
+ "AutoScalingScheduledAction" => {},
35
+ "CodeDeployLambdaAliasUpdate" => {},
36
+ "AutoScalingInstanceRefresh" => { "Preferences" => { "AlarmSpecification" => {} } }
37
+ }.freeze
38
+
39
+ def call(context)
40
+ issues = []
41
+ Scans::Resources.each(context) do |_name, body, path|
42
+ issues.concat(refused(body["CreationPolicy"], path + [ "CreationPolicy" ], "CreationPolicy", CREATION))
43
+ issues.concat(refused(body["UpdatePolicy"], path + [ "UpdatePolicy" ], "UpdatePolicy", UPDATE))
44
+ end
45
+ issues
46
+ end
47
+
48
+ private
49
+
50
+ # The intrinsics written in the place of the policy or of one of its sections.
51
+ def refused(object, at, label, sections)
52
+ return [] unless object.is_a?(Hash)
53
+
54
+ issues = Template.intrinsic?(object) ? [ not_an_object(at, label) ] : []
55
+ sections.each do |section, deeper|
56
+ next unless object[section].is_a?(Hash)
57
+
58
+ issues.concat(refused(object[section], at + [ section ], "#{label}.#{section}", deeper))
59
+ end
60
+ issues
61
+ end
62
+
63
+ def not_an_object(at, label)
64
+ error(at, "an intrinsic is written where #{label} takes an object: CloudFormation resolves an intrinsic there only to a string, a number or a boolean",
65
+ hint: "keep the object, and put the intrinsic in one of its leaves, as ResourceSignal: { Count: { Fn::If: [ ... ] } }")
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../normalizer"
4
+ require_relative "template"
5
+
6
+ module RubyCdk
7
+ module Validation
8
+ # Walks a resource's properties the way the schema model describes them. Nothing here
9
+ # knows a rule: it is how the rules reach every node and every value the model names.
10
+ #
11
+ # A node is a TypeSchema or one of its Definitions: both answer properties and required.
12
+ # A key is read as the normalizer reads it (Normalizer.lookup), so the walk reaches the
13
+ # same values whether or not the template was normalized.
14
+ #
15
+ # The walk stops where the model stops: at a value of kind :json, past a scalar, and at
16
+ # every intrinsic, which stands for a value only CloudFormation will know. An Fn::If is
17
+ # the exception: each of its branches stands for the property's value, so each is
18
+ # walked as that value would have been (the CDK leaves an intrinsic alone,
19
+ # packages/aws-cdk-lib/core/lib/runtime.ts, canInspect, and keeps both branches of an
20
+ # Fn::If in helpers-internal/cfn-parse.ts).
21
+ module SchemaWalk
22
+ module_function
23
+
24
+ # Yields [ type, node, hash, segments ] for the resource's properties (node is then
25
+ # the type itself) and for every object below them that the model describes.
26
+ def each_node(type, properties, segments = [], &block)
27
+ visit_node(type, type, properties, segments, block)
28
+ end
29
+
30
+ # Yields [ type, node, property, value, segments ] for every value the model gives a
31
+ # kind, under the resource's properties: a list as the list, each of its elements,
32
+ # and so on. node is the type or the definition the property belongs to.
33
+ def each_value(type, properties, segments = [], &block)
34
+ visit_values(type, type, properties, segments, block)
35
+ end
36
+
37
+ def visit_node(type, node, value, segments, block)
38
+ return unless value.is_a?(Hash) && !Template.intrinsic?(value)
39
+
40
+ block.call(type, node, value, segments)
41
+ value.each do |key, item|
42
+ property = Normalizer.lookup(node, key.to_s)
43
+ visit_node_property(type, property, item, segments + [ key.to_s ], block) if property
44
+ end
45
+ end
46
+
47
+ def visit_values(type, node, hash, segments, block)
48
+ return unless hash.is_a?(Hash) && !Template.intrinsic?(hash)
49
+
50
+ hash.each do |key, item|
51
+ property = Normalizer.lookup(node, key.to_s)
52
+ visit_value(type, node, property, item, segments + [ key.to_s ], block) if property
53
+ end
54
+ end
55
+
56
+ def visit_node_property(type, property, value, segments, block)
57
+ definition = type.definitions[property.definition]
58
+ return unless definition
59
+
60
+ branches(value, segments) do |inner, path|
61
+ objects(property, inner, path) { |object, at| visit_node(type, definition, object, at, block) }
62
+ end
63
+ end
64
+
65
+ def visit_value(type, node, property, value, segments, block)
66
+ branches(value, segments) do |inner, path|
67
+ block.call(type, node, property, inner, path)
68
+ definition = type.definitions[property.definition]
69
+ objects(property, inner, path) { |object, at| visit_values(type, definition, object, at, block) } if definition
70
+ end
71
+ end
72
+
73
+ # Yields the value where a property has one, or each branch of an Fn::If in its place
74
+ # (and a branch of a branch, and so on). Never a nil, never another intrinsic: those
75
+ # stand for nothing a rule can look at.
76
+ def branches(value, segments)
77
+ if Template.function?(value, "Fn::If")
78
+ list = value["Fn::If"]
79
+ return unless list.is_a?(Array) && list.size == 3
80
+
81
+ branches = list[1, 2]
82
+ return unless branches.is_a?(Array)
83
+
84
+ branches.each_with_index do |branch, index|
85
+ path = segments + [ "Fn::If", "[#{index + 1}]" ]
86
+ next if branch.nil? || (Template.intrinsic?(branch) && !Template.function?(branch, "Fn::If"))
87
+
88
+ branches(branch, path) { |inner, at| yield inner, at }
89
+ end
90
+ elsif !value.nil? && !Template.intrinsic?(value)
91
+ yield value, segments
92
+ end
93
+ end
94
+
95
+ # Yields each object a property holds a definition for: the value of a :nested
96
+ # property, each object of a list or a map of them. Nothing when the model stops here.
97
+ def objects(property, value, segments)
98
+ case [ property.kind, property.item_kind ]
99
+ when [ :nested, nil ]
100
+ yield value, segments if value.is_a?(Hash)
101
+ when [ :list, :nested ]
102
+ value.each_with_index { |item, index| yield item, segments + [ "[#{index}]" ] } if value.is_a?(Array)
103
+ when [ :map, :nested ]
104
+ value.each { |key, item| yield item, segments + [ key.to_s ] } if value.is_a?(Hash)
105
+ end
106
+ end
107
+
108
+ # Yields [ definition, segments ] for each nil element of a list of objects with
109
+ # required properties. A nil where such an object belongs is how a template says
110
+ # "nothing here", and its required properties are missing, as the CDK's list
111
+ # validators report (core/lib/runtime.ts: "allowedMethods: required but missing").
112
+ def each_nil_element(type, property, value, segments)
113
+ return unless property.kind == :list && property.item_kind == :nested && value.is_a?(Array)
114
+
115
+ definition = type.definitions[property.definition]
116
+ return unless definition
117
+
118
+ value.each_with_index { |item, index| yield definition, segments + [ "[#{index}]" ] if item.nil? }
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The validation component: the normalizer and the rules of architecture §6, over a
4
+ # template as a plain Hash and on the schema model. It depends on nothing above it. The
5
+ # pipeline learns it only through what is registered with it, and the umbrella does the
6
+ # registering:
7
+ #
8
+ # RubyCdk::Synth.register_normalizer(RubyCdk::Validation.normalizers.first)
9
+ # RubyCdk::Validation.validators.each { |validator| RubyCdk::Synth.register_validator(validator) }
10
+ #
11
+ # A normalizer answers call(context) with a new template, its keys resolved. A validator
12
+ # answers call(context) with an Array of RubyCdk::Issue. The context is anything that
13
+ # answers stack, template and issues.
14
+ require_relative "normalizer"
15
+ require_relative "validator/rule"
16
+ require_relative "validator/template"
17
+ require_relative "validator/walk"
18
+ require_relative "validator/scans"
19
+ require_relative "validator/structure"
20
+ require_relative "validator/refs"
21
+ require_relative "validator/template_rules"
22
+ require_relative "validator/policies"
23
+ require_relative "validator/corpus_check"
24
+ require_relative "validator/report"
25
+
26
+ module RubyCdk
27
+ # What CloudFormation would reject, found before a template leaves the machine.
28
+ module Validation
29
+ # Every rule, by its id, in the order they run: the order of architecture §6.
30
+ RULES = {
31
+ "V1" => Rules::KnownTypes,
32
+ "V2" => Rules::LogicalIds,
33
+ "V3" => Rules::Properties,
34
+ "V4" => Rules::AmbiguousProperties,
35
+ "V5" => Rules::Required,
36
+ "V6" => Rules::Kinds,
37
+ "V7" => Rules::Lists,
38
+ "V8" => Rules::References,
39
+ "V9" => Rules::Attributes,
40
+ "V10" => Rules::Substitutions,
41
+ "V11" => Rules::Targets,
42
+ "V12" => Rules::Quotas,
43
+ "V13" => Rules::StackName,
44
+ "V14" => Rules::Cycles,
45
+ "V15" => Rules::StatefulDeletion,
46
+ "V16" => Rules::Policies,
47
+ "V17" => Rules::QuotedValues,
48
+ "V18" => Rules::AllowedValues,
49
+ "V19" => Rules::Patterns,
50
+ "V20" => Rules::EmptySubstitution,
51
+ "V21" => Rules::ShortFormGetAtt,
52
+ "V22" => Rules::NoGetAttInConditions,
53
+ "V23" => Rules::PolicyIntrinsics
54
+ }.freeze
55
+
56
+ # The rules the pipeline runs itself, from the tree, where the tree sees more than a
57
+ # template does (synth/rules.rb): V2 over the logical IDs of the whole stack, V13 over
58
+ # the stack's name. validators leaves them out unless told that it is the only check.
59
+ PIPELINE_RULES = %w[V2 V13].freeze
60
+
61
+ # The one rule that needs something this component does not own: the list of stateful
62
+ # types (core/stateful.rb), given as stateful_types.
63
+ STATEFUL_RULE = "V15"
64
+
65
+ # How a validator answers a warning. The pipeline fails a synthesis on every Issue it
66
+ # is handed, so by default the validators hand it the errors alone. A pipeline that
67
+ # keeps warnings apart asks for warnings: true and sorts them with
68
+ # Validation.warning?; until then, warnings: :only is the separate list of validators
69
+ # that answer the warnings and nothing else.
70
+ WARNINGS = %i[only].freeze
71
+
72
+ # What the rules receive, as the pipeline gives it to them: the stack (nil when the
73
+ # template has none), the template, and what the phases before found.
74
+ Context = Data.define(:stack, :template, :issues)
75
+
76
+ # What stands for a stack when there is no tree: a name, and nothing else.
77
+ NamedStack = Data.define(:stack_name)
78
+
79
+ class << self
80
+ # The ids of every rule, in the order they run.
81
+ def rule_ids
82
+ RULES.keys
83
+ end
84
+
85
+ # One rule on its own, built with what it takes: strict_types: for V1, types: for V15.
86
+ def rule(id, **options)
87
+ build(id, **options)
88
+ end
89
+
90
+ # The normalizers, in the order they run: one, which resolves the keys.
91
+ def normalizers(**_options)
92
+ [ Normalizer ]
93
+ end
94
+
95
+ # The validators, in the order they run, each answering call(context) with its issues.
96
+ #
97
+ # strict_types: V1 makes an unknown type an error, not a warning.
98
+ # disabled: the ids of rules to leave out.
99
+ # pipeline_rules: false when the pipeline's own V2 and V13 are not running, so
100
+ # that this list must carry them (a template with no tree).
101
+ # stateful_types: the types architecture §7 retains; V15 is built only with it.
102
+ # warnings: true to answer warnings with the errors; :only to answer the
103
+ # warnings alone; the default answers the errors alone.
104
+ def validators(strict_types: false, disabled: [], pipeline_rules: true, stateful_types: nil, warnings: false)
105
+ raise ArgumentError, "warnings: takes true, false or :only, got #{warnings.inspect}" unless warnings == true || warnings == false || WARNINGS.include?(warnings)
106
+
107
+ off = Array(disabled).map(&:to_s)
108
+ unknown = off - RULES.keys
109
+ raise ArgumentError, "no rule #{unknown.join(", ").inspect}: the rules are #{RULES.keys.join(", ")}" unless unknown.empty?
110
+
111
+ RULES.filter_map do |id, klass|
112
+ next if off.include?(id) || (pipeline_rules && PIPELINE_RULES.include?(id)) || (id == STATEFUL_RULE && stateful_types.nil?)
113
+
114
+ klass.new(**options_of(id, strict_types, stateful_types))
115
+ end.map { |rule| answering(rule, warnings) }
116
+ end
117
+
118
+ # One template, normalized and validated, with nothing left out: for a command
119
+ # that reports, and for a template that has no tree behind it. stack_name is the
120
+ # name of the stack the template belongs to, which V13 needs.
121
+ def validate(template, stack_name: nil, **options)
122
+ normalized = Normalizer.normalize(template)
123
+ context = Context.new(stack: stack_name && NamedStack.new(stack_name.to_s), template: normalized, issues: [])
124
+ issues = validators(**{ pipeline_rules: false, warnings: true }.merge(options)).flat_map { |validator| validator.call(context) }
125
+ Report.new(issues, normalized)
126
+ end
127
+
128
+ private
129
+
130
+ def build(id, strict_types: false, types: nil, **options)
131
+ raise ArgumentError, "rule #{id} takes no #{options.keys.join(", ")}:" unless options.empty?
132
+ raise ArgumentError, "rule V15 needs types:, the stateful types of architecture §7" if id.to_s == STATEFUL_RULE && types.nil?
133
+
134
+ klass = RULES[id.to_s] or raise ArgumentError, "no rule #{id.inspect}: the rules are #{RULES.keys.join(", ")}"
135
+ klass.new(**options_of(id.to_s, strict_types, types))
136
+ end
137
+
138
+ def options_of(id, strict_types, stateful_types)
139
+ case id
140
+ when "V1" then { strict_types: }
141
+ when STATEFUL_RULE then { types: stateful_types }
142
+ else {}
143
+ end
144
+ end
145
+
146
+ # The callable the pipeline registers: the rule, answering only what was asked for.
147
+ def answering(rule, warnings)
148
+ case warnings
149
+ when false
150
+ ->(context) { rule.call(context).reject { |issue| Validation.warning?(issue) } }
151
+ when :only
152
+ ->(context) { rule.call(context).select { |issue| Validation.warning?(issue) } }
153
+ else
154
+ ->(context) { rule.call(context) }
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "schema/property_schema"
4
+ require_relative "schema/type_schema"
5
+ require_relative "schema/schema"
6
+
7
+ module RubyCdk
8
+ # What AWS resources look like: one small model vendored from the two sources AWS
9
+ # publishes, the provider schemas and the resource specification. The rules and the
10
+ # diff talk to the model, never to a source.
11
+ #
12
+ # RubyCdk::Schema.type("AWS::Lambda::Function") # a TypeSchema, or nil for an unknown type
13
+ # RubyCdk::Schema.update_type(type, "Code.S3Bucket") # :immutable
14
+ #
15
+ # The model is parsed on the first lookup. `rake spec:update` rebuilds it, from
16
+ # lib/ruby_cdk/schema/sources, which nothing else loads.
17
+ module Schema
18
+ end
19
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require_relative "../errors"
5
+ require_relative "../version"
6
+ require_relative "serializer"
7
+ require_relative "writer"
8
+
9
+ module RubyCdk
10
+ module Synth
11
+ # What synthesis wrote (architecture §12): manifest.json and, in it, one
12
+ # entry per stack. A template carries nothing that varies from one machine
13
+ # to another; the manifest does.
14
+ class Manifest
15
+ FILE_NAME = "manifest.json"
16
+
17
+ # The key that says which model was consulted. The component that owns
18
+ # the model registers it with Synth.register_manifest_fact; until one
19
+ # does it is written as null.
20
+ SCHEMA_VERSION_KEY = "schema_version"
21
+
22
+ # The key of every stack's entry that lists the hashes of its assets.
23
+ # The component that owns assets registers it with Synth.register_entry_fact.
24
+ ASSETS_KEY = "assets"
25
+
26
+ # One stack. env is nil for a stack that goes wherever the credentials point.
27
+ Entry = Data.define(:name, :template, :template_hash, :env, :dependencies, :assets) do
28
+ def to_h
29
+ {
30
+ "name" => name, "template" => template, "template_hash" => template_hash,
31
+ "env" => env, "dependencies" => dependencies, "assets" => assets
32
+ }
33
+ end
34
+ end
35
+
36
+ include Enumerable
37
+
38
+ attr_reader :out_dir, :path, :entries
39
+
40
+ def initialize(out_dir:, entries:)
41
+ @out_dir = out_dir
42
+ @path = File.join(out_dir, FILE_NAME)
43
+ @entries = entries.freeze
44
+ end
45
+
46
+ alias_method :stacks, :entries
47
+
48
+ def each(&block)
49
+ entries.each(&block)
50
+ end
51
+
52
+ # The manifest as the file holds it.
53
+ def to_h
54
+ manifest = { "ruby_cdk_version" => RubyCdk::VERSION }
55
+ ([ SCHEMA_VERSION_KEY ] | Synth.manifest_fact_keys).each { |key| manifest[key] = Synth.manifest_fact(key) }
56
+ manifest["stacks"] = entries.map(&:to_h)
57
+ manifest
58
+ end
59
+
60
+ # The bytes of manifest.json.
61
+ def bytes
62
+ Serializer.render(to_h)
63
+ end
64
+
65
+ class << self
66
+ # The write phase: every template whose bytes changed, then the
67
+ # manifest. Returns what was written.
68
+ def write(app, results)
69
+ manifest = build(app, results)
70
+ Writer.mkdir(app.out_dir)
71
+ results.each do |result|
72
+ Writer.write_if_changed(Serializer.path(app.out_dir, result.stack.stack_name), result.bytes)
73
+ end
74
+ Writer.write_if_changed(manifest.path, manifest.bytes)
75
+ manifest
76
+ end
77
+
78
+ # The manifest of results, each a Result of Synth.run, without writing anything.
79
+ def build(app, results)
80
+ new(out_dir: app.out_dir, entries: results.map { |result| entry_of(result, app) })
81
+ end
82
+
83
+ private
84
+
85
+ def entry_of(result, app)
86
+ stack = result.stack
87
+ Entry.new(
88
+ name: stack.stack_name,
89
+ template: File.basename(Serializer.path(app.out_dir, stack.stack_name)),
90
+ template_hash: "sha256:#{Digest::SHA256.hexdigest(result.bytes)}",
91
+ env: stack.env && { "account" => stack.env.account, "region" => stack.env.region },
92
+ dependencies: dependencies_of(stack, app, result.template),
93
+ assets: Synth.entry_fact(ASSETS_KEY, stack, result.template) || []
94
+ )
95
+ end
96
+
97
+ # The stacks this one is deployed after: those it was made to depend
98
+ # on, then those whose export it imports by name.
99
+ def dependencies_of(stack, app, template)
100
+ names = stack.dependencies.map(&:stack_name)
101
+ imported = imported_names(template)
102
+ app.stacks.each do |other|
103
+ next if other.equal?(stack) || names.include?(other.stack_name)
104
+
105
+ exports = other.outputs.any? do |_, output|
106
+ output.export && imported.include?(other.export_name(output.id))
107
+ end
108
+ names << other.stack_name if exports
109
+ end
110
+ names
111
+ end
112
+
113
+ # Every name written as {"Fn::ImportValue" => name} in the template.
114
+ def imported_names(value, found = [])
115
+ case value
116
+ when Hash
117
+ value.each do |key, item|
118
+ key == "Fn::ImportValue" && item.is_a?(String) ? found << item : imported_names(item, found)
119
+ end
120
+ when Array
121
+ value.each { |item| imported_names(item, found) }
122
+ end
123
+ found
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end