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,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "failure"
|
|
5
|
+
require_relative "selection"
|
|
6
|
+
|
|
7
|
+
module RubyCdk
|
|
8
|
+
module Cli
|
|
9
|
+
# `validate`: the report of what the schema rules (V1 to V23) say about the
|
|
10
|
+
# templates, without writing anything.
|
|
11
|
+
#
|
|
12
|
+
# Selection is deploy's (packages/aws-cdk/lib/cli/cli.ts:800-819 in
|
|
13
|
+
# aws-cdk-cli at eca1b73d682d7763955bec30ab5dd52bbc4327b6): the app's only
|
|
14
|
+
# stack when nothing is named, a usage error otherwise, --all for every
|
|
15
|
+
# stack. Not deploy's dependency expansion: every stack validates on its
|
|
16
|
+
# own, and bringing dependencies along would report the same template twice
|
|
17
|
+
# (recorded decision).
|
|
18
|
+
#
|
|
19
|
+
# Per stack it runs every phase of architecture section 5 except write
|
|
20
|
+
# (RubyCdk::Synth.run), and then the validation track's own RubyCdk::Validation.validate over
|
|
21
|
+
# the result's template, which is the whole report: every rule, the errors
|
|
22
|
+
# and the warnings apart, the suggestions and AWS's own documentation page
|
|
23
|
+
# for the type an issue is about. Nothing is written: the templates are
|
|
24
|
+
# never handed to the write phase.
|
|
25
|
+
module Validate
|
|
26
|
+
# Section 14: local validation failed.
|
|
27
|
+
VALIDATION_FAILED = 2
|
|
28
|
+
|
|
29
|
+
# names are the STACK words. Answers the exit code: 0 when every selected
|
|
30
|
+
# stack is clean of errors, 2 otherwise (warnings are reported, not fatal).
|
|
31
|
+
def self.run(session, names)
|
|
32
|
+
session.register_strict_validators if session.options.strict_types
|
|
33
|
+
selected = Selection.choose(session.stacks, names:, all: session.options.all, command: "validate")
|
|
34
|
+
io = session.io
|
|
35
|
+
io.debug("validate: #{selected.map(&:stack_name).join(", ")}")
|
|
36
|
+
|
|
37
|
+
failed = []
|
|
38
|
+
selected.each do |stack|
|
|
39
|
+
result = RubyCdk::Synth.run(stack)
|
|
40
|
+
report = RubyCdk::Validation.validate(result.template, stack_name: stack.stack_name,
|
|
41
|
+
strict_types: !!session.options.strict_types, stateful_types: session.stateful_types)
|
|
42
|
+
extra = unreported(result, report)
|
|
43
|
+
failing = report.errors.any? || result.issues.any?
|
|
44
|
+
if session.options.json
|
|
45
|
+
print_json(io, stack.stack_name, report, extra, failing)
|
|
46
|
+
else
|
|
47
|
+
print_report(io, stack.stack_name, report, extra)
|
|
48
|
+
end
|
|
49
|
+
failed << stack.stack_name if failing
|
|
50
|
+
end
|
|
51
|
+
failed.empty? ? Cli::SUCCESS : VALIDATION_FAILED
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The report for a person, on standard error: the stack, every error, then
|
|
55
|
+
# every warning prefixed as one, then what the pipeline found that the
|
|
56
|
+
# template's own report cannot see.
|
|
57
|
+
def self.print_report(io, name, report, extra_issues)
|
|
58
|
+
io.say(name)
|
|
59
|
+
report.errors.each do |issue|
|
|
60
|
+
io.say(" #{issue.rule} #{issue.path}: #{issue.message}")
|
|
61
|
+
io.say(" #{issue.hint}") if issue.hint
|
|
62
|
+
end
|
|
63
|
+
report.warnings.each do |warning|
|
|
64
|
+
io.say(" warning #{warning.rule} #{warning.path}: #{warning.message}")
|
|
65
|
+
io.say(" #{warning.hint}") if warning.hint
|
|
66
|
+
end
|
|
67
|
+
extra_issues.each do |issue|
|
|
68
|
+
io.say(" #{issue.rule} #{issue.path}: #{issue.message}")
|
|
69
|
+
io.say(" #{issue.hint}") if issue.hint
|
|
70
|
+
end
|
|
71
|
+
count = "#{report.errors.length} error(s), #{report.warnings.length} warning(s)"
|
|
72
|
+
io.say(extra_issues.any? ? "#{name}: #{count}, #{extra_issues.length} from the tree" : "#{name}: #{count}")
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# What the pipeline found that the report does not hold: the tree's own
|
|
76
|
+
# findings (a duplicate logical ID across sections, for one) that no rule
|
|
77
|
+
# of the template-only report answers. Duplicates of the report are left
|
|
78
|
+
# out, so nothing is printed twice.
|
|
79
|
+
def self.unreported(result, report)
|
|
80
|
+
known = report.issues.map { |issue| [ issue.rule, issue.path, issue.message ] }
|
|
81
|
+
result.issues.reject { |issue| known.include?([ issue.rule, issue.path, issue.message ]) }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# For --json, one object per stack on standard out, and nothing else there.
|
|
85
|
+
def self.print_json(io, name, report, extra_issues, failed)
|
|
86
|
+
io.result("#{JSON.generate(
|
|
87
|
+
"name" => name,
|
|
88
|
+
"exit_code" => failed ? VALIDATION_FAILED : Cli::SUCCESS,
|
|
89
|
+
"errors" => rows(report.errors + extra_issues),
|
|
90
|
+
"warnings" => rows(report.warnings)
|
|
91
|
+
)}\n")
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def self.rows(issues)
|
|
95
|
+
issues.map do |issue|
|
|
96
|
+
{ "rule" => issue.rule, "path" => issue.path, "message" => issue.message, "hint" => issue.hint }.compact
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
data/lib/ruby_cdk/cli.rb
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "thor"
|
|
4
|
+
require_relative "version"
|
|
5
|
+
require_relative "core"
|
|
6
|
+
require_relative "synth"
|
|
7
|
+
require_relative "assets"
|
|
8
|
+
require_relative "deploy"
|
|
9
|
+
require_relative "diff"
|
|
10
|
+
require_relative "bootstrap"
|
|
11
|
+
require_relative "cli/io"
|
|
12
|
+
require_relative "cli/failure"
|
|
13
|
+
require_relative "cli/shell"
|
|
14
|
+
require_relative "cli/arguments"
|
|
15
|
+
require_relative "cli/pairs"
|
|
16
|
+
require_relative "cli/selection"
|
|
17
|
+
require_relative "cli/session"
|
|
18
|
+
require_relative "cli/aws"
|
|
19
|
+
require_relative "cli/synth"
|
|
20
|
+
require_relative "cli/list"
|
|
21
|
+
require_relative "cli/validate"
|
|
22
|
+
require_relative "cli/diff"
|
|
23
|
+
require_relative "cli/new"
|
|
24
|
+
require_relative "cli/operation"
|
|
25
|
+
require_relative "cli/bootstrap"
|
|
26
|
+
require_relative "cli/report"
|
|
27
|
+
|
|
28
|
+
module RubyCdk
|
|
29
|
+
# The command line. One executable, `ruby-cdk` (architecture section 11).
|
|
30
|
+
#
|
|
31
|
+
# This class is the edge of the command line. It declares the options and the
|
|
32
|
+
# commands, hands each command to one object under RubyCdk::Cli that answers
|
|
33
|
+
# with an exit code, and turns what was raised into a message and a code
|
|
34
|
+
# (section 14). Nothing under it exits, prints to the real streams on its own
|
|
35
|
+
# or builds a client: `main` is the one place that exits, and it is all that
|
|
36
|
+
# `exe/ruby-cdk` calls.
|
|
37
|
+
#
|
|
38
|
+
# The CDK CLI is the reference for every option here (aws/aws-cdk-cli at
|
|
39
|
+
# eca1b73d682d7763955bec30ab5dd52bbc4327b6, packages/aws-cdk/lib/cli/parse-command-line-arguments.ts):
|
|
40
|
+
# the names it gives them, their short forms, and what they say they do.
|
|
41
|
+
class CLI < Thor
|
|
42
|
+
package_name "ruby-cdk"
|
|
43
|
+
check_unknown_options!
|
|
44
|
+
|
|
45
|
+
# Thor comes with a `tree` command of its own, which lists Thor classes.
|
|
46
|
+
# It is not one of this tool's.
|
|
47
|
+
remove_command :tree
|
|
48
|
+
map.delete("-t")
|
|
49
|
+
map.delete("--tree")
|
|
50
|
+
|
|
51
|
+
# What Ctrl-C ends a command with: 128 and the number of SIGINT.
|
|
52
|
+
INTERRUPTED = 130
|
|
53
|
+
|
|
54
|
+
# --- the options every command takes (architecture section 11)
|
|
55
|
+
|
|
56
|
+
class_option :app, type: :string, default: "cdk.rb", aliases: "-a",
|
|
57
|
+
desc: "The app file, which creates exactly one RubyCdk::App. A relative path is read from the working directory"
|
|
58
|
+
|
|
59
|
+
class_option :out, type: :string, default: App::DEFAULT_OUT_DIR, aliases: "-o",
|
|
60
|
+
desc: "Directory the templates and manifest.json are written to. An App the app file creates with out_dir: keeps its own"
|
|
61
|
+
|
|
62
|
+
class_option :profile, type: :string, desc: "The AWS profile that selects the credentials"
|
|
63
|
+
|
|
64
|
+
class_option :region, type: :string,
|
|
65
|
+
desc: "The AWS region the clients are built for. A stack with an env deploys only into that region"
|
|
66
|
+
|
|
67
|
+
class_option :account, type: :string,
|
|
68
|
+
desc: "The 12-digit account the credentials must be for. Required with --yes for a stack without env"
|
|
69
|
+
|
|
70
|
+
class_option :yes, type: :boolean, aliases: "-y", desc: "Deploy or destroy without asking for approval"
|
|
71
|
+
|
|
72
|
+
class_option :force, type: :boolean, aliases: "-f",
|
|
73
|
+
desc: "Deploy even when the template and the parameters are unchanged; with destroy, do not ask for confirmation; " \
|
|
74
|
+
"with new, write into the directory named only when it is empty"
|
|
75
|
+
|
|
76
|
+
class_option :quiet, type: :boolean, aliases: "-q", desc: "Print no progress, and for synth no template"
|
|
77
|
+
|
|
78
|
+
class_option :verbose, type: :boolean, aliases: "-v",
|
|
79
|
+
desc: "Say what the command decides, and print the backtrace of an error"
|
|
80
|
+
|
|
81
|
+
class_option :json, type: :boolean, aliases: "-j",
|
|
82
|
+
desc: "Print the result as JSON on standard output; what is for a person goes to standard error"
|
|
83
|
+
|
|
84
|
+
class_option :color, type: :boolean,
|
|
85
|
+
desc: "Use colour even when standard output is not a terminal. --no-color never uses it"
|
|
86
|
+
|
|
87
|
+
# --- the commands
|
|
88
|
+
|
|
89
|
+
desc "version", "Print the gem version"
|
|
90
|
+
def version
|
|
91
|
+
session.io.line(RubyCdk::VERSION)
|
|
92
|
+
Cli::SUCCESS
|
|
93
|
+
end
|
|
94
|
+
map "--version" => :version
|
|
95
|
+
|
|
96
|
+
desc "synth [STACK...]", "Synthesize the app into --out, and print the template of the stack when there is one"
|
|
97
|
+
method_option :no_validate, type: :boolean,
|
|
98
|
+
desc: "Skip the schema rules (V1 to V23). The tree's own rules (V2, V13) still run"
|
|
99
|
+
def synth(*names)
|
|
100
|
+
Cli::Synth.run(session, names)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
desc "validate [STACK...]", "Report what the schema rules say about the templates, without writing anything"
|
|
104
|
+
method_option :all, type: :boolean, desc: "Validate every stack of the app"
|
|
105
|
+
method_option :strict_types, type: :boolean,
|
|
106
|
+
desc: "An unknown resource type (rule V1) is an error, not a warning"
|
|
107
|
+
def validate(*names)
|
|
108
|
+
Cli::Validate.run(session, names)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
desc "ls [STACK...]", "List the stacks of the app, one per line"
|
|
112
|
+
method_option :long, type: :boolean, aliases: "-l", desc: "Show where each stack deploys"
|
|
113
|
+
def ls(*names)
|
|
114
|
+
Cli::List.run(session, names)
|
|
115
|
+
end
|
|
116
|
+
map "list" => :ls
|
|
117
|
+
|
|
118
|
+
desc "deploy [STACK...]", "Deploy the stacks through CloudFormation change sets"
|
|
119
|
+
method_option :all, type: :boolean, desc: "Deploy every stack of the app, dependencies first"
|
|
120
|
+
method_option :exclusively, type: :boolean, aliases: "-e",
|
|
121
|
+
desc: "Deploy only the stacks named, not the ones they depend on"
|
|
122
|
+
method_option :execute, type: :boolean, default: true,
|
|
123
|
+
desc: "Execute the change set. --no-execute leaves it to execute in the console"
|
|
124
|
+
method_option :on_failure, type: :string, default: "DELETE",
|
|
125
|
+
desc: "What CloudFormation does when a first deploy fails: DELETE, ROLLBACK or DO_NOTHING"
|
|
126
|
+
method_option :parameters, type: :string,
|
|
127
|
+
desc: "Template parameters for the deploy, as KEY=VALUE pairs separated by commas"
|
|
128
|
+
method_option :tags, type: :string, desc: "Tags for the stacks, as KEY=VALUE pairs separated by commas"
|
|
129
|
+
method_option :outputs_file, type: :string, aliases: "-O",
|
|
130
|
+
desc: "File the outputs of each stack are written to, as JSON"
|
|
131
|
+
def deploy(*names)
|
|
132
|
+
Cli::Operation.new(session, kind: :deploy).call(names)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
desc "destroy [STACK...]", "Delete the stacks from CloudFormation"
|
|
136
|
+
method_option :all, type: :boolean, desc: "Destroy every stack of the app, the depended-on ones last"
|
|
137
|
+
method_option :exclusively, type: :boolean, aliases: "-e",
|
|
138
|
+
desc: "Destroy only the stacks named, not the ones that depend on them"
|
|
139
|
+
def destroy(*names)
|
|
140
|
+
Cli::Operation.new(session, kind: :destroy).call(names)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
desc "diff [STACK...]", "Show what would change in each stack, against the template that is deployed"
|
|
144
|
+
method_option :all, type: :boolean, desc: "Diff every stack of the app, dependencies first"
|
|
145
|
+
method_option :exclusively, type: :boolean, aliases: "-e",
|
|
146
|
+
desc: "Diff only the stacks named, not the ones they depend on"
|
|
147
|
+
method_option :fail, type: :boolean,
|
|
148
|
+
desc: "Exit with 1 when a stack has differences. Without it the command exits 0 either way, as the CDK's diff does"
|
|
149
|
+
method_option :security_only, type: :boolean,
|
|
150
|
+
desc: "Show only the security view: the IAM statements and the capability-affecting resources a deployment adds or " \
|
|
151
|
+
"takes away. It is exact by resource type: every property name AWS publishes a policy document in is read, " \
|
|
152
|
+
"where the CDK guesses from service metadata and warns that it may miss some"
|
|
153
|
+
def diff(*names)
|
|
154
|
+
Cli::Diff.run(session, names)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
desc "new PROJECT", "Create the directory PROJECT with the app scaffold: cdk.rb, one stack, the Gemfile and the git files"
|
|
158
|
+
def new(project = nil)
|
|
159
|
+
Cli::New.run(session, project)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
desc "bootstrap", "Deploy the tool's asset bucket as the ruby-cdk-bootstrap stack"
|
|
163
|
+
method_option :deployer, type: :string,
|
|
164
|
+
desc: "ARN of the principal that reads the assets back out of the bucket. Nothing given: the account's own principals"
|
|
165
|
+
def bootstrap
|
|
166
|
+
Cli::Bootstrap.run(session)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
no_commands do
|
|
170
|
+
# What a command starts with: its session, built from what the edge was
|
|
171
|
+
# given. Colour is decided here as the CDK decides it (cli.ts:74-83 in
|
|
172
|
+
# aws-cdk-cli): --no-color, then --color, then whether standard output is
|
|
173
|
+
# a terminal.
|
|
174
|
+
def session
|
|
175
|
+
parsed = Cli::Session.options_from(options)
|
|
176
|
+
io = shell.io
|
|
177
|
+
io.color = parsed.color.nil? ? io.terminal? : parsed.color
|
|
178
|
+
io.verbose = !!parsed.verbose
|
|
179
|
+
Cli::Session.new(command: current_command_chain.first.to_s, options: parsed, io:, aws: shell.aws)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
class << self
|
|
184
|
+
# Help and usage errors name the executable, however the file was called.
|
|
185
|
+
def basename
|
|
186
|
+
"ruby-cdk"
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Exits with the command's exit code. The one place the command line
|
|
190
|
+
# exits, and what `exe/ruby-cdk` calls.
|
|
191
|
+
def main(argv = ARGV)
|
|
192
|
+
exit run(argv)
|
|
193
|
+
rescue Interrupt
|
|
194
|
+
exit INTERRUPTED
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# Runs the command line in this process and answers its exit code
|
|
198
|
+
# (architecture section 14). io is where it writes; aws is the object that
|
|
199
|
+
# builds its clients, which a test replaces so that none is ever built.
|
|
200
|
+
def run(argv = ARGV, io: Cli::Io.standard, aws: Cli::Aws)
|
|
201
|
+
words = Cli::Arguments.command_first(Array(argv), commands: all_commands.keys + map.keys, switches: class_options)
|
|
202
|
+
named = words.first
|
|
203
|
+
shell = Cli::Shell.new(io, aws)
|
|
204
|
+
|
|
205
|
+
unless named && (command?(named) || !named.start_with?("-"))
|
|
206
|
+
# No command was named. The CDK asks for one too (demandCommand(1),
|
|
207
|
+
# parse-command-line-arguments.ts:1281 in aws-cdk-cli): the help, and exit 1.
|
|
208
|
+
help(shell)
|
|
209
|
+
io.failure("Run `ruby-cdk help COMMAND` to read about a command, for example `ruby-cdk help deploy`.")
|
|
210
|
+
return Cli::USAGE
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# A command named with a help flag prints its own help and succeeds,
|
|
214
|
+
# as the CDK's parser answers `cdk deploy --help` (yargs, wired in
|
|
215
|
+
# cli.ts of aws-cdk-cli). Thor answers `ruby-cdk help COMMAND` and
|
|
216
|
+
# maps --help at the head of the line, but takes the flag after a
|
|
217
|
+
# command for an unknown switch, which is what the audit found. All
|
|
218
|
+
# four of Thor's own help flags are answered, not only the two the
|
|
219
|
+
# audit named.
|
|
220
|
+
if words.drop(1).any? { |word| Thor::HELP_MAPPINGS.include?(word) }
|
|
221
|
+
command_help(shell, named)
|
|
222
|
+
return Cli::SUCCESS
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Thor shifts the command's word off the array it is given.
|
|
226
|
+
code = dispatch(nil, words, nil, shell:)
|
|
227
|
+
# A command answers its exit code. Thor's own help answers whatever it printed with.
|
|
228
|
+
code.is_a?(Integer) ? code : Cli::SUCCESS
|
|
229
|
+
rescue Errno::EPIPE
|
|
230
|
+
# Standard out was closed under the command, as by `head`: nothing more to write.
|
|
231
|
+
Cli::SUCCESS
|
|
232
|
+
rescue StandardError, ScriptError => e
|
|
233
|
+
Cli.report(e, io:, command: command?(named) ? named : nil)
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# True for a word that names a command, a command's alias, --version or -h.
|
|
237
|
+
def command?(word)
|
|
238
|
+
!!(word && (all_commands.key?(word.tr("-", "_")) || map.key?(word)))
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
end
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
require_relative "../fn"
|
|
5
|
+
require_relative "grant"
|
|
6
|
+
|
|
7
|
+
module RubyCdk
|
|
8
|
+
module S3
|
|
9
|
+
# An S3 bucket: the port of the CDK's Bucket (aws-s3/lib/bucket.ts), read
|
|
10
|
+
# at commit c070afb54b9e4dbddd52a85a21077fe970354985, Apache-2.0.
|
|
11
|
+
#
|
|
12
|
+
# RubyCdk::S3::Bucket.new(stack, "Images",
|
|
13
|
+
# versioned: true, bucket_name: "my-images-1a2b3c4d")
|
|
14
|
+
#
|
|
15
|
+
# Defaults, and where they differ from the CDK (architecture section 7
|
|
16
|
+
# wins; both rows are listed in section 21):
|
|
17
|
+
#
|
|
18
|
+
# - Public access is blocked on all four settings, written out. The CDK
|
|
19
|
+
# writes no PublicAccessBlockConfiguration and leans on S3's
|
|
20
|
+
# account-level default (bucket.ts, setDefaultPublicAccessBlockConfig:
|
|
21
|
+
# "this is already the case as an s3 default in aws"); here the four
|
|
22
|
+
# settings travel with the template, so the bucket is private wherever
|
|
23
|
+
# it lands. public_access_block: false writes all four as false, and a
|
|
24
|
+
# Hash fills its missing keys with true, as the CDK's
|
|
25
|
+
# S3_PUBLIC_ACCESS_BLOCKED_BY_DEFAULT behaviour does.
|
|
26
|
+
# - Encryption is on: ServerSideEncryptionConfiguration with AES256
|
|
27
|
+
# (S3_MANAGED) is written out. The CDK writes no BucketEncryption,
|
|
28
|
+
# because S3 applies SSE-S3 to every bucket without a rule (bucket.ts,
|
|
29
|
+
# parseEncryption: UNENCRYPTED is its default); the written rule is the
|
|
30
|
+
# same encryption, stated. encryption: :kms_managed writes aws:kms with
|
|
31
|
+
# the AWS-managed key, as KMS_MANAGED does; encryption: nil leaves the
|
|
32
|
+
# key out entirely.
|
|
33
|
+
# - The bucket is a retained type (architecture section 7), which the
|
|
34
|
+
# synthesis gives every AWS::S3::Bucket, as the CDK's
|
|
35
|
+
# applyRemovalPolicy does.
|
|
36
|
+
#
|
|
37
|
+
# The grants are the CDK's read and readWrite (aws-s3/lib/bucket-grants.ts
|
|
38
|
+
# and perms.ts, at the same commit): read is s3:GetObject*, s3:GetBucket*
|
|
39
|
+
# and s3:List* on the bucket ARN and on the objects' ARN; read_write adds
|
|
40
|
+
# the delete and put actions. A bucket encrypted with a KMS key here has no
|
|
41
|
+
# key object to grant on, so the kms:* actions the CDK adds on the key are
|
|
42
|
+
# not granted; use encryption: :kms_managed only with principals that
|
|
43
|
+
# already may use aws/s3.
|
|
44
|
+
class Bucket < Construct
|
|
45
|
+
# The four settings, all true, as the CDK's BlockPublicAccess.BLOCK_ALL.
|
|
46
|
+
PUBLIC_ACCESS_BLOCK = {
|
|
47
|
+
"BlockPublicAcls" => true,
|
|
48
|
+
"BlockPublicPolicy" => true,
|
|
49
|
+
"IgnorePublicAcls" => true,
|
|
50
|
+
"RestrictPublicBuckets" => true
|
|
51
|
+
}.freeze
|
|
52
|
+
|
|
53
|
+
# The CDK's read actions for a bucket and its objects
|
|
54
|
+
# (aws-s3/lib/perms.ts, BUCKET_READ_ACTIONS).
|
|
55
|
+
READ_ACTIONS = [ "s3:GetObject*", "s3:GetBucket*", "s3:List*" ].freeze
|
|
56
|
+
|
|
57
|
+
# The read plus delete and put actions of readWrite, in the CDK's order:
|
|
58
|
+
# BUCKET_READ_ACTIONS, then BUCKET_DELETE_ACTIONS and the put actions of
|
|
59
|
+
# S3_GRANT_WRITE_WITHOUT_ACL (perms.ts, BUCKET_PUT_ACTIONS).
|
|
60
|
+
READ_WRITE_ACTIONS = [
|
|
61
|
+
"s3:GetObject*", "s3:GetBucket*", "s3:List*",
|
|
62
|
+
"s3:DeleteObject*",
|
|
63
|
+
"s3:PutObject", "s3:PutObjectLegalHold", "s3:PutObjectRetention",
|
|
64
|
+
"s3:PutObjectTagging", "s3:PutObjectVersionTagging", "s3:Abort*"
|
|
65
|
+
].freeze
|
|
66
|
+
|
|
67
|
+
attr_reader :encryption, :public_access_block
|
|
68
|
+
|
|
69
|
+
def initialize(scope, id, bucket_name: nil, versioned: false, encryption: :s3_managed, public_access_block: nil)
|
|
70
|
+
super(scope, id)
|
|
71
|
+
@encryption = valid_encryption(encryption)
|
|
72
|
+
@bucket_name = valid_bucket_name(bucket_name)
|
|
73
|
+
@public_access_block = public_access_settings(public_access_block)
|
|
74
|
+
|
|
75
|
+
@resource = resource("Resource", "AWS::S3::Bucket",
|
|
76
|
+
BucketName: @bucket_name,
|
|
77
|
+
BucketEncryption: encryption_property,
|
|
78
|
+
VersioningConfiguration: versioned ? { "Status" => "Enabled" } : nil,
|
|
79
|
+
PublicAccessBlockConfiguration: @public_access_block)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The logical ID of the AWS::S3::Bucket under this construct, so that the
|
|
83
|
+
# bucket can sit in another resource's depends_on:.
|
|
84
|
+
def logical_id
|
|
85
|
+
@resource.logical_id
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The bucket's ARN.
|
|
89
|
+
def arn
|
|
90
|
+
Fn.get_att(@resource, "Arn")
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The bucket's name: the one given, or the Ref of the resource.
|
|
94
|
+
def bucket_name
|
|
95
|
+
@bucket_name || Fn.ref(@resource)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The ARN of the bucket's objects under key_pattern, as the CDK's
|
|
99
|
+
# arnForObjects: the bucket ARN, then the pattern.
|
|
100
|
+
def objects_arn(key_pattern = "*")
|
|
101
|
+
unless key_pattern.is_a?(String) && !key_pattern.empty?
|
|
102
|
+
raise UsageError, "#{path}: objects_arn takes a key pattern String, got #{key_pattern.inspect}"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
Fn.join("", [ arn, "/#{key_pattern}" ])
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Grants read on the bucket and its contents to the principal: the CDK's
|
|
109
|
+
# grantRead (bucket-grants.ts, read). An IAM::Role carries the statement
|
|
110
|
+
# on its own policy; a principal named by ARN gets the Grant back
|
|
111
|
+
# unapplied.
|
|
112
|
+
def grant_read(principal, objects_key_pattern: "*")
|
|
113
|
+
Constructs::Grant.add_to_principal(principal, READ_ACTIONS, [ arn, objects_arn(objects_key_pattern) ])
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Grants read and write: the CDK's grantReadWrite (bucket-grants.ts,
|
|
117
|
+
# readWrite, with the put actions its S3_GRANT_WRITE_WITHOUT_ACL flag
|
|
118
|
+
# enables).
|
|
119
|
+
def grant_read_write(principal, objects_key_pattern: "*")
|
|
120
|
+
Constructs::Grant.add_to_principal(principal, READ_WRITE_ACTIONS, [ arn, objects_arn(objects_key_pattern) ])
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
private
|
|
124
|
+
|
|
125
|
+
def valid_encryption(value)
|
|
126
|
+
return value if value.nil? || %i[s3_managed kms_managed].include?(value)
|
|
127
|
+
|
|
128
|
+
raise UsageError, "#{path}: encryption takes :s3_managed, :kms_managed or nil, got #{value.inspect}"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# S3's own rules for a bucket name, as the CDK validates them
|
|
132
|
+
# (bucket.ts, _validateBucketName, read at the commit in the header):
|
|
133
|
+
# 3 to 63 characters, lower-case letters, digits, dots and hyphens,
|
|
134
|
+
# starting and ending with a letter or a digit, no period next to a dash
|
|
135
|
+
# and no consecutive periods, and no name that resembles an IP address.
|
|
136
|
+
def valid_bucket_name(name)
|
|
137
|
+
return nil if name.nil?
|
|
138
|
+
unless name.is_a?(String) && !name.empty?
|
|
139
|
+
raise UsageError, "#{path}: the bucket name must be a String, got #{name.inspect}"
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
errors = bucket_name_errors(name)
|
|
143
|
+
return name if errors.empty?
|
|
144
|
+
|
|
145
|
+
raise UsageError, "Invalid S3 bucket name (value: #{name})#{errors.map { |error| "\n #{error}" }.join}"
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# The CDK's rule list, with its own words and offsets.
|
|
149
|
+
def bucket_name_errors(name)
|
|
150
|
+
errors = []
|
|
151
|
+
errors << "Bucket name must be at least 3 and no more than 63 characters" if name.length < 3 || name.length > 63
|
|
152
|
+
if (offset = name.index(/[^a-z0-9.-]/))
|
|
153
|
+
errors << "Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: #{offset})"
|
|
154
|
+
end
|
|
155
|
+
errors << "Bucket name must start with a lowercase character or number (offset: 0)" unless name.match?(/\A[a-z0-9]/)
|
|
156
|
+
errors << "Bucket name must end with a lowercase character or number (offset: #{name.length - 1})" unless name.match?(/[a-z0-9]\z/)
|
|
157
|
+
if (offset = name.index(/\.-|-\.|\.\./))
|
|
158
|
+
errors << "Bucket name must not have dash next to period, or period next to dash, or consecutive periods (offset: #{offset})"
|
|
159
|
+
end
|
|
160
|
+
errors << "Bucket name must not resemble an IP address" if name.match?(/\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/)
|
|
161
|
+
errors
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def public_access_settings(value)
|
|
165
|
+
case value
|
|
166
|
+
when nil then PUBLIC_ACCESS_BLOCK.dup
|
|
167
|
+
when false then PUBLIC_ACCESS_BLOCK.to_h { |key, _| [ key, false ] }
|
|
168
|
+
when true then PUBLIC_ACCESS_BLOCK.dup
|
|
169
|
+
when Hash
|
|
170
|
+
PUBLIC_ACCESS_BLOCK.to_h { |key, default| [ key, value.key?(key) ? value[key] : default ] }
|
|
171
|
+
else
|
|
172
|
+
raise UsageError, "#{path}: public_access_block takes true, false or a Hash of the four settings, got #{value.inspect}"
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# The CDK's rendering per mode (bucket.ts, parseEncryption): SSE-S3 is
|
|
177
|
+
# the AES256 algorithm, KMS_MANAGED the aws:kms one with the AWS-managed
|
|
178
|
+
# key, and nothing is written when encryption is nil.
|
|
179
|
+
def encryption_property
|
|
180
|
+
return nil if @encryption.nil?
|
|
181
|
+
|
|
182
|
+
algorithm = @encryption == :kms_managed ? "aws:kms" : "AES256"
|
|
183
|
+
{
|
|
184
|
+
"ServerSideEncryptionConfiguration" => [
|
|
185
|
+
{ "ServerSideEncryptionByDefault" => { "SSEAlgorithm" => algorithm } }
|
|
186
|
+
]
|
|
187
|
+
}
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|