<?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>DEV Community: Shreya sharma</title>
    <description>The latest articles on DEV Community by Shreya sharma (@shreya99).</description>
    <link>https://dev.to/shreya99</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%2F2483361%2F6970e296-116e-4a93-ab7f-24d4be92b1b0.png</url>
      <title>DEV Community: Shreya sharma</title>
      <link>https://dev.to/shreya99</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shreya99"/>
    <language>en</language>
    <item>
      <title>How I Secured a Flask API with CI/CD Integration</title>
      <dc:creator>Shreya sharma</dc:creator>
      <pubDate>Tue, 26 Nov 2024 04:13:09 +0000</pubDate>
      <link>https://dev.to/shreya99/how-i-secured-a-flask-api-with-cicd-integration-48i3</link>
      <guid>https://dev.to/shreya99/how-i-secured-a-flask-api-with-cicd-integration-48i3</guid>
      <description>&lt;p&gt;Introduction&lt;/p&gt;

&lt;p&gt;The need for API security cannot be overstated. From sensitive data exposure to unauthorized access, APIs are often targeted by attackers. To address these challenges, I implemented key security features in my Flask API while leveraging CI/CD to ensure robust testing and seamless deployment.&lt;/p&gt;

&lt;p&gt;This blog covers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Building a secure Flask API.&lt;/li&gt;
&lt;li&gt;Adding authentication with JSON Web Tokens (JWT).&lt;/li&gt;
&lt;li&gt;Testing the API for correctness and security.&lt;/li&gt;
&lt;li&gt;Automating testing and deployment using GitHub Actions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;APIs are the backbone of modern software applications, enabling communication between systems. However, with increasing reliance on APIs comes a critical responsibility: security. This post walks through how I built a secure Flask API, tested its functionality, and integrated CI/CD pipelines to automate testing and deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up the Flask API
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Functionality&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Flask API handles user requests and provides dynamic responses while enforcing authentication. Key features include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JWT-based Authentication: Ensures that only authorized users can access the API.&lt;/li&gt;
&lt;li&gt;Secure Input Handling: Protects against common vulnerabilities like SQL injection.&lt;/li&gt;
&lt;li&gt;Role-Based Access Control: Provides different access levels based on user roles.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of basic endpoint : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fpw8leyhv7390oq978oxp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fpw8leyhv7390oq978oxp.png" alt="Here’s a simple endpoint for demonstration" width="800" height="704"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features Explained&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Token-Based Security: Each user is assigned a JWT, which must be included in the request headers.&lt;/li&gt;
&lt;li&gt;Expiration Time: Tokens expire after an hour, reducing the risk of token misuse.&lt;/li&gt;
&lt;li&gt;Error Handling: Provides clear messages for unauthorized or expired tokens.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Testing the API
&lt;/h2&gt;

&lt;p&gt;Testing was crucial to validate the API’s security and functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unit Tests&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unit tests ensured the API endpoints worked as &lt;br&gt;
expected. For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fpfo0qfvntxnc1a6nwfio.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fpfo0qfvntxnc1a6nwfio.png" alt="unit test for endpoint" width="800" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integration Tests&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Integration tests simulated real-world scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Valid and invalid tokens.&lt;/li&gt;
&lt;li&gt;Expired tokens.&lt;/li&gt;
&lt;li&gt;Requests without tokens&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Test Results&lt;/strong&gt;: All unit and integration tests passed, confirming that the API enforced security policies effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up CI/CD with GitHub Actions
&lt;/h2&gt;

&lt;p&gt;To ensure a seamless workflow, I implemented a &lt;br&gt;
CI/CD pipeline using GitHub Actions. The pipeline automates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Building the application.&lt;/li&gt;
&lt;li&gt;Running tests.&lt;/li&gt;
&lt;li&gt;Deploying to the production environment.&lt;/li&gt;
&lt;li&gt;Pipeline Configuration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fsg92jqyv2lw458mnpyaa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsg92jqyv2lw458mnpyaa.png" alt="Image description" width="800" height="900"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automated Testing: Prevents faulty code from being deployed.&lt;/li&gt;
&lt;li&gt;Secure Deployment: Secrets like API keys are managed using GitHub Secrets.&lt;/li&gt;
&lt;li&gt;Continuous Feedback: Developers are notified instantly if tests fail.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Challenges and Solutions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Challenges&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Debugging Failing Tests:

&lt;ul&gt;
&lt;li&gt;Initial tests failed due to incorrect token 
encoding.&lt;/li&gt;
&lt;li&gt;Mocking secure headers was challenging for 
some test cases.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Managing Sensitive Data

&lt;ul&gt;
&lt;li&gt;Storing and retrieving API keys securely was a critical concern.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solutions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Added detailed logging to pinpoint test failures quickly.&lt;/li&gt;
&lt;li&gt;Used GitHub Secrets to securely store sensitive data like SECRET_KEY, ensuring it wasn’t exposed in the repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This project emphasized the importance of API security and the role of automation in maintaining software quality. Key takeaways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API Security: Implementing token-based authentication and role-based access improved the API’s resilience to unauthorized access.&lt;/li&gt;
&lt;li&gt;CI/CD Integration: Automated pipelines ensured consistent testing and reliable deployments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Future Improvements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adding multi-factor authentication (MFA) for enhanced security.&lt;/li&gt;
&lt;li&gt;Monitoring and logging suspicious API activity for proactive threat detection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By combining Flask, JWT, and CI/CD, I built an &lt;br&gt;
API that not only functions effectively but also safeguards user data. If you’re building APIs, security should never be an afterthought.&lt;/p&gt;

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