From 3a9e42e4e4074386389bf6a493dc3509211a80be Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 26 Aug 2024 12:52:31 -0700 Subject: [PATCH 01/10] fix: modernize background app --- background/app/app.yaml | 2 +- background/app/main.py | 12 ++++-------- background/app/requirements.txt | 6 +++--- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/background/app/app.yaml b/background/app/app.yaml index fb63451a..02c0651c 100644 --- a/background/app/app.yaml +++ b/background/app/app.yaml @@ -13,5 +13,5 @@ # limitations under the License. # [START getting_started_background_config] -runtime: python37 +runtime: python312 # [END getting_started_background_config] diff --git a/background/app/main.py b/background/app/main.py index 5474d54c..0bb1666d 100644 --- a/background/app/main.py +++ b/background/app/main.py @@ -64,12 +64,10 @@ def translate(): to_language = request.form.get("lang", "") if source_string == "": - error_message = "Empty value" - return error_message, 400 + return f"Invalid request, you must provide a value.", 400 if to_language not in ACCEPTABLE_LANGUAGES: - error_message = "Unsupported language: {}".format(to_language) - return error_message, 400 + return f"Unsupported language: {to_language}", 400 message = { "Original": source_string, @@ -78,11 +76,9 @@ def translate(): "OriginalLanguage": "", } - topic_name = "projects/{}/topics/{}".format( - os.getenv("GOOGLE_CLOUD_PROJECT"), "translate" - ) + topic_name = f"projects/{os.getenv('GOOGLE_CLOUD_PROJECT')}/topics/translate" publisher.publish( - topic=topic_name, data=json.dumps(message).encode("utf8") + topic=topic_name, data=json.dumps(message).encode("utf-8") ) return redirect("/") diff --git a/background/app/requirements.txt b/background/app/requirements.txt index 7cfba806..f70d16b7 100644 --- a/background/app/requirements.txt +++ b/background/app/requirements.txt @@ -1,3 +1,3 @@ -google-cloud-firestore==2.11.1 -google-cloud-pubsub==2.16.1 -flask==2.2.5 +google-cloud-firestore==2.18.0 +google-cloud-pubsub==2.23.0 +flask==3.0.3 From c0b5f606f7155017de29f801d95ac79c4db235ee Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 26 Aug 2024 13:01:51 -0700 Subject: [PATCH 02/10] fix: attempt to fix linting issues --- background/app/main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/background/app/main.py b/background/app/main.py index 0bb1666d..9a23df7f 100644 --- a/background/app/main.py +++ b/background/app/main.py @@ -64,7 +64,7 @@ def translate(): to_language = request.form.get("lang", "") if source_string == "": - return f"Invalid request, you must provide a value.", 400 + return "Invalid request, you must provide a value.", 400 if to_language not in ACCEPTABLE_LANGUAGES: return f"Unsupported language: {to_language}", 400 @@ -76,7 +76,9 @@ def translate(): "OriginalLanguage": "", } - topic_name = f"projects/{os.getenv('GOOGLE_CLOUD_PROJECT')}/topics/translate" + topic_name = ( + f"projects/{os.getenv('GOOGLE_CLOUD_PROJECT')}/topics/translate" + ) publisher.publish( topic=topic_name, data=json.dumps(message).encode("utf-8") ) From b4b058dec8a86b7bd51c0eae25eead8f8c7b4ac2 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 26 Aug 2024 13:04:49 -0700 Subject: [PATCH 03/10] fix: resolve server-side cross-site scripting --- background/app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/background/app/main.py b/background/app/main.py index 9a23df7f..f7e8cafa 100644 --- a/background/app/main.py +++ b/background/app/main.py @@ -20,7 +20,7 @@ import json import os -from flask import Flask, redirect, render_template, request +from flask import Flask, redirect, render_template, request, escape from google.cloud import firestore from google.cloud import pubsub @@ -61,7 +61,7 @@ def translate(): language (form field 'lang'), by sending a PubSub message to a topic. """ source_string = request.form.get("v", "") - to_language = request.form.get("lang", "") + to_language = escape(request.form.get("lang", "")) if source_string == "": return "Invalid request, you must provide a value.", 400 From 346b82ab881833fd60d077ea108384adaf2abb1b Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 26 Aug 2024 13:11:33 -0700 Subject: [PATCH 04/10] fix: resolve lint issue with import order. --- background/app/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/background/app/main.py b/background/app/main.py index f7e8cafa..bd71d20a 100644 --- a/background/app/main.py +++ b/background/app/main.py @@ -20,9 +20,8 @@ import json import os -from flask import Flask, redirect, render_template, request, escape -from google.cloud import firestore -from google.cloud import pubsub +from flask import escape, Flask, redirect, render_template, request +from google.cloud import firestore, pubsub app = Flask(__name__) From 7905e56b8d4296fa2bc12a2809783979691f0652 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 26 Aug 2024 13:29:22 -0700 Subject: [PATCH 05/10] chore: add temporary use of storage to check if auth is working correctly. --- background/app/main_test.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/background/app/main_test.py b/background/app/main_test.py index f3bf28c2..00f833fd 100644 --- a/background/app/main_test.py +++ b/background/app/main_test.py @@ -16,8 +16,7 @@ import uuid import google.auth -from google.cloud import firestore -from google.cloud import pubsub +from google.cloud import firestore, pubsub, storage import main import pytest @@ -38,8 +37,11 @@ def clear_collection(collection): """ for doc in collection.stream(): doc.reference.delete() + bucket_name = 'system-test-bucket' client = firestore.Client() + storage_client = storage.Client() + bucket = storage_client.bucket(bucket_name) translations = client.collection("translations") clear_collection(translations) translations.add( From a570113dde36904f1e8159c463d790c184ac1fda Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 26 Aug 2024 13:50:06 -0700 Subject: [PATCH 06/10] fix: moving the bucket_name to the right level of indentation. --- background/app/main_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/background/app/main_test.py b/background/app/main_test.py index 00f833fd..aaa9e136 100644 --- a/background/app/main_test.py +++ b/background/app/main_test.py @@ -37,8 +37,9 @@ def clear_collection(collection): """ for doc in collection.stream(): doc.reference.delete() - bucket_name = 'system-test-bucket' + + bucket_name = 'system-test-bucket' client = firestore.Client() storage_client = storage.Client() bucket = storage_client.bucket(bucket_name) From bbfc3bbbafb41759ce426f48fc31f2baab2fbac0 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 26 Aug 2024 14:01:50 -0700 Subject: [PATCH 07/10] tmp: adding an assert to use the bucket variable that should be removed. --- background/app/main_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/background/app/main_test.py b/background/app/main_test.py index aaa9e136..41b529d3 100644 --- a/background/app/main_test.py +++ b/background/app/main_test.py @@ -37,7 +37,6 @@ def clear_collection(collection): """ for doc in collection.stream(): doc.reference.delete() - bucket_name = 'system-test-bucket' client = firestore.Client() @@ -54,6 +53,7 @@ def clear_collection(collection): }, document_id="test translation", ) + assert bucket in locals() yield client From 05aea8a9cdb53cc6d4070a8b38d8f64670c982b8 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 26 Aug 2024 14:15:56 -0700 Subject: [PATCH 08/10] fix: based on https://github.com/pallets/flask/pull/5223/files moving to the use of markupsafe for escape --- background/app/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/background/app/main.py b/background/app/main.py index bd71d20a..24ee042c 100644 --- a/background/app/main.py +++ b/background/app/main.py @@ -20,7 +20,8 @@ import json import os -from flask import escape, Flask, redirect, render_template, request +from flask import Flask, redirect, render_template, request +from markupsafe import escape from google.cloud import firestore, pubsub From 30ecf811b954cdc632d3531d40bb095b9a781716 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 26 Aug 2024 14:54:24 -0700 Subject: [PATCH 09/10] fix: import order --- background/app/main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/background/app/main.py b/background/app/main.py index 24ee042c..022df934 100644 --- a/background/app/main.py +++ b/background/app/main.py @@ -17,11 +17,12 @@ """ # [START getting_started_background_app_main] -import json -import os from flask import Flask, redirect, render_template, request +import json +import os from markupsafe import escape + from google.cloud import firestore, pubsub From bba20f097f4032df80326bb5f6d07e6a48eac762 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 26 Aug 2024 15:07:57 -0700 Subject: [PATCH 10/10] fix: really fix the library order --- background/app/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/background/app/main.py b/background/app/main.py index 022df934..fad93d46 100644 --- a/background/app/main.py +++ b/background/app/main.py @@ -17,13 +17,12 @@ """ # [START getting_started_background_app_main] - -from flask import Flask, redirect, render_template, request import json import os -from markupsafe import escape +from flask import Flask, redirect, render_template, request from google.cloud import firestore, pubsub +from markupsafe import escape app = Flask(__name__)