<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Niyi</title>
    <description>The latest articles on Forem by Niyi (@niyi).</description>
    <link>https://forem.com/niyi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F123139%2F47ede93e-5cf8-480f-babd-818b40fd9062.jpg</url>
      <title>Forem: Niyi</title>
      <link>https://forem.com/niyi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/niyi"/>
    <language>en</language>
    <item>
      <title>Secure Your Firebase: Database Rules and Client Authentication</title>
      <dc:creator>Niyi</dc:creator>
      <pubDate>Mon, 19 Jun 2023 19:56:26 +0000</pubDate>
      <link>https://forem.com/niyi/secure-your-firebase-database-rules-and-client-authentication-1ebf</link>
      <guid>https://forem.com/niyi/secure-your-firebase-database-rules-and-client-authentication-1ebf</guid>
      <description>&lt;h2&gt;
  
  
  Secure Your Firebase: Database Rules and Client Authentication
&lt;/h2&gt;


&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;As a member of the engineering team, one of our most important responsibilities is securing both internal and external data. &lt;br&gt;
It is crucial to ensure that your Firebase databases are secure and protected from unauthorized access.&lt;/p&gt;
&lt;h3&gt;
  
  
  Understanding Firebase Database Rules
&lt;/h3&gt;
&lt;h4&gt;
  
  
  What are Firebase Database Rules?
&lt;/h4&gt;

&lt;p&gt;Firebase makes developers' lives easier by providing a NoSQL cloud database solution that synchronizes data across all clients in realtime, while remaining available even when the app goes offline. \&lt;br&gt;
Firebase Database Rules allow you to define access control and validation logic for your Firebase databases. These rules determine who can read or write data to your database and help protect your data from unauthorized access.&lt;/p&gt;
&lt;h4&gt;
  
  
  How Database Rules Work
&lt;/h4&gt;

&lt;p&gt;Firebase Database Rules are written in a JSON-like syntax and are evaluated in order for each database request. The rules define conditions that must be met for a request to be allowed or denied. By properly configuring these rules, you can control access at various levels, such as the database root, specific nodes, or individual fields.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Defining Secure Database Rules
&lt;/h4&gt;

&lt;p&gt;To ensure the security of your Firebase database, it is important to follow certain best practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Limit read and write access to only authorized users or user groups.\&lt;br&gt;
For example:\&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;allow read: if request.auth != null;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Within your app/client, use Firebase Authentication to authenticate users before granting access.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implement validation rules to ensure the integrity of the data being written.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be cautious while using wildcard or recursive rules and only use them when necessary.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "rules": {
    "items": {
      "$itemId": {
        ".validate": "newData.isString() &amp;amp;&amp;amp; newData.val().length &amp;lt;= 100"
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;example of a validation rule that checks for a valid date&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementing Database Rules Example
&lt;/h4&gt;

&lt;p&gt;Checkout fireship.io's recipes for writing simple and complex rules&lt;br&gt;
&lt;a href="https://fireship.io/snippets/firestore-rules-recipes/"&gt;https://fireship.io/snippets/firestore-rules-recipes/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Client Authentication for Firebase Databases
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Why Client Authentication is Important
&lt;/h4&gt;

&lt;p&gt;Client authentication plays a vital role in securing your Firebase databases. By authenticating client connections, you can ensure that only authorized users can access your database, thereby preventing unauthorized data manipulation or leakage.&lt;/p&gt;

&lt;h4&gt;
  
  
  Firebase Authentication
&lt;/h4&gt;

&lt;p&gt;Firebase provides a robust authentication system that supports various authentication providers, including email/password, Google, Facebook, and more. By integrating Firebase Authentication into your application, you can authenticate users and obtain unique user identifiers, which can be used for controlling database access.\&lt;br&gt;
&lt;a href="https://firebase.google.com/docs/auth"&gt;https://firebase.google.com/docs/auth&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Best Practices for Client Authentication
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Always use secure authentication methods, such as email/password, OAuth, or federated identity providers.&lt;/li&gt;
&lt;li&gt;Implement appropriate security measures, such as enabling two-factor authentication (2FA) for admin users.&lt;/li&gt;
&lt;li&gt;Regularly review and update your authentication system to incorporate the latest security practices.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Securing your Firebase databases is essential to protect sensitive data, maintain user trust and adhere to federal security guidelines. By following the guidelines provided in this blog post, you can establish robust database rules and authenticate client connections effectively.&lt;/p&gt;

</description>
      <category>firebase</category>
      <category>cybersecurity</category>
      <category>data</category>
      <category>gcp</category>
    </item>
    <item>
      <title>Set up automated deployments with Google Cloud Run and Gitlab</title>
      <dc:creator>Niyi</dc:creator>
      <pubDate>Fri, 07 May 2021 20:03:45 +0000</pubDate>
      <link>https://forem.com/niyi/set-up-automated-deployments-with-google-cloud-run-and-gitlab-53j5</link>
      <guid>https://forem.com/niyi/set-up-automated-deployments-with-google-cloud-run-and-gitlab-53j5</guid>
      <description>&lt;p&gt;Let's look at how we can set up a continuous delivery pipeline for our Google Cloud Run projects with Gitlab CI/CD&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Google Cloud Run:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cloud Run is a serverless, managed compute platform that enables you to run stateless containers that are invocable via web requests or Pub/Sub events.&lt;br&gt;
 To run a Cloud Run service, you need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have a &lt;a href="https://console.cloud.google.com/project" rel="noopener noreferrer"&gt;Google Project&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://console.developers.google.com/apis/api/run.googleapis.com/overview" rel="noopener noreferrer"&gt;Enable Cloud Run API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://console.cloud.google.com/cloud-build/builds" rel="noopener noreferrer"&gt;Enable Cloud Build API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Gitlab:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GitLab is a web-based DevOps lifecycle tool that provides a Git-repository manager providing wiki, issue-tracking, and continuous integration and deployment pipeline features, using an open-source license, developed by GitLab Inc.&lt;br&gt;
 You can create a Gitlab project here: &lt;a href="https://gitlab.com/projects/new" rel="noopener noreferrer"&gt;New Gitlab project&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Clone sample repo here:&lt;br&gt;
&lt;a href="https://gitlab.com/niyi/myhelloworldapp" rel="noopener noreferrer"&gt;https://gitlab.com/niyi/myhelloworldapp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1a: Enable Service Account&lt;/strong&gt;&lt;br&gt;
A service account is a special kind of account used by an application to make authorized API calls on the GCP platform.&lt;br&gt;
On your Google Cloud project, navigate through Cloud Build &amp;gt; Settings.&lt;br&gt;
Under Service account permissions, make sure both Cloud Run and Service Accounts are enabled&lt;br&gt;
&lt;strong&gt;Step 1b: Create a Google Service Account&lt;/strong&gt;&lt;br&gt;
We'll create a new service account for your application to use&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On your Google Cloud project, navigate through IAM &amp;amp; Admin &amp;gt; Service Accounts &amp;gt; Click on CREATE SERVICE ACCOUNT
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fikb4jdw0pgf35uvxf4cz.jpg" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Give your new service account any name you want and click CREATE&lt;/li&gt;
&lt;li&gt;Add the following roles to your service account by clicking Select Role input under task number 2

&lt;ol&gt;
&lt;li&gt;Cloud Build Service Agent&lt;/li&gt;
&lt;li&gt;Service Account User&lt;/li&gt;
&lt;li&gt;Cloud Run Admin&lt;/li&gt;
&lt;li&gt;Project Viewer&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Click Create then click Done to add the account.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsse7hrlpzsezhw6no3no.jpg" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Generate a credential file for this account by navigating to the newly created service account &amp;gt; Keys &amp;gt; Click on Add Key &amp;gt; Create New Key. Select JSON and click CREATE
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbyg0btv07j354147jg58.jpg" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Setup Gitlab CICD variables&lt;/strong&gt;&lt;br&gt;
In this step, we'll create variables that we'll use in our code. One for the GCP Project ID and another for the Service Account we created earlier&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to the project repository on Gitlab &amp;gt; Settings &amp;gt; CI/CD&lt;/li&gt;
&lt;li&gt;To add a variable, under the Variables section, click the Expand button and click on Add Variable
We need to add two variables, one names GCP_PROJECT_ID with the value of our GCP Project ID and the other named GCP_SERVICE_ACCOUNT for the content of the JSON we downloaded earlier
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F01ijlhysfoenyxljfuo6.jpg" alt="Alt Text"&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ldxultn8bj3k19e4745.jpg" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Setup Application code&lt;/strong&gt;&lt;br&gt;
We need to configure our code to connect to Gitlab CI/CD. We'll also use Docker to containerize our application so it runs the same across multiple platforms.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We've added a Dockerfile in our application that will run on PORT 8080, which is Google Cloud Run's default port&lt;/li&gt;
&lt;li&gt;We've also added a .gitlab-ci.yml file which is the file the triggers our &lt;a href="https://docs.gitlab.com/ee/ci/quick_start/index.html" rel="noopener noreferrer"&gt;CI/CD pipeline on Gitlab&lt;/a&gt;
*
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# File: .gitlab-ci.yml
variables:
  SERVICE_NAME: "myHelloWorldApp"

deploy:
  stage: deploy
  only:
    - master # This pipeline stage will run on this branch alone

  image: google/cloud-sdk:latest # We'll use Google Cloud SDK for Cloud Run related commands
  script:
    - echo $GCP_SERVICE_ACCOUNT &amp;gt; gcloud-service-key.json # Save Google cloud contents in a temporary json file
    - gcloud auth activate-service-account --key-file gcloud-service-key.json # Activate your service account
    - gcloud auth configure-docker # Configure docker environment
    - gcloud config set project $GCP_PROJECT_ID #Set the GCP Project ID to the variable name
    - gcloud builds submit --tag gcr.io/$GCP_PROJECT_ID/$SERVICE_NAME #Run the gcloud build command to build our image
    - gcloud run deploy $SERVICE_NAME --image gcr.io/$GCP_PROJECT_ID/$SERVICE_NAME --region=us-east4 --platform managed --allow-unauthenticated # Run the gcloud run deploy command to deploy our new service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace the SERVICE_NAME value with the desired name for your application and save the changes.&lt;/p&gt;

&lt;p&gt;At the end of the file, we're running the commands &lt;a href="https://cloud.google.com/run/docs/building/containers" rel="noopener noreferrer"&gt;gcloud build&lt;/a&gt; and &lt;a href="https://cloud.google.com/run/docs/deploying" rel="noopener noreferrer"&gt;gcloud run deploy&lt;/a&gt; to build and deploy our application respectively.&lt;/p&gt;

&lt;p&gt;Push your changes to the remote Gitlab repository and watch as your new baby is created.&lt;/p&gt;

&lt;p&gt;To monitor the progress of your deployment on Gitlab navigate to CI/CD &amp;gt; Pipelines and click on the latest job.&lt;br&gt;
To see your new application on Cloud Run, navigate to GCP &amp;gt; Cloud Run and search for the name of the service&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3wtnne0vnv6p76aoxn4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3wtnne0vnv6p76aoxn4.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
