Module: Himari::Aws::LambdaHandler

Defined in:
lib/himari/aws/lambda_handler.rb

Class Method Summary collapse

Class Method Details

.appObject



13
14
15
# File 'lib/himari/aws/lambda_handler.rb', line 13

def self.app
  @app ||= make_app()
end

.config_ruObject



17
18
19
20
21
22
23
# File 'lib/himari/aws/lambda_handler.rb', line 17

def self.config_ru
  a = Time.now
  retval = config_ru_from_task_root || config_ru_from_dynamodb
  b = Time.now
  $stdout.puts(JSON.generate(config_ru: {ts: b, elapsed_time: b-a}))
  retval
end

.config_ru_from_dynamodbObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/himari/aws/lambda_handler.rb', line 32

def self.config_ru_from_dynamodb
  dgst = ENV.fetch('HIMARI_RACK_DIGEST')
  table_name = ENV.fetch('HIMARI_RACK_DYNAMODB_TABLE')
  pk, sk = "rack", "rack:#{dgst}"

  ddb = ::Aws::DynamoDB::Client.new()
  item = ddb.query(
    table_name: table_name,
    select: 'ALL_ATTRIBUTES',
    limit: 1,
    key_condition_expression: 'pk = :pk AND sk = :sk',
    expression_attribute_values: {":pk" => pk, ":sk" => sk},
  ).items.first

  unless item
    raise "item not found (pk=#{pk.inspect}, sk=#{sk.inspect}) on dynamodb table #{table_name} for config.ru"
  end

  content = item.fetch('file')
  content_dgst = Digest::SHA256.digest(content)
  raise "config.ru item content digest mismatch" if content_dgst != Base64.decode64(dgst)

  content
end

.config_ru_from_task_rootObject



25
26
27
28
29
30
# File 'lib/himari/aws/lambda_handler.rb', line 25

def self.config_ru_from_task_root
  return nil unless ENV['LAMBDA_TASK_ROOT']
  File.read(File.join(ENV['LAMBDA_TASK_ROOT'], 'config.ru'))
rescue Errno::ENOENT, Errno::EPERM
  nil
end

.make_appObject



57
58
59
60
61
# File 'lib/himari/aws/lambda_handler.rb', line 57

def self.make_app
  require 'rack'
  require 'rack/builder'
  Rack::Builder.new_from_string(config_ru)
end

.rack_handler(event:, context:) ⇒ Object



63
64
65
# File 'lib/himari/aws/lambda_handler.rb', line 63

def self.rack_handler(event:, context:)
  Apigatewayv2Rack.handle_request(event: event, context: context, app: app)
end

.secrets_rotation_handler(event:, context:) ⇒ Object



67
68
69
# File 'lib/himari/aws/lambda_handler.rb', line 67

def self.secrets_rotation_handler(event:, context:)
  Himari::Aws::SecretsmanagerSigningKeyRotationHandler.handler(event: event, context: context)
end