feat(enterprise): add auth TLS and recovery hardening
This commit is contained in:
@@ -248,6 +248,62 @@ const server = new SmartdbServer({
|
||||
persistPath: './data/snapshot.json',
|
||||
persistIntervalMs: 30000, // Save every 30s
|
||||
});
|
||||
|
||||
// TLS transport for TCP mode
|
||||
const tlsServer = new SmartdbServer({
|
||||
port: 27017,
|
||||
tls: {
|
||||
enabled: true,
|
||||
certPath: './certs/server.pem',
|
||||
keyPath: './certs/server.key',
|
||||
// caPath: './certs/client-ca.pem',
|
||||
// requireClientCert: true, // Enables mTLS client certificate checks
|
||||
},
|
||||
});
|
||||
|
||||
// SCRAM-SHA-256 authentication
|
||||
const secureServer = new SmartdbServer({
|
||||
port: 27017,
|
||||
auth: {
|
||||
enabled: true,
|
||||
usersPath: './data/smartdb-users.json', // Optional: persists derived SCRAM credentials
|
||||
users: [
|
||||
{
|
||||
username: 'root',
|
||||
password: 'change-me',
|
||||
database: 'admin',
|
||||
roles: ['root'],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
When `auth.enabled` is true, protected commands require successful SCRAM-SHA-256 authentication through the official MongoDB driver:
|
||||
|
||||
```typescript
|
||||
const client = new MongoClient('mongodb://root:change-me@127.0.0.1:27017/admin?authSource=admin', {
|
||||
directConnection: true,
|
||||
});
|
||||
await client.connect();
|
||||
```
|
||||
|
||||
TLS is available for TCP listeners. `getConnectionUri()` includes `?tls=true` when TLS is enabled; pass the trusted CA to the MongoDB driver with `tlsCAFile`, `ca`, or `secureContext`.
|
||||
|
||||
Authentication verifies SCRAM credentials, denies unauthenticated commands, and enforces command-level built-in roles for supported operations.
|
||||
|
||||
Supported built-in role names are `root`, `read`, `readWrite`, `dbAdmin`, `userAdmin`, `clusterMonitor`, plus `readAnyDatabase`, `readWriteAnyDatabase`, `dbAdminAnyDatabase`, and `userAdminAnyDatabase`. When `usersPath` is set, SmartDB persists SCRAM credential material atomically and does not store plaintext passwords.
|
||||
|
||||
Basic user management commands are available for authenticated users with `root` or `userAdmin` privileges:
|
||||
|
||||
```typescript
|
||||
await client.db('admin').command({
|
||||
createUser: 'reader',
|
||||
pwd: 'readpass',
|
||||
roles: [{ role: 'read', db: 'myapp' }],
|
||||
});
|
||||
|
||||
await client.db('admin').command({ usersInfo: 'reader' });
|
||||
```
|
||||
|
||||
#### Methods & Properties
|
||||
|
||||
Reference in New Issue
Block a user