Note: This article was originally published on Feb 5, 2024
Hello and welcome to this article in a journey of migrating a legacy-built app to the cloud. In this section, we will focus on three aspects: interfacing the application with Cloud Firestore, automating deployment, and exploring how Binary Authorization can reinforce supply chain security while aligning with security policies.
If you're joining us midway, I encourage you to take a look at the previous articles to get up to speed. Otherwise, let's dive in! 😊
integrating the app with Cloud Firestore
Our previous code interacted with MongoDB. With the migration to Google Cloud, we are transitioning away from MongoDB in favor of Firestore, which is Google Cloud's managed NoSQL document database built for automatic scaling, high performance, and ease of application development. To achieve this, we'll need to make modifications to our code, ensuring that our application seamlessly integrates and functions with Firestore.
We will replace the old MongoDB code with the following Firestore integration:
Old:
CODEfrom pymongo import MongoClient
from bson.objectid import ObjectId
import mongomock
...
if os.environ.get('TESTING'):
client = mongomock.MongoClient()
else:
client = MongoClient(os.environ['MONGO_URI'])
db = client.flask_db
todos = db.todos
New:
CODEfrom google.auth import compute_engine
from google.cloud import firestore
...
credentials = compute_engine.Credentials()
db = firestore.Client(credentials=credentials)
todos = db.collection('todos')
from google.auth import compute_engine: This line imports thecompute_enginemodule from thegoogle.authlibrary, which is used for authentication in Google Cloud environments
fromlibrary, enabling interaction with Google Cloud Firestore.
The
compute_engine.Credentials()call retrieves the default credentials provided by Google Cloud in its environment. These credentials are essential for authenticating with Firestore. In a local or non-Google Cloud service environment, you would need to generate a service account key before being able to authenticate with Firestore. However, in our case, since the code will be deployed on Cloud Run, authentication will be handled using the default service account of Cloud Run.
todos = db.collection('todos'). Here, we're defining a Firestore collection. Collections are used to organize documents in Firestore.
Data Insertion: When a POST request is made, the new todo item is added to the Firestore collection 'todos' using the
addmethod. The data is stored as a dictionary.
CODE@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
content = request.form.get('content')
degree = request.form.get('degree')
todos.add({'content': content, 'degree': degree})
Old:
CODEtodos.insert_one({'content': content, 'degree': degree})
New:
CODEtodos.add({'content': content, 'degree': degree})
This modification reflects the adjustment needed in the code for Firestore, moving from the
insert_onemethod in MongoDB to theaddmethod in Firestore for adding documents.
Data Retrieval: In the new code, we utilize
()to iterate over documents and convert them to a dictionary format for retrieval. The '_id' field represents the document ID in Firestore.
Data Deletion: In the new code, we employ
todos.document(id).delete()to remove a document from the Firestore collection. In the old code, we usedtodos.delete_one({"_id": ObjectId(id)})to delete a document from the MongoDB collection.
Old:
CODEtodos.delete_one({"_id": ObjectId(id)})
New:
CODEtodos.document(id).delete()
The
todos.document(id).delete()method is used to delete a specific document by its ID in Firestore.
After all these updates, the new
()), and deletion (todos.document(id).delete()), along with integrating the appropriate syntax for Firestore operations.
Testing the new code
To ensure the correctness of the new '
': Specifies the Docker image to be used for this step, which, in this case, is the Google Cloud SDK image.
entrypoint: 'gcloud': Sets the Docker entrypoint to 'gcloud,' the command-line interface for Google Cloud Platform.
args: A list of arguments passed to the 'gcloud' command.
'run' 'deploy' '$_SERVICE_NAME': Deploys a new revision of the Cloud Run service identified by$_SERVICE_NAME.
'--image' '$_for the setup of Binary Authorization."
This concludes the article. Thank you for reading. You can find the configurations and code for this project in the following Git repository.
↗ Original-Artikel auf dev.to lesenVollständiger Original-ArtikelDen kompletten Beitrag mit allen Details direkt auf dev.to lesen.
Ähnliche Beiträge
Auch interessante Nachrichten From legacy to cloud serverless - Part 4
Thematisch verwandte Begriffe: From, legacy, cloud, serverless · 6 Treffer
The Trusted Domain Is Dead. A Hacked Thai College Just Proved It.
Wie Teams neue CVEs schnell auf echte Exploitierbarkeit prüfen
Hacked Thai College Website Abused to Redirect Google Searchers to Illegal Online Casino
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
Beiträge werden geladen ...
Videos werden geladen ...
SOCIAL SHARE CARD GENERATOR