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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +253 -0
  3. data/README.md +28 -5
  4. data/lib/ruby_cdk/assets/hasher.rb +4 -0
  5. data/lib/ruby_cdk/assets.rb +4 -8
  6. data/lib/ruby_cdk/cli/aws.rb +23 -5
  7. data/lib/ruby_cdk/cli/drift.rb +94 -0
  8. data/lib/ruby_cdk/cli/import.rb +306 -0
  9. data/lib/ruby_cdk/cli/lookup.rb +214 -0
  10. data/lib/ruby_cdk/cli/session.rb +114 -3
  11. data/lib/ruby_cdk/cli/watch.rb +297 -0
  12. data/lib/ruby_cdk/cli.rb +76 -1
  13. data/lib/ruby_cdk/constructs/function.rb +24 -3
  14. data/lib/ruby_cdk/constructs/hosted_zone.rb +342 -0
  15. data/lib/ruby_cdk/constructs/record_set.rb +330 -0
  16. data/lib/ruby_cdk/constructs/ssm_parameter.rb +174 -0
  17. data/lib/ruby_cdk/constructs/vpc.rb +822 -0
  18. data/lib/ruby_cdk/constructs/vpc_network.rb +159 -0
  19. data/lib/ruby_cdk/constructs.rb +5 -1
  20. data/lib/ruby_cdk/core/app.rb +28 -2
  21. data/lib/ruby_cdk/core/construct.rb +2 -0
  22. data/lib/ruby_cdk/core/context.rb +138 -0
  23. data/lib/ruby_cdk/core/stack.rb +4 -0
  24. data/lib/ruby_cdk/core.rb +1 -0
  25. data/lib/ruby_cdk/deploy/deployer.rb +283 -8
  26. data/lib/ruby_cdk/deploy/drift.rb +389 -0
  27. data/lib/ruby_cdk/deploy/ec2_adapter.rb +214 -0
  28. data/lib/ruby_cdk/deploy/errors.rb +8 -0
  29. data/lib/ruby_cdk/deploy/fakes.rb +230 -22
  30. data/lib/ruby_cdk/deploy/output/base.rb +8 -0
  31. data/lib/ruby_cdk/deploy/output/json.rb +7 -0
  32. data/lib/ruby_cdk/deploy/poller.rb +10 -0
  33. data/lib/ruby_cdk/deploy/ports.rb +124 -8
  34. data/lib/ruby_cdk/deploy/sdk_adapters.rb +141 -6
  35. data/lib/ruby_cdk/deploy/ssm_adapter.rb +111 -0
  36. data/lib/ruby_cdk/deploy.rb +1 -0
  37. data/lib/ruby_cdk/diff/compare.rb +1 -1
  38. data/lib/ruby_cdk/diff/identity_center.rb +64 -0
  39. data/lib/ruby_cdk/diff/render.rb +147 -8
  40. data/lib/ruby_cdk/diff/security.rb +195 -10
  41. data/lib/ruby_cdk/diff/security_group.rb +94 -0
  42. data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +2 -0
  43. data/lib/ruby_cdk/schema/sources/provider_type.rb +3 -0
  44. data/lib/ruby_cdk/test/private/cyclic.rb +4 -0
  45. data/lib/ruby_cdk/test/private/js.rb +1 -3
  46. data/lib/ruby_cdk/version.rb +1 -1
  47. metadata +43 -1
@@ -274,7 +274,9 @@ module RubyCdk
274
274
  id: stack.stack_id,
275
275
  status: stack.stack_status,
276
276
  outputs: (stack.outputs || []).to_h { |output| [ output.output_key, output.output_value ] },
277
- termination_protection: !!stack.enable_termination_protection
277
+ termination_protection: !!stack.enable_termination_protection,
278
+ notification_arns: stack.notification_arns || [],
279
+ rollback_configuration: rollback_configuration_of(stack.rollback_configuration)
278
280
  )
279
281
  end
280
282
 
@@ -305,28 +307,56 @@ module RubyCdk
305
307
 
306
308
  # Answers the change set's ID. A template above the inline limit goes to
307
309
  # the bucket, and the request carries its URL instead of its body: the
308
- # two never go together, as the port promises.
310
+ # two never go together, as the port promises. resources_to_import
311
+ # makes the change set an import: the request's ChangeSetType is
312
+ # IMPORT and it carries the list (deploy-stack.ts:636-637).
309
313
  def create_change_set(name, change_set_name, template:, change_set_type:, parameters:, tags:, capabilities:,
310
- on_stack_failure: nil, template_url: nil)
314
+ on_stack_failure: nil, template_url: nil, notification_arns: nil, rollback_configuration: nil,
315
+ import_existing_resources: false, resources_to_import: nil)
311
316
  params = {
312
317
  stack_name: name,
313
318
  change_set_name: change_set_name,
314
319
  change_set_type: change_set_type.to_s.upcase,
315
320
  parameters: parameter_pairs(parameters),
316
321
  tags: pairs(tags, :key, :value),
317
- capabilities: capabilities
322
+ capabilities: capabilities,
323
+ # The CDK names the field on every change set, false included
324
+ # (deploy-stack.ts:640).
325
+ import_existing_resources: import_existing_resources
318
326
  }
319
327
  params[:template_body] = template if template
320
328
  params[:template_url] = template_url if template_url
321
329
  params[:on_stack_failure] = on_stack_failure.to_s.upcase if on_stack_failure
330
+ params[:notification_arns] = notification_arns if notification_arns
331
+ params[:rollback_configuration] = requested_rollback_configuration(rollback_configuration) if rollback_configuration
332
+ params[:resources_to_import] = import_pairs(resources_to_import) if resources_to_import
322
333
  response = Sdk.request(rejects: true) { @raw.create_change_set(params) }
323
334
  ChangeSet.new(id: response.id || change_set_name)
324
335
  end
325
336
 
337
+ # GetTemplateSummary's ResourceIdentifierSummaries for the template:
338
+ # the property sets that identify an existing resource of each type
339
+ # it uses, the call the CDK's importer questions people with
340
+ # (deployments.ts:375-415, importer.ts:332-341). CloudFormation joins
341
+ # a compound set's names with commas ("HostedZoneId,Name"); the CDK
342
+ # splits them (importer.ts:337), and so does this.
343
+ def resource_identifier_summaries(template)
344
+ response = Sdk.request { @raw.get_template_summary(template_body: template) }
345
+ (response.resource_identifier_summaries || []).map do |summary|
346
+ ResourceIdentifierSummary.new(
347
+ type: summary.resource_type,
348
+ identifier_sets: (summary.resource_identifiers || []).map { |names| names.to_s.split(",") }
349
+ )
350
+ end
351
+ end
352
+
326
353
  # The direct update --hotswap takes. TemplateBody and TemplateURL
327
354
  # never go together, as the port promises; the parameters go as
328
- # values, the way the change set carries them.
329
- def update_stack(name, template:, parameters:, tags:, capabilities:, template_url: nil)
355
+ # values, the way the change set carries them. NotificationARNs and
356
+ # RollbackConfiguration go on both, as the CDK's commonPrepareOptions
357
+ # puts them on its UpdateStack as well.
358
+ def update_stack(name, template:, parameters:, tags:, capabilities:, template_url: nil, notification_arns: nil,
359
+ rollback_configuration: nil)
330
360
  params = {
331
361
  stack_name: name,
332
362
  parameters: parameter_pairs(parameters),
@@ -335,6 +365,8 @@ module RubyCdk
335
365
  }
336
366
  params[:template_body] = template if template
337
367
  params[:template_url] = template_url if template_url
368
+ params[:notification_arns] = notification_arns if notification_arns
369
+ params[:rollback_configuration] = requested_rollback_configuration(rollback_configuration) if rollback_configuration
338
370
  Sdk.request { @raw.update_stack(params) }
339
371
  nil
340
372
  end
@@ -466,6 +498,58 @@ module RubyCdk
466
498
  end
467
499
  end
468
500
 
501
+ # Starts the drift detection of a stack and answers its ID
502
+ # (DetectStackDrift, as the CDK's detectStackDrift calls it,
503
+ # api/drift/drift.ts:22).
504
+ def detect_stack_drift(name_or_id)
505
+ Sdk.request { @raw.detect_stack_drift(stack_name: name_or_id) }.stack_drift_detection_id
506
+ end
507
+
508
+ # Where a drift detection has got to (DescribeStackDriftDetectionStatus,
509
+ # api/drift/drift.ts:88-90).
510
+ def describe_stack_drift_detection_status(detection_id)
511
+ response = Sdk.request { @raw.describe_stack_drift_detection_status(stack_drift_detection_id: detection_id) }
512
+ DriftDetectionStatus.new(
513
+ detection_status: response.detection_status,
514
+ stack_drift_status: response.stack_drift_status,
515
+ status_reason: response.detection_status_reason
516
+ )
517
+ end
518
+
519
+ # Every page of the stack's resource drifts, so that the list is never
520
+ # truncated (DescribeStackResourceDrifts, api/drift/drift.ts:42-54): a
521
+ # page cut short would read as resources unchecked, and `--fail` would
522
+ # pass on a drifted resource. nil when CloudFormation holds no list.
523
+ def describe_stack_resource_drifts(name_or_id)
524
+ response = Sdk.request { @raw.describe_stack_resource_drifts(stack_name: name_or_id) }
525
+ return nil if response.stack_resource_drifts.nil? && response.next_token.nil?
526
+
527
+ drifts = []
528
+ loop do
529
+ drifts.concat(response.stack_resource_drifts || [])
530
+ break unless response.next_token
531
+
532
+ response = Sdk.request { @raw.describe_stack_resource_drifts(stack_name: name_or_id, next_token: response.next_token) }
533
+ end
534
+ drifts.map do |drift|
535
+ ResourceDrift.new(
536
+ logical_id: drift.logical_resource_id,
537
+ physical_id: drift.physical_resource_id,
538
+ resource_type: drift.resource_type,
539
+ status: drift.stack_resource_drift_status,
540
+ status_reason: drift.drift_status_reason,
541
+ property_differences: (drift.property_differences || []).map do |difference|
542
+ PropertyDifference.new(
543
+ property_path: difference.property_path,
544
+ expected_value: difference.expected_value,
545
+ actual_value: difference.actual_value,
546
+ difference_type: difference.difference_type
547
+ )
548
+ end
549
+ )
550
+ end
551
+ end
552
+
469
553
  private
470
554
 
471
555
  # The options of a client for this region and profile. STS gets the same
@@ -506,6 +590,42 @@ module RubyCdk
506
590
  end
507
591
  end
508
592
 
593
+ # One entry of the request's ResourcesToImport, from the port's
594
+ # ResourceToImport: the logical id, the type, and the identifier
595
+ # properties that name the existing physical resource.
596
+ def import_pairs(list)
597
+ list.map do |import|
598
+ {
599
+ logical_resource_id: import.logical_resource_id,
600
+ resource_type: import.resource_type,
601
+ resource_identifier: import.resource_identifier || {}
602
+ }
603
+ end
604
+ end
605
+
606
+ # The desired rollback configuration of a deploy as the request
607
+ # carries it (RollbackConfiguration): every trigger a metric alarm,
608
+ # the type the CDK's conversion always names (util/cloudformation.ts,
609
+ # toCloudFormationRollbackConfiguration: `Type: trigger.type ??
610
+ # RollbackTriggerType.ALARM`).
611
+ def requested_rollback_configuration(config)
612
+ {
613
+ rollback_triggers: (config.triggers || []).map { |arn| { arn: arn, type: RollbackTrigger::ALARM } },
614
+ monitoring_time_in_minutes: config.monitoring_time_in_minutes
615
+ }
616
+ end
617
+
618
+ # The stack's rollback configuration as the port describes it. The SDK
619
+ # answers its own structure; nil is a stack that carries none.
620
+ def rollback_configuration_of(deployed)
621
+ return nil if deployed.nil?
622
+
623
+ RollbackConfiguration.new(
624
+ triggers: (deployed.rollback_triggers || []).map { |trigger| RollbackTrigger.new(arn: trigger.arn, type: trigger.type) },
625
+ monitoring_time_in_minutes: deployed.monitoring_time_in_minutes
626
+ )
627
+ end
628
+
509
629
  # A structure of the SDK as a plain Hash, all the way down.
510
630
  def plain(value)
511
631
  case value
@@ -602,6 +722,21 @@ module RubyCdk
602
722
  raise
603
723
  end
604
724
 
725
+ # The REST URL of the object: the origin of the endpoint this client
726
+ # was built with, then the bucket and the key on the path. That is the
727
+ # shape CloudFormation takes a TemplateURL in, and the shape the CDK
728
+ # sends (template-body-parameter.ts, restUrlFromManifest at
729
+ # f7c9dca95f440a076c2abd61b66b984624b8548e: the endpoint origin the
730
+ # SDK resolved, then bucket and key). The endpoint is the client's
731
+ # own: the region's, or the override that built the client
732
+ # (AWS_ENDPOINT_URL_S3, AWS_ENDPOINT_URL) -- the emulator scenario
733
+ # names itself, and CloudFormation there reads from the same place.
734
+ # No form on the key is escaped: a template key is
735
+ # templates/<stack name>/<sha256>.json, an asset key is <sha256>.zip.
736
+ def object_url(bucket, key)
737
+ "#{@raw.config.endpoint.to_s.chomp("/")}/#{bucket}/#{key}"
738
+ end
739
+
605
740
  private
606
741
 
607
742
  CONTENT_TYPES = { ".zip" => "application/zip", ".json" => "application/json" }.freeze
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "aws-sdk-ssm"
4
+ require_relative "../errors"
5
+ require_relative "sdk_adapters"
6
+
7
+ module RubyCdk
8
+ module Deploy
9
+ # The SSM client behind the parameter lookups of D-022, beside the EC2
10
+ # one in the adapters component. Only the lookup command loads this file
11
+ # (through the command line's client seam), so `synth` never loads the
12
+ # AWS SDK and a deploy never loads the SSM gem.
13
+ module Sdk
14
+ # The lookup client of one region, built like every client: the region
15
+ # is passed explicitly, the profile selects credentials.
16
+ def self.ssm_client(region:, profile: nil, credentials: nil, stub_responses: false)
17
+ SsmClient.new(region: region, profile: profile, credentials: credentials, stub_responses: stub_responses)
18
+ end
19
+
20
+ # The parameter lookups. What a query reads is shaped here, so that the
21
+ # construct that reads the cache and this client agree on one answer
22
+ # (the shape is written out in SSM::Parameter.from_lookup, and the file
23
+ # is the contract):
24
+ #
25
+ # "name" the parameter's name, as AWS answers it
26
+ # "value" its value, as AWS answered it when the lookup ran
27
+ # "type" its type, as AWS reads it: String or StringList
28
+ # "owner_account_id" the account the credentials are for, which the
29
+ # lookup command has already checked against the
30
+ # query's environment
31
+ # "region" where the parameter lives
32
+ #
33
+ # The AWS call is the one the CDK's provider makes (aws-cdk-cli,
34
+ # toolkit-lib ssm-parameters.ts): one get_parameter with the Name and
35
+ # nothing else, no WithDecryption.
36
+ class SsmClient
37
+ attr_reader :raw
38
+
39
+ def initialize(region:, profile: nil, credentials: nil, stub_responses: false)
40
+ @profile = profile
41
+ @credentials = credentials
42
+ @stub_responses = stub_responses
43
+ @raw = Aws::SSM::Client.new(**Sdk.options_for(region: region, profile: profile,
44
+ credentials: credentials, stub_responses: stub_responses))
45
+ end
46
+
47
+ # The account the credentials are for, asked of STS with the same
48
+ # region and profile as the EC2 client asks it. The lookup command
49
+ # refuses a query whose env names another.
50
+ def account
51
+ Sdk.request { sts.get_caller_identity }.account
52
+ end
53
+
54
+ # The STS client, built on first use with the options the main
55
+ # client was built with, so that a test stubs it the same way.
56
+ def sts
57
+ @sts ||= Aws::STS::Client.new(**Sdk.options_for(region: @raw.config.region, profile: @profile,
58
+ credentials: @credentials, stub_responses: @stub_responses))
59
+ end
60
+
61
+ # The answer for one parameter query: the parameter of exactly that
62
+ # name. A name is the whole query, as the CDK's is, so there is no
63
+ # filter to narrow a refusal with; what is checkable is the name and
64
+ # the environment the query ran against.
65
+ def lookup_parameter(name:)
66
+ parameter = read_parameter(name)
67
+ unless parameter
68
+ raise PreconditionError, "The parameter #{name} does not exist in account #{account}, region " \
69
+ "#{region} (ssm get_parameter answers ParameterNotFound). Nothing was cached. The name is " \
70
+ "the whole query, so there is no filter to narrow it with: check the name, and the profile and " \
71
+ "region the query runs against (--profile NAME, --region NAME), then run `ruby-cdk lookup` again."
72
+ end
73
+ if parameter.type == "SecureString"
74
+ raise PreconditionError, "The parameter #{name} is a SecureString. This lookup reads it without " \
75
+ "decryption, as the CDK's provider reads it, and what AWS answers then is a KMS ciphertext that " \
76
+ "differs on every call: written into the committed cache it would churn, and into a template it " \
77
+ "would deploy the ciphertext, not the value. Nothing was cached. Read the parameter at deploy " \
78
+ "time with the dynamic reference {{resolve:ssm-secure:#{name}}} instead."
79
+ end
80
+
81
+ {
82
+ "name" => parameter.name,
83
+ "value" => parameter.value,
84
+ "type" => parameter.type,
85
+ "owner_account_id" => account,
86
+ "region" => region
87
+ }
88
+ end
89
+
90
+ private
91
+
92
+ def region
93
+ @raw.config.region
94
+ end
95
+
96
+ # One get_parameter. SSM answers a parameter that is not there with
97
+ # the ParameterNotFound error; the CDK's provider catches that name
98
+ # and treats the call as answered and empty (ssm-parameters.ts), so
99
+ # the refusal above can speak of the query and not of a failed
100
+ # request.
101
+ def read_parameter(name)
102
+ Sdk.request { @raw.get_parameter(name: name) }.parameter
103
+ rescue Sdk::RequestFailed => e
104
+ raise unless e.code == "ParameterNotFound"
105
+
106
+ nil
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -8,6 +8,7 @@ require_relative "deploy/capabilities"
8
8
  require_relative "deploy/parameters"
9
9
  require_relative "deploy/states"
10
10
  require_relative "deploy/hotswap"
11
+ require_relative "deploy/drift"
11
12
  require_relative "deploy/deployer"
12
13
  require_relative "deploy/output/human"
13
14
  require_relative "deploy/output/quiet"
@@ -121,7 +121,7 @@ module RubyCdk
121
121
  # with numbers, booleans with booleans, lists with lists, objects with
122
122
  # objects.
123
123
  def same_kind?(lvalue, rvalue)
124
- case [ lvalue.class, rvalue.class ]
124
+ case [ lvalue, rvalue ]
125
125
  in [ String, String ] then true
126
126
  in [ TrueClass | FalseClass, TrueClass | FalseClass ] then true
127
127
  in [ Integer | Float, Integer | Float ] then true
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyCdk
4
+ module Diff
5
+ # The three IAM Identity Center resources the security view reads, the
6
+ # CDK's SsoPermissionSet, SsoAssignment and SsoInstanceACAConfig
7
+ # (cloudformation-diff, lib/iam/iam-identity-center.ts at
8
+ # f7c9dca95f440a076c2abd61b66b984624b8548e): what the resource says,
9
+ # rendered, under the names its summary tables print. An entry added is
10
+ # what the CDK's hasAdditions answers on and what its
11
+ # permissionsBroadened counts (iam-changes.ts:79-81); two entries are the
12
+ # same when everything they say is the same, the CDK's deepEqual.
13
+ SsoPermissionSet = Data.define(:logical_id, :name, :instance_arn, :permissions_boundary,
14
+ :customer_managed_policy_references) do
15
+ def equal?(other)
16
+ other.is_a?(SsoPermissionSet) && self == other
17
+ end
18
+
19
+ def to_h
20
+ {
21
+ "logical_id" => logical_id,
22
+ "name" => name,
23
+ "instance_arn" => instance_arn,
24
+ "permissions_boundary" => permissions_boundary,
25
+ "customer_managed_policy_references" => customer_managed_policy_references
26
+ }
27
+ end
28
+ end
29
+
30
+ SsoAssignment = Data.define(:logical_id, :instance_arn, :permission_set_arn, :principal_id,
31
+ :principal_type, :target_id, :target_type) do
32
+ def equal?(other)
33
+ other.is_a?(SsoAssignment) && self == other
34
+ end
35
+
36
+ def to_h
37
+ {
38
+ "logical_id" => logical_id,
39
+ "instance_arn" => instance_arn,
40
+ "permission_set_arn" => permission_set_arn,
41
+ "principal_id" => principal_id,
42
+ "principal_type" => principal_type,
43
+ "target_id" => target_id,
44
+ "target_type" => target_type
45
+ }
46
+ end
47
+ end
48
+
49
+ # AWS::SSO::InstanceAccessControlAttributeConfiguration
50
+ SsoInstanceAcaConfig = Data.define(:logical_id, :instance_arn, :access_control_attributes) do
51
+ def equal?(other)
52
+ other.is_a?(SsoInstanceAcaConfig) && self == other
53
+ end
54
+
55
+ def to_h
56
+ {
57
+ "logical_id" => logical_id,
58
+ "instance_arn" => instance_arn,
59
+ "access_control_attributes" => access_control_attributes
60
+ }
61
+ end
62
+ end
63
+ end
64
+ end
@@ -7,12 +7,14 @@ module RubyCdk
7
7
  # The diff, as text for a person. What is printed is the CDK's
8
8
  # formatDifferences (cloudformation-diff, lib/format.ts at eca1b73d):
9
9
  # the stack's name, then the sections in its order -- the template's own
10
- # attributes, the security tables, Parameters, Metadata, Mappings,
11
- # Conditions, Resources with the property changes under each resource, and
12
- # Outputs, with anything else under Other Changes. A resource carries one
13
- # of the markers [+], [-] and [~], the words "replace", "may be replaced",
14
- # "destroy" and "orphan" for what happens to it, and a line per changed
15
- # property path with the values on both sides.
10
+ # attributes, the security tables (the statements, the managed policies,
11
+ # the IAM Identity Center entries, the security group rules), Parameters,
12
+ # Metadata, Mappings, Conditions, Resources with the property changes
13
+ # under each resource, and Outputs, with anything else under Other
14
+ # Changes. A resource carries one of the markers [+], [-] and [~], the
15
+ # words "replace", "may be replaced", "destroy" and "orphan" for what
16
+ # happens to it, and a line per changed property path with the values on
17
+ # both sides.
16
18
  #
17
19
  # Two departures from the CDK, both recorded (architecture section 21):
18
20
  # its tables are drawn with unicode box characters and colour; this
@@ -99,6 +101,8 @@ module RubyCdk
99
101
  lines.concat(table([ "", "Resource", "Managed Policy ARN" ], rows))
100
102
  lines << ""
101
103
  end
104
+ lines.concat(identity_center_tables(security))
105
+ lines.concat(security_group_table(security))
102
106
  if security.capabilities_added.any?
103
107
  reason = if security.capabilities_added.include?("CAPABILITY_NAMED_IAM")
104
108
  "the new template creates IAM resources, some with explicit names"
@@ -110,6 +114,131 @@ module RubyCdk
110
114
  lines
111
115
  end
112
116
 
117
+ # The IAM Identity Center tables under the CDK's one header
118
+ # (format.ts:430-441): the permission sets, then the access-control
119
+ # attribute configurations, then the assignments, each the summary its
120
+ # summarize method lays out.
121
+ def identity_center_tables(security)
122
+ lines = []
123
+ if security.sso_permission_set_additions.any? || security.sso_permission_set_removals.any?
124
+ rows = security.sso_permission_set_additions.map { |entry| permission_set_row("+", entry) } +
125
+ security.sso_permission_set_removals.map { |entry| permission_set_row("-", entry) }
126
+ rows.sort_by! { |cells| cells[1].to_s }
127
+ lines << "IAM Identity Center Changes"
128
+ lines.concat(table(PERMISSION_SET_HEADER, rows))
129
+ lines << ""
130
+ end
131
+ if security.sso_instance_config_additions.any? || security.sso_instance_config_removals.any?
132
+ rows = security.sso_instance_config_additions.map { |entry| instance_config_row("+", entry) } +
133
+ security.sso_instance_config_removals.map { |entry| instance_config_row("-", entry) }
134
+ rows.sort_by! { |cells| cells[1].to_s }
135
+ lines << "IAM Identity Center Changes" unless lines.any?
136
+ lines.concat(table(INSTANCE_CONFIG_HEADER, rows))
137
+ lines << ""
138
+ end
139
+ if security.sso_assignment_additions.any? || security.sso_assignment_removals.any?
140
+ rows = security.sso_assignment_additions.map { |entry| assignment_row("+", entry) } +
141
+ security.sso_assignment_removals.map { |entry| assignment_row("-", entry) }
142
+ rows.sort_by! { |cells| cells[1].to_s }
143
+ lines << "IAM Identity Center Changes" unless lines.any?
144
+ lines.concat(table(ASSIGNMENT_HEADER, rows))
145
+ lines << ""
146
+ end
147
+ lines
148
+ end
149
+
150
+ # The security group table, the CDK's summarize under its "Security Group
151
+ # Changes" header (format.ts:444-451, security-group-changes.ts:55-84):
152
+ # additions first, sorted by group, ingress before egress.
153
+ def security_group_table(security)
154
+ return [] unless security.rule_additions.any? || security.rule_removals.any?
155
+
156
+ rows = security.rule_additions.map { |rule| rule_row("+", rule) } +
157
+ security.rule_removals.map { |rule| rule_row("-", rule) }
158
+ rows.sort_by! { |cells| [ cells[1].to_s, cells[2] == "In" ? 0 : 1 ] }
159
+ [ "Security Group Changes", *table(GROUP_HEADER, rows), "" ]
160
+ end
161
+
162
+ # One row of the permission set table, the CDK's summarizeSsoPermissionSets:
163
+ # the marker, then what the set says, its boundary and its customer
164
+ # managed policy references the way it renders them.
165
+ def permission_set_row(marker, entry)
166
+ [
167
+ marker,
168
+ entry.logical_id,
169
+ entry.instance_arn,
170
+ entry.name,
171
+ permissions_boundary(entry.permissions_boundary),
172
+ customer_managed_policy_references(entry.customer_managed_policy_references)
173
+ ]
174
+ end
175
+
176
+ # The PermissionsBoundary cell (formatSsoPermissionsBoundary): the ARN,
177
+ # or the customer managed policy reference in braces. The ARN is taken
178
+ # where AWS lets both be given.
179
+ def permissions_boundary(boundary)
180
+ return "" unless boundary.is_a?(Hash)
181
+
182
+ arn = boundary["ManagedPolicyArn"]
183
+ return "ManagedPolicyArn: #{arn}" unless arn.nil?
184
+
185
+ reference = boundary["CustomerManagedPolicyReference"]
186
+ return "" if reference.nil?
187
+
188
+ "CustomerManagedPolicyReference: {\n #{managed_policy_reference(reference)}\n}"
189
+ end
190
+
191
+ # One "Name: ..., Path: ..." pair (formatManagedPolicyRef), empty where
192
+ # nothing is named.
193
+ def managed_policy_reference(reference)
194
+ reference = {} unless reference.is_a?(Hash)
195
+ "Name: #{reference["Name"] || ""}, Path: #{reference["Path"] || ""}"
196
+ end
197
+
198
+ def customer_managed_policy_references(references)
199
+ return "" unless references.is_a?(Array)
200
+
201
+ references.map { |reference| managed_policy_reference(reference) }.join("\n")
202
+ end
203
+
204
+ # One row of the access-control attribute configuration table, the CDK's
205
+ # summarizeSsoInstanceACAConfigs.
206
+ def instance_config_row(marker, entry)
207
+ [ marker, entry.logical_id, entry.instance_arn, access_control_attributes(entry.access_control_attributes) ]
208
+ end
209
+
210
+ # One "Key: ..., Values: [...]" line per attribute.
211
+ def access_control_attributes(attributes)
212
+ return "" unless attributes.is_a?(Array)
213
+
214
+ attributes.map do |attribute|
215
+ attribute = {} unless attribute.is_a?(Hash)
216
+ raw = attribute["Value"].is_a?(Hash) ? attribute["Value"]["Source"] : nil
217
+ sources = raw.is_a?(Array) ? raw : [ raw ].compact
218
+ "Key: #{attribute["Key"]}, Values: [#{sources.join(", ")}]"
219
+ end.join("\n")
220
+ end
221
+
222
+ # One row of the assignment table, the CDK's summarizeSsoAssignments.
223
+ def assignment_row(marker, entry)
224
+ [
225
+ marker,
226
+ entry.logical_id,
227
+ entry.instance_arn,
228
+ entry.permission_set_arn,
229
+ entry.principal_id,
230
+ entry.principal_type,
231
+ entry.target_id,
232
+ entry.target_type
233
+ ]
234
+ end
235
+
236
+ # One row of the security group table: the marker, the group, the
237
+ # direction, the protocol and the peer.
238
+ def rule_row(marker, rule)
239
+ [ marker, rule.group_id, rule.direction == :ingress ? "In" : "Out", rule.describe_protocol, rule.describe_peer ]
240
+ end
241
+
113
242
  # One row of the statement table, the CDK's summarizeStatements: the
114
243
  # marker, then what the statement says, rendered for a person.
115
244
  def row(marker, statement)
@@ -235,8 +364,18 @@ module RubyCdk
235
364
  end
236
365
  end
237
366
 
238
- NO_CHANGES = "There were no differences"
239
- NO_SECURITY_CHANGES = "There were no security-related changes"
367
+ NO_CHANGES = "There were no differences"
368
+ NO_SECURITY_CHANGES = "There were no security-related changes"
369
+
370
+ # The columns of the security view's later tables, the CDK's summary
371
+ # headers (iam-changes.ts:90, 126, 153, 189, 221;
372
+ # security-group-changes.ts:58).
373
+ PERMISSION_SET_HEADER = [ "", "Resource", "InstanceArn", "PermissionSet name", "PermissionsBoundary",
374
+ "CustomerManagedPolicyReferences" ].freeze
375
+ INSTANCE_CONFIG_HEADER = [ "", "Resource", "InstanceArn", "AccessControlAttributes" ].freeze
376
+ ASSIGNMENT_HEADER = [ "", "Resource", "InstanceArn", "PermissionSetArn", "PrincipalId", "PrincipalType",
377
+ "TargetId", "TargetType" ].freeze
378
+ GROUP_HEADER = [ "", "Group", "Dir", "Protocol", "Peer" ].freeze
240
379
  end
241
380
  end
242
381
  end