-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wwi21/46 implementing rate limiting (#122)
* Basic logic for token checking * Initial Firebase Setup * Implemented token system * Linting checks * Linting checks * firebase_admin dependency added * added requirement * firebase_admin added to specific requirements * adds service account certificate env var from github secrests * removes superflous dependency 'pnmlbpmntransformer' * 💚 * 💚 Changes service account certificate env var handling * Check Tokens Function added * adds basic decodation of base64 service account certificate * Refactor service account certificate handling * fixes set_force_std_xml flag position * now uses correct var for decoding base64 * added tempfile for secret * remove unused dependency * test prints added * test * test * test * test * test * test * test * test * test * test * test * test * t * test * test * test * test * test * improved code quality * final touches --------- Co-authored-by: Jeldrik Merkelbach <[email protected]> Co-authored-by: Niyada <[email protected]>
- Loading branch information
1 parent
30400fb
commit 2ce682c
Showing
16 changed files
with
146 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# This file specifies files that are *not* uploaded to Google Cloud | ||
# using gcloud. It follows the same syntax as .gitignore, with the addition of | ||
# "#!include" directives (which insert the entries of the given .gitignore-style | ||
# file at that point). | ||
# | ||
# For more information, run: | ||
# $ gcloud topic gcloudignore | ||
# | ||
.gcloudignore | ||
# If you would like to upload your .git directory, .gitignore file or files | ||
# from your .gitignore file, remove the corresponding line | ||
# below: | ||
.git | ||
.gitignore | ||
|
||
node_modules | ||
#!include:.gitignore |
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 |
---|---|---|
|
@@ -13,5 +13,8 @@ src/transform/test_log | |
# VS Code | ||
.vscode/ | ||
|
||
# Service Account Secrets | ||
secrets/ | ||
|
||
# MacOS | ||
.DS_Store |
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,45 @@ | ||
"""Implements the 'check_Tokens' HTTP Cloud Function. | ||
This module defines a Google Cloud Function for RateLimiting the transform Endpoint. | ||
""" | ||
import base64 | ||
import firebase_admin | ||
import functions_framework | ||
import json | ||
from firebase_admin import credentials, firestore | ||
from flask import jsonify | ||
import os | ||
|
||
GCP_SERVICE_ACCOUNT_CERTIFICATE_BASE64 = os.getenv( "GCP_SERVICE_ACCOUNT_CERTIFICATE" ) | ||
if( GCP_SERVICE_ACCOUNT_CERTIFICATE_BASE64 is None ): | ||
print( "Env var GCP_SERVICE_ACCOUNT_CERTIFICATE not found!" ) | ||
|
||
GCP_SERVICE_ACCOUNT_CERTIFICATE_DECODED_BYTES = \ | ||
base64.b64decode(GCP_SERVICE_ACCOUNT_CERTIFICATE_BASE64) | ||
|
||
GCP_SERVICE_ACCOUNT_CERTIFICATE_DECODED_STRING = \ | ||
GCP_SERVICE_ACCOUNT_CERTIFICATE_DECODED_BYTES.decode('utf-8') | ||
|
||
cred_dict = json.loads(GCP_SERVICE_ACCOUNT_CERTIFICATE_DECODED_STRING, strict=False) | ||
cred = credentials.Certificate(cred_dict) | ||
firebase_admin.initialize_app(cred) | ||
db = firestore.client() | ||
|
||
@functions_framework.http | ||
def check_tokens(request): | ||
"""Check if there are tokens available in the Firestore database.""" | ||
if db is None: | ||
return jsonify({"error": "No database available"}), 500 | ||
|
||
doc_ref = db.collection("api-tokens").document("token-document") | ||
doc = doc_ref.get() | ||
if doc.exists: | ||
tokens = doc.to_dict().get("tokens", 0) | ||
if tokens <= 0: | ||
return jsonify({"error": "No tokens available"}), 400 | ||
else: | ||
doc_ref.update({"tokens": tokens-1}) | ||
return jsonify({"tokens": tokens-1}), 200 | ||
else: | ||
return jsonify({"error": "No document available"}), 404 | ||
|
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,2 @@ | ||
firebase_admin==6.5.0 | ||
functions-framework==3.7.0 |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# test index | ||
-i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple | ||
firebase_admin==6.5.0 | ||
lxml==5.2.2 | ||
pydantic==2.7.4 | ||
pydantic_xml==2.11.0 | ||
defusedxml==0.7.1 | ||
defusedxml==0.7.1 |
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,2 @@ | ||
|
||
"""This is the __init__ module for the unit tests.""" |
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,10 @@ | ||
"""Unit tests for the transform endpoint of the application.""" | ||
|
||
import unittest | ||
|
||
class TestUnitCheckTokens(unittest.TestCase): | ||
"""A unit test class for testing the CheckTokens Endpoint of the application.""" | ||
|
||
def test_boilerplate(self): | ||
"""Boilerplate empty test function. This will always pass.""" | ||
self.assertTrue(True) |
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,2 @@ | ||
|
||
"""This is the __init__ module for the unit tests.""" |
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,10 @@ | ||
"""Integration tests for the transform endpoint of the application.""" | ||
|
||
import unittest | ||
|
||
class TestUnitCheckTokens(unittest.TestCase): | ||
"""An integration test class for testing the CheckTokens endpoint of the applic..""" | ||
|
||
def test_boilerplate(self): | ||
"""Boilerplate empty test function. This will always pass.""" | ||
self.assertTrue(True) |
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,2 @@ | ||
|
||
"""This is the __init__ module for the unit tests.""" |
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,10 @@ | ||
"""Unit tests for the transform endpoint of the application.""" | ||
|
||
import unittest | ||
|
||
class TestUnitCheckTokens(unittest.TestCase): | ||
"""A unit test class for testing the CheckTokens Endpoint of the application.""" | ||
|
||
def test_boilerplate(self): | ||
"""Boilerplate empty test function. This will always pass.""" | ||
self.assertTrue(True) |