<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mohan Ganesh</title>
    <description>The latest articles on Forem by Mohan Ganesh (@mohanganesh).</description>
    <link>https://forem.com/mohanganesh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F474154%2Faaa297fa-a5aa-485f-9008-2a7828693132.png</url>
      <title>Forem: Mohan Ganesh</title>
      <link>https://forem.com/mohanganesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mohanganesh"/>
    <language>en</language>
    <item>
      <title>GCP API Gateway (Servlerless)</title>
      <dc:creator>Mohan Ganesh</dc:creator>
      <pubDate>Tue, 20 Oct 2020 02:10:17 +0000</pubDate>
      <link>https://forem.com/mohanganesh/gcp-api-gateway-servlerless-187n</link>
      <guid>https://forem.com/mohanganesh/gcp-api-gateway-servlerless-187n</guid>
      <description>&lt;p&gt;Recently google has introduced a new API management service. Google Cloud API Gateway is a fully managed service that makes it easy for organizations/developers to create, publish, maintain, monitor and secure API's.  It acts as one of the "front door" &lt;em&gt;(ideally we want managed load balancer)&lt;/em&gt; for an application deployed on backend services like App Engine, Cloud Run and Cloud Functions, Compute Engine, or Google Kubernetes Engine.&lt;/p&gt;

&lt;p&gt;In this article, we are going to deploy couple of endpoints from cloud run environment with front end proxy by API Gateway. We will have two API's to demonstrate public and secured (secured by firebase auth) by oauth2 Bearer Token.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GCP Products&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cloud Run: HTTP backend services and accessible only through API Gateway &lt;/li&gt;
&lt;li&gt;API Gateway: Frontend service to process the incoming request, and check the security if the services are secured.&lt;/li&gt;
&lt;li&gt;Firebase: Used to generate the custom token and exchange firebase custom token for google id token. &lt;/li&gt;
&lt;li&gt;App Engine: HTTP endpoints for generating firebase custom token&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will breakdown the exercise in to multipart, first we will set up the backend services and integrated with API Gateway. Once that step is complete we will focus on the firebase/appengine parts to validate the security aspect.&lt;/p&gt;

&lt;h6&gt;
  
  
  Setup
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;Git: Git is used to managing the binary&lt;/li&gt;
&lt;li&gt;GCP: You will need a google cloud project with billing enabled&lt;/li&gt;
&lt;li&gt;JDK11 environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create a GCP project and clone the repository &lt;a href="https://github.com/mohan-ganesh/apigateway-gcp.git" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the Google Cloud Shell or terminal clone the repository(&lt;a href="https://github.com/mohan-ganesh/apigateway-gcp.git" rel="noopener noreferrer"&gt;https://github.com/mohan-ganesh/apigateway-gcp.git&lt;/a&gt;) and switch to cloud-run directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export PROJECT_ID=&amp;lt;YOUR-PROJECT-ID&amp;gt;
git clone https://github.com/mohan-ganesh/apigateway-gcp.git
cd apigateway-gcp/cloud-run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cloud run application contains simple two services end points &lt;code&gt;healthcheck&lt;/code&gt; and &lt;code&gt;identity&lt;/code&gt;. &lt;code&gt;healthcheck&lt;/code&gt; is a simple endpoint that just responds with &amp;gt; alive if the application is running. &lt;code&gt;identity&lt;/code&gt; has small block of code that reads one of the header named &lt;em&gt;x-apigateway-api-userinfo&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; @RequestMapping (path="/identity", method= RequestMethod.GET,produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity identity(HttpServletRequest request)  {

        String authInfo = request.getHeader("x-apigateway-api-userinfo");
        byte data[] = Base64.getDecoder().decode(authInfo);
        return ResponseEntity.ok(new String(data));

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Build the application. This step may ask for the enablement of the Container Registry if it's not enabled.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud builds submit --tag gcr.io/$PROJECT_ID/apigateway-cloudrun
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enable the cloud run services, if it's not enabled.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud services enable run.googleapis.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, deploy the container in private mode to the cloud run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud run deploy --image gcr.io/$PROJECT_ID/apigateway-cloudrun --platform managed --region us-central1 --no-allow-unauthenticated --memory=512Mi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the deploy completes, please make a note of the Cloud Run Service URL: https://.run.app. &lt;/p&gt;

&lt;p&gt;Also, make a note of the cloud run service account. To get the cloud service account, execute the below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud iam service-accounts list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The account that has the following pattern &lt;em&gt;&lt;a href="mailto:123456-compute@developer.gserviceaccount.com"&gt;123456-compute@developer.gserviceaccount.com&lt;/a&gt;&lt;/em&gt; is the cloud service account. We will need this information to use in open API definition. &lt;/p&gt;

&lt;p&gt;now, change the directory to the openapi-definition directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; cd ..
 cd openapi-definition
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Replace the &lt;code&gt;x-google-backend&lt;/code&gt; address with cloud run service url. &lt;br&gt;
Edit &lt;code&gt;x-google-issuer&lt;/code&gt; url to add your project name.&lt;br&gt;
Edit &lt;code&gt;x-google-audiences&lt;/code&gt; with your project name.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;paths:
  /healthcheck:
    get:
      summary: healthcheck
      operationId: healthcheck
      responses:
        '200':
          description: A successful response
          schema:
            type: string

  /identity:
    get:
      summary: print user identity
      operationId: headers
      security:
        - firebase: [ ]
      responses:
        '200':
          description: A successful response
          schema:
            type: string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note that in the above block of code &lt;em&gt;healthcheck&lt;/em&gt; does not have any security, whereas &lt;em&gt;identity&lt;/em&gt; path is secured with firebase security definition. &lt;/p&gt;

&lt;p&gt;At this point in time, you are ready to create the API Gateway &lt;em&gt;configs&lt;/em&gt; and gateway's.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud beta api-gateway api-configs create &amp;lt;open-api-config-name-v1&amp;gt; \
  --api=&amp;lt;cloudrun-sevice-name&amp;gt; --openapi-spec=openapi-spec.yaml \
  --project=$PROJECT_ID --backend-auth-service-account=&amp;lt;cloud-run-service-account&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above step creates the api-gateway config, now you are ready to create API &lt;em&gt;gateways&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud beta api-gateway gateways create &amp;lt;open-api-gateway-v1&amp;gt; \
  --api=&amp;lt;cloudrun-sevice-name&amp;gt; --api-config=&amp;lt;open-api-config-name&amp;gt; \
  --location=us-central1 --project=$PROJECT_ID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need to update the config, replace create with an update.&lt;/p&gt;

&lt;p&gt;The cloud run services are private at this point in time, we would like to grant "roles/run.invoker" permission to cloud run service account so that it can invoke the services.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud run services add-iam-policy-binding &amp;lt;cloudrun-service-name&amp;gt; \
  --member "serviceAccount:&amp;lt;cloud-run-serviceaccount&amp;gt;" \
  --role "roles/run.invoker" \
  --platform managed \
  --region us-central1 \
  --project $PROJECT_ID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With these steps, you are almost ready to test the API service endpoints via GCP API Gateway.&lt;/p&gt;

&lt;p&gt;To get the API Gateway generated url, please execute the command or navigate to API Gateway in GCP Console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud beta api-gateway gateways describe &amp;lt;open-api-gateway-v1&amp;gt; \
  --location=us-central1 --project=$PROJECT_ID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see something like this. Click on the API's name and in the next section glance at the Configs and Gateways&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdmbyt48h8lixdop1ddpw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdmbyt48h8lixdop1ddpw.png" alt="Alt Text" width="800" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You would see the information like below. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgvj2svo7ou8e0usceoz6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgvj2svo7ou8e0usceoz6.png" alt="Alt Text" width="800" height="204"&gt;&lt;/a&gt;&lt;br&gt;
The url displayed under Gateways is the API Gateway generated front-end url.&lt;/p&gt;

&lt;p&gt;You can also get that url via gcloud command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud beta api-gateway gateways describe &amp;lt;open-api-gateway-config-name&amp;gt; \
  --location=us-central1 --project=$PROJECT_ID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Test API&lt;/strong&gt;&lt;br&gt;
Now you can send request's to your API&lt;br&gt;
&lt;code&gt;&lt;br&gt;
$ curl -X GET \&lt;br&gt;
  https://open-api-gateway-&amp;lt;hash&amp;gt;.gateway.dev/healthcheck \&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
At the very first request, you may see a response like below.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;{&lt;br&gt;
   "code": 504,&lt;br&gt;
   "message": "upstream request timeout"&lt;br&gt;
} &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is because, via api gateway, it request had tried to reach the backend, and response did not come in time. &lt;br&gt;
If you retry after a few seconds, you should see the response &lt;code&gt;alive&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This step's complete you have successfully able to invoke the API endpoint via GCP API Gateway. &lt;/p&gt;

&lt;p&gt;Now, if you do curl on the identity endpoint, you should see &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;{&lt;br&gt;
   "code": 401,&lt;br&gt;
   "message": "Jwt is missing"&lt;br&gt;
} &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At this point in time, this is a valid response for this endpoint.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At the beginning of the article, we said we will also demonstrate the API's secured by the firebase auth token.  As you can see when you make a request to 'identity' service, it's expecting the caller to pass a valid bearer token. It's time to generate one.&lt;/p&gt;

&lt;p&gt;Head over to App Engine folder. This is also a simple spring-boot application that contains a couple of endpoints and we are interested in 'firebase/custom/token'. &lt;/p&gt;

&lt;p&gt;For more about firebase custom token, please refer &lt;a href="https://firebase.google.com/docs/auth/admin/create-custom-tokens" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ..
cd app-engine-standard
mvn clean package
gcloud app deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the above steps, the application gets deployed to App Engine Standard instance. &lt;/p&gt;

&lt;p&gt;Time to grant, a couple of permissions. &lt;br&gt;
below one for enabling IAM and the next one is for granting Account Token Creator permission for appspot service account.&lt;/p&gt;

&lt;p&gt;Enable IAM services&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; gcloud services enable iamcredentials.googleapis.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add TokenCreator role to app-engine service account. This step is needed in order to sign the JWT token that is going to be created. &lt;/p&gt;

&lt;p&gt;For more information, please read at &lt;a href="https://firebase.google.com/docs/auth/admin/create-custom-tokens#create_custom_tokens_using_the_firebase_admin_sdk" rel="noopener noreferrer"&gt;firebase documentation&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud projects add-iam-policy-binding $PROJECT_ID \
    --member="serviceAccount:&amp;lt;replace-your-id&amp;gt;@appspot.gserviceaccount.com" \
    --role="roles/iam.serviceAccountTokenCreator"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything is fine, if you do test the custom token API you would get JWT. If you experience any error's you could also refer to the triage section at  &lt;a href="https://firebase.google.com/docs/auth/admin/create-custom-tokens#troubleshooting" rel="noopener noreferrer"&gt;firebase troubleshooting documentation&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -X GET \
  'https://&amp;lt;app-engine-generated-value&amp;gt;.appspot.com/firebase/custom/token?userId=&amp;lt;unique-id&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The response would be valid JWT, which is valid for one hour. &lt;br&gt;
This is still a valid token, but very short-lived and lesser scope. Most of the products such as endpoint and API gateway respect's OAuth bearer token. &lt;/p&gt;

&lt;p&gt;Now we will trade or exchange, the custom generated JWT token with google ID-Token. Before we do that, we need to get the Firebase project web api key. To get that value go to &lt;br&gt;
&lt;a href="https://console.firebase.google.com" rel="noopener noreferrer"&gt;Link&lt;/a&gt; and add the created GCP project to enable the firebase. &lt;/p&gt;

&lt;p&gt;Then navigate to url (replace the project id with your project id) or click on Project Overview gear icon --&amp;gt; Project Settings. Under &lt;code&gt;Your Project&lt;/code&gt; section you should have web-api-key there. Or visit the link by replacing your GCP project id. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://console.firebase.google.com/u/0/project/&amp;lt;project_id&amp;gt;/settings/general&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Under the general section note down the &lt;em&gt;Web API Key&lt;/em&gt;. This Web API key is being passed in the below api call to get the idToken. The reason for getting this Web API Key, this is how GCP knows the token that was generated belong to the respective project.  You can also able to find this information in GCP Console under the &lt;code&gt;Credentials&lt;/code&gt; section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -X POST \
  https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=&amp;lt;Firebase-Web-API-Token&amp;gt;' \
 -d '{
    "token":"&amp;lt;jwt-cutom-token&amp;gt;",
    "returnSecureToken":true
}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also, get the same key via GCP console under &lt;code&gt;Credentials&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcwgupb8d9qyg4f1ktpnc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcwgupb8d9qyg4f1ktpnc.png" alt="Alt Text" width="800" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if everything is fine, you will see a response like below&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;{&lt;br&gt;
   "kind": "identitytoolkit#VerifyCustomTokenResponse",&lt;br&gt;
   "idToken": "long-base64encoded-string",&lt;br&gt;
   "refreshToken": "refreshToken",&lt;br&gt;
   "expiresIn": "3600",&lt;br&gt;
   "isNewUser": false&lt;br&gt;
}&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;from the response, grab the idToken, now you are ready to test the 'identity' api from api-gateway.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -X GET \
  https://open-api-gateway-&amp;lt;hash&amp;gt;.gateway.dev/identity \
  -H 'authorization: Bearer &amp;lt;idToken&amp;gt;' \
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, when we pass the Bearer token, API gateway does validate the token, if the token is valid then it would let the request to process to reach the backend. As a part of that step, API gateway adds two additional auth related headers.  Now you can have backend API's to just expect &lt;code&gt;x-apigateway-api-userinfo&lt;/code&gt; if its present process the business logic. &lt;/p&gt;

&lt;p&gt;You should see JSON response like below, and that's the end of the article. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;{&lt;br&gt;
    "iss": "&lt;a href="https://securetoken.google.com/" rel="noopener noreferrer"&gt;https://securetoken.google.com/&lt;/a&gt;",&lt;br&gt;
    "aud": "",&lt;br&gt;
    "auth_time": ,&lt;br&gt;
    "user_id": "",&lt;br&gt;
    "sub": "",&lt;br&gt;
    "iat": 1603075386,&lt;br&gt;
    "exp": 1603078986,&lt;br&gt;
    "firebase": {&lt;br&gt;
        "identities": {},&lt;br&gt;
        "sign_in_provider": "custom"&lt;br&gt;
      }&lt;br&gt;
}&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With this implementation, the API can just focus on the business logic, and security is completely handled by the API Gateway layer. &lt;/p&gt;

</description>
      <category>googlecloud</category>
      <category>apigateway</category>
      <category>serverless</category>
      <category>cloudrun</category>
    </item>
  </channel>
</rss>
