Overview
Introduction
This document provides a comprehensive overview of the Ravenxcope.Backend ASP.NET Core backend service. The service is the central API for the RavenXcope defense center platform, responsible for authentication, organization and user management, role-based access control, sensor lifecycle orchestration, and analytics integration. It follows a layered architecture with clear separation of concerns across API, Application, Domain, and Infrastructure layers.
Architecture Pattern
The backend follows a Layered Architecture with thin controllers handling API transport, dedicated service classes handling business logic, entities representing the data model, and repositories under infrastructure handling persistence. External service integrations (Redis, InfluxDB, OpenSearch, Ansible) are wrapped in infrastructure service classes. Cross-cutting concerns like circuit breaking, rate limiting, and global exception handling are implemented via middleware and shared services.
Project Structure
Technology Stack
| Category | Technology | Version | Purpose |
|---|---|---|---|
| Runtime | .NET | 10.0 | Application runtime |
| Framework | ASP.NET Core | 10.0 | Web API framework |
| ORM | Entity Framework Core | 9.0.1 | Database access and migrations |
| Database | PostgreSQL (Npgsql) | 9.0.4 | Primary relational database |
| Caching | Redis (StackExchange.Redis) | 2.10.1 | Token blacklist & session storage |
| Search/Analytics | OpenSearch | 1.8.0 | Log and analytics retrieval |
| Time-Series | InfluxDB | 4.18.0 | Metrics and heartbeat data |
| Password Hashing | BCrypt.Net-Next | 4.0.3 | Secure password hashing |
| Logging | Serilog | 9.0.0 | Structured logging |
| JWT | Microsoft.AspNetCore.Authentication.JwtBearer | 10.0.0 | Token auth |
| API Docs | Swashbuckle (Swagger) | 6.9.0 | OpenAPI documentation |
| gRPC | Grpc.AspNetCore | 2.70.0 | Sensor healthcheck endpoint |
Key Design Principles
-
Fail-Fast on Configuration: The service validates all required configuration keys at startup and throws immediately if any are missing or contain placeholder values.
-
Fail-Fast on Dependencies: Before serving any traffic, the service verifies connectivity to PostgreSQL, Redis, InfluxDB, and OpenSearch using retry-based health checks.
-
Startup Composition via Extensions: All startup responsibilities (service registration, middleware pipeline, health checks, migrations) are isolated in dedicated extension methods in the
Extensions/directory, keepingProgram.csclean and declarative. -
Typed Options Pattern: All configuration sections are bound to strongly-typed options classes (e.g.,
PostgresqlSettingsOptions,JwtSettingsOptions,InfluxDbOptions) usingIOptions<T>, avoiding scattered string-based configuration reads. -
Automatic Timestamp Management: The
ApplicationDbContextoverridesSaveChangesandSaveChangesAsyncto automatically setCreatedAtandUpdatedAttimestamps on all tracked entities. -
Seed Permissions via Migrations: Core permissions (CRUD for Users, Sensors, Roles, Assets, and Organization edit) are seeded through EF Core
HasDatainOnModelCreating, ensuring they exist in every environment. -
JWT with Token Blacklisting and Refresh Tokens: Authentication uses symmetric JWT tokens with an
OnTokenValidatedevent that checks Redis for token revocation. Refresh tokens are issued on login/register and stored in Redis with 30-day TTL. -
Organization-Scoped Access: Users, sensors, assets, and roles are scoped to organizations. Controllers extract the organization ID from JWT claims to filter data access.
-
Thin Controllers with Service Layer: All controllers delegate business logic to dedicated service classes (e.g.,
IAuthService,ISensorsService,IAssetService). Controllers handle only HTTP transport concerns. -
Standardized Response Envelope: All endpoints return responses via
ApiEnvelope.Success()/ApiEnvelope.Error(), providing a consistent{ success, data, errors, traceId }contract. -
Global Exception Handling: A global exception handler middleware (
GlobalExceptionHandlingExtensions) catches unhandled exceptions and maps them to appropriate HTTP status codes viaApiExceptionMapper, eliminating try-catch blocks in controllers. -
Rate Limiting: Auth endpoints (login, register) are protected by fixed-window rate limiters to prevent brute-force attacks.
-
Resilient External Calls: The
ResilientHttpServiceprovides circuit breaker and retry patterns for external HTTP calls (e.g., Ansible), preventing cascading failures.
Startup Guarantees
On startup, the service performs the following steps in strict order:
- Initializes Serilog structured logging from appsettings
- Validates all required configuration keys (throws on missing/placeholder values)
- Emits a warning if the JWT secret is shorter than 32 characters
- Registers all DI services, typed options, and middleware dependencies
- Establishes the Redis connection (eagerly connects during DI registration)
- Executes dependency health checks with retry logic for PostgreSQL, Redis, InfluxDB, and OpenSearch
- Applies EF Core database migrations automatically when
Database:AutoMigrateistrue - Configures the HTTP middleware pipeline and maps controllers + gRPC endpoints
- Starts accepting HTTP and gRPC traffic
Deployment Model
The backend is designed to run in containerized stacks as part of the defense center Docker Compose deployment. It expects environment variables (using ASP.NET Core's double-underscore convention) to populate appsettings.json placeholders at runtime.
- Docker exposed port:
5144 - Entry point:
dotnet Ravenxcope.Backend.dll - Base image:
mcr.microsoft.com/dotnet/aspnet:10.0 - Build image:
mcr.microsoft.com/dotnet/sdk:10.0