@gouravniit/zero-trust-api-monitor 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +48 -0
- package/API_INTEGRATION_GUIDE.md +329 -0
- package/LICENSE +21 -0
- package/README.md +288 -0
- package/dist/analysis/baseline-engine.d.ts +57 -0
- package/dist/analysis/baseline-engine.d.ts.map +1 -0
- package/dist/analysis/baseline-engine.js +296 -0
- package/dist/analysis/baseline-engine.js.map +1 -0
- package/dist/collector/collector.d.ts +64 -0
- package/dist/collector/collector.d.ts.map +1 -0
- package/dist/collector/collector.js +224 -0
- package/dist/collector/collector.js.map +1 -0
- package/dist/config/config.d.ts +58 -0
- package/dist/config/config.d.ts.map +1 -0
- package/dist/config/config.js +64 -0
- package/dist/config/config.js.map +1 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +108 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/security-copilot.d.ts +66 -0
- package/dist/llm/security-copilot.d.ts.map +1 -0
- package/dist/llm/security-copilot.js +351 -0
- package/dist/llm/security-copilot.js.map +1 -0
- package/dist/security/throttle-engine.d.ts +75 -0
- package/dist/security/throttle-engine.d.ts.map +1 -0
- package/dist/security/throttle-engine.js +305 -0
- package/dist/security/throttle-engine.js.map +1 -0
- package/dist/store/feature-store.d.ts +84 -0
- package/dist/store/feature-store.d.ts.map +1 -0
- package/dist/store/feature-store.js +327 -0
- package/dist/store/feature-store.js.map +1 -0
- package/dist/tests/test-simulator.d.ts +2 -0
- package/dist/tests/test-simulator.d.ts.map +1 -0
- package/dist/tests/test-simulator.js +215 -0
- package/dist/tests/test-simulator.js.map +1 -0
- package/dist/types.d.ts +161 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +15 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/logger.d.ts +3 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +27 -0
- package/dist/utils/logger.js.map +1 -0
- package/package.json +78 -0
package/.env.example
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Server Configuration
|
|
2
|
+
PORT=3000
|
|
3
|
+
NODE_ENV=development
|
|
4
|
+
|
|
5
|
+
# Redis Configuration
|
|
6
|
+
REDIS_HOST=localhost
|
|
7
|
+
REDIS_PORT=6379
|
|
8
|
+
REDIS_PASSWORD=
|
|
9
|
+
|
|
10
|
+
# PostgreSQL Configuration
|
|
11
|
+
POSTGRES_HOST=localhost
|
|
12
|
+
POSTGRES_PORT=5432
|
|
13
|
+
POSTGRES_DB=api_monitoring
|
|
14
|
+
POSTGRES_USER=postgres
|
|
15
|
+
POSTGRES_PASSWORD=
|
|
16
|
+
|
|
17
|
+
# ML Service Configuration
|
|
18
|
+
ML_SERVICE_URL=http://localhost:5000
|
|
19
|
+
ML_INFERENCE_TIMEOUT=10000
|
|
20
|
+
|
|
21
|
+
# LLM Configuration
|
|
22
|
+
LLM_PROVIDER=openai
|
|
23
|
+
OPENAI_API_KEY=
|
|
24
|
+
ANTHROPIC_API_KEY=
|
|
25
|
+
|
|
26
|
+
# Security Thresholds
|
|
27
|
+
ANOMALY_THRESHOLD_ALERT=31
|
|
28
|
+
ANOMALY_THRESHOLD_THROTTLE=61
|
|
29
|
+
ANOMALY_THRESHOLD_QUARANTINE=81
|
|
30
|
+
ANOMALY_THRESHOLD_BLOCK=96
|
|
31
|
+
|
|
32
|
+
# Throttle Configuration
|
|
33
|
+
THROTTLE_LATENCY_MS=500
|
|
34
|
+
QUARANTINE_PAYLOAD_LIMIT=1000
|
|
35
|
+
STEP_UP_CHALLENGE_TIMEOUT=300000
|
|
36
|
+
|
|
37
|
+
# Feature Store TTL (seconds)
|
|
38
|
+
FEATURE_TTL_PROFILE=2592000
|
|
39
|
+
FEATURE_TTL_SEQUENCE=3600
|
|
40
|
+
FEATURE_TTL_GEOLOCATION=86400
|
|
41
|
+
|
|
42
|
+
# Dashboard Configuration
|
|
43
|
+
DASHBOARD_PORT=3001
|
|
44
|
+
ENABLE_WEBSOCKET=true
|
|
45
|
+
|
|
46
|
+
# Deployment Mode
|
|
47
|
+
SHADOW_MODE=true
|
|
48
|
+
ENABLE_AUTO_THROTTLE=false
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
# API Integration Guide - Zero-Trust Monitoring
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The Zero-Trust API Monitor works as a **middleware layer** that sits between your API consumers (partners) and your actual API endpoints. It monitors all traffic passing through it.
|
|
6
|
+
|
|
7
|
+
## How It Works
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
Partner API Request → Zero-Trust Monitor → Your Real API → Response → Monitor → Partner
|
|
11
|
+
↓
|
|
12
|
+
Behavioral Analysis
|
|
13
|
+
Anomaly Detection
|
|
14
|
+
Threat Response
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Integration Methods
|
|
18
|
+
|
|
19
|
+
### Method 1: Express Middleware (Recommended)
|
|
20
|
+
|
|
21
|
+
If you have an existing Express.js API, integrate the monitor as middleware:
|
|
22
|
+
|
|
23
|
+
#### Step 1: Install in Your Existing API
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
// your-api/server.js
|
|
27
|
+
import express from 'express';
|
|
28
|
+
import { telemetryCollector } from './path/to/collector/collector';
|
|
29
|
+
import { throttleEngine } from './path/to/security/throttle-engine';
|
|
30
|
+
|
|
31
|
+
const app = express();
|
|
32
|
+
|
|
33
|
+
// Add monitoring middleware BEFORE your routes
|
|
34
|
+
app.use(throttleEngine.middleware()); // Enforce throttle rules
|
|
35
|
+
app.use(telemetryCollector.middleware()); // Collect telemetry
|
|
36
|
+
|
|
37
|
+
// Your existing API routes
|
|
38
|
+
app.get('/api/accounts/:id', (req, res) => {
|
|
39
|
+
// Your existing code
|
|
40
|
+
res.json({ account: 'data' });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
app.listen(3000);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Method 2: Reverse Proxy (Production)
|
|
47
|
+
|
|
48
|
+
Use the monitor as a reverse proxy in front of your existing APIs:
|
|
49
|
+
|
|
50
|
+
#### Step 1: Configure Proxy in `src/index.ts`
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
54
|
+
|
|
55
|
+
// Add to your routes
|
|
56
|
+
this.app.use('/api', createProxyMiddleware({
|
|
57
|
+
target: 'http://your-actual-api:8080', // Your real API
|
|
58
|
+
changeOrigin: true,
|
|
59
|
+
onProxyReq: (proxyReq, req, res) => {
|
|
60
|
+
// Telemetry is already collected by middleware
|
|
61
|
+
}
|
|
62
|
+
}));
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
#### Step 2: Update Docker Compose
|
|
66
|
+
|
|
67
|
+
```yaml
|
|
68
|
+
services:
|
|
69
|
+
api-monitor:
|
|
70
|
+
environment:
|
|
71
|
+
- BACKEND_API_URL=http://your-api:8080
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Method 3: API Gateway Integration
|
|
75
|
+
|
|
76
|
+
Deploy as a sidecar to your API Gateway (Kong, AWS API Gateway, etc.)
|
|
77
|
+
|
|
78
|
+
## Partner Registration
|
|
79
|
+
|
|
80
|
+
### How Partners Are Identified
|
|
81
|
+
|
|
82
|
+
Partners are automatically identified by:
|
|
83
|
+
|
|
84
|
+
1. **API Key** (from `Authorization: Bearer <key>` header)
|
|
85
|
+
2. **Partner ID** (from `X-Partner-ID` header)
|
|
86
|
+
3. **IP Address** (fallback)
|
|
87
|
+
|
|
88
|
+
### Example Partner Request Format
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
curl https://your-api.com/api/accounts/123 \
|
|
92
|
+
-H "Authorization: Bearer sk_live_abc123xyz" \
|
|
93
|
+
-H "X-Partner-ID: partner_fintech_alpha" \
|
|
94
|
+
-H "Content-Type: application/json"
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Partner Data Format
|
|
98
|
+
|
|
99
|
+
The system automatically creates profiles for partners. No manual registration needed!
|
|
100
|
+
|
|
101
|
+
**Automatic Profile Creation:**
|
|
102
|
+
```json
|
|
103
|
+
{
|
|
104
|
+
"partnerId": "partner_fintech_alpha",
|
|
105
|
+
"apiKeyHash": "sha256_hash_of_key",
|
|
106
|
+
"createdAt": 1705564800000,
|
|
107
|
+
"lastUpdated": 1705651200000,
|
|
108
|
+
"statistics": {
|
|
109
|
+
"avgPayloadSize": 1024,
|
|
110
|
+
"requestsPerHour": 150,
|
|
111
|
+
"commonEndpoints": {
|
|
112
|
+
"/api/accounts": 100,
|
|
113
|
+
"/api/transactions": 50
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Real-World Integration Example
|
|
120
|
+
|
|
121
|
+
### Scenario: You have a Banking API
|
|
122
|
+
|
|
123
|
+
**Your Current Setup:**
|
|
124
|
+
```
|
|
125
|
+
Partners → Your API (port 8080)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**With Zero-Trust Monitor:**
|
|
129
|
+
```
|
|
130
|
+
Partners → Monitor (port 3000) → Your API (port 8080)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Implementation
|
|
134
|
+
|
|
135
|
+
#### 1. Update Your API to Run on Different Port
|
|
136
|
+
|
|
137
|
+
```javascript
|
|
138
|
+
// your-banking-api/server.js
|
|
139
|
+
app.listen(8080); // Change from 3000 to 8080
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### 2. Configure Monitor to Proxy to Your API
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
// src/index.ts (in the monitor)
|
|
146
|
+
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
147
|
+
|
|
148
|
+
// Add after telemetry middleware
|
|
149
|
+
this.app.use('/api', createProxyMiddleware({
|
|
150
|
+
target: 'http://localhost:8080', // Your banking API
|
|
151
|
+
changeOrigin: true,
|
|
152
|
+
pathRewrite: {
|
|
153
|
+
'^/api': '/api' // Keep path as-is
|
|
154
|
+
}
|
|
155
|
+
}));
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### 3. Update Partner Documentation
|
|
159
|
+
|
|
160
|
+
Tell your partners to use:
|
|
161
|
+
```
|
|
162
|
+
OLD: https://api.yourbank.com/accounts
|
|
163
|
+
NEW: https://api.yourbank.com/accounts (same URL!)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Just add the header:
|
|
167
|
+
```
|
|
168
|
+
X-Partner-ID: their_partner_id
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Configuration
|
|
172
|
+
|
|
173
|
+
### Environment Variables
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
# .env
|
|
177
|
+
# Your backend API
|
|
178
|
+
BACKEND_API_URL=http://localhost:8080
|
|
179
|
+
|
|
180
|
+
# Partner identification
|
|
181
|
+
PARTNER_ID_HEADER=X-Partner-ID
|
|
182
|
+
API_KEY_HEADER=Authorization
|
|
183
|
+
|
|
184
|
+
# Monitoring settings
|
|
185
|
+
SHADOW_MODE=true # Start in monitoring-only mode
|
|
186
|
+
ENABLE_AUTO_THROTTLE=false # Don't block yet
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Testing the Integration
|
|
190
|
+
|
|
191
|
+
### 1. Start Your Existing API
|
|
192
|
+
```bash
|
|
193
|
+
cd your-api
|
|
194
|
+
npm start # Runs on port 8080
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### 2. Start the Monitor
|
|
198
|
+
```bash
|
|
199
|
+
cd api-monitoring
|
|
200
|
+
docker-compose up -d # Runs on port 3000
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### 3. Test a Request Through the Monitor
|
|
204
|
+
```bash
|
|
205
|
+
# This goes through the monitor to your API
|
|
206
|
+
curl http://localhost:3000/api/accounts/123 \
|
|
207
|
+
-H "Authorization: Bearer test_key" \
|
|
208
|
+
-H "X-Partner-ID: partner_test"
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### 4. Check the Dashboard
|
|
212
|
+
Open http://localhost:3001 - you should see:
|
|
213
|
+
- Partner "partner_test" appear
|
|
214
|
+
- Request count increase
|
|
215
|
+
- Anomaly score calculated
|
|
216
|
+
|
|
217
|
+
## Partner Onboarding Flow
|
|
218
|
+
|
|
219
|
+
### For New Partners
|
|
220
|
+
|
|
221
|
+
1. **Partner makes first API call** with their API key
|
|
222
|
+
2. **Monitor automatically creates profile** in Redis
|
|
223
|
+
3. **Baseline learning begins** (1-2 weeks recommended)
|
|
224
|
+
4. **Anomaly detection activates** after sufficient data
|
|
225
|
+
|
|
226
|
+
### For Existing Partners
|
|
227
|
+
|
|
228
|
+
1. **Enable shadow mode** first
|
|
229
|
+
2. **Let monitor observe** for 1-2 weeks
|
|
230
|
+
3. **Review anomaly scores** in dashboard
|
|
231
|
+
4. **Tune thresholds** if needed
|
|
232
|
+
5. **Enable auto-throttle** when confident
|
|
233
|
+
|
|
234
|
+
## API Endpoints for Partner Management
|
|
235
|
+
|
|
236
|
+
### Get Partner Status
|
|
237
|
+
```bash
|
|
238
|
+
GET /api/partner/:partnerId/status
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Response:
|
|
242
|
+
```json
|
|
243
|
+
{
|
|
244
|
+
"partnerId": "partner_fintech_alpha",
|
|
245
|
+
"profile": {
|
|
246
|
+
"requestsPerHour": 150,
|
|
247
|
+
"avgPayloadSize": 1024
|
|
248
|
+
},
|
|
249
|
+
"avgAnomalyScore": 15,
|
|
250
|
+
"throttleState": null
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Manually Restore Partner
|
|
255
|
+
```bash
|
|
256
|
+
POST /api/partner/:partnerId/restore
|
|
257
|
+
Content-Type: application/json
|
|
258
|
+
|
|
259
|
+
{
|
|
260
|
+
"reason": "False positive resolved"
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Production Deployment
|
|
265
|
+
|
|
266
|
+
### Architecture
|
|
267
|
+
|
|
268
|
+
```
|
|
269
|
+
Internet → Load Balancer → Zero-Trust Monitor → Your API Cluster
|
|
270
|
+
↓
|
|
271
|
+
Redis (profiles)
|
|
272
|
+
PostgreSQL (audit logs)
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### Docker Compose for Production
|
|
276
|
+
|
|
277
|
+
```yaml
|
|
278
|
+
version: '3.8'
|
|
279
|
+
services:
|
|
280
|
+
monitor:
|
|
281
|
+
image: your-registry/api-monitor:latest
|
|
282
|
+
environment:
|
|
283
|
+
- BACKEND_API_URL=http://your-api:8080
|
|
284
|
+
- REDIS_HOST=redis
|
|
285
|
+
- SHADOW_MODE=false
|
|
286
|
+
- ENABLE_AUTO_THROTTLE=true
|
|
287
|
+
depends_on:
|
|
288
|
+
- redis
|
|
289
|
+
- your-api
|
|
290
|
+
|
|
291
|
+
your-api:
|
|
292
|
+
image: your-registry/your-api:latest
|
|
293
|
+
ports:
|
|
294
|
+
- "8080:8080"
|
|
295
|
+
|
|
296
|
+
redis:
|
|
297
|
+
image: redis:7-alpine
|
|
298
|
+
volumes:
|
|
299
|
+
- redis-data:/data
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## FAQ
|
|
303
|
+
|
|
304
|
+
### Q: Do I need to modify my existing API?
|
|
305
|
+
**A:** No! The monitor works as a proxy. Just point partners to the monitor instead.
|
|
306
|
+
|
|
307
|
+
### Q: How do partners get registered?
|
|
308
|
+
**A:** Automatically! First request creates their profile.
|
|
309
|
+
|
|
310
|
+
### Q: What if I don't want to use X-Partner-ID header?
|
|
311
|
+
**A:** Configure `PARTNER_ID_HEADER` to use a different header, or use API key hashing.
|
|
312
|
+
|
|
313
|
+
### Q: Can I use this with REST, GraphQL, gRPC?
|
|
314
|
+
**A:** Yes for REST and GraphQL. gRPC requires additional configuration.
|
|
315
|
+
|
|
316
|
+
### Q: How do I migrate existing partners?
|
|
317
|
+
**A:** Enable shadow mode, let them make requests normally, profiles auto-create.
|
|
318
|
+
|
|
319
|
+
## Next Steps
|
|
320
|
+
|
|
321
|
+
1. ✅ **Test locally** with the demo endpoints
|
|
322
|
+
2. ✅ **Integrate with your API** using proxy method
|
|
323
|
+
3. ✅ **Run in shadow mode** for 1-2 weeks
|
|
324
|
+
4. ✅ **Review dashboard** for anomalies
|
|
325
|
+
5. ✅ **Enable auto-throttle** when ready
|
|
326
|
+
|
|
327
|
+
## Support
|
|
328
|
+
|
|
329
|
+
See the main [README.md](file:///Users/gouravniitdev/Desktop/Applications/API%20Monitoring/README.md) for more details.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FIS Global
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# @gouravniit/zero-trust-api-monitor
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@gouravniit/zero-trust-api-monitor)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
Zero-Trust API monitoring with behavioral analysis, anomaly detection, and automated threat response for fintech APIs.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- 🔍 **Behavioral Baselining** - Learns normal API usage patterns per partner
|
|
11
|
+
- 🚨 **Real-time Anomaly Detection** - Detects unusual behavior using statistical analysis
|
|
12
|
+
- 🛡️ **Automated Threat Response** - 5-level response system (Monitor → Alert → Throttle → Quarantine → Block)
|
|
13
|
+
- 🤖 **LLM-Powered Analysis** - AI-driven threat classification and recommendations
|
|
14
|
+
- 📊 **Real-time Dashboard** - Beautiful monitoring interface
|
|
15
|
+
- ⚡ **High Performance** - <1ms overhead, async processing
|
|
16
|
+
- 🔒 **Privacy-First** - Zero PII collection, GDPR/PCI-DSS compliant
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @gouravniit/zero-trust-api-monitor
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
const express = require('express');
|
|
28
|
+
const { createMonitor } = require('@gouravniit/zero-trust-api-monitor');
|
|
29
|
+
|
|
30
|
+
const app = express();
|
|
31
|
+
|
|
32
|
+
// Initialize monitor
|
|
33
|
+
const monitor = await createMonitor({
|
|
34
|
+
redis: {
|
|
35
|
+
host: 'localhost',
|
|
36
|
+
port: 6379
|
|
37
|
+
},
|
|
38
|
+
security: {
|
|
39
|
+
shadowMode: true // Start in monitoring-only mode
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Add monitoring middleware
|
|
44
|
+
app.use(monitor.middleware());
|
|
45
|
+
|
|
46
|
+
// Your API routes
|
|
47
|
+
app.get('/api/accounts/:id', (req, res) => {
|
|
48
|
+
res.json({ id: req.params.id, balance: 1000 });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
app.listen(3000);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## TypeScript Support
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { createMonitor, MonitorConfig } from '@gouravniit/zero-trust-api-monitor';
|
|
58
|
+
|
|
59
|
+
const config: MonitorConfig = {
|
|
60
|
+
redis: {
|
|
61
|
+
host: process.env.REDIS_HOST || 'localhost',
|
|
62
|
+
port: 6379
|
|
63
|
+
},
|
|
64
|
+
security: {
|
|
65
|
+
shadowMode: false,
|
|
66
|
+
enableAutoThrottle: true
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const monitor = await createMonitor(config);
|
|
71
|
+
app.use(monitor.middleware());
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## How It Works
|
|
75
|
+
|
|
76
|
+
The monitor sits between your API consumers (partners) and your API endpoints:
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
Partner Request → Monitor → Your API → Response → Monitor → Partner
|
|
80
|
+
↓
|
|
81
|
+
Behavioral Analysis
|
|
82
|
+
Anomaly Detection
|
|
83
|
+
Threat Response
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Automatic Partner Profiling
|
|
87
|
+
|
|
88
|
+
Partners are automatically identified and profiled on their first request:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
curl https://your-api.com/endpoint \
|
|
92
|
+
-H "Authorization: Bearer partner_api_key" \
|
|
93
|
+
-H "X-Partner-ID: partner_fintech_alpha"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The monitor automatically:
|
|
97
|
+
1. Creates a behavioral profile
|
|
98
|
+
2. Learns normal patterns (payload sizes, endpoints, timing)
|
|
99
|
+
3. Detects anomalies in real-time
|
|
100
|
+
4. Responds to threats automatically
|
|
101
|
+
|
|
102
|
+
## Configuration
|
|
103
|
+
|
|
104
|
+
### Environment Variables
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
# Redis (required)
|
|
108
|
+
REDIS_HOST=localhost
|
|
109
|
+
REDIS_PORT=6379
|
|
110
|
+
REDIS_PASSWORD=your_password
|
|
111
|
+
|
|
112
|
+
# Security Settings
|
|
113
|
+
SHADOW_MODE=true # Monitor only, no blocking
|
|
114
|
+
ENABLE_AUTO_THROTTLE=false # Enable automatic blocking
|
|
115
|
+
|
|
116
|
+
# Thresholds (0-100)
|
|
117
|
+
ANOMALY_THRESHOLD_ALERT=31 # Generate alert
|
|
118
|
+
ANOMALY_THRESHOLD_THROTTLE=61 # Add latency
|
|
119
|
+
ANOMALY_THRESHOLD_QUARANTINE=81 # Require re-auth
|
|
120
|
+
ANOMALY_THRESHOLD_BLOCK=96 # Temporary block
|
|
121
|
+
|
|
122
|
+
# LLM (optional)
|
|
123
|
+
LLM_PROVIDER=openai # or 'anthropic'
|
|
124
|
+
OPENAI_API_KEY=your_key
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Advanced Usage
|
|
128
|
+
|
|
129
|
+
### Access Individual Components
|
|
130
|
+
|
|
131
|
+
```javascript
|
|
132
|
+
const {
|
|
133
|
+
telemetryCollector,
|
|
134
|
+
baselineEngine,
|
|
135
|
+
throttleEngine,
|
|
136
|
+
securityCopilot,
|
|
137
|
+
featureStore
|
|
138
|
+
} = require('@gouravniit/zero-trust-api-monitor');
|
|
139
|
+
|
|
140
|
+
// Custom alert handling
|
|
141
|
+
throttleEngine.onAlert((response) => {
|
|
142
|
+
console.log('Threat detected:', {
|
|
143
|
+
partnerId: response.partnerId,
|
|
144
|
+
action: response.action,
|
|
145
|
+
score: response.anomalyScore
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Send to your alerting system
|
|
149
|
+
sendToSlack(response);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// Manual partner management
|
|
153
|
+
await throttleEngine.restorePartner('partner_id', 'False positive');
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Get Partner Status
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
const status = await featureStore.getProfile('partner_fintech_alpha');
|
|
160
|
+
console.log(status);
|
|
161
|
+
// {
|
|
162
|
+
// partnerId: 'partner_fintech_alpha',
|
|
163
|
+
// statistics: {
|
|
164
|
+
// requestsPerHour: 150,
|
|
165
|
+
// avgPayloadSize: 1024,
|
|
166
|
+
// commonEndpoints: { '/api/accounts': 100 }
|
|
167
|
+
// }
|
|
168
|
+
// }
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Dashboard
|
|
172
|
+
|
|
173
|
+
Access the real-time monitoring dashboard:
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
npm run dashboard
|
|
177
|
+
# Open http://localhost:3001
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Features:
|
|
181
|
+
- Live partner risk scores
|
|
182
|
+
- Anomaly score trends
|
|
183
|
+
- Active alerts
|
|
184
|
+
- Manual throttle controls
|
|
185
|
+
|
|
186
|
+
## Deployment
|
|
187
|
+
|
|
188
|
+
### Docker
|
|
189
|
+
|
|
190
|
+
```yaml
|
|
191
|
+
version: '3.8'
|
|
192
|
+
services:
|
|
193
|
+
api-monitor:
|
|
194
|
+
image: your-registry/api-monitor
|
|
195
|
+
ports:
|
|
196
|
+
- "3000:3000"
|
|
197
|
+
environment:
|
|
198
|
+
- REDIS_HOST=redis
|
|
199
|
+
- SHADOW_MODE=false
|
|
200
|
+
depends_on:
|
|
201
|
+
- redis
|
|
202
|
+
|
|
203
|
+
redis:
|
|
204
|
+
image: redis:7-alpine
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Kubernetes
|
|
208
|
+
|
|
209
|
+
```yaml
|
|
210
|
+
apiVersion: apps/v1
|
|
211
|
+
kind: Deployment
|
|
212
|
+
metadata:
|
|
213
|
+
name: api-monitor
|
|
214
|
+
spec:
|
|
215
|
+
containers:
|
|
216
|
+
- name: monitor
|
|
217
|
+
image: your-registry/api-monitor
|
|
218
|
+
env:
|
|
219
|
+
- name: REDIS_HOST
|
|
220
|
+
value: redis-service
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Non-Node.js Applications
|
|
224
|
+
|
|
225
|
+
The monitor works with **any** backend language! Deploy as a standalone proxy:
|
|
226
|
+
|
|
227
|
+
```yaml
|
|
228
|
+
services:
|
|
229
|
+
# Your Python/Java/Go API
|
|
230
|
+
your-api:
|
|
231
|
+
image: your-api:latest
|
|
232
|
+
ports:
|
|
233
|
+
- "8080:8080"
|
|
234
|
+
|
|
235
|
+
# Monitor (protects your API)
|
|
236
|
+
monitor:
|
|
237
|
+
image: zero-trust-monitor
|
|
238
|
+
ports:
|
|
239
|
+
- "3000:3000"
|
|
240
|
+
environment:
|
|
241
|
+
- BACKEND_API_URL=http://your-api:8080
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
See [NON_NODEJS_GUIDE.md](./NON_NODEJS_GUIDE.md) for details.
|
|
245
|
+
|
|
246
|
+
## API Reference
|
|
247
|
+
|
|
248
|
+
### `createMonitor(config?)`
|
|
249
|
+
|
|
250
|
+
Creates and initializes the monitor.
|
|
251
|
+
|
|
252
|
+
**Parameters:**
|
|
253
|
+
- `config.redis` - Redis connection settings
|
|
254
|
+
- `config.security` - Security configuration
|
|
255
|
+
|
|
256
|
+
**Returns:** `Promise<Monitor>`
|
|
257
|
+
|
|
258
|
+
### `Monitor`
|
|
259
|
+
|
|
260
|
+
**Properties:**
|
|
261
|
+
- `middleware()` - Express middleware
|
|
262
|
+
- `featureStore` - Access to behavioral profiles
|
|
263
|
+
- `throttleEngine` - Threat response engine
|
|
264
|
+
- `telemetryCollector` - Request collector
|
|
265
|
+
- `shutdown()` - Graceful shutdown
|
|
266
|
+
|
|
267
|
+
## Documentation
|
|
268
|
+
|
|
269
|
+
- [API Integration Guide](./API_INTEGRATION_GUIDE.md)
|
|
270
|
+
- [Non-Node.js Guide](./NON_NODEJS_GUIDE.md)
|
|
271
|
+
- [NPM Publishing Guide](./NPM_PUBLISHING_GUIDE.md)
|
|
272
|
+
|
|
273
|
+
## Examples
|
|
274
|
+
|
|
275
|
+
See the [examples](./examples) directory for:
|
|
276
|
+
- Express.js integration
|
|
277
|
+
- TypeScript usage
|
|
278
|
+
- Custom alert handling
|
|
279
|
+
- Multi-language deployment
|
|
280
|
+
|
|
281
|
+
## License
|
|
282
|
+
|
|
283
|
+
MIT © FIS Global
|
|
284
|
+
|
|
285
|
+
## Support
|
|
286
|
+
|
|
287
|
+
- 📧 Issues: https://github.com/fis-global/zero-trust-monitor/issues
|
|
288
|
+
- 📖 Docs: https://github.com/fis-global/zero-trust-monitor
|