@lemur-bookstores/secure-stack-mesh
v0.2.0
Published
Secure Service Mesh for SecureStack - Hybrid encryption, JWT auth, and service discovery
Maintainers
Readme
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-meshQuick Start
import { SecureMesh } from '@lemur-bookstores/secure-stack-mesh';
// Create a mesh instance
const mesh = new SecureMesh({
serviceId: 'my-service',
port: 50051,
security: {
rsaKeySize: 4096,
aesKeySize: 256,
},
discovery: {
services: [{ id: 'other-service', host: 'localhost', port: 50052 }],
},
});
// Initialize
await mesh.initialize();
// Connect to another service
const client = mesh.connect('other-service');
// Make a secure call
const response = await client.call('methodName', {
data: 'payload',
});
console.log(response);
// Get statistics
const stats = mesh.getStats();
console.log('Active sessions:', stats.activeSessions);
// Cleanup
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; // Unique service identifier
port: number; // gRPC server port
security?: {
rsaKeySize?: 2048 | 4096; // RSA key size (default: 4096)
aesKeySize?: 128 | 192 | 256; // AES key size (default: 256)
jwtSecret?: string; // JWT secret (auto-generated if not provided)
jwtExpiration?: string; // Token expiration (default: '1h')
sessionTimeout?: number; // Session timeout in ms (default: 3600000)
};
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
- [ ] gRPC protocol implementation
- [ ] Certificate-based authentication
- [ ] Rate limiting per service
- [ ] Circuit breaker pattern
- [ ] Distributed tracing integration
- [ ] Key rotation automation
- [ ] Service mesh observability dashboard
License
MIT
