A professional secure bridge for integrating FTP/SFTP servers with Power BI and analytics systems.
- Features
- Security
- Quick Start
- Installation
- Configuration
- Usage
- Administrative Utilities
- Monitoring
- Docker
- Power BI Integration
- Troubleshooting
- π SFTP Host Key Verification: Host key verification for maximum security
- π‘ HEAD Endpoint: Retrieve file metadata without downloading (for Power BI preflight)
- π‘οΈ Path Sanitizer: Advanced protection against path traversal attacks
- π PII Masking: Masking of personal data in logs (email, users)
- β‘ Auto-Chunk Tuning: Automatic chunk size optimization for large files
- πΆ Degraded Mode: Graceful startup without tokens with limited functionality
- ποΈ Storage Backend Architecture: Unified architecture for FTP/FTPS/SFTP
- π§ Default Rate Limits: Built-in 100/minute limits for extra protection
- π Enhanced Security: Tokens only via environment variables
- β‘ Rate Limiting: Protection from DDoS and abuse with
slowapi - π Secure CORS: Configurable domains instead of wildcard
* - π SFTP Support: Encrypted file transfer via paramiko
- π Log Rotation: Automatic log size management
- βοΈ Pydantic v2: Modern configuration validation with BaseSettings
- π οΈ Advanced Admin Utilities: Monitoring, diagnostics, maintenance
- Streaming large files without loading into memory
- Protocol support: FTP, FTPS, SFTP
- RESTful API with automatic documentation
- File validation by extension and size
- Path Traversal Protection
- Detailed logging with rotation
- Health Check endpoints for monitoring
- Docker Ready with prebuilt containers
β Fixed in v2.0.0:
- β Hardcoded tokens removed from code
- β Added rate limiting for attack protection
- β
CORS configured for specific domains (not
*) - β Added SFTP support for encrypted transfer
- β Implemented log rotation
- β Modern secret management with Pydantic v2
-
Token Management π
- Tokens only via environment variables
- Minimum length 32 characters
- Automatic token strength validation
-
Rate Limiting β‘
- Configurable request limits
- DDoS protection
- IP-based blocking
-
CORS Policy π
- Domain restriction in production
- Configurable methods and headers
- Credentials support
-
File Validation π
- File extension checks
- Path traversal protection
- File size limits
- Token Storage: All hardcoded tokens removed; tokens only from environment variables; minimum length 32 characters; automatic validation.
- Rate Limiting: Added
slowapimiddleware; default 100/minute; configurable via environment. - CORS: Wildcard
*removed; customizable via env; separate dev/prod configs; validation in monitoring. - SFTP/FTPS: Full SFTP support with host key verification; FTPS by default; warnings for insecure FTP.
- Log Rotation: RotatingFileHandler with configurable size/count; log size monitoring.
- Secrets Management: Full transition to Pydantic v2 BaseSettings; .env support; type-level validation.
- Additional: Path sanitizer, PII masking in logs, degraded mode, HEAD endpoint, auto-chunk tuning, unified backend architecture.
- Authentication: Tokens only from environment, min 32 chars, auto validation.
- Authorization: Bearer token, client identification, access logging.
- Rate Limiting: DDoS protection, IP blocking, configurable limits.
- CORS Policy: Domain restriction, secure headers, method control.
- Data Validation: Pydantic v2, file checks, path traversal protection.
- Encryption: SFTP/FTPS support, secure transfer.
- Automated: Path traversal, token validation, rate limiting, CORS, SFTP, HEAD endpoint, degraded mode.
- Manual: Penetration testing, brute force protection, file system boundaries, protocol validation.
- Tokens
- Generate unique tokens 32+ characters
- Set environment variables
- Remove all example tokens
- CORS
- Specify exact domains (not "*")
- Test access from Power BI
- Rate Limiting
- Set limits for your load
- Test attack protection
- Protocols
- Enable FTPS or SFTP
- Test encrypted connections
- Monitoring
- Set up health check
- Check log rotation
Additional Measures:
- Regular token rotation (every 90 days)
- Monitoring rate limiting and security
- Use Docker Secrets in production
- Log auditing for suspicious activity
- OWASP Top 10 2021:
- A01 Broken Access Control: Resolved by tokens/validation
- A02 Cryptographic Failures: Resolved by FTPS/SFTP
- A03 Injection: Resolved by path sanitizer
- A05 Security Misconfiguration: Resolved by Pydantic v2
- A07 Identification/Authentication Failures: Resolved by tokens
- A09 Security Logging: Resolved by PII masking
- Security Standards:
- ISO 27001: Secrets management
- NIST Cybersecurity Framework: Multi-layered protection
- PCI DSS: Encrypted data transfer
git clone https://github.com/your-repo/ftp_bridge.git
cd ftp_bridge
pip install -r requirements.txt# Generate tokens
python -c "import secrets; print('FTP_BRIDGE_TOKEN_POWERBI=' + secrets.token_hex(16))"
python -c "import secrets; print('FTP_BRIDGE_TOKEN_EXCEL=' + secrets.token_hex(16))"
# Create .env file
echo "FTP_BRIDGE_TOKEN_POWERBI=your_generated_token_here" > .env
echo "FTP_BRIDGE_TOKEN_EXCEL=another_generated_token_here" >> .envpython start.pycurl -H "Authorization: Bearer your_token" \
"http://localhost:8000/download?host=ftp.example.com&user=demo&password=password&path=/&file=test.txt"- Python 3.8+
- FastAPI 0.104+
- Pydantic v2
python start.py # Will check and install dependencies automaticallypip install -r requirements.txt# For SFTP support
pip install paramiko
# For production
pip install gunicorn# Client tokens (minimum 32 characters)
FTP_BRIDGE_TOKEN_POWERBI=aabbccdd11223344556677889900aabbccddee
FTP_BRIDGE_TOKEN_EXCEL=1122334455667788990011223344556677889900
FTP_BRIDGE_TOKEN_ANALYTICS=9988776655443322110099887766554433221100# Allowed domains (DO NOT use * in production!)
FTP_BRIDGE_CORS_ORIGINS=https://app.powerbi.com,https://your-domain.com
# Rate limiting
FTP_BRIDGE_RATE_LIMIT_ENABLED=true
FTP_BRIDGE_RATE_LIMIT_REQUESTS=100 # Requests per hour
FTP_BRIDGE_RATE_LIMIT_WINDOW=3600 # Window in seconds# Default protocol (ftps recommended for security)
FTP_BRIDGE_DEFAULT_PROTOCOL=ftps # ftp, ftps, sftp
# FTPS settings
FTP_BRIDGE_USE_FTPS=true
# Ports
FTP_BRIDGE_FTP_PORT=21 # FTP port
FTP_BRIDGE_SFTP_PORT=22 # SFTP port
# SFTP Host Key Verification (for maximum security)
FTP_BRIDGE_KNOWN_HOSTS_PATH=/home/user/.ssh/known_hosts
# Degraded mode (graceful fallback)
FTP_BRIDGE_DEGRADED_MODE=falseFTP_BRIDGE_LOG_ROTATION=true
FTP_BRIDGE_LOG_MAX_SIZE=10485760 # 10MB
FTP_BRIDGE_LOG_BACKUP_COUNT=5# Copy from template
cp env_example.txt .env
# Edit tokens
nano .envGET /download?host=ftp.example.com&user=demo&password=password&path=/reports/data.xlsx&protocol=ftps
Authorization: Bearer your_token_hereHEAD /download?host=ftp.example.com&user=demo&password=password&path=/reports/data.xlsx&protocol=ftps
Authorization: Bearer your_token_hereReturns headers:
X-File-Size- File size in bytesX-Protocol- Protocol used (ftp/ftps/sftp)X-File-Name- File nameContent-Length- Download size
Used by Power BI for preflight requests and optimized loading
host- FTP/SFTP serveruser- Usernamepassword- Passwordpath- Full file path on the server (including file name)protocol-auto,ftp,ftps,sftp(optional, default is ftps)
GET /healthAdvanced diagnostics v2.1.0:
{
"status": "healthy",
"degraded_mode": false,
"default_protocol": "ftps",
"sftp_host_key_verification": true,
"protocols_available": ["ftp", "ftps", "sftp"]
}- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
import requests
headers = {"Authorization": "Bearer your_token"}
params = {
"host": "ftp.example.com",
"user": "demo",
"password": "password",
"path": "/data/report.xlsx", # Full file path
"protocol": "ftps" # Secure FTPS by default
}
# First, get metadata (optional)
head_response = requests.head("http://localhost:8000/download",
headers=headers, params=params)
file_size = head_response.headers.get('X-File-Size')
print(f"File size: {file_size} bytes")
# Download file
response = requests.get("http://localhost:8000/download",
headers=headers, params=params, stream=True)
with open("downloaded_file.xlsx", "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)# HEAD request for metadata
curl -I -H "Authorization: Bearer your_token" \
"http://localhost:8000/download?host=ftp.example.com&user=demo&password=password&path=/data/report.xlsx&protocol=ftps"
# Download file
curl -H "Authorization: Bearer your_token" \
-o downloaded_file.xlsx \
"http://localhost:8000/download?host=ftp.example.com&user=demo&password=password&path=/data/report.xlsx&protocol=ftps"# Check degraded mode
curl "http://localhost:8000/health" | jq '.degraded_mode'
# Auto-optimization for large files
curl -H "Authorization: Bearer your_token" \
-I "http://localhost:8000/download?path=/large_file.zip" \
| grep "X-Auto-Chunk-Tuned"# List tokens with security analysis
python admin_utils.py tokens list
# Generate new token
python admin_utils.py tokens generate --length 32 --client "Power BI"
# Validate token
python admin_utils.py tokens validate your_token_here# Full system diagnostics
python admin_utils.py monitor health
# JSON output for automation
python admin_utils.py monitor health --json# Clean up old temp files
python admin_utils.py maintenance cleanup --max-age-hours 24
# Validate configuration
python admin_utils.py maintenance validate
# Backup configuration
python admin_utils.py maintenance backup --dir backups{
"status": "healthy",
"temp_dir": "./temp",
"temp_dir_exists": true,
"temp_dir_writable": true,
"active_tokens": 3,
"protocols_available": ["ftp", "ftps", "sftp"],
"rate_limit_enabled": true
}- β Temp directory status
- β Log rotation configuration
- β Settings validation
- β Token security analysis
- β Performance settings
# Comprehensive testing
python test_api.py
# Tests include:
# - Basic endpoints
# - Authentication and tokens
# - Parameter validation
# - Rate limiting
# - CORS headers
# - SFTP support
# - Real file downloads# Build and run
docker-compose up -d
# Set tokens via environment
docker-compose exec ftp-bridge python admin_utils.py tokens generate# docker-compose.prod.yml
services:
ftp-bridge:
environment:
- FTP_BRIDGE_TOKEN_POWERBI=${POWERBI_TOKEN}
- FTP_BRIDGE_CORS_ORIGINS=https://app.powerbi.com
- FTP_BRIDGE_RATE_LIMIT_REQUESTS=50
- FTP_BRIDGE_USE_FTPS=truelet
// Settings
ApiUrl = "http://your-server:8000/download",
ApiToken = "your_secure_token_here",
// FTP Parameters
FtpHost = "ftp.your-company.com",
FtpUser = "your_user",
FtpPassword = "your_password",
FilePath = "/reports/monthly_data.xlsx",
// Build URL
Url = ApiUrl & "?" &
"host=" & FtpHost &
"&user=" & FtpUser &
"&password=" & FtpPassword &
"&path=" & FilePath &
"&file=data.xlsx" &
"&protocol=sftp",
// Authorization headers
Headers = [
#"Authorization" = "Bearer " & ApiToken,
#"Content-Type" = "application/octet-stream"
],
// Download file
Response = Web.Contents(Url, [Headers=Headers]),
// Process Excel file
Excel = Excel.Workbook(Response),
Sheet = Excel[Data]{[Item="Sheet1",Kind="Sheet"]}[Data]
in
Sheet
-
Add domain to CORS:
FTP_BRIDGE_CORS_ORIGINS=https://app.powerbi.com,https://msit.powerbi.com
-
Use HTTPS in production
-
Set up auto-refresh with appropriate limits
β CRITICAL ERROR: No tokens found!
# Solution:
export FTP_BRIDGE_TOKEN_CLIENT1=$(python -c 'import secrets; print(secrets.token_hex(16))')β οΈ CORS allows any domains in production
# Solution:
FTP_BRIDGE_CORS_ORIGINS=https://app.powerbi.com,https://your-domain.com{"detail": "Rate limit exceeded: 100 per 3600 seconds"}Solution: Increase limits or optimize request frequency
β οΈ SFTP support unavailable. Install paramiko.
# Solution:
pip install paramikoβ Port 8000 already in use
# Solution:
FTP_BRIDGE_PORT=8001# Full diagnostics
python admin_utils.py monitor health
# Check configuration
python admin_utils.py maintenance validate
# API testing
python test_api.pyMIT License - see LICENSE for details
- Documentation: API Docs
- Issues: GitHub Issues
- Security: Report vulnerabilities via private channels
- β¨ Pydantic v2 BaseSettings for configuration
- π Hardcoded tokens removed
- β‘ Rate limiting with slowapi
- π Secure CORS for production
- π SFTP support via paramiko
- π Log rotation with RotatingFileHandler
- π οΈ Advanced admin utilities
- π§ͺ Comprehensive security testing
- π Basic FTP Bridge functionality
- π‘ File streaming via FastAPI
- π Simple token authentication
- π Automatic API documentation
FTP Bridge v2.0.0 - A professional solution for secure FTP/SFTP integration with analytics systems π