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,175 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require_relative "../errors"
|
|
5
|
+
|
|
6
|
+
module RubyCdk
|
|
7
|
+
module Assets
|
|
8
|
+
# The path given as an asset is not a directory, or something under it
|
|
9
|
+
# could not be read.
|
|
10
|
+
class SourceNotFoundError < Error; end
|
|
11
|
+
|
|
12
|
+
# A symbolic link sits inside an asset directory.
|
|
13
|
+
class SymlinkError < Error; end
|
|
14
|
+
|
|
15
|
+
# Something under an asset directory that cannot be hashed and zipped as it
|
|
16
|
+
# is: a file that is not a regular file, or a name that would not survive.
|
|
17
|
+
class UnsupportedEntryError < Error; end
|
|
18
|
+
|
|
19
|
+
# The content hash of a directory of files, which is the identity of an
|
|
20
|
+
# asset (D-008).
|
|
21
|
+
#
|
|
22
|
+
# The manifest lists every file under the directory, sorted by relative
|
|
23
|
+
# path, with the mode git would store for it and the SHA-256 of its bytes.
|
|
24
|
+
# The hash is the SHA-256 of that manifest written as one line per file,
|
|
25
|
+
# "<mode> <relative path> <content hash>", each line followed by a newline.
|
|
26
|
+
#
|
|
27
|
+
# Two machines hash the same files the same way whatever their umask or
|
|
28
|
+
# modification times: only the owner's executable bit of the mode is kept,
|
|
29
|
+
# and no timestamp is read at all.
|
|
30
|
+
class Hasher
|
|
31
|
+
Entry = Data.define(:path, :mode, :sha256)
|
|
32
|
+
|
|
33
|
+
# Excluded unless the caller says otherwise.
|
|
34
|
+
DEFAULT_EXCLUDES = %w[.git .DS_Store *.log].freeze
|
|
35
|
+
|
|
36
|
+
EXECUTABLE = "0755"
|
|
37
|
+
PLAIN = "0644"
|
|
38
|
+
|
|
39
|
+
# `*` also matches a leading dot and `{a,b}` is allowed. In a pattern
|
|
40
|
+
# that contains a slash, `*` does not cross a directory boundary.
|
|
41
|
+
NAME_FLAGS = File::FNM_DOTMATCH | File::FNM_EXTGLOB
|
|
42
|
+
PATH_FLAGS = File::FNM_DOTMATCH | File::FNM_EXTGLOB | File::FNM_PATHNAME
|
|
43
|
+
|
|
44
|
+
class << self
|
|
45
|
+
# Every file under source_path, sorted by relative path (forward
|
|
46
|
+
# slashes, UTF-8). An excluded file is not listed and an excluded
|
|
47
|
+
# directory is not entered.
|
|
48
|
+
#
|
|
49
|
+
# exclude: patterns in addition to DEFAULT_EXCLUDES. A pattern without
|
|
50
|
+
# a slash is matched against the base name of every file and directory;
|
|
51
|
+
# a pattern with a slash, against the whole relative path.
|
|
52
|
+
#
|
|
53
|
+
# Raises SourceNotFoundError when the path is empty or is not a
|
|
54
|
+
# directory, or when something under it cannot be read; SymlinkError
|
|
55
|
+
# for a symbolic link that is not excluded; UnsupportedEntryError for
|
|
56
|
+
# anything else that is not a regular file, or a name that would not
|
|
57
|
+
# survive the canonical text or the zip.
|
|
58
|
+
def manifest(source_path, exclude: [])
|
|
59
|
+
entries = []
|
|
60
|
+
walk(File.expand_path(usable(source_path)), "", DEFAULT_EXCLUDES + Array(exclude), entries)
|
|
61
|
+
entries.sort_by(&:path).freeze
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# The text the hash digests: one "<mode> <relative path> <content hash>"
|
|
65
|
+
# line per file, in manifest order, each ended by a newline.
|
|
66
|
+
def canonical(entries)
|
|
67
|
+
entries.map { |entry| "#{entry.mode} #{entry.path} #{entry.sha256}\n" }.join
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The SHA-256 of canonical(entries), as 64 lower-case hex characters.
|
|
71
|
+
def manifest_hash(entries)
|
|
72
|
+
Digest::SHA256.hexdigest(canonical(entries))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The manifest hash of the directory. The same files in another
|
|
76
|
+
# directory, created in another order or with other timestamps, give
|
|
77
|
+
# the same hash.
|
|
78
|
+
#
|
|
79
|
+
# Not named hash: that is Object#hash on the class itself, which has to
|
|
80
|
+
# answer an Integer for the class to be a Hash key or to sit in a Set.
|
|
81
|
+
def asset_hash(source_path, exclude: [])
|
|
82
|
+
manifest_hash(manifest(source_path, exclude: exclude))
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def usable(source_path)
|
|
88
|
+
path = source_path.to_s
|
|
89
|
+
raise SourceNotFoundError, "Asset path cannot be empty" if path.strip.empty?
|
|
90
|
+
return path if File.directory?(path)
|
|
91
|
+
|
|
92
|
+
raise SourceNotFoundError, "Cannot find asset directory at #{path}" unless File.exist?(path)
|
|
93
|
+
|
|
94
|
+
raise SourceNotFoundError, "Asset path is not a directory: #{path}"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def walk(directory, prefix, patterns, entries)
|
|
98
|
+
readable_children(directory).each do |child|
|
|
99
|
+
name = child.dup.force_encoding(Encoding::UTF_8)
|
|
100
|
+
relative = prefix.empty? ? name : "#{prefix}/#{name}"
|
|
101
|
+
next if excluded?(relative, patterns)
|
|
102
|
+
|
|
103
|
+
path = File.join(directory, name)
|
|
104
|
+
stat = readable_stat(path, relative)
|
|
105
|
+
if stat.symlink?
|
|
106
|
+
raise SymlinkError, "Symbolic links are not allowed in an asset: #{relative} -> #{File.readlink(path)}. " \
|
|
107
|
+
"Copy the file instead."
|
|
108
|
+
end
|
|
109
|
+
check_name(relative)
|
|
110
|
+
|
|
111
|
+
if stat.directory?
|
|
112
|
+
walk(path, relative, patterns, entries)
|
|
113
|
+
elsif stat.file?
|
|
114
|
+
entries << Entry.new(path: relative, mode: mode_for(stat), sha256: readable_digest(path, relative))
|
|
115
|
+
else
|
|
116
|
+
raise UnsupportedEntryError, "#{relative} is a #{stat.ftype}, not a regular file, so it cannot be in an asset"
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# A file-system failure under the walk is not a bug but a source this
|
|
122
|
+
# tool cannot read, so it is reported as one and named.
|
|
123
|
+
def readable_children(directory)
|
|
124
|
+
Dir.children(directory).sort
|
|
125
|
+
rescue SystemCallError => error
|
|
126
|
+
raise SourceNotFoundError, "Asset directory #{directory} cannot be read (#{error.message})"
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def readable_stat(path, relative)
|
|
130
|
+
File.lstat(path)
|
|
131
|
+
rescue SystemCallError => error
|
|
132
|
+
raise SourceNotFoundError, "Asset entry #{relative} cannot be read (#{error.message})"
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def readable_digest(path, relative)
|
|
136
|
+
Digest::SHA256.file(path).hexdigest
|
|
137
|
+
rescue SystemCallError => error
|
|
138
|
+
raise SourceNotFoundError, "Asset entry #{relative} cannot be read (#{error.message})"
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def excluded?(relative, patterns)
|
|
142
|
+
base = File.basename(relative)
|
|
143
|
+
patterns.any? do |pattern|
|
|
144
|
+
pattern.include?("/") ? File.fnmatch?(pattern, relative, PATH_FLAGS) : File.fnmatch?(pattern, base, NAME_FLAGS)
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# A line break would split one file into two lines of the canonical
|
|
149
|
+
# text. A backslash is legal in a POSIX name, but the zip carries it
|
|
150
|
+
# verbatim and a reader that takes it for a separator would not find
|
|
151
|
+
# the file where the hash says it is. A zip entry name must be valid
|
|
152
|
+
# UTF-8.
|
|
153
|
+
def check_name(relative)
|
|
154
|
+
if relative.include?("\n")
|
|
155
|
+
raise UnsupportedEntryError, "A file name with a line break cannot be in an asset: #{relative.inspect}"
|
|
156
|
+
end
|
|
157
|
+
if relative.include?("\\")
|
|
158
|
+
raise UnsupportedEntryError, "A file name with a backslash cannot be in an asset: #{relative.inspect}. " \
|
|
159
|
+
"The zip would carry it verbatim, and readers that take a backslash " \
|
|
160
|
+
"for a separator would not find the file where the hash says it is."
|
|
161
|
+
end
|
|
162
|
+
return if relative.valid_encoding?
|
|
163
|
+
|
|
164
|
+
raise UnsupportedEntryError, "A file name that is not valid UTF-8 cannot be in an asset: #{relative.inspect}"
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# The way git stores a mode: 0755 when the owner may execute the file,
|
|
168
|
+
# 0644 for everything else. No other permission bit is kept.
|
|
169
|
+
def mode_for(stat)
|
|
170
|
+
(stat.mode & 0o100).zero? ? PLAIN : EXECUTABLE
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "hasher"
|
|
4
|
+
|
|
5
|
+
module RubyCdk
|
|
6
|
+
module Assets
|
|
7
|
+
# Files changed between the moment an asset was hashed and the moment its
|
|
8
|
+
# hash is checked again or its zip is built.
|
|
9
|
+
class AssetChangedError < Error; end
|
|
10
|
+
|
|
11
|
+
# One directory of files on its way to the asset bucket: where the files
|
|
12
|
+
# are, what was left out, and the identity both derive from.
|
|
13
|
+
#
|
|
14
|
+
# The identity of an asset is the manifest hash (Hasher), and the object
|
|
15
|
+
# key is "<manifest hash>.zip". Equality is not identity: as a Data value,
|
|
16
|
+
# == compares every member, so two records of the same files in different
|
|
17
|
+
# directories are different records that share hash and key.
|
|
18
|
+
Record = Data.define(:source_path, :exclude, :manifest, :manifest_hash)
|
|
19
|
+
|
|
20
|
+
class Record
|
|
21
|
+
# Hashes the directory now and returns its record.
|
|
22
|
+
#
|
|
23
|
+
# source_path: the directory, absolute or relative to the working
|
|
24
|
+
# directory. The record keeps the absolute path.
|
|
25
|
+
# exclude: the patterns Hasher.manifest takes.
|
|
26
|
+
# expected_hash: the manifest hash recorded earlier, at synthesis. Given,
|
|
27
|
+
# the directory must still hash to it, or AssetChangedError is raised:
|
|
28
|
+
# a template that names the old key must not be served other files.
|
|
29
|
+
#
|
|
30
|
+
# Raises what Hasher.manifest raises.
|
|
31
|
+
def self.build(source_path, exclude: [], expected_hash: nil)
|
|
32
|
+
manifest = Hasher.manifest(source_path, exclude: exclude)
|
|
33
|
+
hash = Hasher.manifest_hash(manifest)
|
|
34
|
+
if expected_hash && expected_hash != hash
|
|
35
|
+
raise AssetChangedError, "The asset at #{File.expand_path(source_path.to_s)} changed after it was hashed: " \
|
|
36
|
+
"it now hashes to #{hash}, not to #{expected_hash}. Synthesize again."
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
new(source_path: File.expand_path(source_path.to_s), exclude: Array(exclude).map(&:dup).freeze,
|
|
40
|
+
manifest:, manifest_hash: hash)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# "<manifest hash>.zip"
|
|
44
|
+
def key
|
|
45
|
+
"#{manifest_hash}.zip"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
require_relative "zipper"
|
|
5
|
+
|
|
6
|
+
module RubyCdk
|
|
7
|
+
module Assets
|
|
8
|
+
# The asset bucket exists but is not ours, or does not exist at all.
|
|
9
|
+
# Nothing was uploaded.
|
|
10
|
+
class BucketUnavailableError < Error
|
|
11
|
+
# state: what the port answered, :foreign or :absent.
|
|
12
|
+
attr_reader :bucket, :state
|
|
13
|
+
|
|
14
|
+
def initialize(bucket, state, owner)
|
|
15
|
+
@bucket = bucket
|
|
16
|
+
@state = state
|
|
17
|
+
super(sentence(bucket, state, owner))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def sentence(bucket, state, owner)
|
|
23
|
+
case state
|
|
24
|
+
when :foreign
|
|
25
|
+
"Bucket named '#{bucket}' exists, but we don't have access to it as account #{owner}: its owner is not " \
|
|
26
|
+
"that account (unexpected bucket owner), so nothing was uploaded. The bucket's name is predictable and " \
|
|
27
|
+
"somebody else may be holding it. Find out who owns it before you go on; `ruby-cdk bootstrap` refuses it too."
|
|
28
|
+
when :absent
|
|
29
|
+
"No bucket named '#{bucket}' in account #{owner}, so nothing was uploaded. Run `ruby-cdk bootstrap` first."
|
|
30
|
+
else
|
|
31
|
+
"The object store answered #{state.inspect} for bucket '#{bucket}' in account #{owner}, " \
|
|
32
|
+
"which is neither :mine, :foreign nor :absent. Nothing was uploaded."
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Uploads assets to a bucket through the object-store port, and never
|
|
38
|
+
# talks to S3 in any other way. Every call names the account that must own
|
|
39
|
+
# the bucket, and a bucket that is not theirs is refused before anything
|
|
40
|
+
# is uploaded.
|
|
41
|
+
class Uploader
|
|
42
|
+
ACCOUNT_ID = /\A\d{12}\z/
|
|
43
|
+
|
|
44
|
+
# store: an object store, with the three methods of the port.
|
|
45
|
+
# owner: the 12-digit ID of the account that must own the bucket.
|
|
46
|
+
def initialize(store:, owner:)
|
|
47
|
+
account = owner.to_s
|
|
48
|
+
unless ACCOUNT_ID.match?(account)
|
|
49
|
+
raise ArgumentError, "owner must be the 12-digit ID of the account that owns the bucket, not #{owner.inspect}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
@store = store
|
|
53
|
+
@owner = account
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Uploads the zip of the record unless its key is already in the bucket.
|
|
57
|
+
# Answers :uploaded or :skipped.
|
|
58
|
+
#
|
|
59
|
+
# Raises BucketUnavailableError when the bucket belongs to another
|
|
60
|
+
# account or does not exist, before anything is looked up or uploaded,
|
|
61
|
+
# and what Zipper.build raises while the zip is built.
|
|
62
|
+
def upload(record, bucket:)
|
|
63
|
+
ensure_ours(bucket)
|
|
64
|
+
put_object_unless_present(record.key, bucket, -> { Zipper.build(record) })
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Uploads every record and answers { uploaded:, skipped: }, the counts of
|
|
68
|
+
# records uploaded and of records left alone because their key was
|
|
69
|
+
# already in the bucket.
|
|
70
|
+
#
|
|
71
|
+
# An empty list is not a request: { uploaded: 0, skipped: 0 } is answered
|
|
72
|
+
# and the store is not called, the bucket's owner included. A stack with
|
|
73
|
+
# no assets deploys into an account that was never bootstrapped.
|
|
74
|
+
#
|
|
75
|
+
# Otherwise the bucket's owner is checked once, for the whole batch. A
|
|
76
|
+
# key that appears more than once is uploaded once; its later
|
|
77
|
+
# appearances count as skipped and make no call.
|
|
78
|
+
def upload_all(records, bucket:)
|
|
79
|
+
counts = { uploaded: 0, skipped: 0 }
|
|
80
|
+
return counts if records.empty?
|
|
81
|
+
|
|
82
|
+
ensure_ours(bucket)
|
|
83
|
+
seen = {}
|
|
84
|
+
records.each do |record|
|
|
85
|
+
if seen[record.key]
|
|
86
|
+
counts[:skipped] += 1
|
|
87
|
+
next
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
seen[record.key] = true
|
|
91
|
+
counts[put_object_unless_present(record.key, bucket, -> { Zipper.build(record) })] += 1
|
|
92
|
+
end
|
|
93
|
+
counts
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Puts a body at a key with the same treatment an asset gets: the bucket
|
|
97
|
+
# checked for its owner first, then the key — one already there is left
|
|
98
|
+
# alone, and since a key here names its content, what is in the bucket is
|
|
99
|
+
# the same bytes. Answers :uploaded or :skipped.
|
|
100
|
+
#
|
|
101
|
+
# This is the path of the other thing a deploy places in the bucket: the
|
|
102
|
+
# template too large for a request (architecture §8), at
|
|
103
|
+
# templates/<stack name>/<sha256>.json, deployed by its URL.
|
|
104
|
+
#
|
|
105
|
+
# Raises BucketUnavailableError under the same rule as upload.
|
|
106
|
+
def upload_body(key, body:, bucket:)
|
|
107
|
+
ensure_ours(bucket)
|
|
108
|
+
put_object_unless_present(key, bucket, -> { body })
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
private
|
|
112
|
+
|
|
113
|
+
def ensure_ours(bucket)
|
|
114
|
+
if bucket.nil? || bucket.to_s.strip.empty?
|
|
115
|
+
raise ArgumentError, "bucket must be named, not #{bucket.inspect}"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
state = @store.bucket_state(bucket, expected_owner: @owner)
|
|
119
|
+
return if state == :mine
|
|
120
|
+
|
|
121
|
+
raise BucketUnavailableError.new(bucket, state, @owner)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# The directory is read, and the body built, only when it is needed: body
|
|
125
|
+
# is a lambda, called for the put and never for a skip.
|
|
126
|
+
#
|
|
127
|
+
# The port answers object_exists? with a boolean, and only `true` counts
|
|
128
|
+
# as present. Anything else is taken for absent and the object is put:
|
|
129
|
+
# the key is content-addressed, so a needless put is harmless and a
|
|
130
|
+
# wrong skip is not.
|
|
131
|
+
def put_object_unless_present(key, bucket, body)
|
|
132
|
+
return :skipped if @store.object_exists?(bucket, key, expected_owner: @owner) == true
|
|
133
|
+
|
|
134
|
+
@store.put(bucket, key, body: body.call, expected_owner: @owner)
|
|
135
|
+
:uploaded
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "zip"
|
|
5
|
+
require_relative "../errors"
|
|
6
|
+
require_relative "record"
|
|
7
|
+
|
|
8
|
+
module RubyCdk
|
|
9
|
+
module Assets
|
|
10
|
+
# Builds the zip of a record. The same record always gives the same bytes.
|
|
11
|
+
#
|
|
12
|
+
# What goes into the archive, and nothing else:
|
|
13
|
+
#
|
|
14
|
+
# - one entry per file of the record's manifest, in manifest order, named
|
|
15
|
+
# by its relative path; no directory entries;
|
|
16
|
+
# - deflate at COMPRESSION_LEVEL, the whole file fed to zlib in one piece;
|
|
17
|
+
# - the mode 0644 or 0755 of the manifest in the external attributes;
|
|
18
|
+
# - FIXED_TIME on every entry, and no extra field: timestamps are not
|
|
19
|
+
# written with sub-second or time-zone precision anywhere;
|
|
20
|
+
# - no comment, on an entry or on the archive.
|
|
21
|
+
#
|
|
22
|
+
# A file whose content no longer matches its manifest entry is never
|
|
23
|
+
# packed: AssetChangedError is raised instead.
|
|
24
|
+
#
|
|
25
|
+
# Byte for byte, the result depends on the versions of rubyzip and zlib,
|
|
26
|
+
# and on rubyzip's `unicode_names` setting staying off (it would change one
|
|
27
|
+
# byte: the version that made the archive). Nothing relies on the bytes:
|
|
28
|
+
# an asset is identified by its manifest hash.
|
|
29
|
+
class Zipper
|
|
30
|
+
# The earliest date a zip entry can hold: 1980-01-01 00:00:00.
|
|
31
|
+
FIXED_TIME = Time.utc(1980, 1, 1, 0, 0, 0).freeze
|
|
32
|
+
|
|
33
|
+
# zlib's own default level, and the one yazl, the zip writer of the CDK
|
|
34
|
+
# CLI, uses when it is given none.
|
|
35
|
+
COMPRESSION_LEVEL = 6
|
|
36
|
+
|
|
37
|
+
# General purpose bit 11: the entry name is UTF-8.
|
|
38
|
+
UTF8_NAME = 0x800
|
|
39
|
+
|
|
40
|
+
MODES = { "0644" => 0o644, "0755" => 0o755 }.freeze
|
|
41
|
+
|
|
42
|
+
class << self
|
|
43
|
+
# The zip of the record as a binary String.
|
|
44
|
+
#
|
|
45
|
+
# Raises AssetChangedError when a file of the manifest is gone or has
|
|
46
|
+
# another content than when it was hashed.
|
|
47
|
+
def build(record)
|
|
48
|
+
# rubyzip is named with its leading "::": Assets::Zip, the asset, would
|
|
49
|
+
# otherwise answer for Zip in this namespace.
|
|
50
|
+
::Zip::OutputStream.write_buffer do |output|
|
|
51
|
+
record.manifest.each do |file|
|
|
52
|
+
content = read(record, file)
|
|
53
|
+
output.put_next_entry(entry_for(file, content.bytesize))
|
|
54
|
+
output << content
|
|
55
|
+
end
|
|
56
|
+
end.string
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Writes the zip of the record to path, in binary mode. Returns the
|
|
60
|
+
# number of bytes written.
|
|
61
|
+
def write(record, path)
|
|
62
|
+
File.binwrite(path, build(record))
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
# The file as it was hashed, or AssetChangedError. It is never followed
|
|
68
|
+
# through a symbolic link: the Hasher refuses those, so a link here is
|
|
69
|
+
# a change too. A file that cannot be read at all is a change as well,
|
|
70
|
+
# reported as one and named, not as a bare system error.
|
|
71
|
+
def read(record, file)
|
|
72
|
+
path = File.join(record.source_path, file.path)
|
|
73
|
+
stat = File.lstat(path)
|
|
74
|
+
unless stat.file?
|
|
75
|
+
raise AssetChangedError, "#{path} is no longer a regular file. The asset changed after it was hashed. " \
|
|
76
|
+
"Synthesize again."
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
content = File.binread(path)
|
|
80
|
+
return content if Digest::SHA256.hexdigest(content) == file.sha256
|
|
81
|
+
|
|
82
|
+
raise AssetChangedError, "#{path} changed after the asset was hashed. Synthesize again."
|
|
83
|
+
rescue Errno::ENOENT
|
|
84
|
+
raise AssetChangedError, "#{path} no longer exists. The asset changed after it was hashed. Synthesize again."
|
|
85
|
+
rescue SystemCallError => error
|
|
86
|
+
raise AssetChangedError, "#{path} cannot be read (#{error.message}). The asset changed after it was hashed. " \
|
|
87
|
+
"Synthesize again."
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Every field that could carry something time-, machine- or
|
|
91
|
+
# configuration-derived is set here. `size` is one of them: left
|
|
92
|
+
# unknown, rubyzip would put a zip64 extra field in every local header.
|
|
93
|
+
def entry_for(file, size)
|
|
94
|
+
entry = ::Zip::Entry.new(
|
|
95
|
+
"", file.path,
|
|
96
|
+
comment: "", size: size, compression_method: ::Zip::Entry::DEFLATED, compression_level: COMPRESSION_LEVEL,
|
|
97
|
+
time: FIXED_TIME, extra: ::Zip::ExtraField.new
|
|
98
|
+
)
|
|
99
|
+
entry.fstype = ::Zip::FSTYPE_UNIX
|
|
100
|
+
entry.unix_perms = MODES.fetch(file.mode)
|
|
101
|
+
entry.internal_file_attributes = 0
|
|
102
|
+
entry.gp_flags = UTF8_NAME
|
|
103
|
+
entry
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require_relative "deploy/ports"
|
|
5
|
+
require_relative "assets/builder"
|
|
6
|
+
require_relative "assets/hasher"
|
|
7
|
+
require_relative "assets/record"
|
|
8
|
+
require_relative "assets/zipper"
|
|
9
|
+
require_relative "assets/uploader"
|
|
10
|
+
|
|
11
|
+
module RubyCdk
|
|
12
|
+
# File assets (architecture §8): a directory of files, hashed when the stack
|
|
13
|
+
# is resolved, zipped and uploaded at deploy to the asset bucket of the
|
|
14
|
+
# stack's environment, through the object-store port. The adapter behind that
|
|
15
|
+
# port is the only code that knows S3.
|
|
16
|
+
module Assets
|
|
17
|
+
BUCKET_PREFIX = "ruby-cdk-assets-"
|
|
18
|
+
|
|
19
|
+
# The name of the asset bucket of an account in a region. A function of
|
|
20
|
+
# public facts, which is why every call on the bucket names its owner.
|
|
21
|
+
def self.bucket_name(account, region)
|
|
22
|
+
"#{BUCKET_PREFIX}#{account}-#{region}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Uploads, before any change set, what the deploy of a stack needs in its
|
|
26
|
+
# bucket: the zips of the assets the stack resolved (architecture §8). The
|
|
27
|
+
# command that deploys calls it after synthesis, in the same process, so the
|
|
28
|
+
# records are the ones synthesis built.
|
|
29
|
+
#
|
|
30
|
+
# store is the object-store port. owner is the account that must own the
|
|
31
|
+
# bucket: the resolved identity's, and it has to be the stack's env account,
|
|
32
|
+
# because the bucket's name is made of that account and every check and
|
|
33
|
+
# message names it. A stack with no asset uploads nothing and checks
|
|
34
|
+
# nothing, which is what lets it deploy into an account that was never
|
|
35
|
+
# bootstrapped.
|
|
36
|
+
#
|
|
37
|
+
# Answers { uploaded:, skipped: }. Raises what Uploader raises: a bucket
|
|
38
|
+
# that is not ours stops the deploy before anything is looked up.
|
|
39
|
+
#
|
|
40
|
+
# The records are read before the owner is checked, so a stack without env
|
|
41
|
+
# — which the account rule allows, and which cannot have resolved an asset
|
|
42
|
+
# (Builder#zip refuses) — uploads nothing and reads nothing of its env.
|
|
43
|
+
def self.publish(stack, store:, owner:)
|
|
44
|
+
records = stack.asset.records
|
|
45
|
+
return { uploaded: 0, skipped: 0 } if records.empty?
|
|
46
|
+
|
|
47
|
+
account = stack.env.account.to_s
|
|
48
|
+
unless owner.to_s == account
|
|
49
|
+
# A precondition, not a usage error: the command line explains this one
|
|
50
|
+
# to the user (the deploy stopped because the stack's env names another
|
|
51
|
+
# account than the credentials carry), instead of reporting a bug.
|
|
52
|
+
raise RubyCdk::PreconditionError, "publish was given owner #{owner.inspect}, but the bucket is named for the stack's " \
|
|
53
|
+
"env account #{account.inspect}. Pass the resolved identity's account, and deploy a stack with an env " \
|
|
54
|
+
"only with credentials of that account."
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
Uploader.new(store:, owner: account).upload_all(records, bucket: bucket_name(account, stack.env.region))
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Places a template in the bucket for a deploy by its URL (architecture
|
|
61
|
+
# §8): CloudFormation takes at most 51,200 bytes of template in a request,
|
|
62
|
+
# and above that the change set carries the address of the object instead
|
|
63
|
+
# of the template. The key is templates/<stack name>/<sha256>.json, the
|
|
64
|
+
# SHA-256 of the body, so the same template lands on the same key and the
|
|
65
|
+
# uploader leaves a key that is already there alone — the second deploy of
|
|
66
|
+
# the same template puts nothing. Answers the address of the object,
|
|
67
|
+
# https://<bucket>.s3.<region>.amazonaws.com/<key>, the one the change set
|
|
68
|
+
# carries as TemplateURL. That address is the commercial partition's shape,
|
|
69
|
+
# written from the bucket's name and region, not read from the SDK: under
|
|
70
|
+
# another partition, or with the endpoint overridden (the emulator
|
|
71
|
+
# scenario), CloudFormation would not resolve it — the assumption is
|
|
72
|
+
# recorded in architecture section 8.
|
|
73
|
+
#
|
|
74
|
+
# The bucket is the stack's own, named for its env as an asset bucket is,
|
|
75
|
+
# and owner must be the stack's env account, as publish requires. The
|
|
76
|
+
# command line wires this as the deployer's template publisher.
|
|
77
|
+
def self.publish_template(stack, template, store:, owner:)
|
|
78
|
+
env = stack.env
|
|
79
|
+
unless env
|
|
80
|
+
raise RubyCdk::PreconditionError,
|
|
81
|
+
"The template of stack #{stack.stack_name} is too large to go in a request, so it is placed in the asset " \
|
|
82
|
+
"bucket, and the bucket is named for the stack's env. Stack #{stack.stack_name} has no env, so there is no " \
|
|
83
|
+
"bucket to place it in. Nothing was deployed.\n" \
|
|
84
|
+
"Give the stack an env (account and region), or deploy a template that fits in a request."
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
account = env.account.to_s
|
|
88
|
+
unless owner.to_s == account
|
|
89
|
+
raise RubyCdk::PreconditionError, "publish_template was given owner #{owner.inspect}, but the bucket is named for the stack's " \
|
|
90
|
+
"env account #{account.inspect}. Pass the resolved identity's account, and deploy a stack with an env " \
|
|
91
|
+
"only with credentials of that account."
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
bucket = bucket_name(account, env.region)
|
|
95
|
+
key = "templates/#{stack.stack_name}/#{Digest::SHA256.hexdigest(template)}.json"
|
|
96
|
+
Uploader.new(store:, owner: account).upload_body(key, body: template, bucket: bucket)
|
|
97
|
+
"https://#{bucket}.s3.#{env.region}.amazonaws.com/#{key}"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# The tree's contract declares these; here are the implementations, in the
|
|
102
|
+
# component that owns assets. The tree never names the assets component.
|
|
103
|
+
class Stack
|
|
104
|
+
# The builder of the stack's file assets.
|
|
105
|
+
def asset
|
|
106
|
+
@asset_builder ||= Assets::Builder.new(self)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The manifest hashes of the assets this stack declared and resolved, in
|
|
110
|
+
# the order they were declared and without a repeat. Synthesis writes them
|
|
111
|
+
# to the manifest.
|
|
112
|
+
def asset_hashes
|
|
113
|
+
instance_variable_defined?(:@asset_builder) ? @asset_builder.hashes : []
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|