soajs.controller 4.2.42 → 4.2.43

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.
Files changed (37) hide show
  1. package/.github/workflows/ci.yml +43 -0
  2. package/.github/workflows/release.yml +92 -0
  3. package/ARCHITECTURE.md +193 -0
  4. package/Gruntfile.js +1 -1
  5. package/docs/configuration-custom-registry.md +421 -0
  6. package/docs/configuration-services-config.md +275 -0
  7. package/docs/data-flow.md +138 -0
  8. package/docs/deployment.md +205 -0
  9. package/docs/external-integrations.md +159 -0
  10. package/docs/gateway-routes.md +259 -0
  11. package/docs/maintenance-routes.md +308 -0
  12. package/docs/middleware/awareness.md +120 -0
  13. package/docs/middleware/cors.md +88 -0
  14. package/docs/middleware/enhancer.md +75 -0
  15. package/docs/middleware/favicon.md +36 -0
  16. package/docs/middleware/gotoService.md +146 -0
  17. package/docs/middleware/ip2ban.md +70 -0
  18. package/docs/middleware/key.md +196 -0
  19. package/docs/middleware/keyACL.md +139 -0
  20. package/docs/middleware/lastSeen.md +111 -0
  21. package/docs/middleware/maintenanceMode.md +81 -0
  22. package/docs/middleware/mt.md +188 -0
  23. package/docs/middleware/oauth.md +173 -0
  24. package/docs/middleware/response.md +106 -0
  25. package/docs/middleware/soajs.md +71 -0
  26. package/docs/middleware/traffic.md +190 -0
  27. package/docs/middleware/url.md +102 -0
  28. package/docs/multi-tenant-security.md +183 -0
  29. package/docs/request-flow.md +230 -0
  30. package/docs/service-discovery.md +167 -0
  31. package/mw/key/index.js +16 -1
  32. package/mw/mt/urac.js +21 -1
  33. package/mw/mt/utils.js +2 -0
  34. package/package.json +7 -7
  35. package/test/unit/mw/key/index.js +150 -0
  36. package/test/unit/mw/mt/urac.js +157 -4
  37. package/.travis.yml +0 -72
@@ -0,0 +1,138 @@
1
+ # Data Flow
2
+
3
+ Database interactions and data flow in the SOAJS Controller Gateway.
4
+
5
+ ## Database Overview
6
+
7
+ ```
8
+ ┌─────────────────────────────────────────────────────────────────────────────────┐
9
+ │ DATA FLOW │
10
+ └─────────────────────────────────────────────────────────────────────────────────┘
11
+
12
+ ┌─────────────────────────────────────────────────────────────────────────────────┐
13
+ │ MONGODB DATABASES │
14
+ │ │
15
+ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────────────┐ │
16
+ │ │ Core DB │ │ Meta DB │ │ Throttle DB (Optional) │ │
17
+ │ ├─────────────────┤ ├─────────────────┤ ├─────────────────────────────┤ │
18
+ │ │ • Environment │ │ • Tenant- │ │ • Rate limit counters │ │
19
+ │ │ • Tenants │ │ specific │ │ • Request windows │ │
20
+ │ │ • Applications │ │ databases │ │ • IP-based tracking │ │
21
+ │ │ • Keys │ │ • Custom data │ │ │ │
22
+ │ │ • Packages │ │ │ │ │ │
23
+ │ │ • Services │ │ │ │ │ │
24
+ │ │ • OAuth tokens │ │ │ │ │ │
25
+ │ └────────┬────────┘ └────────┬────────┘ └──────────────┬──────────────┘ │
26
+ │ │ │ │ │
27
+ └────────────┼──────────────────────┼────────────────────────────┼────────────────┘
28
+ │ │ │
29
+ ▼ ▼ ▼
30
+ ┌─────────────────────────────────────────────────────────────────────────────┐
31
+ │ SOAJS CONTROLLER │
32
+ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
33
+ │ │ Registry │ │ Provision │ │ URAC │ │ Traffic │ │
34
+ │ │ Module │ │ Module │ │ Driver │ │ Module │ │
35
+ │ └──────────────┘ └──────────────┘ └──────────────┘ └──────────────────┘ │
36
+ └─────────────────────────────────────────────────────────────────────────────┘
37
+ ```
38
+
39
+ ## Core Database Collections
40
+
41
+ The Core DB contains the central configuration for the SOAJS platform.
42
+
43
+ ### Environment Registry
44
+
45
+ **Collection:** `environment`
46
+
47
+ Stores environment-specific configuration including services, hosts, and settings.
48
+
49
+ ### Tenants
50
+
51
+ **Collection:** `tenants`
52
+
53
+ Stores tenant information, applications, and keys.
54
+
55
+ ### Products & Packages
56
+
57
+ **Collection:** `products`
58
+
59
+ Stores product definitions and package ACLs.
60
+
61
+ ### OAuth Tokens
62
+
63
+ **Collection:** `oauth_tokens`
64
+
65
+ Stores OAuth2 access and refresh tokens.
66
+
67
+ ## Module Interactions
68
+
69
+ ### Registry Module
70
+
71
+ **Source:** `soajs.core.modules/soajs.core/registry`
72
+
73
+ - Loads environment configuration from Core DB
74
+ - Caches registry data in memory
75
+ - Provides service definitions and hosts
76
+ - Manages custom gateway settings
77
+
78
+ ### Provision Module
79
+
80
+ **Source:** `soajs.core.modules/soajs.core/provision`
81
+
82
+ - Loads tenant/key data
83
+ - Validates external keys
84
+ - Retrieves package ACLs
85
+ - Caches provision data with TTL
86
+
87
+ ### URAC Driver
88
+
89
+ **Source:** `soajs.urac.driver`
90
+
91
+ - Loads user profiles
92
+ - Resolves user groups
93
+ - Manages user-specific ACLs
94
+
95
+ ### Traffic Module
96
+
97
+ **File:** `mw/traffic/index.js`
98
+
99
+ - Tracks request counts
100
+ - Enforces rate limits
101
+ - Supports memory or MongoDB storage
102
+
103
+ ## Data Caching
104
+
105
+ The gateway implements multi-level caching:
106
+
107
+ ### Registry Cache
108
+
109
+ - Cached in memory
110
+ - Refreshed via maintenance endpoint
111
+ - TTL-based expiration
112
+
113
+ ### Provision Cache
114
+
115
+ - Per-key caching with TTL
116
+ - Configurable via `_TTL` field
117
+ - Refreshed on cache miss
118
+
119
+ ### Token Cache
120
+
121
+ - OAuth tokens validated against DB
122
+ - Short-lived cache for performance
123
+
124
+ ## Throttle Database
125
+
126
+ When using MongoDB for rate limiting (`traffic.model: "mongo"`):
127
+
128
+ **Collection:** `throttle`
129
+
130
+ ```javascript
131
+ {
132
+ "_id": ObjectId("..."),
133
+ "key": "192.168.1.1:oauth:/token", // IP:service:api
134
+ "count": 15, // Request count
135
+ "timestamp": ISODate("..."), // Window start
136
+ "window": 60000 // Window size (ms)
137
+ }
138
+ ```
@@ -0,0 +1,205 @@
1
+ # Deployment Architecture
2
+
3
+ High availability deployment patterns for the SOAJS Controller Gateway.
4
+
5
+ ## Architecture Overview
6
+
7
+ ```
8
+ ┌─────────────────────────────────────────────────────────────────────────────────┐
9
+ │ DEPLOYMENT ARCHITECTURE │
10
+ └─────────────────────────────────────────────────────────────────────────────────┘
11
+
12
+ ┌─────────────────────────────┐
13
+ │ Load Balancer │
14
+ │ (External/Cloud LB) │
15
+ └─────────────┬───────────────┘
16
+ │
17
+ ┌───────────────────────┼───────────────────────┐
18
+ │ │ │
19
+ ▼ ▼ ▼
20
+ ┌─────────────────────┐ ┌─────────────────────┐ ┌─────────────────────┐
21
+ │ Controller Node 1 │ │ Controller Node 2 │ │ Controller Node N │
22
+ │ ┌───────────────┐ │ │ ┌───────────────┐ │ │ ┌───────────────┐ │
23
+ │ │ Main: 4000 │ │ │ │ Main: 4000 │ │ │ │ Main: 4000 │ │
24
+ │ │ Maint: 5000 │ │ │ │ Maint: 5000 │ │ │ │ Maint: 5000 │ │
25
+ │ └───────────────┘ │ │ └───────────────┘ │ │ └───────────────┘ │
26
+ └─────────┬───────────┘ └─────────┬───────────┘ └─────────┬───────────┘
27
+ │ │ │
28
+ └───────────────────────┼───────────────────────┘
29
+ │
30
+ ▼
31
+ ┌─────────────────────────────────────────────────────────────────────────────┐
32
+ │ SHARED INFRASTRUCTURE │
33
+ │ │
34
+ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────────────┐ │
35
+ │ │ MongoDB Cluster│ │ Kubernetes/Swarm│ │ Microservices │ │
36
+ │ │ (Core DB) │ │ (Service Mesh) │ │ (Backend Services) │ │
37
+ │ └─────────────────┘ └─────────────────┘ └─────────────────────────────┘ │
38
+ │ │
39
+ └─────────────────────────────────────────────────────────────────────────────┘
40
+ ```
41
+
42
+ ## Deployment Modes
43
+
44
+ ### Standalone Mode
45
+
46
+ Single instance deployment for development/testing.
47
+
48
+ ```bash
49
+ # Environment variables
50
+ export SOAJS_ENV=dev
51
+ export SOAJS_PROFILE=/path/to/profile.js
52
+
53
+ # Start
54
+ node .
55
+ ```
56
+
57
+ ### Cluster Mode (Custom)
58
+
59
+ Multiple instances with external load balancer.
60
+
61
+ ```bash
62
+ # Each node
63
+ export SOAJS_ENV=prod
64
+ export SOAJS_PROFILE=/path/to/profile.js
65
+
66
+ # Start multiple instances behind LB
67
+ node .
68
+ ```
69
+
70
+ ### Kubernetes Mode
71
+
72
+ Deploy as Kubernetes Deployment/Service.
73
+
74
+ ```yaml
75
+ apiVersion: apps/v1
76
+ kind: Deployment
77
+ metadata:
78
+ name: soajs-controller
79
+ labels:
80
+ soajs.service.name: controller
81
+ soajs.service.version: "1"
82
+ spec:
83
+ replicas: 3
84
+ selector:
85
+ matchLabels:
86
+ soajs.service.name: controller
87
+ template:
88
+ metadata:
89
+ labels:
90
+ soajs.service.name: controller
91
+ soajs.service.version: "1"
92
+ soajs.env.code: prod
93
+ spec:
94
+ containers:
95
+ - name: controller
96
+ image: soajsorg/gateway:latest
97
+ ports:
98
+ - containerPort: 4000
99
+ name: main
100
+ - containerPort: 5000
101
+ name: maintenance
102
+ env:
103
+ - name: SOAJS_ENV
104
+ value: "prod"
105
+ - name: SOAJS_DEPLOY_HA
106
+ value: "kubernetes"
107
+ - name: SOAJS_PROFILE
108
+ value: "/opt/soajs/profiles/profile.js"
109
+ livenessProbe:
110
+ httpGet:
111
+ path: /heartbeat
112
+ port: 5000
113
+ initialDelaySeconds: 30
114
+ periodSeconds: 10
115
+ readinessProbe:
116
+ httpGet:
117
+ path: /heartbeat
118
+ port: 5000
119
+ initialDelaySeconds: 5
120
+ periodSeconds: 5
121
+ ---
122
+ apiVersion: v1
123
+ kind: Service
124
+ metadata:
125
+ name: soajs-controller
126
+ spec:
127
+ selector:
128
+ soajs.service.name: controller
129
+ ports:
130
+ - port: 4000
131
+ targetPort: 4000
132
+ name: main
133
+ - port: 5000
134
+ targetPort: 5000
135
+ name: maintenance
136
+ ```
137
+
138
+ ### Docker Swarm Mode
139
+
140
+ Deploy as Docker Swarm service.
141
+
142
+ ```bash
143
+ docker service create \
144
+ --name soajs-controller \
145
+ --replicas 3 \
146
+ --publish 4000:4000 \
147
+ --publish 5000:5000 \
148
+ --env SOAJS_ENV=prod \
149
+ --env SOAJS_DEPLOY_HA=swarm \
150
+ --env SOAJS_PROFILE=/opt/soajs/profiles/profile.js \
151
+ --mount type=bind,source=/opt/soajs/profiles,target=/opt/soajs/profiles \
152
+ soajsorg/gateway:latest
153
+ ```
154
+
155
+ ## Port Configuration
156
+
157
+ | Port | Purpose | Protocol |
158
+ |------|---------|----------|
159
+ | 4000 | Main gateway | HTTP |
160
+ | 5000 | Maintenance | HTTP |
161
+
162
+ ## Health Checks
163
+
164
+ ### Liveness Check
165
+
166
+ ```bash
167
+ curl http://localhost:5000/heartbeat
168
+ ```
169
+
170
+ Returns `200 OK` if the gateway is running.
171
+
172
+ ### Readiness Check
173
+
174
+ ```bash
175
+ curl http://localhost:5000/awarenessStat
176
+ ```
177
+
178
+ Returns service awareness state.
179
+
180
+ ## Scaling Considerations
181
+
182
+ ### Horizontal Scaling
183
+
184
+ - All instances are stateless
185
+ - Share MongoDB for state
186
+ - Load balance with sticky sessions not required
187
+
188
+ ### Resource Recommendations
189
+
190
+ | Environment | CPU | Memory |
191
+ |-------------|-----|--------|
192
+ | Development | 0.5 | 512MB |
193
+ | Staging | 1 | 1GB |
194
+ | Production | 2+ | 2GB+ |
195
+
196
+ ## Environment Variables
197
+
198
+ | Variable | Description | Default |
199
+ |----------|-------------|---------|
200
+ | `SOAJS_ENV` | Environment code | `"dev"` |
201
+ | `SOAJS_SOLO` | Solo mode (no DB) | - |
202
+ | `SOAJS_DEPLOY_HA` | HA mode | - |
203
+ | `SOAJS_PROFILE` | Profile path | `"./profiles/single.js"` |
204
+ | `SOAJS_MAX_BODY_SIZE` | Max body size | `"10mb"` |
205
+ | `SOAJS_SENSITIVE_ENVS` | Sensitive envs | `["dashboard"]` |
@@ -0,0 +1,159 @@
1
+ # External Integrations
2
+
3
+ The SOAJS Controller Gateway integrates with external services and infrastructure platforms.
4
+
5
+ ## Integration Overview
6
+
7
+ ```
8
+ ┌─────────────────────────────────────────────────────────────────────────────────┐
9
+ │ EXTERNAL INTEGRATIONS │
10
+ └─────────────────────────────────────────────────────────────────────────────────┘
11
+
12
+ ┌─────────────────────────────────────────────────────────────────────────────────┐
13
+ │ │
14
+ │ ┌──────────────────────┐ ┌──────────────────────┐ ┌────────────────────┐ │
15
+ │ │ OAuth Service │ │ URAC Service │ │ SOA Monitor │ │
16
+ │ ├──────────────────────┤ ├──────────────────────┤ ├────────────────────┤ │
17
+ │ │ │ │ │ │ │ │
18
+ │ │ Endpoints: │ │ Endpoints: │ │ Endpoints: │ │
19
+ │ │ • /token │ │ • /user/getprofile │ │ • /monitor/item │ │
20
+ │ │ • /authorization │ │ • /user/last/seen │ │ │ │
21
+ │ │ │ │ │ │ │ │
22
+ │ │ Functions: │ │ Functions: │ │ Functions: │ │
23
+ │ │ • Token validation │ │ • User profiles │ │ • Request metrics │ │
24
+ │ │ • Token refresh │ │ • Group resolution │ │ • Response times │ │
25
+ │ │ • Authorization │ │ • Activity tracking │ │ • Error tracking │ │
26
+ │ │ │ │ • ACL by user │ │ │ │
27
+ │ └──────────────────────┘ └──────────────────────┘ └────────────────────┘ │
28
+ │ │
29
+ │ ┌──────────────────────┐ ┌──────────────────────┐ │
30
+ │ │ Kubernetes API │ │ Docker Swarm API │ │
31
+ │ ├──────────────────────┤ ├──────────────────────┤ │
32
+ │ │ │ │ │ │
33
+ │ │ Functions: │ │ Functions: │ │
34
+ │ │ • Service discovery │ │ • Service discovery │ │
35
+ │ │ • Pod listing │ │ • Task listing │ │
36
+ │ │ • Health checks │ │ • Health checks │ │
37
+ │ │ • Label selectors │ │ • Label selectors │ │
38
+ │ │ │ │ │ │
39
+ │ └──────────────────────┘ └──────────────────────┘ │
40
+ │ │
41
+ └─────────────────────────────────────────────────────────────────────────────────┘
42
+ ```
43
+
44
+ ## OAuth Service Integration
45
+
46
+ The gateway integrates with the SOAJS OAuth service for authentication.
47
+
48
+ ### Token Validation
49
+
50
+ **Middleware:** `mw/oauth/index.js`
51
+
52
+ - Validates access tokens against OAuth service
53
+ - Supports both OAuth2 password grant and JWT
54
+ - Handles token refresh
55
+
56
+ ### Endpoints Used
57
+
58
+ | Endpoint | Method | Purpose |
59
+ |----------|--------|---------|
60
+ | `/token` | POST | Exchange credentials for tokens |
61
+ | `/authorization` | GET | Validate token |
62
+
63
+ ### Configuration
64
+
65
+ OAuth is configured via the registry:
66
+
67
+ ```javascript
68
+ {
69
+ "serviceConfig": {
70
+ "oauth": {
71
+ "grants": ["password", "refresh_token"],
72
+ "tokenLife": 7200,
73
+ "refreshTokenLife": 1209600
74
+ }
75
+ }
76
+ }
77
+ ```
78
+
79
+ ## URAC Service Integration
80
+
81
+ The gateway integrates with URAC (User Registration and Access Control) for user management.
82
+
83
+ ### User Profile Loading
84
+
85
+ **Middleware:** `mw/mt/urac.js`
86
+
87
+ - Loads user profiles for authenticated requests
88
+ - Resolves user groups and permissions
89
+ - Applies user-specific ACL overrides
90
+
91
+ ### Activity Tracking (Last Seen)
92
+
93
+ **Middleware:** `mw/lastSeen/index.js`
94
+
95
+ - Notifies URAC of user activity
96
+ - Configurable via custom registry
97
+
98
+ ### Endpoints Used
99
+
100
+ | Endpoint | Method | Purpose |
101
+ |----------|--------|---------|
102
+ | `/user/getprofile` | GET | Get user profile |
103
+ | `/user/last/seen` | POST | Update last seen timestamp |
104
+
105
+ ## SOA Monitor Integration
106
+
107
+ Optional integration for request monitoring and metrics.
108
+
109
+ ### Request Monitoring
110
+
111
+ **Middleware:** `mw/gotoService/redirectToService.js`
112
+
113
+ - Sends request metrics to SOA Monitor
114
+ - Configurable via custom registry blacklist/whitelist
115
+
116
+ ### Endpoints Used
117
+
118
+ | Endpoint | Method | Purpose |
119
+ |----------|--------|---------|
120
+ | `/monitor/item` | POST | Submit request metrics |
121
+
122
+ ## Kubernetes API Integration
123
+
124
+ **Module:** `modules/driver/kubernetes.js`
125
+
126
+ Used when `SOAJS_DEPLOY_HA=kubernetes`.
127
+
128
+ ### Operations
129
+
130
+ | Operation | API Endpoint | Purpose |
131
+ |-----------|--------------|---------|
132
+ | List Pods | `/api/v1/namespaces/{ns}/pods` | Service discovery |
133
+ | Get Pod | `/api/v1/namespaces/{ns}/pods/{name}` | Health check |
134
+ | List Services | `/api/v1/namespaces/{ns}/services` | Service info |
135
+
136
+ ### Authentication
137
+
138
+ - Uses in-cluster service account token
139
+ - Token path: `/var/run/secrets/kubernetes.io/serviceaccount/token`
140
+ - CA path: `/var/run/secrets/kubernetes.io/serviceaccount/ca.crt`
141
+
142
+ ## Docker Swarm API Integration
143
+
144
+ **Module:** `modules/driver/swarm.js`
145
+
146
+ Used when `SOAJS_DEPLOY_HA=swarm`.
147
+
148
+ ### Operations
149
+
150
+ | Operation | API Endpoint | Purpose |
151
+ |-----------|--------------|---------|
152
+ | List Tasks | `/tasks` | Service discovery |
153
+ | List Services | `/services` | Service info |
154
+ | Inspect Node | `/nodes/{id}` | Node info |
155
+
156
+ ### Authentication
157
+
158
+ - Connects to Docker socket
159
+ - Default: `/var/run/docker.sock`