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,195 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "zlib"
5
+ require_relative "../errors"
6
+ require_relative "type_schema"
7
+
8
+ module RubyCdk
9
+ module Schema
10
+ # The two files under data/spec, built by `rake spec:update` and never edited by hand.
11
+ MODEL_FILE = "model.json.gz"
12
+ META_FILE = "META.json"
13
+
14
+ # The shape of the model. It changes only in a way an older loader cannot read.
15
+ MODEL_FORMAT = 1
16
+
17
+ # The shape of META.json. It changes only when the task starts recording something
18
+ # new there, which is why a META of another format is written again even though the
19
+ # model did not change. 2: schema_version no longer carries the provider zip's
20
+ # Last-Modified.
21
+ META_FORMAT = 2
22
+
23
+ # The kinds of a value, as the architecture lists them.
24
+ KINDS = %i[string integer long double boolean timestamp json list map nested].freeze
25
+ UPDATE_BEHAVIOURS = %i[mutable immutable conditional unknown].freeze
26
+
27
+ KIND_NAMES = KINDS.to_h { |kind| [ kind.to_s, kind ] }.freeze
28
+ UPDATE_NAMES = UPDATE_BEHAVIOURS.to_h { |behaviour| [ behaviour.to_s, behaviour ] }.freeze
29
+ NOTHING = [].freeze
30
+
31
+ # Where the gem keeps the model.
32
+ DIR = File.expand_path("../../../data/spec", __dir__)
33
+
34
+ class << self
35
+ # The directory the model is read from: data/spec inside the gem.
36
+ def dir
37
+ DIR
38
+ end
39
+
40
+ # The TypeSchema of a resource type, or nil when no source describes it.
41
+ def type(name)
42
+ model.type(name)
43
+ end
44
+
45
+ def known_type?(name)
46
+ model.known_type?(name)
47
+ end
48
+
49
+ # Every resource type the model knows, sorted.
50
+ def types
51
+ model.types
52
+ end
53
+
54
+ # The parsed META.json: the region, both sources, the counts, the model's SHA-256 and
55
+ # its schema_version.
56
+ def meta
57
+ model.meta
58
+ end
59
+
60
+ # What the manifest records as schema_version: one String naming the version of each
61
+ # source and the model that was consulted. The same on every machine that has the
62
+ # same gem, and different only when the model is.
63
+ def schema_version
64
+ model.schema_version
65
+ end
66
+
67
+ # :mutable, :immutable, :conditional, or :unknown. See TypeSchema#update_behaviour.
68
+ def update_type(type, path)
69
+ model.update_type(type, path)
70
+ end
71
+
72
+ private
73
+
74
+ # Parsed on the first lookup and kept.
75
+ def model
76
+ @model ||= Model.load(DIR)
77
+ end
78
+ end
79
+
80
+ # The vendored model, parsed. Schema holds the one in the gem; a test can load
81
+ # another directory.
82
+ class Model
83
+ attr_reader :dir, :region, :meta
84
+
85
+ # Reads model.json.gz and META.json under dir.
86
+ def self.load(dir)
87
+ raw = read(File.join(dir, MODEL_FILE))
88
+ format = raw.is_a?(Hash) ? raw["format"] : nil
89
+ unless format == MODEL_FORMAT
90
+ raise RubyCdk::Error, "the schema model in #{dir} has format #{format.inspect} and this version of ruby-cdk reads " \
91
+ "format #{MODEL_FORMAT}: rebuild it with `rake spec:update`, or update the gem"
92
+ end
93
+
94
+ new(dir:, raw:, meta: read_meta(File.join(dir, META_FILE)))
95
+ end
96
+
97
+ def self.read(path)
98
+ raise RubyCdk::Error, "the schema model is missing at #{path}: the gem is not installed completely" unless File.file?(path)
99
+
100
+ JSON.parse(Zlib.gunzip(File.binread(path)))
101
+ rescue Zlib::GzipFile::Error, JSON::ParserError
102
+ raise RubyCdk::Error, "the schema model at #{path} is not a gzip of one JSON object: rebuild it with `rake spec:update`"
103
+ end
104
+
105
+ def self.read_meta(path)
106
+ raise RubyCdk::Error, "#{path} is missing: the gem is not installed completely" unless File.file?(path)
107
+
108
+ JSON.parse(File.read(path))
109
+ rescue JSON::ParserError
110
+ raise RubyCdk::Error, "#{path} is not JSON: rebuild it with `rake spec:update`"
111
+ end
112
+
113
+ def initialize(dir:, raw:, meta:)
114
+ @dir = dir
115
+ @region = raw["region"]
116
+ @raw_types = raw.fetch("types")
117
+ @meta = meta
118
+ @types = {}
119
+ @names = nil
120
+ end
121
+
122
+ # The TypeSchema of a resource type, or nil. Built on first use and kept.
123
+ def type(name)
124
+ raw = @raw_types[name]
125
+ return nil unless raw
126
+
127
+ @types[name] ||= build(name, raw)
128
+ end
129
+
130
+ def known_type?(name)
131
+ @raw_types.key?(name)
132
+ end
133
+
134
+ # Every resource type, sorted.
135
+ def types
136
+ @names ||= @raw_types.keys.sort.freeze
137
+ end
138
+
139
+ # See TypeSchema#update_behaviour.
140
+ def update_type(type, path)
141
+ schema = type(type)
142
+ schema ? schema.update_behaviour(path) : :unknown
143
+ end
144
+
145
+ # See Schema.schema_version.
146
+ def schema_version
147
+ @meta["schema_version"] or raise RubyCdk::Error, "#{File.join(dir, META_FILE)} has no schema_version: rebuild it with `rake spec:update`"
148
+ end
149
+
150
+ private
151
+
152
+ def build(name, raw)
153
+ TypeSchema.new(
154
+ name:,
155
+ properties: properties_of(raw["properties"], raw["required"]),
156
+ definitions: raw.fetch("definitions", {}).to_h do |definition, body|
157
+ [ definition, Definition.new(name: definition, properties: properties_of(body["properties"], body["required"]), required: list(body["required"])) ]
158
+ end.freeze,
159
+ required: list(raw["required"]),
160
+ create_only: list(raw["create_only"]),
161
+ read_only: list(raw["read_only"]),
162
+ documentation_url: raw["documentation_url"],
163
+ source: raw.fetch("source").to_sym,
164
+ additional_properties: raw["additional_properties"] == true
165
+ )
166
+ end
167
+
168
+ # A Hash of name to PropertySchema, in the model's order, which is the source's.
169
+ def properties_of(entries, required)
170
+ required = Array(required)
171
+ entries.to_h do |name, entry|
172
+ [ name, PropertySchema.new(
173
+ name:,
174
+ kind: KIND_NAMES.fetch(entry["kind"]) { unknown("kind", entry["kind"]) },
175
+ item_kind: entry["item"] && KIND_NAMES.fetch(entry["item"]) { unknown("item kind", entry["item"]) },
176
+ definition: entry["of"],
177
+ required: required.include?(name),
178
+ update_behaviour: entry["update"] ? UPDATE_NAMES.fetch(entry["update"]) { unknown("update behaviour", entry["update"]) } : :unknown,
179
+ allowed_values: entry["values"]&.dup&.freeze,
180
+ pattern: entry["pattern"],
181
+ unique_items: entry["unique"]
182
+ ) ]
183
+ end.freeze
184
+ end
185
+
186
+ def unknown(what, value)
187
+ raise RubyCdk::Error, "the schema model names an unknown #{what}, #{value.inspect}: rebuild it with `rake spec:update`"
188
+ end
189
+
190
+ def list(value)
191
+ value.nil? || value.empty? ? NOTHING : value.dup.freeze
192
+ end
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../errors"
4
+ require_relative "provider_type"
5
+ require_relative "specification_type"
6
+
7
+ module RubyCdk
8
+ module Schema
9
+ module Sources
10
+ # What a call to Compiler yields: the model as a Hash of plain values, ready to be
11
+ # serialized, and the counts META.json records.
12
+ Compiled = Data.define(:model, :counts)
13
+
14
+ # Compiles both sources into the model: every type the provider schemas describe, with
15
+ # what the resource specification adds, and every type only the specification has.
16
+ class Compiler
17
+ def initialize(provider_schemas:, specification:)
18
+ @provider_schemas = provider_schemas
19
+ @specification = specification
20
+ end
21
+
22
+ # region goes into the model; it is what the two sources were downloaded for.
23
+ def call(region:)
24
+ types = @provider_schemas.to_h do |name, schema|
25
+ [ name, ProviderType.new(schema, @specification).call ]
26
+ end
27
+ (@specification.resource_types.keys - @provider_schemas.keys).each do |name|
28
+ types[name] = SpecificationType.new(@specification, name).call
29
+ end
30
+
31
+ model = {
32
+ "format" => RubyCdk::Schema::MODEL_FORMAT,
33
+ "region" => region,
34
+ "specification_version" => @specification.version,
35
+ "types" => types
36
+ }
37
+ Compiled.new(model:, counts: counts(types))
38
+ end
39
+
40
+ private
41
+
42
+ def counts(types)
43
+ provider = @provider_schemas.keys
44
+ specification = @specification.resource_types.keys
45
+ {
46
+ "types" => types.size,
47
+ "provider_schemas" => provider.size,
48
+ "resource_specification" => specification.size,
49
+ "only_provider_schemas" => (provider - specification).size,
50
+ "only_resource_specification" => (specification - provider).size
51
+ }
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyCdk
4
+ module Schema
5
+ module Sources
6
+ # One property, list element or map value as the compilers describe it, before it is
7
+ # written into the model.
8
+ #
9
+ # kind one of the ten kinds
10
+ # item for :list and :map, the kind of each element; nil for the other kinds
11
+ # of the definition that describes a :nested value, or a :nested element of a list or map
12
+ # values the literals the source allows, or nil when it lists none or leaves the choice open
13
+ # pattern the one pattern the source states, or nil when it states none or several
14
+ # unique for :list: whether duplicates are allowed, or nil when no source says
15
+ Descriptor = Data.define(:kind, :item, :of, :values, :pattern, :unique)
16
+
17
+ # Anything at all: a value no source describes closely.
18
+ ANY_VALUE = Descriptor.new(kind: :json, item: nil, of: nil, values: nil, pattern: nil, unique: nil).freeze
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "time"
5
+ require "uri"
6
+ require_relative "../../errors"
7
+
8
+ module RubyCdk
9
+ module Schema
10
+ module Sources
11
+ # What one GET returned.
12
+ #
13
+ # url the address that was asked for
14
+ # status the HTTP status code
15
+ # body the bytes as served
16
+ # last_modified the Last-Modified header as a Time, or nil when absent or unreadable
17
+ # etag the ETag header as served, or nil
18
+ Download = Data.define(:url, :status, :body, :last_modified, :etag)
19
+
20
+ # A source AWS no longer publishes, or has not changed for ninety days: the alarm of
21
+ # decision D-005. Nothing is written when it is raised.
22
+ class SourceUnavailable < RubyCdk::Error
23
+ attr_reader :problems
24
+
25
+ # problems: one line per thing that is wrong.
26
+ def initialize(problems)
27
+ @problems = problems.freeze
28
+ super([ "a schema source cannot be reached, is gone or is stale, so data/spec was left as it is:", *problems.map { |problem| " - #{problem}" } ].join("\n"))
29
+ end
30
+ end
31
+
32
+ # Fetches a URL with Net::HTTP and judges whether what came back can be trusted.
33
+ class Fetcher
34
+ # Decision D-005: ninety days without a change means the source is dead.
35
+ MAX_AGE = 90 * 24 * 60 * 60
36
+
37
+ DAY = 24 * 60 * 60
38
+ OPEN_TIMEOUT = 30
39
+ READ_TIMEOUT = 120
40
+
41
+ GONE = ". AWS has stopped publishing it there: see decision D-005"
42
+
43
+ # GET url. A network failure raises, as a task that cannot reach its source should.
44
+ #
45
+ # body is the transfer this request receives, not what plain curl prints: Net::HTTP
46
+ # asks for gzip on its own and quietly decodes a Content-Encoding body, which for
47
+ # the specification is 20 MB of JSON where the file is 1 MB of gzip. Asking for gzip
48
+ # here turns that decoding off, so the SHA-256 and the size recorded in META.json
49
+ # are of the bytes as served, and what `curl -H 'Accept-Encoding: gzip' URL |
50
+ # shasum -a 256` prints. (Plain `curl URL | shasum -a 256` prints something else
51
+ # again: for the provider zip the endpoint compresses on the fly only when asked.)
52
+ def call(url)
53
+ uri = URI.parse(url)
54
+ request = Net::HTTP::Get.new(uri)
55
+ request["Accept-Encoding"] = "gzip"
56
+ response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: OPEN_TIMEOUT, read_timeout: READ_TIMEOUT) do |http|
57
+ http.request(request)
58
+ end
59
+ Download.new(url:, status: response.code.to_i, body: response.body, last_modified: http_time(response["Last-Modified"]), etag: response["ETag"])
60
+ end
61
+
62
+ # What is wrong with a download: empty when the source answered and is fresh.
63
+ def problems(download, now:)
64
+ return [ "#{download.url}: HTTP #{download.status}#{download.status == 404 ? GONE : ""}" ] unless download.status == 200
65
+ return [ "#{download.url}: no Last-Modified header, so its age cannot be checked" ] unless download.last_modified
66
+
67
+ age = now - download.last_modified
68
+ return [] unless age > MAX_AGE
69
+
70
+ [ "#{download.url}: Last-Modified #{download.last_modified.utc.strftime("%Y-%m-%d")}, #{(age / DAY).floor} days ago, " \
71
+ "more than the #{MAX_AGE / DAY} of decision D-005" ]
72
+ end
73
+
74
+ private
75
+
76
+ def http_time(value)
77
+ return nil unless value
78
+
79
+ Time.httpdate(value)
80
+ rescue ArgumentError
81
+ nil
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "zlib"
5
+ require "zip"
6
+ require_relative "../../errors"
7
+
8
+ module RubyCdk
9
+ module Schema
10
+ module Sources
11
+ # The provider schema zip of one region, read straight from the archive in memory:
12
+ # nothing is extracted to disk.
13
+ module ProviderSchemas
14
+ SCHEMA_FILE = /\A[A-Za-z0-9-]+\.json\z/
15
+ GZIP_MAGIC = "\x1f\x8b".b
16
+
17
+ class << self
18
+ # bytes: the zip as downloaded, which may come wrapped in a transfer gzip. Returns
19
+ # { "AWS::Lambda::Function" => schema, ... }, keyed by each schema's own typeName.
20
+ def parse(bytes)
21
+ bytes = Zlib.gunzip(bytes) if bytes.start_with?(GZIP_MAGIC)
22
+ schemas = {}
23
+ zip = Zip::File.open_buffer(bytes)
24
+ begin
25
+ zip.entries.each do |entry|
26
+ next unless entry.file?
27
+
28
+ name = entry.name
29
+ raise RubyCdk::Error, "the provider schema zip holds #{name.inspect}, which is not a schema file" unless name.match?(SCHEMA_FILE)
30
+
31
+ schema = JSON.parse(entry.get_input_stream { |io| io.read })
32
+ type_name = schema.is_a?(Hash) ? schema["typeName"] : nil
33
+ raise RubyCdk::Error, "#{name} in the provider schema zip has no typeName" unless type_name.is_a?(String)
34
+ raise RubyCdk::Error, "the provider schema zip describes #{type_name} twice" if schemas.key?(type_name)
35
+
36
+ schemas[type_name] = schema
37
+ end
38
+ ensure
39
+ zip.close
40
+ end
41
+ schemas
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end