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
@@ -3,6 +3,7 @@
3
3
  require_relative "../errors"
4
4
  require_relative "../fn"
5
5
  require_relative "../lazy"
6
+ require_relative "../core/context"
6
7
  require_relative "grant"
7
8
 
8
9
  module RubyCdk
@@ -52,10 +53,44 @@ module RubyCdk
52
53
  # encryptionKey.grantDecrypt/grantEncrypt beside them when the parameter
53
54
  # carries a KMS key prop; there is no key prop here, so no KMS actions
54
55
  # travel with the grant, as in the CDK without one.
56
+ #
57
+ # Pwd = RubyCdk::SSM::Parameter.from_lookup(stack, "Pwd", name: "/app/db-password")
58
+ #
59
+ # from_lookup reads an existing parameter at synthesis, as the CDK's
60
+ # StringParameter.valueFromLookup does (parameter.ts:637-661): its
61
+ # provider is aws-cdk-cli's SSM one (toolkit-lib ssm-parameters.ts), which
62
+ # makes one get_parameter call with the Name and nothing else, no
63
+ # WithDecryption. As decision D-022 shapes it, the answer is read from
64
+ # the context cache cdk.context.json beside the app file, which
65
+ # `ruby-cdk lookup` writes and synth only reads: on a hit the construct
66
+ # answers literals - the value, the type as AWS reads it, the parameter's
67
+ # name - and synthesizes no resource, because the parameter exists; a
68
+ # consumer writes the value literally, into an environment or a property,
69
+ # the way a consumer of a looked-up VPC writes its ids. On a miss
70
+ # from_lookup raises the error that names `ruby-cdk lookup`; this path
71
+ # never calls AWS. The query needs the stack's concrete env, as the CDK
72
+ # needs a concrete account and region for a context lookup
73
+ # (context-provider.ts:167-172): the cache key carries them, and an
74
+ # answer is never read for the wrong environment. The CDK's
75
+ # defaultValue is not carried - its dummy would put
76
+ # "dummy-value-for-<name>" into a template when the parameter is absent,
77
+ # and here a parameter that is not there stops the synthesis with the
78
+ # command - and neither is its additionalCacheKey: the cache key carries
79
+ # the whole query already (Core::Context). A SecureString is refused by
80
+ # the lookup command: read without decryption, as the CDK's provider
81
+ # reads it, the answer is a per-call KMS ciphertext, which the committed
82
+ # cache must not carry; the dynamic reference reads the value at deploy
83
+ # time instead.
55
84
  class Parameter < Construct
56
85
  # The CDK's two createable types (parameter.ts:759, 889).
57
86
  TYPES = { string: "String", string_list: "StringList" }.freeze
58
87
 
88
+ # The provider name in the context cache (the CDK's
89
+ # SSM_PARAMETER_PROVIDER, cxschema.ContextProvider.SSM_PARAMETER_PROVIDER).
90
+ # The key of one answer is "ssm:account=...:region=...:name=..."
91
+ # (Core::Context.key).
92
+ PROVIDER = "ssm"
93
+
59
94
  # The CDK's tiers (parameter.ts:343-356).
60
95
  TIERS = { standard: "Standard", advanced: "Advanced", intelligent_tiering: "Intelligent-Tiering" }.freeze
61
96
 
@@ -72,6 +107,17 @@ module RubyCdk
72
107
 
73
108
  attr_reader :type
74
109
 
110
+ # Reads an existing parameter whose answer the context cache beside the
111
+ # app file already holds (the header above; the CDK's closest origin is
112
+ # StringParameter.valueFromLookup, parameter.ts:637-661). The name is
113
+ # the whole query and must be concrete, as every argument of the CDK's
114
+ # lookups must ("All arguments to Vpc.fromLookup() must be concrete
115
+ # (no Tokens)", vpc.ts:1414-1416). A miss raises the error that names
116
+ # `ruby-cdk lookup`; this path never calls AWS.
117
+ def self.from_lookup(scope, id, name:)
118
+ LookedUp.read(scope, id, name: name)
119
+ end
120
+
75
121
  def initialize(scope, id, value:, type: :string, name: nil, description: nil, allowed_pattern: nil,
76
122
  tier: nil, data_type: nil)
77
123
  super(scope, id)
@@ -234,5 +280,133 @@ module RubyCdk
234
280
  @values.first
235
281
  end
236
282
  end
283
+
284
+ # The looked-up parameter, reached through Parameter.from_lookup: a tree
285
+ # node that synthesizes no resources. The parameter exists; what the
286
+ # construct answers are the literals the cache holds - the value, the
287
+ # type as AWS reads it, the name - so a consumer writes them literally,
288
+ # with no Fn::GetAtt, no dynamic reference and no Fn::ImportValue
289
+ # anywhere. The resource side of the created Parameter - the logical ID
290
+ # and the grants, which the CDK puts on the StringParameter class and not
291
+ # on the IStringParameter interface its imports answer (parameter.ts:203) -
292
+ # is refused here: nothing of this stack carries it.
293
+ class Parameter
294
+ class LookedUp < Construct
295
+ # The nearest Stack carries the env the query is cached under.
296
+ def self.read(scope, id, name:)
297
+ stack = lookup_stack(scope, id)
298
+ unless stack.env
299
+ raise UsageError, "#{stack.path}: Cannot retrieve value from context provider " \
300
+ "#{PROVIDER} since account/region are not specified at the stack level. " \
301
+ "Configure \"env\" with an account and region when you define your stack."
302
+ end
303
+ query = Context::Query.new(provider: PROVIDER, account: stack.env.account, region: stack.env.region,
304
+ args: lookup_args(id, scope, name))
305
+ cache = stack.app.context_file
306
+ unless cache
307
+ raise UsageError, "#{stack.path}: the App has no context cache to read: an App built from an app " \
308
+ "file reads #{Context::FILE_NAME} from the file's directory. Run the command from the app's " \
309
+ "directory, or build the App with App.build!."
310
+ end
311
+
312
+ where = "the Parameter #{id.inspect} of the stack #{stack.stack_name.inspect}"
313
+ answer = validate_answer(Context.answer(cache, query, where: where), cache, query)
314
+ new(scope, id, answer)
315
+ end
316
+
317
+ # Every attribute answers the literal of the answer.
318
+ def initialize(scope, id, answer)
319
+ super(scope, id)
320
+ @answer = answer
321
+ end
322
+
323
+ # The parameter's name, as the cache holds it: the one the query asked for.
324
+ def parameter_name
325
+ @answer.fetch("name")
326
+ end
327
+
328
+ # The value, as AWS answered it when the lookup ran.
329
+ def value
330
+ @answer.fetch("value")
331
+ end
332
+
333
+ # The type, as AWS reads the parameter: "String" or "StringList", the
334
+ # same strings the created Parameter writes into the Type property.
335
+ def type
336
+ @answer.fetch("type")
337
+ end
338
+
339
+ # The parameter's ARN, built from the name as the created Parameter
340
+ # builds it (util.ts, arnForParameterName, one Fn::Sub): the name is
341
+ # literal, and the environment stays CloudFormation's pseudo
342
+ # parameters, so the string resolves where the stack deploys.
343
+ def arn
344
+ Fn.sub("arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${ParameterName}",
345
+ "ParameterName" => parameter_name.delete_prefix("/"))
346
+ end
347
+
348
+ # No resource to name: the parameter exists, and where a Ref of a
349
+ # created parameter was meant, parameter_name is what a consumer
350
+ # writes.
351
+ def logical_id
352
+ raise_lookup_has_no_resource("logical_id")
353
+ end
354
+
355
+ # The grants are methods of the created Parameter; a looked-up
356
+ # parameter answers literals only. The four read actions on its ARN
357
+ # can be added to a principal's policy with the IAM construct
358
+ # directly.
359
+ def grant_read(_principal)
360
+ raise_lookup_has_no_resource("grant_read")
361
+ end
362
+
363
+ def grant_write(_principal)
364
+ raise_lookup_has_no_resource("grant_write")
365
+ end
366
+
367
+ private
368
+
369
+ # The answer this construct reads, as the lookup provider wrote it:
370
+ # "name", "value", "type", "owner_account_id" and "region", the last
371
+ # two checked against the key by Core::Context.answer. An entry of
372
+ # another shape is refused, not partly read: a cache this tool did
373
+ # not write is a hand edit. The value is read as a String and nothing
374
+ # more: SSM writes no empty value, and an answer with an absent one
375
+ # is refused with the rest.
376
+ def self.validate_answer(answer, cache, query)
377
+ return answer if answer["name"].is_a?(String) && !answer["name"].empty? &&
378
+ answer["value"].is_a?(String) && answer["type"].is_a?(String) && !answer["type"].empty?
379
+
380
+ raise PreconditionError, "The cache #{cache} answers #{query.key} with an entry this tool did not write: " \
381
+ "an SSM answer carries name, value, type, owner_account_id and region. " \
382
+ "Remove the entry and run `ruby-cdk lookup` again."
383
+ end
384
+
385
+ def self.lookup_stack(scope, id)
386
+ stack = scope.is_a?(Construct) ? scope.stack : nil
387
+ return stack if stack
388
+
389
+ raise UsageError, "Parameter.from_lookup is made in a Stack: Parameter.from_lookup(stack, " \
390
+ "#{id.inspect}), got #{scope.inspect} as its scope"
391
+ end
392
+
393
+ # The query's one argument as a flat pair for the cache key
394
+ # (Core::Context): the name, concrete.
395
+ def self.lookup_args(id, scope, name)
396
+ where = "#{scope.path}: the Parameter #{id.inspect}"
397
+ unless name.is_a?(String) && !name.empty?
398
+ raise UsageError, "#{where}: all arguments to from_lookup must be concrete: name takes the " \
399
+ "parameter's name String, got #{name.inspect}"
400
+ end
401
+
402
+ { "name" => name }.freeze
403
+ end
404
+
405
+ def raise_lookup_has_no_resource(what)
406
+ raise UsageError, "#{path}: a looked-up parameter synthesizes no resources, so it answers no #{what}; " \
407
+ "the parameter exists, and #{path}.parameter_name and #{path}.arn are the literals a consumer writes."
408
+ end
409
+ end
410
+ end
237
411
  end
238
412
  end