-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: cloud function security improvement with shared secret (#31)
* terraform and backend changes to support the auth token fix auth and cors use the X-Signature header instaed of Authorization add type definitions for crypto-js ignore all dist folders * Add a test script for the local script * improve readme for cloud function * Update changelog * docs: minor typos * docs: updating extension source for new cloud function deployment env var --------- Co-authored-by: Luka Fontanilla <[email protected]>
- Loading branch information
1 parent
aa75432
commit b59eb53
Showing
14 changed files
with
212 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,6 @@ terraform.tfstate* | |
*.tfstate | ||
.venv | ||
node_modules | ||
dist/ | ||
|
||
.vertex_cf_auth_token | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import hmac | ||
import hashlib | ||
import requests | ||
import json | ||
|
||
def generate_hmac_signature(secret_key, data): | ||
""" | ||
Generate HMAC-SHA256 signature for the given data using the secret key. | ||
""" | ||
hmac_obj = hmac.new(secret_key.encode(), json.dumps(data).encode(), hashlib.sha256) | ||
return hmac_obj.hexdigest() | ||
|
||
def send_request(url, data, signature): | ||
""" | ||
Send a POST request to the given URL with the provided data and HMAC signature. | ||
""" | ||
headers = { | ||
'Content-Type': 'application/json', | ||
'X-Signature': signature | ||
} | ||
response = requests.post(url, headers=headers, json=data) | ||
return response.text | ||
|
||
def main(): | ||
# URL of the endpoint | ||
url = 'http://localhost:8000' | ||
|
||
# Request payload | ||
data = {"contents":"how are you doing?", "parameters":{"max_output_tokens": 1000}} | ||
|
||
# Read the secret key from a file | ||
with open('../.vertex_cf_auth_token', 'r') as file: | ||
secret_key = file.read().strip() # Remove any potential newline characters | ||
|
||
# Generate HMAC signature | ||
signature = generate_hmac_signature(secret_key, data) | ||
|
||
# Send the request | ||
response = send_request(url, data, signature) | ||
print("Response from server:", response) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
VERTEX_AI_ENDPOINT=<This is your Deployed Cloud Function Endpoint> | ||
LOOKER_MODEL=<This is your Looker model name> | ||
LOOKER_EXPLORE=<This is your Looker explore name> | ||
|
||
VERTEX_AI_ENDPOINT=<This is your Deployed Cloud Function Endpoint> | ||
VERTEX_CF_AUTH_TOKEN=<This is the token used to communicate with the cloud function> | ||
|
||
VERTEX_BIGQUERY_LOOKER_CONNECTION_NAME=<This is the connection name that has vertex ai external connector> | ||
VERTEX_BIGQUERY_MODEL_ID=<This is the model id that you want to use for prediction> |
Oops, something went wrong.