Class: GoogleCloudRun::JobsController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/google_cloud_run/jobs_controller.rb

Instance Method Summary collapse

Instance Method Details

#callbackObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/google_cloud_run/jobs_controller.rb', line 7

def callback
  # verify User-Agent and Content-Type
  return head :bad_request unless request.user_agent == "Google-Cloud-Tasks"
  return head :bad_request unless request.headers["Content-type"].include?("application/json")
  return head :bad_request unless request.headers["Authorization"].start_with?("Bearer")

  # verify Bearer token
  begin
    r = Google::Auth::IDTokens.verify_oidc request.headers["Authorization"]&.delete_prefix("Bearer")&.strip
  rescue => e
    Rails.logger.warning "Google Cloud Run Job callback failed: #{e.message}"
    return head :bad_request
  end

  # parse JSON body
  begin
    body = JSON.parse(request.body.read)
  rescue => e
    raise "Google Cloud Run Job callback failed: Unable to parse JSON body: #{e.message}"
  end

  # execute the job
  ActiveJob::Base.execute body

  head :ok
end