<?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: sylroyalle</title>
    <description>The latest articles on Forem by sylroyalle (@vanusquarm).</description>
    <link>https://forem.com/vanusquarm</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%2F1027749%2F60067441-2919-4e26-b1c5-82c6e7bc6e86.jpeg</url>
      <title>Forem: sylroyalle</title>
      <link>https://forem.com/vanusquarm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vanusquarm"/>
    <language>en</language>
    <item>
      <title>Configuring ocelot gateway to act as a "pass-through" for incoming requests</title>
      <dc:creator>sylroyalle</dc:creator>
      <pubDate>Wed, 27 Nov 2024 11:44:34 +0000</pubDate>
      <link>https://forem.com/vanusquarm/configuring-ocelot-gateway-to-act-as-a-pass-through-for-incoming-requests-3p1b</link>
      <guid>https://forem.com/vanusquarm/configuring-ocelot-gateway-to-act-as-a-pass-through-for-incoming-requests-3p1b</guid>
      <description>&lt;p&gt;This setup demonstrates how to configure ocelot gateway as a "pass-through" for incoming requests, while ensuring that only authenticated and authorized users can access the downstream APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In this scenario, Ocelot will:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Accept all incoming requests (no authentication required at the gateway level).&lt;br&gt;
Forward the requests to downstream services, which are protected by Keycloak using JWT tokens.&lt;br&gt;
Enforce authentication and authorization on the downstream services, so only users with valid tokens can access those services.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to Achieve This:&lt;/strong&gt;&lt;br&gt;
Configure Ocelot Gateway:&lt;/p&gt;

&lt;p&gt;Ocelot will not require authentication itself but will forward the JWT token (if provided) to downstream services that are protected.&lt;br&gt;
Configure Downstream APIs:&lt;/p&gt;

&lt;p&gt;Downstream APIs should be secured, meaning they must validate JWT tokens and ensure that only authorized users can access the resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Configuration&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Configure Ocelot Gateway (ocelot.json)
In your Ocelot gateway, you will set up the routes to forward the incoming requests to downstream services. You don't need to protect the gateway itself, so there is no authentication requirement at the gateway level.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/{everything}",
      "UpstreamPathTemplate": "/gateway/{everything}",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ],
      "UpstreamHttpMethod": ["Get", "Post", "Put", "Delete"],
      "AddHeadersToRequest": {
        "Authorization": "RequestHeader"  // Forward the Authorization header to downstream
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5000"
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AddHeadersToRequest: This setting ensures that the Authorization header (which carries the JWT token) is forwarded from the incoming request to the downstream service.&lt;/p&gt;

&lt;p&gt;In this configuration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ocelot will not perform authentication; it will simply forward the request to the downstream service.&lt;/li&gt;
&lt;li&gt;The downstream service will be responsible for authenticating the incoming request using JWT tokens issued by Keycloak.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example Flow&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A client (e.g., a user) sends a request to the Ocelot gateway.&lt;/li&gt;
&lt;li&gt;The request might include an Authorization header containing a JWT token issued by Keycloak (for example, from a frontend app).&lt;/li&gt;
&lt;li&gt;Ocelot forwards the request (including the Authorization header) to the downstream service.&lt;/li&gt;
&lt;li&gt;The downstream service uses JWT authentication middleware to validate the token against Keycloak.&lt;/li&gt;
&lt;li&gt;If the token is valid and the user is authorized (e.g., they have the correct scope), the downstream service processes the request.&lt;/li&gt;
&lt;li&gt;If the token is missing, invalid, or the user is unauthorized, the downstream service responds with an HTTP 401 Unauthorized error.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
This approach allows you to decouple the responsibility for authentication from the gateway, simplifying the gateway's configuration while still ensuring that only authenticated and authorized users can access the protected APIs.&lt;/p&gt;

&lt;p&gt;Follow for the next post on how to &lt;strong&gt;Configure Downstream API for JWT Authentication&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>API integration Considerations</title>
      <dc:creator>sylroyalle</dc:creator>
      <pubDate>Wed, 22 May 2024 10:06:07 +0000</pubDate>
      <link>https://forem.com/vanusquarm/api-integration-considerations-3616</link>
      <guid>https://forem.com/vanusquarm/api-integration-considerations-3616</guid>
      <description>&lt;p&gt;When designing an API that interacts with another API (service-to-service communication), there are several important considerations to ensure robustness, efficiency, and security. Service can communicate with other services synchronously (using rest/grpc protocols) or asynchronously (using message queues like rabbitmq or kafka). Below is comprehensive list for restful communication between API services:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Understand the External API&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Documentation&lt;/strong&gt;: Thoroughly review the documentation of the external API to understand its endpoints, request/response formats, rate limits, authentication mechanisms, and error codes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Models&lt;/strong&gt;: Understand the data structures used by the external API to ensure compatibility with your API.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Authentication and Authorization&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;API Keys/OAuth&lt;/strong&gt;: Implement the appropriate authentication mechanism required by the external API, such as API keys, OAuth, or other methods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Token Management&lt;/strong&gt;: If using OAuth, handle token storage, refresh tokens, and expiration correctly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Rate Limiting&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rate Limits&lt;/strong&gt;: Respect the rate limits imposed by the external API to avoid being throttled or banned.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Throttling and Queuing&lt;/strong&gt;: Implement throttling mechanisms and possibly a request queue to manage the rate of outgoing requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Error Handling and Retries&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Error Codes&lt;/strong&gt;: Handle HTTP status codes and error messages from the external API gracefully.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retry Logic&lt;/strong&gt;: Implement exponential backoff for retries on transient errors (e.g., 5xx errors).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fallbacks&lt;/strong&gt;: Consider fallback strategies if the external API is down or experiencing issues.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;Data Transformation&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mapping&lt;/strong&gt;: Map the data structures from the external API to your internal models. This may involve transformation, validation, and enrichment of data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistency&lt;/strong&gt;: Ensure consistency in how data from the external API is represented in your API responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;Caching&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Response Caching&lt;/strong&gt;: Implement caching strategies to reduce the number of calls to the external API and improve performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache Invalidation&lt;/strong&gt;: Handle cache invalidation appropriately to ensure data freshness.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. &lt;strong&gt;Security&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sensitive Data&lt;/strong&gt;: Encrypt sensitive data in transit and at rest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Input Validation&lt;/strong&gt;: Validate and sanitize all inputs to avoid injection attacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rate Limiting&lt;/strong&gt;: Implement rate limiting on your API to protect against abuse.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  8. &lt;strong&gt;Logging and Monitoring&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Request/Response Logging&lt;/strong&gt;: Log interactions with the external API for debugging and monitoring purposes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring&lt;/strong&gt;: Monitor the performance and availability of the external API and your integration with it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  9. &lt;strong&gt;Scalability&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Load Handling&lt;/strong&gt;: Design your API to handle load spikes efficiently, considering that the external API might also impose its limits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Asynchronous Processing&lt;/strong&gt;: Use asynchronous calls and processing to improve scalability and responsiveness.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  10. &lt;strong&gt;Documentation and User Experience&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;API Documentation&lt;/strong&gt;: Provide clear documentation for your API, including how it interacts with the external API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Messages&lt;/strong&gt;: Provide informative error messages and status codes to help users understand issues.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  11. &lt;strong&gt;Compliance&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Legal and Regulatory&lt;/strong&gt;: Ensure compliance with legal and regulatory requirements, such as GDPR, when dealing with data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terms of Service&lt;/strong&gt;: Abide by the terms of service of the external API.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  12. &lt;strong&gt;Testing&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unit and Integration Tests&lt;/strong&gt;: Write comprehensive tests for your API, including mock interactions with the external API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;End-to-End Tests&lt;/strong&gt;: Perform end-to-end tests to ensure the entire workflow operates as expected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By considering these factors, you can design a robust, secure, and efficient RESTful API that reliably integrates with another API. Proper planning and adherence to best practices are crucial to managing dependencies and providing a smooth experience for your users.&lt;/p&gt;

</description>
      <category>api</category>
      <category>integration</category>
      <category>microservices</category>
    </item>
  </channel>
</rss>
