@friggframework/core 2.0.0-next.3 → 2.0.0-next.31

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.
@@ -1,5 +1,6 @@
1
1
  // This line should be at the top of the webpacked output, so be sure to require createHandler first in any handlers. "Soon" sourcemaps will be built into Node... after that, this package won't be needed.
2
- require('source-map-support').install();
2
+ // REMOVING FOR NOW UNTIL WE ADD WEBPACK BACK IN
3
+ // require('source-map-support').install();
3
4
 
4
5
  const { connectToDatabase } = require('../database/mongo');
5
6
  const { initDebugLog, flushDebugLog } = require('../logs');
@@ -8,6 +8,11 @@ const schema = new mongoose.Schema({
8
8
  // Add a static method to get active connections
9
9
  schema.statics.getActiveConnections = async function () {
10
10
  try {
11
+ // Return empty array if websockets are not configured
12
+ if (!process.env.WEBSOCKET_API_ENDPOINT) {
13
+ return [];
14
+ }
15
+
11
16
  const connections = await this.find({}, 'connectionId');
12
17
  return connections.map((conn) => ({
13
18
  connectionId: conn.connectionId,
@@ -16,28 +16,18 @@ const findOneEvents = [
16
16
 
17
17
  const shouldBypassEncryption = (STAGE) => {
18
18
  const defaultBypassStages = ['dev', 'test', 'local'];
19
- const bypassStageEnv = process.env.BYPASS_ENCRYPTION_STAGE;
20
- // If the env is set to anything or an empty string, use the env. Otherwise, use the default array
21
- const useEnv = !String(bypassStageEnv) || !!bypassStageEnv;
22
- const bypassStages = useEnv
23
- ? bypassStageEnv.split(',').map((stage) => stage.trim())
24
- : defaultBypassStages;
25
- return bypassStages.includes(STAGE);
19
+ return defaultBypassStages.includes(STAGE);
26
20
  };
27
21
 
28
- // The Mongoose plug-in function
29
- function Encrypt(schema, options) {
22
+ function Encrypt(schema) {
30
23
  const { STAGE, KMS_KEY_ARN, AES_KEY_ID } = process.env;
31
24
 
32
25
  if (shouldBypassEncryption(STAGE)) {
33
26
  return;
34
27
  }
35
28
 
36
- if (KMS_KEY_ARN && AES_KEY_ID) {
37
- throw new Error(
38
- 'Local and AWS encryption keys are both set in the environment.'
39
- );
40
- }
29
+ const hasAES = AES_KEY_ID && AES_KEY_ID.trim() !== '';
30
+ const hasKMS = KMS_KEY_ARN && KMS_KEY_ARN.trim() !== '' && !hasAES;
41
31
 
42
32
  const fields = Object.values(schema.paths)
43
33
  .map(({ path, options }) => (options.lhEncrypt === true ? path : ''))
@@ -48,25 +38,17 @@ function Encrypt(schema, options) {
48
38
  }
49
39
 
50
40
  const cryptor = new Cryptor({
51
- // Use AWS if the CMK is present
52
- shouldUseAws: !!KMS_KEY_ARN,
53
- // Find all the fields in the schema with lhEncrypt === true
41
+ shouldUseAws: hasKMS,
54
42
  fields: fields,
55
43
  });
56
44
 
57
- // ---------------------------------------------
58
- // ### Encrypt fields before save/update/insert.
59
- // ---------------------------------------------
60
-
61
45
  schema.pre('save', async function encryptionPreSave() {
62
- // `this` will be a doc
63
46
  await cryptor.encryptFieldsInDocuments([this]);
64
47
  });
65
48
 
66
49
  schema.pre(
67
50
  'insertMany',
68
51
  async function encryptionPreInsertMany(_, docs, options) {
69
- // `this` will be the model
70
52
  if (options?.rawResult) {
71
53
  throw new Error(
72
54
  'Raw result not supported for insertMany with Encrypt plugin'
@@ -78,17 +60,14 @@ function Encrypt(schema, options) {
78
60
  );
79
61
 
80
62
  schema.pre(updateOneEvents, async function encryptionPreUpdateOne() {
81
- // `this` will be a query
82
63
  await cryptor.encryptFieldsInQuery(this);
83
64
  });
84
65
 
85
66
  schema.pre('updateMany', async function encryptionPreUpdateMany() {
86
- // `this` will be a query
87
67
  cryptor.expectNotToUpdateManyEncrypted(this.getUpdate());
88
68
  });
89
69
 
90
70
  schema.pre('update', async function encryptionPreUpdate() {
91
- // `this` will be a query
92
71
  const { multiple } = this.getOptions();
93
72
 
94
73
  if (multiple) {
@@ -99,16 +78,11 @@ function Encrypt(schema, options) {
99
78
  await cryptor.encryptFieldsInQuery(this);
100
79
  });
101
80
 
102
- // --------------------------------------------
103
- // ### Decrypt documents after they are loaded.
104
- // --------------------------------------------
105
81
  schema.post('save', async function encryptionPreSave() {
106
- // `this` will be a doc
107
82
  await cryptor.decryptFieldsInDocuments([this]);
108
83
  });
109
84
 
110
85
  schema.post(findOneEvents, async function encryptionPostFindOne(doc) {
111
- // `this` will be a query
112
86
  const { rawResult } = this.getOptions();
113
87
 
114
88
  if (rawResult) {
@@ -119,12 +93,10 @@ function Encrypt(schema, options) {
119
93
  });
120
94
 
121
95
  schema.post('find', async function encryptionPostFind(docs) {
122
- // `this` will be a query
123
96
  await cryptor.decryptFieldsInDocuments(docs);
124
97
  });
125
98
 
126
99
  schema.post('insertMany', async function encryptionPostInsertMany(docs) {
127
- // `this` will be the model
128
100
  await cryptor.decryptFieldsInDocuments(docs);
129
101
  });
130
102
  }
@@ -0,0 +1,59 @@
1
+ const { createHandler, flushDebugLog } = require('@friggframework/core');
2
+ const express = require('express');
3
+ const bodyParser = require('body-parser');
4
+ const cors = require('cors');
5
+ const Boom = require('@hapi/boom');
6
+ const loadUserManager = require('./routers/middleware/loadUser');
7
+ const serverlessHttp = require('serverless-http');
8
+
9
+ const createApp = (applyMiddleware) => {
10
+ const app = express();
11
+
12
+ app.use(bodyParser.json({ limit: '10mb' }));
13
+ app.use(bodyParser.urlencoded({ extended: true }));
14
+ app.use(
15
+ cors({
16
+ origin: '*',
17
+ allowedHeaders: '*',
18
+ methods: '*',
19
+ credentials: true,
20
+ })
21
+ );
22
+
23
+ app.use(loadUserManager);
24
+
25
+ if (applyMiddleware) applyMiddleware(app);
26
+
27
+ // Handle sending error response and logging server errors to console
28
+ app.use((err, req, res, next) => {
29
+ const boomError = err.isBoom ? err : Boom.boomify(err);
30
+ const {
31
+ output: { statusCode = 500 },
32
+ } = boomError;
33
+
34
+ if (statusCode >= 500) {
35
+ flushDebugLog(boomError);
36
+ res.status(statusCode).json({ error: 'Internal Server Error' });
37
+ } else {
38
+ res.status(statusCode).json({ error: err.message });
39
+ }
40
+ });
41
+
42
+ return app;
43
+ };
44
+
45
+ function createAppHandler(eventName, router, shouldUseDatabase = true) {
46
+ const app = createApp((app) => {
47
+ app.use(router);
48
+ });
49
+ return createHandler({
50
+ eventName,
51
+ method: serverlessHttp(app),
52
+ shouldUseDatabase,
53
+ });
54
+ }
55
+
56
+ module.exports = {
57
+ createApp,
58
+ createAppHandler,
59
+ };
@@ -0,0 +1,85 @@
1
+ const { createFriggBackend, Worker } = require('@friggframework/core');
2
+ const { findNearestBackendPackageJson } = require('@friggframework/core/utils');
3
+ const path = require('node:path');
4
+ const fs = require('fs-extra');
5
+
6
+ const backendPath = findNearestBackendPackageJson();
7
+ if (!backendPath) {
8
+ throw new Error('Could not find backend package.json');
9
+ }
10
+
11
+ const backendDir = path.dirname(backendPath);
12
+ const backendFilePath = path.join(backendDir, 'index.js');
13
+ if (!fs.existsSync(backendFilePath)) {
14
+ throw new Error('Could not find index.js');
15
+ }
16
+
17
+ const backendJsFile = require(backendFilePath);
18
+ const { Router } = require('express');
19
+ const appDefinition = backendJsFile.Definition;
20
+
21
+ const backend = createFriggBackend(appDefinition);
22
+ const loadRouterFromObject = (IntegrationClass, routerObject) => {
23
+ const router = Router();
24
+ const { path, method, event } = routerObject;
25
+ console.log(
26
+ `Registering ${method} ${path} for ${IntegrationClass.Definition.name}`
27
+ );
28
+ router[method.toLowerCase()](path, async (req, res, next) => {
29
+ try {
30
+ const integration = new IntegrationClass({});
31
+ await integration.loadModules();
32
+ await integration.registerEventHandlers();
33
+ const result = await integration.send(event, {req, res, next});
34
+ res.json(result);
35
+ } catch (error) {
36
+ next(error);
37
+ }
38
+ });
39
+
40
+ return router;
41
+ };
42
+ const createQueueWorker = (integrationClass, integrationFactory) => {
43
+ class QueueWorker extends Worker {
44
+ async _run(params, context) {
45
+ try {
46
+ let instance;
47
+ if (!params.integrationId) {
48
+ instance = new integrationClass({});
49
+ await instance.loadModules();
50
+ // await instance.loadUserActions();
51
+ await instance.registerEventHandlers();
52
+ console.log(
53
+ `${params.event} for ${integrationClass.Definition.name} integration with no integrationId`
54
+ );
55
+ } else {
56
+ instance =
57
+ await integrationFactory.getInstanceFromIntegrationId({
58
+ integrationId: params.integrationId,
59
+ });
60
+ console.log(
61
+ `${params.event} for ${instance.record.config.type} of integrationId: ${params.integrationId}`
62
+ );
63
+ }
64
+ const res = await instance.send(params.event, {
65
+ data: params.data,
66
+ context,
67
+ });
68
+ return res;
69
+ } catch (error) {
70
+ console.error(
71
+ `Error in ${params.event} for ${integrationClass.Definition.name}:`,
72
+ error
73
+ );
74
+ throw error;
75
+ }
76
+ }
77
+ }
78
+ return QueueWorker;
79
+ };
80
+
81
+ module.exports = {
82
+ ...backend,
83
+ loadRouterFromObject,
84
+ createQueueWorker,
85
+ };
@@ -0,0 +1,240 @@
1
+ # Frigg Healthcheck Endpoint Documentation
2
+
3
+ ## Overview
4
+
5
+ The Frigg service includes comprehensive healthcheck endpoints to monitor service health, connectivity, and readiness. These endpoints follow industry best practices and are designed for use with monitoring systems, load balancers, and container orchestration platforms.
6
+
7
+ ## Endpoints
8
+
9
+ ### 1. Basic Health Check
10
+ **GET** `/health`
11
+
12
+ Simple health check endpoint that returns basic service information. No authentication required. This endpoint is rate-limited at the API Gateway level.
13
+
14
+ **Response:**
15
+ ```json
16
+ {
17
+ "status": "ok",
18
+ "timestamp": "2024-01-10T12:00:00.000Z",
19
+ "service": "frigg-core-api"
20
+ }
21
+ ```
22
+
23
+ **Status Codes:**
24
+ - `200 OK` - Service is running
25
+
26
+ ### 2. Detailed Health Check
27
+ **GET** `/health/detailed`
28
+
29
+ Comprehensive health check that tests all service components and dependencies.
30
+
31
+ **Authentication Required:**
32
+ - Header: `x-api-key: YOUR_API_KEY`
33
+ - The API key must match the `HEALTH_API_KEY` environment variable
34
+
35
+ **Response:**
36
+ ```json
37
+ {
38
+ "service": "frigg-core-api",
39
+ "status": "healthy", // "healthy" or "unhealthy"
40
+ "timestamp": "2024-01-10T12:00:00.000Z",
41
+ "checks": {
42
+ "database": {
43
+ "status": "healthy",
44
+ "state": "connected",
45
+ "responseTime": 5 // milliseconds
46
+ },
47
+ "externalApis": {
48
+ "github": {
49
+ "status": "healthy",
50
+ "statusCode": 200,
51
+ "responseTime": 150,
52
+ "reachable": true
53
+ },
54
+ "npm": {
55
+ "status": "healthy",
56
+ "statusCode": 200,
57
+ "responseTime": 200,
58
+ "reachable": true
59
+ }
60
+ },
61
+ "integrations": {
62
+ "status": "healthy",
63
+ "modules": {
64
+ "count": 10,
65
+ "available": ["module1", "module2", "..."]
66
+ },
67
+ "integrations": {
68
+ "count": 5,
69
+ "available": ["integration1", "integration2", "..."]
70
+ }
71
+ }
72
+ },
73
+ "responseTime": 250 // total endpoint response time in milliseconds
74
+ }
75
+ ```
76
+
77
+ **Status Codes:**
78
+ - `200 OK` - Service is healthy (all components operational)
79
+ - `503 Service Unavailable` - Service is unhealthy (any component failure)
80
+ - `401 Unauthorized` - Missing or invalid x-api-key header
81
+
82
+ ### 3. Liveness Probe
83
+ **GET** `/health/live`
84
+
85
+ Kubernetes-style liveness probe. Returns whether the service process is alive.
86
+
87
+ **Authentication Required:**
88
+ - Header: `x-api-key: YOUR_API_KEY`
89
+
90
+ **Response:**
91
+ ```json
92
+ {
93
+ "status": "alive",
94
+ "timestamp": "2024-01-10T12:00:00.000Z"
95
+ }
96
+ ```
97
+
98
+ **Status Codes:**
99
+ - `200 OK` - Service process is alive
100
+
101
+ ### 4. Readiness Probe
102
+ **GET** `/health/ready`
103
+
104
+ Kubernetes-style readiness probe. Returns whether the service is ready to receive traffic.
105
+
106
+ **Authentication Required:**
107
+ - Header: `x-api-key: YOUR_API_KEY`
108
+
109
+ **Response:**
110
+ ```json
111
+ {
112
+ "ready": true,
113
+ "timestamp": "2024-01-10T12:00:00.000Z",
114
+ "checks": {
115
+ "database": true,
116
+ "modules": true
117
+ }
118
+ }
119
+ ```
120
+
121
+ **Status Codes:**
122
+ - `200 OK` - Service is ready
123
+ - `503 Service Unavailable` - Service is not ready
124
+
125
+ ## Health Status Definitions
126
+
127
+ - **healthy**: All components are functioning normally
128
+ - **unhealthy**: Any component is failing, service may not function properly
129
+
130
+ ## Component Checks
131
+
132
+ ### Database Connectivity
133
+ - Checks database connection state
134
+ - Performs ping test with 2-second timeout if connected
135
+ - Reports connection state and response time
136
+ - Database type is not exposed for security reasons
137
+
138
+ ### External API Connectivity
139
+ - Tests connectivity to external services (GitHub, npm registry)
140
+ - Configurable timeout (default: 5 seconds)
141
+ - Reports reachability and response times
142
+ - Uses Promise.all for parallel checking
143
+
144
+ ### Integration Status
145
+ - Verifies available modules and integrations are loaded
146
+ - Reports counts and lists of available components
147
+
148
+ ## Usage Examples
149
+
150
+ ### Monitoring Systems
151
+ Configure your monitoring system to poll `/health/detailed` every 30-60 seconds:
152
+ ```bash
153
+ curl -H "x-api-key: YOUR_API_KEY" https://your-frigg-instance.com/health/detailed
154
+ ```
155
+
156
+ ### Load Balancer Health Checks
157
+ Configure load balancers to use the simple `/health` endpoint:
158
+ ```bash
159
+ curl https://your-frigg-instance.com/health
160
+ ```
161
+
162
+ ### Kubernetes Configuration
163
+ ```yaml
164
+ livenessProbe:
165
+ httpGet:
166
+ path: /health/live
167
+ port: 8080
168
+ httpHeaders:
169
+ - name: x-api-key
170
+ value: YOUR_API_KEY
171
+ periodSeconds: 10
172
+ timeoutSeconds: 5
173
+
174
+ readinessProbe:
175
+ httpGet:
176
+ path: /health/ready
177
+ port: 8080
178
+ httpHeaders:
179
+ - name: x-api-key
180
+ value: YOUR_API_KEY
181
+ initialDelaySeconds: 30
182
+ periodSeconds: 10
183
+ ```
184
+
185
+ ## Customization
186
+
187
+ ### Adding External API Checks
188
+ To add more external API checks, modify the `externalAPIs` array in the health router:
189
+ ```javascript
190
+ const externalAPIs = [
191
+ { name: 'github', url: 'https://api.github.com/status' },
192
+ { name: 'npm', url: 'https://registry.npmjs.org' },
193
+ { name: 'your-api', url: 'https://your-api.com/health' }
194
+ ];
195
+ ```
196
+
197
+ ### Adjusting Timeouts
198
+ The default timeout for external API checks is 5 seconds. Database ping timeout is set to 2 seconds:
199
+ ```javascript
200
+ const checkExternalAPI = (url, timeout = 5000) => {
201
+ // ...
202
+ };
203
+
204
+ await mongoose.connection.db.admin().ping({ maxTimeMS: 2000 });
205
+ ```
206
+
207
+ ## Best Practices
208
+
209
+ 1. **Authentication**: Basic `/health` endpoint requires no authentication, but detailed endpoints require `x-api-key` header
210
+ 2. **Rate Limiting**: Configure rate limiting at the API Gateway level to prevent abuse
211
+ 3. **Fast Response**: Health checks should respond quickly (< 1 second)
212
+ 4. **Strict Status Codes**: Return 503 for any non-healthy state to ensure proper alerting
213
+ 5. **Detailed Logging**: Failed health checks are logged for debugging
214
+ 6. **Security**: No sensitive information (DB types, versions) exposed in responses
215
+ 7. **Lambda Considerations**: Uptime and memory metrics not included as they're not relevant in serverless
216
+
217
+ ## Troubleshooting
218
+
219
+ ### Database Connection Issues
220
+ - Check `MONGO_URI` environment variable
221
+ - Verify network connectivity to MongoDB
222
+ - Check MongoDB server status
223
+
224
+ ### External API Failures
225
+ - May indicate network issues or external service downtime
226
+ - Service reports "unhealthy" status if any external API is unreachable
227
+
228
+ ## Security Considerations
229
+
230
+ - Basic health endpoint requires no authentication for monitoring compatibility
231
+ - Detailed endpoints require `x-api-key` header authentication
232
+ - Health endpoints do not expose sensitive information
233
+ - Database connection strings and credentials are never included in responses
234
+ - External API checks use read-only endpoints
235
+ - Rate limiting should be configured at the API Gateway level
236
+ - Consider IP whitelisting for health endpoints in production
237
+
238
+ ## Environment Variables
239
+
240
+ - `HEALTH_API_KEY`: Required API key for accessing detailed health endpoints
@@ -0,0 +1,26 @@
1
+ const { createIntegrationRouter } = require('@friggframework/core');
2
+ const { createAppHandler } = require('./../app-handler-helpers');
3
+ const { requireLoggedInUser } = require('./middleware/requireLoggedInUser');
4
+ const {
5
+ moduleFactory,
6
+ integrationFactory,
7
+ IntegrationHelper,
8
+ } = require('./../backend-utils');
9
+
10
+ const router = createIntegrationRouter({
11
+ factory: { moduleFactory, integrationFactory, IntegrationHelper },
12
+ requireLoggedInUser,
13
+ getUserId: (req) => req.user.getUserId(),
14
+ });
15
+
16
+ router.route('/redirect/:appId').get((req, res) => {
17
+ res.redirect(
18
+ `${process.env.FRONTEND_URI}/redirect/${
19
+ req.params.appId
20
+ }?${new URLSearchParams(req.query)}`
21
+ );
22
+ });
23
+
24
+ const handler = createAppHandler('HTTP Event: Auth', router);
25
+
26
+ module.exports = { handler, router };