An open API service indexing awesome lists of open source software.

https://github.com/gilbertrios/azure-appservice-logging-middleware

Terraform infrastructure and C# middleware for obfuscating sensitive data in Azure App Insights logs
https://github.com/gilbertrios/azure-appservice-logging-middleware

app-insights appservice azure csharp data-masking dotnet logging middleware security terraform

Last synced: 2 months ago
JSON representation

Terraform infrastructure and C# middleware for obfuscating sensitive data in Azure App Insights logs

Awesome Lists containing this project

README

          

# Azure App Service Logging Middleware

[![.NET](https://img.shields.io/badge/.NET-9.0-512BD4?logo=dotnet)](https://dotnet.microsoft.com/)
[![Azure](https://img.shields.io/badge/Azure-App%20Service-0078D4?logo=microsoft-azure)](https://azure.microsoft.com/en-us/services/app-service/)
[![Application Insights](https://img.shields.io/badge/Application%20Insights-Enabled-00BCF2?logo=microsoft-azure)](https://azure.microsoft.com/en-us/services/monitor/)
[![License](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)

A production-ready ASP.NET Core minimal API showcasing **automatic sensitive data obfuscation** in logs with Azure Application Insights integration. Built with clean modular architecture for easy microservice extraction.

## ๐ŸŽฏ Key Features

- **Smart Obfuscation Middleware** - Automatically redacts sensitive data (credit cards, passwords, tokens) from logs before they reach Application Insights
- **Azure Application Insights Integration** - Seamless telemetry with custom properties and structured logging
- **Modular Architecture** - Self-contained modules (Orders, Payments) ready for microservice extraction
- **.NET 9 Minimal APIs** - Fast, lightweight, modern ASP.NET Core
- **Auto-Discovery** - Modules automatically registered via reflection
- **Configurable** - Control obfuscation patterns via `appsettings.json`
- **OpenAPI/Swagger** - Full API documentation out of the box
- **Production-Ready** - Includes health checks, structured logging, and comprehensive testing

## ๐ŸŒŸ What This Repo Demonstrates

### Infrastructure as Code (IaC)
- โœ… Terraform modules and environments
- โœ… Azure resource provisioning
- โœ… Infrastructure versioning and state management

### DevOps & CI/CD
- โœ… 7-stage automated deployment pipeline
- โœ… Blue-green deployment with dual rollback strategies
- โœ… Automated testing (regression + smoke tests)
- โœ… PR validation with Terraform plan preview
- โœ… Reusable workflows for code reuse
- โœ… Auto rollback on production failures
- โœ… Manual rollback for on-demand recovery

### Development Best Practices
- โœ… Modular architecture (Orders, Payments modules)
- โœ… Custom middleware (obfuscation)
- โœ… Clean code and SOLID principles
- โœ… Modern .NET 9.0 patterns

### Cloud & Observability
- โœ… Azure App Service deployment slots
- โœ… Application Insights integration
- โœ… Security-first approach (data obfuscation)
- โœ… Health checks and monitoring

## ๐Ÿ› ๏ธ Tech Stack

### Application
- **.NET 9.0** - ASP.NET Core minimal APIs
- **C# 13** - Records, pattern matching, modern features
- **Application Insights** - Azure monitoring and telemetry
- **Swagger/OpenAPI** - API documentation

### Infrastructure & DevOps
- **Terraform** - Infrastructure as Code
- **Azure App Service** - Linux-based hosting
- **GitHub Actions** - CI/CD automation
- **Bash Scripts** - Deployment utilities

## ๐Ÿ—๏ธ Repository Architecture

```
azure-appservice-logging-middleware/
โ”œโ”€โ”€ .github/
โ”‚ โ””โ”€โ”€ workflows/
โ”‚ โ”œโ”€โ”€ deploy-blue-green.yml # 7-stage deployment pipeline (auto rollback)
โ”‚ โ”œโ”€โ”€ manual-rollback.yml # On-demand rollback workflow
โ”‚ โ”œโ”€โ”€ ci-pr-validation.yml # PR validation (build + terraform)
โ”‚ โ””โ”€โ”€ _build-app.yml # Reusable build workflow
โ”‚
โ”œโ”€โ”€ app/ # .NET 9.0 Application
โ”‚ โ”œโ”€โ”€ Infrastructure/ # Module pattern implementation
โ”‚ โ”œโ”€โ”€ Middleware/ # Obfuscation middleware
โ”‚ โ”œโ”€โ”€ Modules/ # Orders & Payments modules
โ”‚ โ”œโ”€โ”€ Properties/ # launchSettings.json
โ”‚ โ””โ”€โ”€ Program.cs
โ”‚
โ”œโ”€โ”€ tests/ # Test Projects
โ”‚ โ”œโ”€โ”€ AzureAppServiceLoggingMiddleware.UnitTests/
โ”‚ โ”‚ โ””โ”€โ”€ Middleware/
โ”‚ โ”‚ โ””โ”€โ”€ ObfuscationMiddlewareTests.cs
โ”‚ โ””โ”€โ”€ AzureAppServiceLoggingMiddleware.IntegrationTests/
โ”‚ โ””โ”€โ”€ ObfuscationMiddlewareIntegrationTests.cs
โ”‚
โ”œโ”€โ”€ infrastructure/ # Terraform IaC
โ”‚ โ”œโ”€โ”€ terraform/
โ”‚ โ”‚ โ”œโ”€โ”€ environments/
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ dev/ # Dev environment config
โ”‚ โ”‚ โ””โ”€โ”€ modules/
โ”‚ โ”‚ โ””โ”€โ”€ app-service/ # App Service with slots
โ”‚ โ””โ”€โ”€ scripts/
โ”‚
โ””โ”€โ”€ docs/ # Documentation
```

See [Repository Structure](docs/repository-structure.md) for detailed breakdown.

**Module Pattern Benefits:**

Each module is self-contained and follows these principles:
- **Self-contained** - All domain code in one folder
- **Testable** - Clear boundaries and interfaces
- **Discoverable** - Auto-registered via reflection
- **Extractable** - Ready for microservice split

See [Module Pattern Guide](docs/module-pattern.md) for implementation details and best practices.

## ๐Ÿš€ CI/CD Pipeline

Automated 7-stage blue-green deployment pipeline with comprehensive rollback strategies.

```
Build โ†’ Terraform โ†’ Deploy to Green โ†’ Test Green โ†’ Swap โ†’ Smoke Test โ†’ Auto Rollback (if needed)
```

**Key Features:**
- โœ… Zero-downtime deployment with blue-green slots
- โœ… Automated rollback if production smoke tests fail
- โœ… Manual rollback workflow for post-deployment issues
- โœ… PR validation with Terraform plan preview
- โœ… Comprehensive testing before production swap

**Triggers:**
- Push to `main` with changes to `app/**`, `infrastructure/**`, or `.github/workflows/**`
- Pull requests run CI validation only (no deployment)

See [CI/CD Pipeline Documentation](docs/cicd-pipeline.md) for complete details on deployment stages, rollback strategies, and troubleshooting.

## ๐Ÿ’ป Quick Start

### Run Application Locally

```bash
# Clone the repository
git clone https://github.com/gilbertrios/azure-appservice-logging-middleware.git
cd azure-appservice-logging-middleware/app

# Run the application
dotnet run
```

The API will be available at:
- **HTTPS**: `https://localhost:5001`
- **Swagger UI**: `https://localhost:5001/swagger`

**Optional:** Customize obfuscation settings in `app/appsettings.json` - see [Configuration Guide](docs/configuration.md)

### Deploy to Azure

```bash
# 1. Configure Azure credentials (see docs/setup-guide.md)
# 2. Push to main branch
git push origin main

# The 7-stage pipeline will:
# โœ… Build application
# โœ… Provision infrastructure (Terraform)
# โœ… Deploy to green slot
# โœ… Run regression tests on green
# โœ… Swap to production
# โœ… Run smoke tests on production
# โœ… Auto rollback if smoke tests fail
```

## ๐Ÿ”’ Obfuscation Middleware in Action

The middleware automatically detects and obfuscates sensitive properties in request/response bodies:

### Example Request
```bash
curl -X POST http://localhost:5000/api/payments/process \
-H "Content-Type: application/json" \
-d '{
"orderId": 1,
"amount": 299.99,
"creditCard": "1234-5678-9012-3456",
"cvv": "123",
"token": "secret-api-key"
}'
```

### Console Output (Obfuscated)
```json
{
"orderId": 1,
"amount": 299.99,
"creditCard": "***REDACTED***",
"cvv": "***REDACTED***",
"token": "***REDACTED***"
}
```

โœ… **Actual API response remains unchanged** - only logs are obfuscated!

## ๐Ÿ“ก API Endpoints

Explore the API using Swagger UI at `/swagger` when running locally, or view the full endpoint documentation in the [Application README](app/README.md).

**Modules:**
- **Orders** - Order management and status tracking
- **Payments** - Payment processing and refunds
- **Health Check** - API health status

## โš™๏ธ Configuration

Customize obfuscation behavior via `app/appsettings.json`:

```json
{
"ObfuscationMiddleware": {
"Enabled": true,
"ObfuscationMask": "***REDACTED***",
"SensitiveProperties": ["password", "creditCard", "cvv", "ssn", "apiKey", "token"]
}
}
```

**Key features:**
- Case-insensitive property matching
- Recursive JSON traversal (nested objects/arrays)
- Configurable mask pattern and sensitive property list

See [Configuration Guide](docs/configuration.md) for complete options, Application Insights setup, environment-specific settings, and user secrets.

## โ˜๏ธ Azure Infrastructure

Uses Terraform to provision Azure App Service with blue-green deployment slots for zero-downtime releases.

**Resources provisioned:**
- App Service Plan (Linux, S1) with deployment slots
- App Service (production + green slot for zero-downtime deployments)
- Application Insights for telemetry and monitoring
- Log Analytics Workspace

**Deploy infrastructure:**
```bash
cd infrastructure/terraform/environments/dev
terraform init
terraform apply
```

See [Infrastructure README](infrastructure/README.md) for complete details on resources, configuration, and Terraform modules.

## ๐Ÿงช Testing

Run the test suite to verify functionality:

```bash
# Run all tests
dotnet test

# Run only unit tests (fast)
dotnet test --filter "Category=Unit"

# Run only integration tests
dotnet test --filter "Category=Integration"
```

**Test Coverage:**
- Unit tests for ObfuscationMiddleware logic and edge cases
- Integration tests for full API and middleware pipeline
- Automated execution in CI/CD pipeline

See [Testing Guide](docs/testing-guide.md) for detailed test documentation, manual testing with cURL/Swagger, and coverage reports.

## ๐Ÿ“š Documentation

### Architecture & Design
- [Repository Structure](docs/repository-structure.md) - Folder organization
- [Module Pattern Overview](docs/module-pattern.md) - Modular architecture
- [Microservice Split Criteria](docs/microservice-split-criteria.md) - When to extract
- [MVC vs Minimal API Pipeline](docs/mvc-vs-minimal-api-pipeline.md) - Request pipeline internals

### Infrastructure & DevOps
- [Infrastructure Guide](infrastructure/README.md) - Terraform and Azure resources
- [Setup Guide](docs/setup-guide.md) - Deploy to Azure step-by-step
- [CI/CD Pipeline](docs/cicd-pipeline.md) - Deployment pipeline and rollback strategies
- [App Service vs Functions](docs/app-service-vs-functions.md) - Service comparison

### Application
- [Application README](app/README.md) - Run and develop locally
- [Testing Guide](docs/testing-guide.md) - Test strategy, commands, and coverage
- [Configuration Guide](docs/configuration.md) - Application settings and options

## ๐Ÿ”„ Migration Path

Ready for microservice extraction. See [Microservice Split Criteria](docs/microservice-split-criteria.md) for detailed guidance.

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## ๐Ÿ“ž Support & Connect

- ๐Ÿ’ผ LinkedIn: [Connect with me](https://linkedin.com/in/gilbert-rios-22586918)
- ๐Ÿ“ง Email: gilbertrios@hotmail.com
- ๐Ÿ’ก GitHub: [@gilbertrios](https://github.com/gilbertrios)

## ๐ŸŽ“ Quick Links

- **[Setup Guide](docs/setup-guide.md)** - Deploy to Azure in 10 steps
- **[Project Summary](docs/project-summary.md)** - Overview and key decisions
- **[Repository Structure](docs/repository-structure.md)** - Folder organization

---

โญ **Star this repo if you find it useful for learning or reference!**