keep icon indicating copy to clipboard operation
keep copied to clipboard

feat(mailgun): Add EU region support and UI configuration options

Open sanyo4ever opened this issue 3 months ago โ€ข 4 comments

close https://github.com/keephq/keep/issues/5416

๐ŸŽฏ Overview

This PR enhances the Mailgun provider for self-hosted Keep instances with:

  1. EU Region Support - Users can now use Mailgun EU accounts
  2. UI Region Selection - Dropdown to choose US/EU region per provider
  3. API Request Format Fix - Corrects route creation bug
  4. Hybrid Configuration - Supports both UI and environment variable configuration

๐Ÿ› Problems Solved

Issue 1: EU Region Not Supported

The Mailgun API endpoint was hardcoded to https://api.mailgun.net (US region only). Users with EU Mailgun accounts received 401/400 errors when installing the provider.

Issue 2: Incorrect API Request Format

The setup_webhook() method was passing both files and data parameters to requests.post(), causing "400 Bad Request" errors from Mailgun API.

Issue 3: No UI Control for Region

Users had to modify code or use environment variables. No per-provider region configuration was possible.

โœจ Solution

1. UI Dropdown for Region Selection

Added api_region field to MailgunProviderAuthConfig with dropdown:

  • Type: Select field with options ["US", "EU"]
  • Optional: Can be left empty to use environment variable
  • User-friendly: Clear hints and descriptions

2. Hybrid Configuration Priority

region = (
    self.authentication_config.api_region  # 1. Check UI field first
    or MailgunProvider.MAILGUN_REGION      # 2. Fall back to env var
    or "US"                                 # 3. Default to US
).upper()

Benefits:

  • Per-provider region configuration (UI)
  • Global default via MAILGUN_REGION environment variable
  • Backward compatible (existing deployments unaffected)
  • Flexible for multi-region deployments

3. API Endpoint Selection

Dynamically selects the correct API endpoint:

  • US Region: https://api.mailgun.net
  • EU Region: https://api.eu.mailgun.net

4. API Request Format Fix

Before (Broken):

response = requests.post(
    url,
    files=payload,  # โŒ Wrong parameter
    auth=("api", key),
    data=payload,   # โŒ Missing action array
)

After (Fixed):

response = requests.post(
    url,
    auth=("api", key),
    data={
        **payload,
        "action": [  # โœ… Correct format
            f"forward('{keep_api_url}&api_key={api_key}')",
            "stop()",
        ],
    },
)

๐Ÿงช Testing

  • โœ… Tested with US region Mailgun account
  • โœ… Tested with EU region Mailgun account
  • โœ… Route creation works correctly in both regions
  • โœ… UI dropdown displays and saves correctly
  • โœ… Environment variable fallback works
  • โœ… Backward compatible (defaults to US)
  • โœ… No linting errors

๐Ÿ“Š Changes

Files Modified:

  • keep/providers/mailgun_provider/mailgun_provider.py
    • Added api_region field with dropdown (lines 49-59)
    • Added MAILGUN_REGION class variable (line 74)
    • Dynamic API endpoint selection (lines 222-229)
    • Fixed request format in setup_webhook() (lines 231-259)

๐Ÿ”„ Backward Compatibility

  • โœ… Existing deployments unaffected (defaults to US region)
  • โœ… Environment variable MAILGUN_REGION still works
  • โœ… No breaking changes to configuration
  • โœ… Works with both old and new Mailgun API key formats

๐Ÿ’ก Usage Examples

Example 1: UI Configuration (Per-Provider)

sanyo4ever avatar Oct 14 '25 20:10 sanyo4ever