Hashing Backend Service Custom Request Headers

Hey there,

I have a custom Request Header in my backend service that has the value of a variable (lets say {client_city}).

Instead of current value, I want to get hashed version of it as a value of my custom header.
How can i achieve this?

(Kinda new at cloud, sorry if it's a dumb one xD)


0 2 242
2 REPLIES 2

Hi @akaMerto 

To achieve hashing of a custom request header value (in your case, {client_city}) before it reaches your backend service in Google Cloud Platform (GCP), you can leverage Cloud Functions, Cloud Run, or a Google Cloud Load Balancer with advanced routing capabilities. Below, I'll outline steps for each method, including commands for setup, troubleshooting tips, and solution steps.

1. Using Cloud Functions

Implementation Steps:

  • Step 1: Create a Cloud Function that intercepts the incoming request, hashes the {client_city} header value, and forwards the request to your backend service with the new hashed header.

  • Step 2: Use the hashlib library in Python (or a similar library in your language of choice) to hash the header value.

  • Step 3: Modify the request to include the new hashed header and forward it to the backend service.

Commands:

 

 

gcloud functions deploy hashHeaderFunction --runtime python39 --trigger-http --allow-unauthenticated

 

Replace hashHeaderFunction with your function name and choose the appropriate runtime.

Troubleshooting:

  • Ensure your Cloud Function has the correct permissions to access your backend service.
  • Verify that the request is correctly formatted and the header is being modified as expected.

Solution Steps:

  1. Test the Cloud Function locally using the functions-framework for Python to make sure it hashes the header correctly.
  2. Deploy the function to GCP and test with a sample request to ensure the backend receives the hashed header.

 

2. Using Cloud Run

Implementation Steps:

  • Step 1: Create a containerized application that acts as a proxy, which receives requests, hashes the {client_city} header, and forwards them to your backend.

  • Step 2: Deploy this application to Cloud Run.

Commands:

 

 

gcloud run deploy hash-header-service --image gcr.io/[PROJECT-ID]/hash-header-proxy --platform managed

 

Ensure you replace [PROJECT-ID] with your GCP project ID.

Troubleshooting:

  • Check the logs in Cloud Run to identify any issues with the request handling or hashing process.
  • Ensure network routing between Cloud Run and your backend service is correctly configured.

Solution Steps:

  1. Implement health checks in your application to ensure it's running correctly.
  2. Use Cloud Run's revision feature to roll back if the deployment introduces issues.

 

3. Google Cloud Load Balancer with Advanced Routing

Implementation Steps:

  • Step 1: This method requires custom code or a third-party tool capable of modifying HTTP headers based on content, which might be deployed on a VM or a container. Since Google Cloud Load Balancer (GCLB) itself doesn't support modifying request headers based on their content, you would need an intermediary service (like a proxy or a gateway) to handle the hashing.

  • Step 2: Use GCLB to route requests to this intermediary service, which then hashes the header and forwards the request to the actual backend.

Troubleshooting:

  • GCLB configurations can be complex. Ensure that the routing rules are correctly set up to direct traffic to the intermediary service.

Solution Steps:

  1. For a custom solution, consider deploying an NGINX or Envoy proxy that can be configured to hash request headers.
  2. Monitor the setup using Stackdriver (Google Cloud's operations suite) to ensure it's functioning as expected.

Each of these methods involves deploying an intermediary layer that can manipulate HTTP headers. Choose the one that best fits your current architecture and operational preferences. Additionally, remember to secure your service-to-service communications as necessary, especially when handling potentially sensitive information like location data.

I hope that helps

Regards

Mahmoud

Hello Mahmoud,

What if I want to rename a response header? Is that possible to do with the routing rules in load balancer?

I see it can be used to set a response header and remove a response header. And matches allow acting on header values.

So again, I'm wondering if I can in my routing rule, take response header value, set it under another name and hide original response header?

Maksim

Top Labels in this Space