Search code examples
crongoogle-cloud-platformschedulergoogle-cloud-scheduler

Verify HTTP request from Google Cloud Scheduler


What's the process for verifying the HTTP request from Google Cloud scheduler? The docs (https://cloud.google.com/scheduler/docs/creating) mention you can create a job with a target of any publicly available HTTP endpoint but do not mention how the server verifies the cron/scheduler request.


Solution

  • [Update May 28, 2019]

    Google Cloud Scheduler now has two command line options:

    --oidc-service-account-email=<service_account_email>
    --oidc-token-audience=<service_endpoint_being_called>
    

    These options add an additional header to the request that Cloud Scheduler makes:

     Authorization: Bearer ID_TOKEN
    

    You can process the ID_TOKEN inside your endpoint code to verify who is calling your endpoint.

    For example, you can make an HTTP request to decode the ID Token:

    https://oauth2.googleapis.com/tokeninfo?id_token=ID_TOKEN
    

    This will return JSON like this:

    {
      "aud": "https://cloudtask-abcdefabcdef-uc.a.run.app",
      "azp": "0123456789077420983142",
      "email": "[email protected]",
      "email_verified": "true",
      "exp": "1559029789",
      "iat": "1559026189",
      "iss": "https://accounts.google.com",
      "sub": "012345678901234567892",
      "alg": "RS256",
      "kid": "0123456789012345678901234567890123456789c3",
      "typ": "JWT"
    }
    

    Then you can check that the service account email matches the one that you authorized Cloud Scheduler to use and that the token has not expired.

    [End Update]

    You will need to verify the request yourself.

    Google Cloud Scheduler includes several Google specific headers such as User-Agent: Google-Cloud-Scheduler. Refer to the documentation link below.

    However, anyone can forge HTTP headers. You need to create a custom something that you include as an HTTP Header or in the HTTP body that you know how to verify. Using a signed JWT would be secure and easy to create and verify.

    When you create a Google Cloud Scheduler Job you have some control over the headers and body fields. You can embed your custom something in either one.

    Scheduler Jobs

    [Update]

    Here is an example (Windows command line) using gcloud so that you can set HTTP headers and the body. This example calls Cloud Functions on each trigger showing how to include an APIKEY. The Google Console does not have this level of support yet.

    gcloud beta scheduler ^
    --project production ^
    jobs create http myfunction ^
    --time-zone "America/Los_Angeles" ^
    --schedule="0 0 * * 0" ^
    --uri="https://us-central1-production.cloudfunctions.net/myfunction" ^
    --description="Job Description" ^
    --headers="{ \"Authorization\": \"APIKEY=AUTHKEY\", \"Content-Type\": \"application/json\" }" ^
    --http-method="POST" ^
    --message-body="{\"to\":\"/topics/allDevices\",\"priority\":\"low\",\"data\":{\"success\":\"ok\"}}"