ruby-cdk 0.2.0 → 0.3.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 +4 -4
- data/CHANGELOG.md +253 -0
- data/README.md +28 -5
- data/lib/ruby_cdk/assets/hasher.rb +4 -0
- data/lib/ruby_cdk/assets.rb +4 -8
- data/lib/ruby_cdk/cli/aws.rb +23 -5
- data/lib/ruby_cdk/cli/drift.rb +94 -0
- data/lib/ruby_cdk/cli/import.rb +306 -0
- data/lib/ruby_cdk/cli/lookup.rb +214 -0
- data/lib/ruby_cdk/cli/session.rb +114 -3
- data/lib/ruby_cdk/cli/watch.rb +297 -0
- data/lib/ruby_cdk/cli.rb +76 -1
- data/lib/ruby_cdk/constructs/function.rb +24 -3
- data/lib/ruby_cdk/constructs/hosted_zone.rb +342 -0
- data/lib/ruby_cdk/constructs/record_set.rb +330 -0
- data/lib/ruby_cdk/constructs/ssm_parameter.rb +174 -0
- data/lib/ruby_cdk/constructs/vpc.rb +822 -0
- data/lib/ruby_cdk/constructs/vpc_network.rb +159 -0
- data/lib/ruby_cdk/constructs.rb +5 -1
- data/lib/ruby_cdk/core/app.rb +28 -2
- data/lib/ruby_cdk/core/construct.rb +2 -0
- data/lib/ruby_cdk/core/context.rb +138 -0
- data/lib/ruby_cdk/core/stack.rb +4 -0
- data/lib/ruby_cdk/core.rb +1 -0
- data/lib/ruby_cdk/deploy/deployer.rb +283 -8
- data/lib/ruby_cdk/deploy/drift.rb +389 -0
- data/lib/ruby_cdk/deploy/ec2_adapter.rb +214 -0
- data/lib/ruby_cdk/deploy/errors.rb +8 -0
- data/lib/ruby_cdk/deploy/fakes.rb +230 -22
- data/lib/ruby_cdk/deploy/output/base.rb +8 -0
- data/lib/ruby_cdk/deploy/output/json.rb +7 -0
- data/lib/ruby_cdk/deploy/poller.rb +10 -0
- data/lib/ruby_cdk/deploy/ports.rb +124 -8
- data/lib/ruby_cdk/deploy/sdk_adapters.rb +141 -6
- data/lib/ruby_cdk/deploy/ssm_adapter.rb +111 -0
- data/lib/ruby_cdk/deploy.rb +1 -0
- data/lib/ruby_cdk/diff/compare.rb +1 -1
- data/lib/ruby_cdk/diff/identity_center.rb +64 -0
- data/lib/ruby_cdk/diff/render.rb +147 -8
- data/lib/ruby_cdk/diff/security.rb +195 -10
- data/lib/ruby_cdk/diff/security_group.rb +94 -0
- data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +2 -0
- data/lib/ruby_cdk/schema/sources/provider_type.rb +3 -0
- data/lib/ruby_cdk/test/private/cyclic.rb +4 -0
- data/lib/ruby_cdk/test/private/js.rb +1 -3
- data/lib/ruby_cdk/version.rb +1 -1
- metadata +43 -1
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "statement"
|
|
4
|
+
require_relative "security_group"
|
|
5
|
+
require_relative "identity_center"
|
|
4
6
|
|
|
5
7
|
module RubyCdk
|
|
6
8
|
module Diff
|
|
7
9
|
# The security view of a diff (architecture section 10): the IAM
|
|
8
10
|
# statements a deployment adds or takes away, and the managed policies it
|
|
9
11
|
# attaches, in a table of their own, with the capabilities the new
|
|
10
|
-
# template needs that the deployed one did not.
|
|
12
|
+
# template needs that the deployed one did not. Beside them, the security
|
|
13
|
+
# group rules it opens and closes and the IAM Identity Center entries it
|
|
14
|
+
# adds or takes away, which the CDK's permissionsBroadened counts too
|
|
15
|
+
# (diff/types.ts:125-127, iam-changes.ts:75-82,
|
|
16
|
+
# security-group-changes.ts:95-98, all at f7c9dca95f440a076c2abd61b66b984624b8548e).
|
|
11
17
|
#
|
|
12
18
|
# The CDK's view is a heuristic: it reads a property only where AWS's
|
|
13
19
|
# service metadata says the property may carry a policy, and warns for it
|
|
@@ -16,7 +22,9 @@ module RubyCdk
|
|
|
16
22
|
# carries: every resource of both templates is walked for the property
|
|
17
23
|
# names that carry policy documents anywhere AWS publishes one, so a
|
|
18
24
|
# statement cannot hide in a type the metadata does not mention. That is
|
|
19
|
-
# the one line the command's help says it with.
|
|
25
|
+
# the one line the command's help says it with. The rules and the Identity
|
|
26
|
+
# Center entries are exact the other way round, as the CDK's are: named
|
|
27
|
+
# resource types and the rule properties of a group.
|
|
20
28
|
class Security
|
|
21
29
|
# The property names that carry an IAM policy document, wherever AWS
|
|
22
30
|
# publishes one: identity policies, resource policies, trust policies,
|
|
@@ -57,6 +65,24 @@ module RubyCdk
|
|
|
57
65
|
].freeze
|
|
58
66
|
NAME_PROPERTIES = %w[RoleName ManagedPolicyName UserName GroupName InstanceProfileName].freeze
|
|
59
67
|
|
|
68
|
+
# The rule properties of a group, and the resources whose one job is a
|
|
69
|
+
# rule on one: what the CDK scrutinizes as IngressRules and EgressRules,
|
|
70
|
+
# and as IngressRuleResource and EgressRuleResource.
|
|
71
|
+
RULE_PROPERTIES = %w[SecurityGroupIngress SecurityGroupEgress].freeze
|
|
72
|
+
RULE_RESOURCE_TYPES = %w[
|
|
73
|
+
AWS::EC2::SecurityGroupIngress
|
|
74
|
+
AWS::EC2::SecurityGroupEgress
|
|
75
|
+
].freeze
|
|
76
|
+
GROUP_TYPE = "AWS::EC2::SecurityGroup"
|
|
77
|
+
|
|
78
|
+
# The IAM Identity Center resources whose additions the view counts, as
|
|
79
|
+
# the CDK scrutinizes them (iam-changes.ts:30-37).
|
|
80
|
+
IDENTITY_CENTER_TYPES = %w[
|
|
81
|
+
AWS::SSO::PermissionSet
|
|
82
|
+
AWS::SSO::Assignment
|
|
83
|
+
AWS::SSO::InstanceAccessControlAttributeConfiguration
|
|
84
|
+
].freeze
|
|
85
|
+
|
|
60
86
|
# One managed policy attachment: an identity, and the ARN attached to it.
|
|
61
87
|
Attachment = Data.define(:identity, :arn) do
|
|
62
88
|
def equal?(other)
|
|
@@ -71,7 +97,13 @@ module RubyCdk
|
|
|
71
97
|
# The statements of each side of the diff: additions are the ones the new
|
|
72
98
|
# template carries that the deployed one does not, removals the reverse.
|
|
73
99
|
# Managed policies the same, as attachments of an identity to an ARN.
|
|
100
|
+
# Security group rules and IAM Identity Center entries the same, matched
|
|
101
|
+
# one entry to one entry by what they say.
|
|
74
102
|
attr_reader :statement_additions, :statement_removals, :managed_policy_additions, :managed_policy_removals
|
|
103
|
+
attr_reader :rule_additions, :rule_removals
|
|
104
|
+
attr_reader :sso_permission_set_additions, :sso_permission_set_removals
|
|
105
|
+
attr_reader :sso_assignment_additions, :sso_assignment_removals
|
|
106
|
+
attr_reader :sso_instance_config_additions, :sso_instance_config_removals
|
|
75
107
|
attr_reader :capabilities_added
|
|
76
108
|
|
|
77
109
|
def self.build(diff)
|
|
@@ -79,50 +111,96 @@ module RubyCdk
|
|
|
79
111
|
statements_new = []
|
|
80
112
|
policies_old = []
|
|
81
113
|
policies_new = []
|
|
114
|
+
rules_old = []
|
|
115
|
+
rules_new = []
|
|
116
|
+
permission_sets_old = []
|
|
117
|
+
permission_sets_new = []
|
|
118
|
+
assignments_old = []
|
|
119
|
+
assignments_new = []
|
|
120
|
+
instance_configs_old = []
|
|
121
|
+
instance_configs_new = []
|
|
82
122
|
|
|
83
123
|
diff.resources.each_value do |change|
|
|
84
124
|
statements_old.concat(change_statements(change, :old))
|
|
85
125
|
statements_new.concat(change_statements(change, :new))
|
|
86
126
|
policies_old.concat(managed_policies(change, :old))
|
|
87
127
|
policies_new.concat(managed_policies(change, :new))
|
|
128
|
+
rules_old.concat(change_rules(change, :old))
|
|
129
|
+
rules_new.concat(change_rules(change, :new))
|
|
130
|
+
permission_sets_old.concat(change_sso_permission_sets(change, :old))
|
|
131
|
+
permission_sets_new.concat(change_sso_permission_sets(change, :new))
|
|
132
|
+
assignments_old.concat(change_sso_assignments(change, :old))
|
|
133
|
+
assignments_new.concat(change_sso_assignments(change, :new))
|
|
134
|
+
instance_configs_old.concat(change_sso_instance_configs(change, :old))
|
|
135
|
+
instance_configs_new.concat(change_sso_instance_configs(change, :new))
|
|
88
136
|
end
|
|
89
137
|
|
|
90
138
|
capabilities = capability_set(diff_resources(diff, :new)) - capability_set(diff_resources(diff, :old))
|
|
91
139
|
new(statement_additions: added(statements_new, statements_old), statement_removals: added(statements_old, statements_new),
|
|
92
140
|
managed_policy_additions: added(policies_new, policies_old), managed_policy_removals: added(policies_old, policies_new),
|
|
141
|
+
rule_additions: added(rules_new, rules_old), rule_removals: added(rules_old, rules_new),
|
|
142
|
+
sso_permission_set_additions: added(permission_sets_new, permission_sets_old),
|
|
143
|
+
sso_permission_set_removals: added(permission_sets_old, permission_sets_new),
|
|
144
|
+
sso_assignment_additions: added(assignments_new, assignments_old),
|
|
145
|
+
sso_assignment_removals: added(assignments_old, assignments_new),
|
|
146
|
+
sso_instance_config_additions: added(instance_configs_new, instance_configs_old),
|
|
147
|
+
sso_instance_config_removals: added(instance_configs_old, instance_configs_new),
|
|
93
148
|
capabilities_added: capabilities)
|
|
94
149
|
end
|
|
95
150
|
|
|
96
|
-
def initialize(statement_additions:, statement_removals:, managed_policy_additions:, managed_policy_removals:,
|
|
151
|
+
def initialize(statement_additions:, statement_removals:, managed_policy_additions:, managed_policy_removals:,
|
|
152
|
+
rule_additions:, rule_removals:, sso_permission_set_additions:, sso_permission_set_removals:,
|
|
153
|
+
sso_assignment_additions:, sso_assignment_removals:, sso_instance_config_additions:, sso_instance_config_removals:,
|
|
154
|
+
capabilities_added:)
|
|
97
155
|
@statement_additions = statement_additions.freeze
|
|
98
156
|
@statement_removals = statement_removals.freeze
|
|
99
157
|
@managed_policy_additions = managed_policy_additions.freeze
|
|
100
158
|
@managed_policy_removals = managed_policy_removals.freeze
|
|
159
|
+
@rule_additions = rule_additions.freeze
|
|
160
|
+
@rule_removals = rule_removals.freeze
|
|
161
|
+
@sso_permission_set_additions = sso_permission_set_additions.freeze
|
|
162
|
+
@sso_permission_set_removals = sso_permission_set_removals.freeze
|
|
163
|
+
@sso_assignment_additions = sso_assignment_additions.freeze
|
|
164
|
+
@sso_assignment_removals = sso_assignment_removals.freeze
|
|
165
|
+
@sso_instance_config_additions = sso_instance_config_additions.freeze
|
|
166
|
+
@sso_instance_config_removals = sso_instance_config_removals.freeze
|
|
101
167
|
@capabilities_added = capabilities_added.freeze
|
|
102
168
|
end
|
|
103
169
|
|
|
104
170
|
def has_changes?
|
|
105
171
|
statement_additions.any? || statement_removals.any? || managed_policy_additions.any? ||
|
|
106
|
-
managed_policy_removals.any? ||
|
|
172
|
+
managed_policy_removals.any? || rule_additions.any? || rule_removals.any? ||
|
|
173
|
+
sso_permission_set_additions.any? || sso_permission_set_removals.any? ||
|
|
174
|
+
sso_assignment_additions.any? || sso_assignment_removals.any? ||
|
|
175
|
+
sso_instance_config_additions.any? || sso_instance_config_removals.any? || capabilities_added.any?
|
|
107
176
|
end
|
|
108
177
|
|
|
109
178
|
# True when the deployment widens what the deployed template may do, the
|
|
110
|
-
# CDK's permissionsBroadened: a positive statement
|
|
111
|
-
# one taken away, a managed policy attached
|
|
179
|
+
# CDK's permissionsBroadened (iam-changes.ts:75-82): a positive statement
|
|
180
|
+
# granted, a negative one taken away, a managed policy attached, an IAM
|
|
181
|
+
# Identity Center permission set, assignment or access-control attribute
|
|
182
|
+
# configuration added -- or a security group rule added, the CDK's
|
|
183
|
+
# rulesAdded (security-group-changes.ts:95-98), which its
|
|
184
|
+
# permissionsBroadened counts with the IAM half (diff/types.ts:125-127).
|
|
112
185
|
def broadening?
|
|
113
186
|
statement_additions.any? { |statement| !statement.negative? } ||
|
|
114
187
|
statement_removals.any?(&:negative?) ||
|
|
115
|
-
managed_policy_additions.any?
|
|
188
|
+
managed_policy_additions.any? ||
|
|
189
|
+
rule_additions.any? ||
|
|
190
|
+
sso_permission_set_additions.any? || sso_assignment_additions.any? || sso_instance_config_additions.any?
|
|
116
191
|
end
|
|
117
192
|
|
|
118
193
|
# True when the change of this resource is one the security view owns: a
|
|
119
194
|
# capability-affecting type, a policy document it carries, or a managed
|
|
120
|
-
# policy it attaches, on either side of the diff
|
|
195
|
+
# policy it attaches, on either side of the diff; a rule of a group, a
|
|
196
|
+
# rule resource of its own, or an IAM Identity Center resource.
|
|
121
197
|
def security_change?(change)
|
|
122
|
-
[ change.old_type, change.new_type ].compact
|
|
198
|
+
types = [ change.old_type, change.new_type ].compact
|
|
199
|
+
types.any? { |type| CAPABILITY_TYPES.include?(type) || RULE_RESOURCE_TYPES.include?(type) || IDENTITY_CENTER_TYPES.include?(type) } ||
|
|
123
200
|
change.old_type == "AWS::Lambda::Permission" || change.new_type == "AWS::Lambda::Permission" ||
|
|
124
201
|
self.class.properties_of(change, :old)&.key?("ManagedPolicyArns") ||
|
|
125
202
|
self.class.properties_of(change, :new)&.key?("ManagedPolicyArns") ||
|
|
203
|
+
self.class.group_rule_change?(change) ||
|
|
126
204
|
self.class.watched_property?(self.class.properties_of(change, :old)) ||
|
|
127
205
|
self.class.watched_property?(self.class.properties_of(change, :new))
|
|
128
206
|
end
|
|
@@ -140,6 +218,15 @@ module RubyCdk
|
|
|
140
218
|
"managed_policies" => {
|
|
141
219
|
"additions" => managed_policy_additions.map(&:to_h),
|
|
142
220
|
"removals" => managed_policy_removals.map(&:to_h)
|
|
221
|
+
},
|
|
222
|
+
"security_group_rules" => {
|
|
223
|
+
"additions" => rule_additions.map(&:to_h),
|
|
224
|
+
"removals" => rule_removals.map(&:to_h)
|
|
225
|
+
},
|
|
226
|
+
"identity_center" => {
|
|
227
|
+
"permission_sets" => { "additions" => sso_permission_set_additions.map(&:to_h), "removals" => sso_permission_set_removals.map(&:to_h) },
|
|
228
|
+
"assignments" => { "additions" => sso_assignment_additions.map(&:to_h), "removals" => sso_assignment_removals.map(&:to_h) },
|
|
229
|
+
"instance_configs" => { "additions" => sso_instance_config_additions.map(&:to_h), "removals" => sso_instance_config_removals.map(&:to_h) }
|
|
143
230
|
}
|
|
144
231
|
}
|
|
145
232
|
end
|
|
@@ -163,6 +250,102 @@ module RubyCdk
|
|
|
163
250
|
Array(arns).map { |arn| Attachment.new(identity: "${#{change.logical_id}}", arn: arn) }
|
|
164
251
|
end
|
|
165
252
|
|
|
253
|
+
# The security group rules one side of one resource carries: the inline
|
|
254
|
+
# lists of a group itself, and the rule resources of their own. This is
|
|
255
|
+
# the CDK's IngressRules and EgressRules scrutiny beside its
|
|
256
|
+
# IngressRuleResource and EgressRuleResource scrutiny
|
|
257
|
+
# (security-group-changes.ts:23-46).
|
|
258
|
+
def self.change_rules(change, side)
|
|
259
|
+
type = side == :old ? change.old_type : change.new_type
|
|
260
|
+
properties = properties_of(change, side)
|
|
261
|
+
return [] if type.nil? || !properties.is_a?(Hash)
|
|
262
|
+
|
|
263
|
+
case type
|
|
264
|
+
when GROUP_TYPE
|
|
265
|
+
inline_rules(properties["SecurityGroupIngress"], :ingress, change.logical_id) +
|
|
266
|
+
inline_rules(properties["SecurityGroupEgress"], :egress, change.logical_id)
|
|
267
|
+
when "AWS::EC2::SecurityGroupIngress"
|
|
268
|
+
[ SecurityGroupRule.read(RenderIntrinsics.call(properties), :ingress) ]
|
|
269
|
+
when "AWS::EC2::SecurityGroupEgress"
|
|
270
|
+
[ SecurityGroupRule.read(RenderIntrinsics.call(properties), :egress) ]
|
|
271
|
+
else []
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# The inline rules of one group: the list its SecurityGroupIngress or
|
|
276
|
+
# SecurityGroupEgress property holds, each rule's group defaulted to the
|
|
277
|
+
# group itself (the CDK's readInlineRules, whose ref is
|
|
278
|
+
# "${LogicalId.GroupId}").
|
|
279
|
+
def self.inline_rules(rules, direction, logical_id)
|
|
280
|
+
return [] unless rules.is_a?(Array)
|
|
281
|
+
|
|
282
|
+
group_ref = "${#{logical_id}.GroupId}"
|
|
283
|
+
rules.filter_map do |rule|
|
|
284
|
+
rendered = RenderIntrinsics.call(rule)
|
|
285
|
+
SecurityGroupRule.read(rendered, direction, group_ref: group_ref) if rendered.is_a?(Hash)
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# The IAM Identity Center entries one side of one resource carries, read
|
|
290
|
+
# after the intrinsics are rendered, under the resource's logical ID (the
|
|
291
|
+
# CDK's readSsoPermissionSet, readSsoAssignments and
|
|
292
|
+
# readSsoInstanceACAConfigs, iam-changes.ts:375-421).
|
|
293
|
+
def self.change_sso_permission_sets(change, side)
|
|
294
|
+
read_sso(change, side, "AWS::SSO::PermissionSet") do |rendered, logical_id|
|
|
295
|
+
SsoPermissionSet.new(
|
|
296
|
+
logical_id: "${#{logical_id}}",
|
|
297
|
+
name: rendered["Name"],
|
|
298
|
+
instance_arn: rendered["InstanceArn"],
|
|
299
|
+
permissions_boundary: rendered["PermissionsBoundary"],
|
|
300
|
+
customer_managed_policy_references: rendered["CustomerManagedPolicyReferences"]
|
|
301
|
+
)
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def self.change_sso_assignments(change, side)
|
|
306
|
+
read_sso(change, side, "AWS::SSO::Assignment") do |rendered, logical_id|
|
|
307
|
+
SsoAssignment.new(
|
|
308
|
+
logical_id: "${#{logical_id}}",
|
|
309
|
+
instance_arn: rendered["InstanceArn"],
|
|
310
|
+
permission_set_arn: rendered["PermissionSetArn"],
|
|
311
|
+
principal_id: rendered["PrincipalId"],
|
|
312
|
+
principal_type: rendered["PrincipalType"],
|
|
313
|
+
target_id: rendered["TargetId"],
|
|
314
|
+
target_type: rendered["TargetType"]
|
|
315
|
+
)
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def self.change_sso_instance_configs(change, side)
|
|
320
|
+
read_sso(change, side, "AWS::SSO::InstanceAccessControlAttributeConfiguration") do |rendered, logical_id|
|
|
321
|
+
SsoInstanceAcaConfig.new(
|
|
322
|
+
logical_id: "${#{logical_id}}",
|
|
323
|
+
instance_arn: rendered["InstanceArn"],
|
|
324
|
+
access_control_attributes: rendered["AccessControlAttributes"]
|
|
325
|
+
)
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
# The entries of one Identity Center type on one side: none, or the one
|
|
330
|
+
# resource of that type rendered.
|
|
331
|
+
def self.read_sso(change, side, type)
|
|
332
|
+
properties = properties_of(change, side)
|
|
333
|
+
return [] if (side == :old ? change.old_type : change.new_type) != type || !properties.is_a?(Hash)
|
|
334
|
+
|
|
335
|
+
[ yield(RenderIntrinsics.call(properties), change.logical_id) ]
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
# True when either side of the change is a group that carries a rule
|
|
339
|
+
# property: the change is one the rule table owns.
|
|
340
|
+
def self.group_rule_change?(change)
|
|
341
|
+
types = [ change.old_type, change.new_type ].compact
|
|
342
|
+
return false unless types.include?(GROUP_TYPE)
|
|
343
|
+
|
|
344
|
+
[ properties_of(change, :old), properties_of(change, :new) ].any? do |properties|
|
|
345
|
+
properties.is_a?(Hash) && RULE_PROPERTIES.any? { |property| properties.key?(property) }
|
|
346
|
+
end
|
|
347
|
+
end
|
|
348
|
+
|
|
166
349
|
def self.properties_of(change, side)
|
|
167
350
|
value = side == :old ? change.old_value : change.new_value
|
|
168
351
|
value.is_a?(Hash) ? value["Properties"] : nil
|
|
@@ -314,7 +497,9 @@ module RubyCdk
|
|
|
314
497
|
named ? %w[CAPABILITY_IAM CAPABILITY_NAMED_IAM] : %w[CAPABILITY_IAM]
|
|
315
498
|
end
|
|
316
499
|
|
|
317
|
-
private_class_method :change_statements, :managed_policies, :
|
|
500
|
+
private_class_method :change_statements, :managed_policies, :change_rules, :inline_rules,
|
|
501
|
+
:change_sso_permission_sets, :change_sso_assignments, :change_sso_instance_configs, :read_sso,
|
|
502
|
+
:diff_resources, :added,
|
|
318
503
|
:read_statements, :walk, :policy_statements, :identity_policy_resource,
|
|
319
504
|
:attached_policy_resource, :parse_statements, :capability_set
|
|
320
505
|
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyCdk
|
|
4
|
+
module Diff
|
|
5
|
+
# One security group rule, ingress or egress, the CDK's SecurityGroupRule
|
|
6
|
+
# (cloudformation-diff, lib/network/security-group-rule.ts at
|
|
7
|
+
# f7c9dca95f440a076c2abd61b66b984624b8548e): the group it opens a hole in,
|
|
8
|
+
# the protocol and the port range, and the peer the hole lets through -- an
|
|
9
|
+
# IP range, another security group, or a prefix list. Two rules are the
|
|
10
|
+
# same when all of that is the same (security-group-rule.ts:50-56), which
|
|
11
|
+
# is how the security view tells an added rule from one that was already
|
|
12
|
+
# deployed; the direction takes part too, where the CDK keeps two
|
|
13
|
+
# collections apart.
|
|
14
|
+
SecurityGroupRule = Data.define(:direction, :group_id, :ip_protocol, :from_port, :to_port, :peer) do
|
|
15
|
+
# True when the two rules open the same hole in the same group, on the
|
|
16
|
+
# same side.
|
|
17
|
+
def equal?(other)
|
|
18
|
+
other.is_a?(SecurityGroupRule) && self == other
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# The protocol cell of the security group table, the CDK's
|
|
22
|
+
# describeProtocol (security-group-rule.ts:58-72): "Everything" for all
|
|
23
|
+
# protocols, "All TCP" for the whole range of one, the port or the range
|
|
24
|
+
# otherwise.
|
|
25
|
+
def describe_protocol
|
|
26
|
+
return "Everything" if ip_protocol == "-1"
|
|
27
|
+
|
|
28
|
+
protocol = ip_protocol.upcase
|
|
29
|
+
return "All #{protocol}" if from_port == -1
|
|
30
|
+
return "#{protocol} #{from_port}" if from_port == to_port
|
|
31
|
+
|
|
32
|
+
"#{protocol} #{from_port}-#{to_port}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# The peer cell, the CDK's describePeer (security-group-rule.ts:74-91):
|
|
36
|
+
# everyone for the open ranges, the range, the group or the list
|
|
37
|
+
# otherwise, and "?" where the rule names no peer.
|
|
38
|
+
def describe_peer
|
|
39
|
+
case peer&.fetch("kind", nil)
|
|
40
|
+
when "cidr-ip"
|
|
41
|
+
return "Everyone (IPv4)" if peer["ip"] == "0.0.0.0/0"
|
|
42
|
+
return "Everyone (IPv6)" if peer["ip"] == "::/0"
|
|
43
|
+
|
|
44
|
+
peer["ip"].to_s
|
|
45
|
+
when "security-group" then peer["security_group_id"].to_s
|
|
46
|
+
when "prefix-list" then peer["prefix_list_id"].to_s
|
|
47
|
+
else "?"
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The machine-readable rule (--json).
|
|
52
|
+
def to_h
|
|
53
|
+
{
|
|
54
|
+
"direction" => direction.to_s,
|
|
55
|
+
"group_id" => group_id,
|
|
56
|
+
"ip_protocol" => ip_protocol,
|
|
57
|
+
"from_port" => from_port,
|
|
58
|
+
"to_port" => to_port,
|
|
59
|
+
"peer" => peer
|
|
60
|
+
}
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The rule a template carries, rendered: the group named on the rule, or
|
|
64
|
+
# the group the inline list sits on (the CDK's constructor,
|
|
65
|
+
# security-group-rule.ts:30-48, whose groupRef the inline rules get).
|
|
66
|
+
def self.read(rendered, direction, group_ref: nil)
|
|
67
|
+
protocol = rendered["IpProtocol"]
|
|
68
|
+
new(
|
|
69
|
+
direction: direction,
|
|
70
|
+
group_id: rendered["GroupId"] || group_ref || "*unknown*",
|
|
71
|
+
ip_protocol: protocol.nil? || protocol.to_s.empty? ? "*unknown*" : protocol.to_s,
|
|
72
|
+
from_port: rendered["FromPort"],
|
|
73
|
+
to_port: rendered["ToPort"],
|
|
74
|
+
peer: peer_of(rendered)
|
|
75
|
+
)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# The peer keys in the order the CDK's findFirst tries them
|
|
79
|
+
# (security-group-rule.ts:36-47): an IP range, then a security group,
|
|
80
|
+
# then a prefix list, first key present winning.
|
|
81
|
+
def self.peer_of(rendered)
|
|
82
|
+
if (key = %w[CidrIp CidrIpv6].find { |candidate| rendered.key?(candidate) })
|
|
83
|
+
return { "kind" => "cidr-ip", "ip" => rendered[key] }
|
|
84
|
+
end
|
|
85
|
+
if (key = %w[DestinationSecurityGroupId SourceSecurityGroupId].find { |candidate| rendered.key?(candidate) })
|
|
86
|
+
return { "kind" => "security-group", "security_group_id" => rendered[key] }
|
|
87
|
+
end
|
|
88
|
+
return { "kind" => "prefix-list", "prefix_list_id" => rendered[key] } if (key = %w[DestinationPrefixListId SourcePrefixListId].find { |candidate| rendered.key?(candidate) })
|
|
89
|
+
|
|
90
|
+
nil
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -60,6 +60,7 @@ module RubyCdk
|
|
|
60
60
|
|
|
61
61
|
protected
|
|
62
62
|
|
|
63
|
+
# :nocov: abstract base stubs; overridden by every SecurityGroupRule subclass
|
|
63
64
|
# Which of the rule's open ports are this rule's finding. Each of the
|
|
64
65
|
# two rules answers; this body only holds the type together.
|
|
65
66
|
def flags_ports?(_ports)
|
|
@@ -71,6 +72,7 @@ module RubyCdk
|
|
|
71
72
|
def message(_cidr, _ports)
|
|
72
73
|
""
|
|
73
74
|
end
|
|
75
|
+
# :nocov:
|
|
74
76
|
|
|
75
77
|
private
|
|
76
78
|
|
|
@@ -350,7 +350,10 @@ module RubyCdk
|
|
|
350
350
|
|
|
351
351
|
case left.kind
|
|
352
352
|
when :nested
|
|
353
|
+
# :nocov: and_ never receives two :nested descriptors; only the property-level
|
|
354
|
+
# reduce feeds or_ them (term_of yields Shapes, and_shape returns a Shape)
|
|
353
355
|
left.of == right.of ? left : ANY_VALUE
|
|
356
|
+
# :nocov:
|
|
354
357
|
when :list, :map
|
|
355
358
|
element = common_element(left, right)
|
|
356
359
|
element ? left.with(item: element[0], of: element[1], values: shared_values(left.values, right.values), pattern: shared_pattern(left.pattern, right.pattern), unique: unique_and(left.unique, right.unique)) : ANY_VALUE
|
|
@@ -143,7 +143,9 @@ module RubyCdk
|
|
|
143
143
|
cycle = cycle_from(node, [ node ], deps)
|
|
144
144
|
return cycle if cycle
|
|
145
145
|
end
|
|
146
|
+
# :nocov: internal invariant guard; caller peels all zero-out-degree nodes first so a cycle always exists
|
|
146
147
|
Kernel.raise AssertionError, "No cycle found. Assertion failure!"
|
|
148
|
+
# :nocov:
|
|
147
149
|
end
|
|
148
150
|
|
|
149
151
|
def cycle_from(node, path, deps)
|
|
@@ -153,7 +155,9 @@ module RubyCdk
|
|
|
153
155
|
cycle = cycle_from(dependency, path + [ dependency ], deps)
|
|
154
156
|
return cycle if cycle
|
|
155
157
|
end
|
|
158
|
+
# :nocov: every remaining node in deps has out-degree >= 1 so the first DFS branch always closes a cycle
|
|
156
159
|
nil
|
|
160
|
+
# :nocov:
|
|
157
161
|
end
|
|
158
162
|
end
|
|
159
163
|
end
|
|
@@ -112,8 +112,6 @@ module RubyCdk
|
|
|
112
112
|
if exponent > 20 || exponent < -6
|
|
113
113
|
fraction = digits.length > 1 ? ".#{digits[1..]}" : ""
|
|
114
114
|
"#{sign}#{digits[0]}#{fraction}e#{exponent.negative? ? "-" : "+"}#{exponent.abs}"
|
|
115
|
-
elsif exponent >= digits.length - 1
|
|
116
|
-
"#{sign}#{digits}#{"0" * (exponent - digits.length + 1)}"
|
|
117
115
|
elsif exponent >= 0
|
|
118
116
|
"#{sign}#{digits[0..exponent]}.#{digits[(exponent + 1)..]}"
|
|
119
117
|
else
|
|
@@ -123,7 +121,7 @@ module RubyCdk
|
|
|
123
121
|
|
|
124
122
|
# The shortest digits that still round-trip, and the power of ten of the first.
|
|
125
123
|
def shortest_digits(magnitude)
|
|
126
|
-
match = magnitude.to_s.match(/\A(\d)(?:\.(\d+))?(?:e([+-]\d+))?\z/)
|
|
124
|
+
match = magnitude.to_s.match(/\A(\d+)(?:\.(\d+))?(?:e([+-]\d+))?\z/)
|
|
127
125
|
return [ magnitude.to_s, 0 ] unless match
|
|
128
126
|
|
|
129
127
|
whole, fraction, power = match.captures
|
data/lib/ruby_cdk/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ruby-cdk
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- A-Safe Digital
|
|
@@ -37,6 +37,34 @@ dependencies:
|
|
|
37
37
|
- - "~>"
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
39
|
version: '1.0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: aws-sdk-ec2
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '1.0'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '1.0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: aws-sdk-ssm
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '1.0'
|
|
61
|
+
type: :runtime
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '1.0'
|
|
40
68
|
- !ruby/object:Gem::Dependency
|
|
41
69
|
name: thor
|
|
42
70
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -97,9 +125,12 @@ files:
|
|
|
97
125
|
- lib/ruby_cdk/cli/aws.rb
|
|
98
126
|
- lib/ruby_cdk/cli/bootstrap.rb
|
|
99
127
|
- lib/ruby_cdk/cli/diff.rb
|
|
128
|
+
- lib/ruby_cdk/cli/drift.rb
|
|
100
129
|
- lib/ruby_cdk/cli/failure.rb
|
|
130
|
+
- lib/ruby_cdk/cli/import.rb
|
|
101
131
|
- lib/ruby_cdk/cli/io.rb
|
|
102
132
|
- lib/ruby_cdk/cli/list.rb
|
|
133
|
+
- lib/ruby_cdk/cli/lookup.rb
|
|
103
134
|
- lib/ruby_cdk/cli/new.rb
|
|
104
135
|
- lib/ruby_cdk/cli/operation.rb
|
|
105
136
|
- lib/ruby_cdk/cli/pairs.rb
|
|
@@ -109,16 +140,19 @@ files:
|
|
|
109
140
|
- lib/ruby_cdk/cli/shell.rb
|
|
110
141
|
- lib/ruby_cdk/cli/synth.rb
|
|
111
142
|
- lib/ruby_cdk/cli/validate.rb
|
|
143
|
+
- lib/ruby_cdk/cli/watch.rb
|
|
112
144
|
- lib/ruby_cdk/constructs.rb
|
|
113
145
|
- lib/ruby_cdk/constructs/bucket.rb
|
|
114
146
|
- lib/ruby_cdk/constructs/events_rule.rb
|
|
115
147
|
- lib/ruby_cdk/constructs/function.rb
|
|
116
148
|
- lib/ruby_cdk/constructs/grant.rb
|
|
149
|
+
- lib/ruby_cdk/constructs/hosted_zone.rb
|
|
117
150
|
- lib/ruby_cdk/constructs/kms_alias.rb
|
|
118
151
|
- lib/ruby_cdk/constructs/kms_key.rb
|
|
119
152
|
- lib/ruby_cdk/constructs/log_group.rb
|
|
120
153
|
- lib/ruby_cdk/constructs/policy_statement.rb
|
|
121
154
|
- lib/ruby_cdk/constructs/queue.rb
|
|
155
|
+
- lib/ruby_cdk/constructs/record_set.rb
|
|
122
156
|
- lib/ruby_cdk/constructs/restapi.rb
|
|
123
157
|
- lib/ruby_cdk/constructs/role.rb
|
|
124
158
|
- lib/ruby_cdk/constructs/schedule.rb
|
|
@@ -126,9 +160,12 @@ files:
|
|
|
126
160
|
- lib/ruby_cdk/constructs/ssm_parameter.rb
|
|
127
161
|
- lib/ruby_cdk/constructs/table.rb
|
|
128
162
|
- lib/ruby_cdk/constructs/topic.rb
|
|
163
|
+
- lib/ruby_cdk/constructs/vpc.rb
|
|
164
|
+
- lib/ruby_cdk/constructs/vpc_network.rb
|
|
129
165
|
- lib/ruby_cdk/core.rb
|
|
130
166
|
- lib/ruby_cdk/core/app.rb
|
|
131
167
|
- lib/ruby_cdk/core/construct.rb
|
|
168
|
+
- lib/ruby_cdk/core/context.rb
|
|
132
169
|
- lib/ruby_cdk/core/elements.rb
|
|
133
170
|
- lib/ruby_cdk/core/ids.rb
|
|
134
171
|
- lib/ruby_cdk/core/stack.rb
|
|
@@ -136,6 +173,8 @@ files:
|
|
|
136
173
|
- lib/ruby_cdk/deploy.rb
|
|
137
174
|
- lib/ruby_cdk/deploy/capabilities.rb
|
|
138
175
|
- lib/ruby_cdk/deploy/deployer.rb
|
|
176
|
+
- lib/ruby_cdk/deploy/drift.rb
|
|
177
|
+
- lib/ruby_cdk/deploy/ec2_adapter.rb
|
|
139
178
|
- lib/ruby_cdk/deploy/errors.rb
|
|
140
179
|
- lib/ruby_cdk/deploy/fakes.rb
|
|
141
180
|
- lib/ruby_cdk/deploy/hotswap.rb
|
|
@@ -147,13 +186,16 @@ files:
|
|
|
147
186
|
- lib/ruby_cdk/deploy/poller.rb
|
|
148
187
|
- lib/ruby_cdk/deploy/ports.rb
|
|
149
188
|
- lib/ruby_cdk/deploy/sdk_adapters.rb
|
|
189
|
+
- lib/ruby_cdk/deploy/ssm_adapter.rb
|
|
150
190
|
- lib/ruby_cdk/deploy/states.rb
|
|
151
191
|
- lib/ruby_cdk/diff.rb
|
|
152
192
|
- lib/ruby_cdk/diff/compare.rb
|
|
153
193
|
- lib/ruby_cdk/diff/destructive.rb
|
|
194
|
+
- lib/ruby_cdk/diff/identity_center.rb
|
|
154
195
|
- lib/ruby_cdk/diff/normalizer.rb
|
|
155
196
|
- lib/ruby_cdk/diff/render.rb
|
|
156
197
|
- lib/ruby_cdk/diff/security.rb
|
|
198
|
+
- lib/ruby_cdk/diff/security_group.rb
|
|
157
199
|
- lib/ruby_cdk/diff/statement.rb
|
|
158
200
|
- lib/ruby_cdk/diff/template.rb
|
|
159
201
|
- lib/ruby_cdk/env.rb
|