Files
ColaFlow/colaflow-api/tests/Modules/Mcp/ColaFlow.Modules.Mcp.Tests/Domain/McpApiKeyTests.cs
Yaojia Wang 0857a8ba2a feat(backend): Implement MCP API Key Management System (Story 5.2)
Implemented comprehensive API Key authentication and management system
for MCP Server to ensure only authorized AI agents can access ColaFlow.

## Domain Layer
- Created McpApiKey aggregate root with BCrypt password hashing
- Implemented ApiKeyPermissions value object (read/write, resource/tool filtering)
- Added ApiKeyStatus enum (Active, Revoked)
- Created domain events (ApiKeyCreatedEvent, ApiKeyRevokedEvent)
- API key format: cola_<36 random chars> (cryptographically secure)
- Default expiration: 90 days

## Application Layer
- Implemented McpApiKeyService with full CRUD operations
- Created DTOs for API key creation, validation, and updates
- Validation logic: hash verification, expiration check, IP whitelist
- Usage tracking: last_used_at, usage_count

## Infrastructure Layer
- Created McpDbContext with PostgreSQL configuration
- EF Core entity configuration with JSONB for permissions/IP whitelist
- Implemented McpApiKeyRepository with prefix-based lookup
- Database migration: mcp_api_keys table with indexes
- Created McpApiKeyAuthenticationMiddleware for API key validation
- Middleware validates Authorization: Bearer <api_key> header

## API Layer
- Created McpApiKeysController with REST endpoints:
  - POST /api/mcp/keys - Create API Key (returns plain key once!)
  - GET /api/mcp/keys - List tenant's API Keys
  - GET /api/mcp/keys/{id} - Get API Key details
  - PATCH /api/mcp/keys/{id}/metadata - Update name/description
  - PATCH /api/mcp/keys/{id}/permissions - Update permissions
  - DELETE /api/mcp/keys/{id} - Revoke API Key
- Requires JWT authentication (not API key auth)

## Testing
- Created 17 unit tests for McpApiKey entity
- Created 7 unit tests for ApiKeyPermissions value object
- All 49 tests passing (including existing MCP tests)
- Test coverage > 80% for Domain layer

## Security Features
- BCrypt hashing with work factor 12
- API key shown only once at creation (never logged)
- Key prefix lookup for fast validation (indexed)
- Multi-tenant isolation (tenant_id filter)
- IP whitelist support
- Permission scopes (read/write, resources, tools)
- Automatic expiration after 90 days

## Database Schema
Table: mcp.mcp_api_keys
- Indexes: key_prefix (unique), tenant_id, tenant_user, expires_at, status
- JSONB columns for permissions and IP whitelist
- Soft delete via revoked_at

## Integration
- Updated Program.cs to register MCP module with configuration
- Added MCP DbContext migration in development mode
- Authentication middleware runs before MCP protocol handler

Changes:
- Created 31 new files (2321+ lines)
- Domain: 6 files (McpApiKey, events, repository, value objects)
- Application: 9 files (service, DTOs)
- Infrastructure: 8 files (DbContext, repository, middleware, migration)
- API: 1 file (McpApiKeysController)
- Tests: 2 files (17 + 7 unit tests)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-08 18:40:56 +01:00

211 lines
6.1 KiB
C#

using ColaFlow.Modules.Mcp.Domain.Entities;
using ColaFlow.Modules.Mcp.Domain.ValueObjects;
using FluentAssertions;
using Xunit;
namespace ColaFlow.Modules.Mcp.Tests.Domain;
public class McpApiKeyTests
{
[Fact]
public void Create_ShouldGenerateValidApiKey()
{
// Arrange
var name = "Test API Key";
var tenantId = Guid.NewGuid();
var userId = Guid.NewGuid();
var permissions = ApiKeyPermissions.ReadOnly();
// Act
var (apiKey, plainKey) = McpApiKey.Create(name, tenantId, userId, permissions);
// Assert
apiKey.Should().NotBeNull();
apiKey.Name.Should().Be(name);
apiKey.TenantId.Should().Be(tenantId);
apiKey.UserId.Should().Be(userId);
apiKey.Status.Should().Be(ApiKeyStatus.Active);
plainKey.Should().StartWith("cola_");
plainKey.Length.Should().Be(41); // "cola_" (5) + 36 random chars
apiKey.KeyPrefix.Should().Be(plainKey.Substring(0, 12));
}
[Fact]
public void Create_ShouldHashApiKey()
{
// Arrange
var name = "Test API Key";
var tenantId = Guid.NewGuid();
var userId = Guid.NewGuid();
var permissions = ApiKeyPermissions.ReadOnly();
// Act
var (apiKey, plainKey) = McpApiKey.Create(name, tenantId, userId, permissions);
// Assert
apiKey.KeyHash.Should().NotBeNullOrEmpty();
apiKey.KeyHash.Should().NotBe(plainKey); // Hash should not equal plain key
apiKey.VerifyKey(plainKey).Should().BeTrue(); // But should verify correctly
}
[Fact]
public void Create_WithInvalidName_ShouldThrowArgumentException()
{
// Arrange
var tenantId = Guid.NewGuid();
var userId = Guid.NewGuid();
var permissions = ApiKeyPermissions.ReadOnly();
// Act & Assert
Assert.Throws<ArgumentException>(() =>
McpApiKey.Create("", tenantId, userId, permissions));
}
[Fact]
public void Create_WithInvalidTenantId_ShouldThrowArgumentException()
{
// Arrange
var userId = Guid.NewGuid();
var permissions = ApiKeyPermissions.ReadOnly();
// Act & Assert
Assert.Throws<ArgumentException>(() =>
McpApiKey.Create("Test", Guid.Empty, userId, permissions));
}
[Fact]
public void VerifyKey_WithCorrectKey_ShouldReturnTrue()
{
// Arrange
var (apiKey, plainKey) = McpApiKey.Create("Test", Guid.NewGuid(), Guid.NewGuid(), ApiKeyPermissions.ReadOnly());
// Act
var result = apiKey.VerifyKey(plainKey);
// Assert
result.Should().BeTrue();
}
[Fact]
public void VerifyKey_WithIncorrectKey_ShouldReturnFalse()
{
// Arrange
var (apiKey, _) = McpApiKey.Create("Test", Guid.NewGuid(), Guid.NewGuid(), ApiKeyPermissions.ReadOnly());
// Act
var result = apiKey.VerifyKey("cola_wrongkey123456789012345678901234");
// Assert
result.Should().BeFalse();
}
[Fact]
public void Revoke_ShouldMarkAsRevoked()
{
// Arrange
var (apiKey, _) = McpApiKey.Create("Test", Guid.NewGuid(), Guid.NewGuid(), ApiKeyPermissions.ReadOnly());
var revokedBy = Guid.NewGuid();
// Act
apiKey.Revoke(revokedBy);
// Assert
apiKey.Status.Should().Be(ApiKeyStatus.Revoked);
apiKey.RevokedAt.Should().NotBeNull();
apiKey.RevokedBy.Should().Be(revokedBy);
}
[Fact]
public void Revoke_WhenAlreadyRevoked_ShouldThrowInvalidOperationException()
{
// Arrange
var (apiKey, _) = McpApiKey.Create("Test", Guid.NewGuid(), Guid.NewGuid(), ApiKeyPermissions.ReadOnly());
apiKey.Revoke(Guid.NewGuid());
// Act & Assert
Assert.Throws<InvalidOperationException>(() => apiKey.Revoke(Guid.NewGuid()));
}
[Fact]
public void IsExpired_WhenNotExpired_ShouldReturnFalse()
{
// Arrange
var (apiKey, _) = McpApiKey.Create("Test", Guid.NewGuid(), Guid.NewGuid(), ApiKeyPermissions.ReadOnly(), expirationDays: 90);
// Act
var result = apiKey.IsExpired();
// Assert
result.Should().BeFalse();
}
[Fact]
public void IsValid_WhenActiveAndNotExpired_ShouldReturnTrue()
{
// Arrange
var (apiKey, _) = McpApiKey.Create("Test", Guid.NewGuid(), Guid.NewGuid(), ApiKeyPermissions.ReadOnly());
// Act
var result = apiKey.IsValid();
// Assert
result.Should().BeTrue();
}
[Fact]
public void IsValid_WhenRevoked_ShouldReturnFalse()
{
// Arrange
var (apiKey, _) = McpApiKey.Create("Test", Guid.NewGuid(), Guid.NewGuid(), ApiKeyPermissions.ReadOnly());
apiKey.Revoke(Guid.NewGuid());
// Act
var result = apiKey.IsValid();
// Assert
result.Should().BeFalse();
}
[Fact]
public void RecordUsage_ShouldIncrementUsageCount()
{
// Arrange
var (apiKey, _) = McpApiKey.Create("Test", Guid.NewGuid(), Guid.NewGuid(), ApiKeyPermissions.ReadOnly());
var initialCount = apiKey.UsageCount;
// Act
apiKey.RecordUsage();
// Assert
apiKey.UsageCount.Should().Be(initialCount + 1);
apiKey.LastUsedAt.Should().NotBeNull();
}
[Fact]
public void IsIpAllowed_WithNoWhitelist_ShouldReturnTrue()
{
// Arrange
var (apiKey, _) = McpApiKey.Create("Test", Guid.NewGuid(), Guid.NewGuid(), ApiKeyPermissions.ReadOnly());
// Act
var result = apiKey.IsIpAllowed("192.168.1.1");
// Assert
result.Should().BeTrue();
}
[Fact]
public void IsIpAllowed_WithWhitelist_ShouldValidateCorrectly()
{
// Arrange
var permissions = ApiKeyPermissions.ReadOnly();
var ipWhitelist = new List<string> { "192.168.1.1", "10.0.0.1" };
var (apiKey, _) = McpApiKey.Create("Test", Guid.NewGuid(), Guid.NewGuid(), permissions, ipWhitelist: ipWhitelist);
// Act & Assert
apiKey.IsIpAllowed("192.168.1.1").Should().BeTrue();
apiKey.IsIpAllowed("10.0.0.1").Should().BeTrue();
apiKey.IsIpAllowed("172.16.0.1").Should().BeFalse();
}
}