loading...
Cover image for Authenticating & Calling Google Cloud Function with Service Account

Authenticating & Calling Google Cloud Function with Service Account

praneetnadkar profile image Praneet Nadkar ・3 min read

(Cover Image from here)

The services from Google Cloud have been around for a while. Google Cloud Functions is a serverless, event-driven service within Google Cloud Platform meant to create and implement programmatic functions within Google's public cloud.

All infrastructure resources are provisioned and recovered automatically by Google Cloud Platform (GCP). The GCP also gives a good list of console commands to work with them.

Authenticating Cloud Functions

There are various scenarios which are applicable for authenticating the functions. One such scenario for me for authenticating user Service account.

At this point of time, I am assuming you already have a service account created. For the very first step to start with, add your service account to IAM. For this Navigate to IAM on your GCP, and click on Add
Alt Text

Once you do that, there will be an input for member name. Put your service account name there. Next we will add roles to this service account.

To authenticate a could service using service account, make sure your service account has these roles

  1. Service Account Token Creator
  2. Service Account User
  3. Service Usage Consumer

Click on Save to save your data.

One last setup before we jump to the code for generating token is generating keys for the Service Account. Navigate to the service accounts on your GCP. Under the list, that shows the service accounts, click on the Create Key option. This should download a .json file that will have the key information. This is an imp file that has sensitive information. For more, refer here

Our service account is now setup. Now we need to get an auth token for this service account. This generated token, can then be used to call a GCP service that is using auth as service account. I had used a google cloud function in my scenario. Make sure the service we are trying to access, has the same service account set.

Alt Text

Now lets look the code snippet that will generate a JWT Token for us.

from google.oauth2 import id_token
from google.oauth2 import service_account
import google.auth
import google.auth.transport.requests
from google.auth.transport.requests import AuthorizedSession
import requests

# path to your cloud function or any other service
url = 'https://{region}-{project-id}.cloudfunctions.net/{cloud-function-name}'

# path to you keys file that was downloaded when keys for SA were created
keyFilePath = 'key.json'
creds = service_account.IDTokenCredentials.from_service_account_file
(keyFilePath,
target_audience=url)
# auth session
authed_session = AuthorizedSession(creds)

# make authenticated request and print the response, status_code
resp = authed_session.get(url)

# to verify an ID Token
request = google.auth.transport.requests.Request()
token = creds.token
# print the generated token
print(token)
print(id_token.verify_token(token,request))

This should give you the JWT token which will be required to authenticate the subsequent requests. All you have to do is pass this token in the headers when calling the respective cloud service.

headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {token}'.format(token = token)
}

Using these steps we can get a token and then call a service hosted on GCP. The same code and steps can be used to call a GCP cloud function from AWS Lambda or any other HTTP Request.

Posted on by:

praneetnadkar profile

Praneet Nadkar

@praneetnadkar

Developer. Foodie. Working on .Net tech stack, GraphQL, Azure and AWS.

Discussion

pic
Editor guide