Service Mesh Package
Secure service mesh implementation for SecureStack with hybrid encryption and mutual authentication.
Features
🔐 Hybrid Encryption
- RSA-4096 for key exchange and session establishment
- AES-256-GCM for efficient message encryption
- HMAC-SHA256 for message integrity verification
- Key persistence and rotation support
🎫 JWT Authentication
- Mutual authentication between services
- Token-based authorization
- Configurable expiration times
- Secret rotation capabilities
🔄 Session Management
- Automatic session creation and tracking
- Timeout handling (default 1 hour)
- Message count tracking
- Session cleanup
📡 Service Discovery
- Static service registry
- Dynamic service lookup
- Health check support
- Service metadata management
Installation
npm install @lemur-bookstores/secure-stack-mesh
Quick Start
import { SecureMesh } from '@lemur-bookstores/secure-stack-mesh';
const mesh = new SecureMesh({
serviceId: 'my-service',
port: 50051,
security: {
rsaKeySize: 4096,
aesKeySize: 256,
},
discovery: {
services: [{ id: 'other-service', host: 'localhost', port: 50052 }],
},
});
await mesh.initialize();
const client = mesh.connect('other-service');
const response = await client.call('methodName', {
data: 'payload',
});
console.log(response);
const stats = mesh.getStats();
console.log('Active sessions:', stats.activeSessions);
await mesh.cleanup();
Architecture
Components
CryptoManager
Handles all cryptographic operations:
- RSA key pair generation and management
- AES session key generation
- Hybrid encryption/decryption
- HMAC signature generation and verification
const crypto = new CryptoManager({
rsaKeySize: 4096,
aesKeySize: 256,
});
await crypto.initialize();
const encrypted = crypto.encrypt(data, recipientPublicKey);
const decrypted = crypto.decrypt(encrypted, senderPublicKey);
JWTManager
Manages authentication tokens:
- Token generation with claims
- Token verification
- Secret rotation
const jwt = new JWTManager({ secret: 'my-secret' });
const token = jwt.generateToken(serviceId, sessionId, '1h');
const payload = jwt.verifyToken(token);
SessionManager
Tracks active sessions:
- Session creation and lookup
- Timeout management
- Message tracking
const sessions = new SessionManager({ timeout: 3600000 });
const session = sessions.createSession('service1', 'service2');
sessions.trackMessage(session.id);
StaticDiscovery
Service registry implementation:
- Service registration
- Service lookup
- Health checks
const discovery = new StaticDiscovery();
discovery.register({
id: 'my-service',
host: 'localhost',
port: 50051,
publicKey: '<RSA-PUBLIC-KEY>',
});
const service = discovery.lookup('my-service');
Security Features
End-to-End Encryption
All messages are encrypted using a hybrid approach:
- Session Key Generation: AES-256 key generated for each session
- Key Exchange: Session key encrypted with recipient's RSA-4096 public key
- Message Encryption: Data encrypted with AES-256-GCM
- Integrity Check: HMAC-SHA256 signature for tamper detection
Mutual Authentication
Both parties verify each other's identity:
- JWT Tokens: Each request includes a JWT signed by sender
- Claims Verification: Service ID and session ID validated
- Expiration Checks: Tokens expire after configurable time
- Secret Rotation: Periodic secret changes for enhanced security
Configuration
interface MeshConfig {
serviceId: string;
port: number;
security?: {
rsaKeySize?: 2048 | 4096;
aesKeySize?: 128 | 192 | 256;
jwtSecret?: string;
jwtExpiration?: string;
sessionTimeout?: number;
};
discovery?: {
services: Array<{
id: string;
host: string;
port: number;
publicKey?: string;
}>;
};
}
API Reference
SecureMesh
initialize(): Promise<void>
Initialize cryptographic components and start the mesh.
connect(serviceId: string): SecureMeshClient
Create a client connection to another service.
getStats(): MeshStats
Get current mesh statistics (sessions, messages, services).
healthCheck(): Promise<HealthStatus>
Check mesh health status.
cleanup(): Promise<void>
Cleanup resources and close connections.
SecureMeshClient
call<TInput, TResult>(method: string, input: TInput): Promise<TResult>
Make an encrypted, authenticated call to the remote service.
Examples
See examples/basic/src/mesh-example.ts for a complete working example.
Performance
- Encryption Overhead: ~2-5ms per message (depends on payload size)
- Session Establishment: ~50-100ms (includes key exchange)
- JWT Verification: <1ms per token
- Max Throughput: ~10,000 messages/second per connection
Security Considerations
- Key Storage: Private keys are stored in memory and can be persisted to disk (ensure proper file permissions)
- Secret Management: Use environment variables for JWT secrets in production
- Session Timeouts: Configure appropriate timeouts based on your use case
- Key Rotation: Implement periodic key rotation for long-running services
- Network Security: Use TLS for transport layer security in production
Roadmap
License
MIT