@friggframework/core 2.0.0--canary.663.9683f8b.0 → 2.0.0--canary.664.a0b36e4.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/database/documentdb-encryption-service.js +22 -9
- package/database/encryption/README.md +14 -76
- package/database/encryption/documentdb-encryption-service.md +1 -78
- package/database/encryption/integration-mapping-encryption.js +1 -1
- package/database/prisma.js +36 -11
- package/database/use-cases/check-encryption-health-use-case.js +52 -43
- package/package.json +5 -5
- package/database/encryption/encryption-config.js +0 -227
|
@@ -4,7 +4,6 @@ const {
|
|
|
4
4
|
getFieldsToEncryptOnWrite,
|
|
5
5
|
loadCustomEncryptionSchema,
|
|
6
6
|
} = require('./encryption/encryption-schema-registry');
|
|
7
|
-
const { getEncryptionConfig } = require('./encryption/encryption-config');
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Encryption service specifically for DocumentDB repositories
|
|
@@ -42,29 +41,43 @@ class DocumentDBEncryptionService {
|
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
/**
|
|
45
|
-
* Initialize Cryptor with environment-based configuration
|
|
46
|
-
*
|
|
44
|
+
* Initialize Cryptor with environment-based configuration.
|
|
45
|
+
* Matches the logic from @friggframework/core/database/prisma.js
|
|
47
46
|
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* Encryption is bypassed in dev/test/local stages.
|
|
48
|
+
* Production uses AWS KMS (if available) or AES encryption.
|
|
50
49
|
*
|
|
51
50
|
* @private
|
|
52
|
-
* @throws {EncryptionConfigurationError} deployed with no usable key
|
|
53
51
|
*/
|
|
54
52
|
_initializeCryptor() {
|
|
55
53
|
// Load custom encryption schema from app definition BEFORE checking configuration
|
|
56
54
|
// This ensures custom fields (like User.username) are registered before any encryption operations
|
|
57
55
|
loadCustomEncryptionSchema();
|
|
58
56
|
|
|
59
|
-
|
|
57
|
+
// Match logic from packages/core/database/prisma.js
|
|
58
|
+
const stage = process.env.STAGE || process.env.NODE_ENV || 'development';
|
|
59
|
+
const bypassEncryption = ['dev', 'test', 'local'].includes(stage.toLowerCase());
|
|
60
60
|
|
|
61
|
-
if (
|
|
61
|
+
if (bypassEncryption) {
|
|
62
62
|
this.cryptor = null;
|
|
63
63
|
this.enabled = false;
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
// Determine encryption method (ensure boolean values)
|
|
68
|
+
const hasKMS = !!(process.env.KMS_KEY_ARN && process.env.KMS_KEY_ARN.trim() !== '');
|
|
69
|
+
const hasAES = !!(process.env.AES_KEY_ID && process.env.AES_KEY_ID.trim() !== '');
|
|
70
|
+
|
|
71
|
+
if (!hasKMS && !hasAES) {
|
|
72
|
+
console.warn('[DocumentDBEncryptionService] No encryption keys configured. Encryption disabled.');
|
|
73
|
+
this.cryptor = null;
|
|
74
|
+
this.enabled = false;
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// KMS takes precedence over AES
|
|
79
|
+
const shouldUseAws = hasKMS;
|
|
80
|
+
this.cryptor = new Cryptor({ shouldUseAws });
|
|
68
81
|
this.enabled = true;
|
|
69
82
|
}
|
|
70
83
|
|
|
@@ -13,7 +13,7 @@ This module provides **transparent field-level encryption** for sensitive data i
|
|
|
13
13
|
- ✅ **Hexagonal architecture**: Clean separation of concerns
|
|
14
14
|
- ✅ **AWS KMS support**: Enterprise-grade encryption with AWS Key Management Service
|
|
15
15
|
- ✅ **Local AES fallback**: Development mode using local encryption keys
|
|
16
|
-
- ✅ **
|
|
16
|
+
- ✅ **Environment-based**: Automatic bypass in dev/test/local environments
|
|
17
17
|
- ✅ **Envelope encryption**: Secure key management pattern
|
|
18
18
|
|
|
19
19
|
## Architecture
|
|
@@ -107,46 +107,18 @@ AES_KEY=your-32-character-secret-key-here
|
|
|
107
107
|
STAGE=production # or development, staging, etc.
|
|
108
108
|
```
|
|
109
109
|
|
|
110
|
-
|
|
110
|
+
**⚠️ Important**: Encryption is automatically **disabled** when `STAGE` is set to `dev`, `test`, or `local`, regardless of key configuration.
|
|
111
111
|
|
|
112
|
-
|
|
112
|
+
### Bypass Encryption
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
To explicitly disable encryption:
|
|
115
115
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
| Deployed, any stage | `AES_KEY_ID` + `AES_KEY` | any | Encrypt with AES |
|
|
120
|
-
| Deployed, any stage | `AES_KEY_ID` without `AES_KEY` | no | **Refuses to start** (`EncryptionConfigurationError`) |
|
|
121
|
-
| Deployed, any stage | none | no | **Refuses to start** (`EncryptionConfigurationError`) |
|
|
122
|
-
| Deployed, any stage | none | yes | Plaintext, with a warning on every cold start |
|
|
123
|
-
| Local, `STAGE` (or `NODE_ENV`) is `dev`, `test` or `local` | any | any | Skipped |
|
|
124
|
-
| Local, any other stage | `KMS_KEY_ARN` or `AES_KEY_ID` | any | Encrypt |
|
|
125
|
-
| Local, any other stage | none | any | Plaintext, with a warning |
|
|
126
|
-
|
|
127
|
-
KMS wins when both keys are set. A configured key always wins over `FRIGG_ENCRYPTION_DISABLED`: switching encryption off while a key exists would leave already-encrypted data unreadable, so the opt-out is ignored (with a warning).
|
|
128
|
-
|
|
129
|
-
"Refuses to start" means the Prisma client is never created: the first database access on a cold start throws
|
|
130
|
-
|
|
131
|
-
```
|
|
132
|
-
EncryptionConfigurationError: [Frigg] No field-level encryption key is configured for stage "dev". A deployed Frigg app will not write credentials and other sensitive fields in plaintext. Fix: ...
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
and `/health/detailed` reports `checks.encryption.status: "unhealthy"` with the same message. If a key is configured but the encryption extension fails to initialise, the client also refuses to start rather than continuing without encryption.
|
|
136
|
-
|
|
137
|
-
To encrypt a local run, use a stage other than `dev`, `test` or `local` and configure a key.
|
|
138
|
-
|
|
139
|
-
### Running a Deployed Stage Without Encryption
|
|
140
|
-
|
|
141
|
-
Only do this on purpose, for a stage that never holds real credentials. Either set it in the app definition:
|
|
142
|
-
|
|
143
|
-
```javascript
|
|
144
|
-
const appDefinition = {
|
|
145
|
-
encryption: { fieldLevelEncryptionMethod: 'none' },
|
|
146
|
-
};
|
|
116
|
+
```bash
|
|
117
|
+
# Disable encryption (development only)
|
|
118
|
+
STAGE=development # or dev, test, local
|
|
147
119
|
```
|
|
148
120
|
|
|
149
|
-
|
|
121
|
+
Or simply don't configure any encryption keys. In Production field level encryption **must** be enabled.
|
|
150
122
|
|
|
151
123
|
## Encrypted Fields
|
|
152
124
|
|
|
@@ -581,8 +553,8 @@ class MyRepositoryDocumentDB {
|
|
|
581
553
|
|
|
582
554
|
#### Configuration
|
|
583
555
|
|
|
584
|
-
Uses the same
|
|
585
|
-
- `STAGE`:
|
|
556
|
+
Uses the same environment variables and Cryptor as the Prisma Extension:
|
|
557
|
+
- `STAGE`: Bypasses encryption for dev/test/local
|
|
586
558
|
- `KMS_KEY_ARN`: AWS KMS encryption (production)
|
|
587
559
|
- `AES_KEY_ID` + `AES_KEY`: AES encryption (fallback)
|
|
588
560
|
|
|
@@ -643,27 +615,10 @@ curl http://localhost:3000/health/test-encryption
|
|
|
643
615
|
"encryptionWorks": true
|
|
644
616
|
}
|
|
645
617
|
|
|
646
|
-
# Response
|
|
618
|
+
# Response when encryption disabled:
|
|
647
619
|
{
|
|
648
620
|
"status": "disabled",
|
|
649
|
-
"
|
|
650
|
-
"runtime": "local",
|
|
651
|
-
"testResult": "Encryption bypassed for this stage"
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
# Response in a deployed stage with no key (the app itself refuses to start):
|
|
655
|
-
{
|
|
656
|
-
"status": "unhealthy",
|
|
657
|
-
"mode": "none",
|
|
658
|
-
"runtime": "deployed",
|
|
659
|
-
"testResult": "[Frigg] No field-level encryption key is configured for stage \"dev\". ..."
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
# Response in a deployed stage with FRIGG_ENCRYPTION_DISABLED=true and no key:
|
|
663
|
-
{
|
|
664
|
-
"status": "disabled",
|
|
665
|
-
"optedOut": true,
|
|
666
|
-
"testResult": "Encryption explicitly disabled (FRIGG_ENCRYPTION_DISABLED=true); sensitive fields are stored in plaintext"
|
|
621
|
+
"reason": "Encryption bypassed for stage: development"
|
|
667
622
|
}
|
|
668
623
|
```
|
|
669
624
|
|
|
@@ -777,10 +732,9 @@ FRIGG_LOG_LEVEL=INFO
|
|
|
777
732
|
**Check environment variables:**
|
|
778
733
|
|
|
779
734
|
```bash
|
|
735
|
+
echo $STAGE # Should be 'production' (not dev/test/local)
|
|
780
736
|
echo $KMS_KEY_ARN # Should be set (for KMS)
|
|
781
|
-
echo $AES_KEY_ID
|
|
782
|
-
echo $STAGE # Local runs only: dev/test/local skip encryption
|
|
783
|
-
echo $FRIGG_ENCRYPTION_DISABLED # Should be unset
|
|
737
|
+
echo $AES_KEY_ID # Should be set (for AES)
|
|
784
738
|
```
|
|
785
739
|
|
|
786
740
|
**Check console logs:**
|
|
@@ -795,22 +749,6 @@ or
|
|
|
795
749
|
[Frigg] Field-level encryption disabled
|
|
796
750
|
```
|
|
797
751
|
|
|
798
|
-
### Deployed Stage Refuses to Start
|
|
799
|
-
|
|
800
|
-
**Error: "EncryptionConfigurationError: [Frigg] No field-level encryption key is configured for stage ..."**
|
|
801
|
-
|
|
802
|
-
The function runs in AWS and has neither `KMS_KEY_ARN` nor `AES_KEY_ID` + `AES_KEY`. Earlier versions skipped encryption on stages named `dev`, `test` or `local` and wrote plaintext when no key was set; deployed stages now refuse instead. Fix one of:
|
|
803
|
-
|
|
804
|
-
1. Set `encryption: { fieldLevelEncryptionMethod: 'kms' }` in the app definition and redeploy. Frigg creates or discovers a KMS key and sets `KMS_KEY_ARN` on every function, whatever the stage name.
|
|
805
|
-
2. Provide `AES_KEY_ID` and `AES_KEY` (32 characters) through the app definition's `environment` and the deploy environment.
|
|
806
|
-
3. To keep the stage in plaintext on purpose, set `encryption: { fieldLevelEncryptionMethod: 'none' }` (or `FRIGG_ENCRYPTION_DISABLED=true`).
|
|
807
|
-
|
|
808
|
-
Data written in plaintext before the key existed stays readable: reads leave values that are not in the encrypted `keyId:iv:cipher:encKey` format untouched, and the next write of each record encrypts it. Records that are never rewritten stay in plaintext until you re-save them.
|
|
809
|
-
|
|
810
|
-
**Error: "[Frigg] AES_KEY_ID is set but AES_KEY is not"**
|
|
811
|
-
|
|
812
|
-
Provide `AES_KEY` alongside `AES_KEY_ID`, or switch to KMS.
|
|
813
|
-
|
|
814
752
|
### AWS KMS Errors
|
|
815
753
|
|
|
816
754
|
**Error: "User is not authorized to perform: kms:GenerateDataKey"**
|
|
@@ -225,84 +225,7 @@ Database (ENCRYPTED STORAGE) ✅ SECURE
|
|
|
225
225
|
1. **Consistency**: Same encryption format and Cryptor as Prisma Extension
|
|
226
226
|
2. **Reusability**: Single service used by all DocumentDB repositories
|
|
227
227
|
3. **Schema-Driven**: Uses `encryption-schema-registry.js` (same as Prisma)
|
|
228
|
-
4. **
|
|
229
|
-
5. **Error-Tolerant**: Graceful handling of decryption failures
|
|
230
|
-
6. **Testable**: Can be unit tested independently of repositories
|
|
231
|
-
|
|
232
|
-
---
|
|
233
|
-
|
|
234
|
-
## Technical Specification
|
|
235
|
-
|
|
236
|
-
### Class Design
|
|
237
|
-
|
|
238
|
-
```javascript
|
|
239
|
-
/**
|
|
240
|
-
* Encryption service specifically for DocumentDB repositories
|
|
241
|
-
* that use $runCommandRaw and bypass Prisma Extensions.
|
|
242
|
-
*
|
|
243
|
-
* Provides document-level encryption/decryption,
|
|
244
|
-
* handling nested fields according to the encryption schema registry.
|
|
245
|
-
*/
|
|
246
|
-
class DocumentDBEncryptionService {
|
|
247
|
-
constructor()
|
|
248
|
-
_initializeCryptor()
|
|
249
|
-
async encryptFields(modelName, document)
|
|
250
|
-
async decryptFields(modelName, document)
|
|
251
|
-
async _encryptFieldPath(document, fieldPath, modelName)
|
|
252
|
-
async _decryptFieldPath(document, fieldPath, modelName)
|
|
253
|
-
_isEncryptedValue(value)
|
|
254
|
-
}
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
### Method Specifications
|
|
258
|
-
|
|
259
|
-
#### `constructor()`
|
|
260
|
-
|
|
261
|
-
**Purpose**: Initialize the service and configure Cryptor
|
|
262
|
-
|
|
263
|
-
**Behavior**:
|
|
264
|
-
|
|
265
|
-
- Calls `_initializeCryptor()` immediately
|
|
266
|
-
- Sets up `this.cryptor` and `this.enabled` properties
|
|
267
|
-
|
|
268
|
-
**No parameters**
|
|
269
|
-
|
|
270
|
-
---
|
|
271
|
-
|
|
272
|
-
#### `_initializeCryptor()`
|
|
273
|
-
|
|
274
|
-
**Purpose**: Initialize Cryptor with environment-based configuration
|
|
275
|
-
|
|
276
|
-
**Logic**:
|
|
277
|
-
|
|
278
|
-
```javascript
|
|
279
|
-
1. loadCustomEncryptionSchema()
|
|
280
|
-
2. config = getEncryptionConfig() // encryption/encryption-config.js, shared with the Prisma client
|
|
281
|
-
- throws EncryptionConfigurationError in a deployed runtime with no key
|
|
282
|
-
3. If !config.enabled (local dev/test/local stage, local run without keys,
|
|
283
|
-
or FRIGG_ENCRYPTION_DISABLED=true opt-out):
|
|
284
|
-
- Set this.cryptor = null
|
|
285
|
-
- Set this.enabled = false
|
|
286
|
-
- Return
|
|
287
|
-
4. Create Cryptor({ shouldUseAws: config.method === 'kms' })
|
|
288
|
-
5. Set this.enabled = true
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
**Environment Variables Used** (see "When Encryption Runs" in README.md):
|
|
292
|
-
|
|
293
|
-
- `AWS_LAMBDA_FUNCTION_NAME` / `LAMBDA_TASK_ROOT` / `AWS_EXECUTION_ENV=AWS_Lambda_*`, minus `IS_OFFLINE` / `IS_LOCAL` / `JEST_WORKER_ID`: deployed or local run
|
|
294
|
-
- `STAGE` or `NODE_ENV`: dev/test/local skip encryption on local runs only
|
|
295
|
-
- `KMS_KEY_ARN`: AWS KMS key ARN (enables KMS encryption)
|
|
296
|
-
- `AES_KEY_ID`: AES key identifier (enables AES encryption)
|
|
297
|
-
- `AES_KEY`: AES encryption key (required with AES_KEY_ID in a deployed runtime)
|
|
298
|
-
- `FRIGG_ENCRYPTION_DISABLED`: explicit plaintext opt-out for a deployed stage with no key
|
|
299
|
-
|
|
300
|
-
### Design Principles
|
|
301
|
-
|
|
302
|
-
1. **Consistency**: Same encryption format and Cryptor as Prisma Extension
|
|
303
|
-
2. **Reusability**: Single service used by all DocumentDB repositories
|
|
304
|
-
3. **Schema-Driven**: Uses `encryption-schema-registry.js` (same as Prisma)
|
|
305
|
-
4. **Fails Closed**: Same rule as the Prisma client; a deployed runtime with no key throws, only local runs skip encryption
|
|
228
|
+
4. **Environment-Aware**: Respects STAGE-based bypass (dev/test/local)
|
|
306
229
|
5. **Error-Tolerant**: Graceful handling of decryption failures
|
|
307
230
|
6. **Testable**: Can be unit tested independently of repositories
|
|
308
231
|
|
package/database/prisma.js
CHANGED
|
@@ -4,7 +4,6 @@ const {
|
|
|
4
4
|
const { loadCustomEncryptionSchema } = require('./encryption/encryption-schema-registry');
|
|
5
5
|
const { logger } = require('./encryption/logger');
|
|
6
6
|
const { Cryptor } = require('../encrypt/Cryptor');
|
|
7
|
-
const { getEncryptionConfig } = require('./encryption/encryption-config');
|
|
8
7
|
const config = require('./config');
|
|
9
8
|
|
|
10
9
|
/**
|
|
@@ -33,6 +32,33 @@ function ensureMongoDbUrl() {
|
|
|
33
32
|
);
|
|
34
33
|
}
|
|
35
34
|
|
|
35
|
+
function getEncryptionConfig() {
|
|
36
|
+
const STAGE = process.env.STAGE || process.env.NODE_ENV || 'development';
|
|
37
|
+
const shouldBypassEncryption = ['dev', 'test', 'local'].includes(STAGE);
|
|
38
|
+
|
|
39
|
+
if (shouldBypassEncryption) {
|
|
40
|
+
return { enabled: false };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const hasKMS =
|
|
44
|
+
process.env.KMS_KEY_ARN && process.env.KMS_KEY_ARN.trim() !== '';
|
|
45
|
+
const hasAES =
|
|
46
|
+
process.env.AES_KEY_ID && process.env.AES_KEY_ID.trim() !== '';
|
|
47
|
+
|
|
48
|
+
if (!hasKMS && !hasAES) {
|
|
49
|
+
logger.warn(
|
|
50
|
+
'No encryption keys configured (KMS_KEY_ARN or AES_KEY_ID). ' +
|
|
51
|
+
'Field-level encryption disabled. Set STAGE=production and configure keys to enable.'
|
|
52
|
+
);
|
|
53
|
+
return { enabled: false };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
enabled: true,
|
|
58
|
+
method: hasKMS ? 'kms' : 'aes',
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
36
62
|
const prismaClientSingleton = () => {
|
|
37
63
|
let PrismaClient;
|
|
38
64
|
|
|
@@ -77,13 +103,9 @@ const prismaClientSingleton = () => {
|
|
|
77
103
|
errorFormat: 'pretty',
|
|
78
104
|
});
|
|
79
105
|
|
|
80
|
-
// Throws when a deployed runtime has no encryption key: the app must not
|
|
81
|
-
// start and write sensitive fields in plaintext.
|
|
82
106
|
const encryptionConfig = getEncryptionConfig();
|
|
83
107
|
|
|
84
108
|
if (encryptionConfig.enabled) {
|
|
85
|
-
// Fail closed: if the extension cannot be installed, do not hand out
|
|
86
|
-
// a client that would write plaintext.
|
|
87
109
|
try {
|
|
88
110
|
// Load custom encryption schema from appDefinition before creating extension
|
|
89
111
|
loadCustomEncryptionSchema();
|
|
@@ -98,14 +120,17 @@ const prismaClientSingleton = () => {
|
|
|
98
120
|
enabled: true,
|
|
99
121
|
})
|
|
100
122
|
);
|
|
123
|
+
|
|
124
|
+
logger.info(
|
|
125
|
+
`Field-level encryption enabled using ${encryptionConfig.method.toUpperCase()}`
|
|
126
|
+
);
|
|
101
127
|
} catch (error) {
|
|
102
|
-
logger.error(
|
|
103
|
-
|
|
128
|
+
logger.error(
|
|
129
|
+
'Failed to initialize encryption extension:',
|
|
130
|
+
error
|
|
131
|
+
);
|
|
132
|
+
logger.warn('Continuing without encryption...');
|
|
104
133
|
}
|
|
105
|
-
|
|
106
|
-
logger.info(
|
|
107
|
-
`Field-level encryption enabled using ${encryptionConfig.method.toUpperCase()}`
|
|
108
|
-
);
|
|
109
134
|
} else {
|
|
110
135
|
logger.info('Field-level encryption disabled');
|
|
111
136
|
}
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
const {
|
|
2
|
-
resolveEncryptionConfig,
|
|
3
|
-
OPT_OUT_VAR,
|
|
4
|
-
} = require('../encryption/encryption-config');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Reports whether field-level encryption is on and working, using the same
|
|
8
|
-
* rule the Prisma client applies at startup (encryption/encryption-config.js).
|
|
9
|
-
*/
|
|
10
1
|
class CheckEncryptionHealthUseCase {
|
|
11
2
|
constructor({ testEncryptionUseCase }) {
|
|
12
3
|
this.testEncryptionUseCase = testEncryptionUseCase;
|
|
@@ -14,60 +5,78 @@ class CheckEncryptionHealthUseCase {
|
|
|
14
5
|
|
|
15
6
|
async execute() {
|
|
16
7
|
const config = this._getEncryptionConfiguration();
|
|
17
|
-
const summary = {
|
|
18
|
-
mode: config.mode,
|
|
19
|
-
bypassed: config.bypassed,
|
|
20
|
-
optedOut: config.optedOut,
|
|
21
|
-
runtime: config.runtime,
|
|
22
|
-
stage: config.stage,
|
|
23
|
-
debug: {
|
|
24
|
-
hasKMS: config.hasKMS,
|
|
25
|
-
hasAES: config.hasAES,
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
8
|
|
|
29
|
-
if (config.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
testResult: config.error,
|
|
34
|
-
encryptionWorks: false,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
9
|
+
if (config.isBypassed || config.mode === 'none') {
|
|
10
|
+
const testResult = config.isBypassed
|
|
11
|
+
? 'Encryption bypassed for this stage'
|
|
12
|
+
: 'No encryption keys configured';
|
|
37
13
|
|
|
38
|
-
if (!config.enabled) {
|
|
39
14
|
return {
|
|
40
15
|
status: 'disabled',
|
|
41
|
-
|
|
42
|
-
|
|
16
|
+
mode: config.mode,
|
|
17
|
+
bypassed: config.isBypassed,
|
|
18
|
+
stage: config.stage,
|
|
19
|
+
testResult,
|
|
43
20
|
encryptionWorks: false,
|
|
21
|
+
debug: {
|
|
22
|
+
hasKMS: config.hasKMS,
|
|
23
|
+
hasAES: config.hasAES,
|
|
24
|
+
},
|
|
44
25
|
};
|
|
45
26
|
}
|
|
46
27
|
|
|
47
28
|
try {
|
|
48
29
|
const testResults = await this.testEncryptionUseCase.execute();
|
|
49
|
-
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
...testResults,
|
|
33
|
+
mode: config.mode,
|
|
34
|
+
bypassed: config.isBypassed,
|
|
35
|
+
stage: config.stage,
|
|
36
|
+
debug: {
|
|
37
|
+
hasKMS: config.hasKMS,
|
|
38
|
+
hasAES: config.hasAES,
|
|
39
|
+
},
|
|
40
|
+
};
|
|
50
41
|
} catch (error) {
|
|
51
42
|
return {
|
|
52
43
|
status: 'unhealthy',
|
|
53
|
-
|
|
44
|
+
mode: config.mode,
|
|
45
|
+
bypassed: config.isBypassed,
|
|
46
|
+
stage: config.stage,
|
|
54
47
|
testResult: `Encryption test failed: ${error.message}`,
|
|
55
48
|
encryptionWorks: false,
|
|
49
|
+
debug: {
|
|
50
|
+
hasKMS: config.hasKMS,
|
|
51
|
+
hasAES: config.hasAES,
|
|
52
|
+
},
|
|
56
53
|
};
|
|
57
54
|
}
|
|
58
55
|
}
|
|
59
56
|
|
|
60
|
-
_disabledReason(config) {
|
|
61
|
-
if (config.bypassed) return 'Encryption bypassed for this stage';
|
|
62
|
-
if (config.optedOut) {
|
|
63
|
-
return `Encryption explicitly disabled (${OPT_OUT_VAR}=true); sensitive fields are stored in plaintext`;
|
|
64
|
-
}
|
|
65
|
-
return 'No encryption keys configured';
|
|
66
|
-
}
|
|
67
|
-
|
|
68
57
|
_getEncryptionConfiguration() {
|
|
69
|
-
const
|
|
70
|
-
|
|
58
|
+
const { STAGE, BYPASS_ENCRYPTION_STAGE, KMS_KEY_ARN, AES_KEY_ID } =
|
|
59
|
+
process.env;
|
|
60
|
+
|
|
61
|
+
const defaultBypassStages = ['dev', 'test', 'local'];
|
|
62
|
+
const useEnv = BYPASS_ENCRYPTION_STAGE !== undefined;
|
|
63
|
+
const bypassStages = useEnv
|
|
64
|
+
? BYPASS_ENCRYPTION_STAGE.split(',').map((s) => s.trim())
|
|
65
|
+
: defaultBypassStages;
|
|
66
|
+
|
|
67
|
+
const isBypassed = bypassStages.includes(STAGE);
|
|
68
|
+
const hasAES = AES_KEY_ID && AES_KEY_ID.trim() !== '';
|
|
69
|
+
const hasKMS = KMS_KEY_ARN && KMS_KEY_ARN.trim() !== '';
|
|
70
|
+
// Prefer KMS over AES when both are configured (KMS is more secure)
|
|
71
|
+
const mode = hasKMS ? 'kms' : hasAES ? 'aes' : 'none';
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
stage: STAGE || null,
|
|
75
|
+
isBypassed,
|
|
76
|
+
hasAES,
|
|
77
|
+
hasKMS,
|
|
78
|
+
mode,
|
|
79
|
+
};
|
|
71
80
|
}
|
|
72
81
|
}
|
|
73
82
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/core",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "2.0.0--canary.
|
|
4
|
+
"version": "2.0.0--canary.664.a0b36e4.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
|
|
7
7
|
"@aws-sdk/client-kms": "^3.588.0",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
}
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@friggframework/eslint-config": "2.0.0--canary.
|
|
51
|
-
"@friggframework/prettier-config": "2.0.0--canary.
|
|
52
|
-
"@friggframework/test": "2.0.0--canary.
|
|
50
|
+
"@friggframework/eslint-config": "2.0.0--canary.664.a0b36e4.0",
|
|
51
|
+
"@friggframework/prettier-config": "2.0.0--canary.664.a0b36e4.0",
|
|
52
|
+
"@friggframework/test": "2.0.0--canary.664.a0b36e4.0",
|
|
53
53
|
"@prisma/client": "^6.19.3",
|
|
54
54
|
"@types/lodash": "4.17.15",
|
|
55
55
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
@@ -89,5 +89,5 @@
|
|
|
89
89
|
"publishConfig": {
|
|
90
90
|
"access": "public"
|
|
91
91
|
},
|
|
92
|
-
"gitHead": "
|
|
92
|
+
"gitHead": "a0b36e4677d1a9b2b0387e9b2b6b59ab76cf8098"
|
|
93
93
|
}
|
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Field-level encryption configuration: decides whether sensitive fields are
|
|
3
|
-
* encrypted, and with which key, from the process environment.
|
|
4
|
-
*
|
|
5
|
-
* The rule fails closed:
|
|
6
|
-
*
|
|
7
|
-
* - **Deployed** (running in AWS Lambda, and not under
|
|
8
|
-
* serverless-offline, `serverless invoke local` or Jest): the stage name is
|
|
9
|
-
* ignored. A configured key (KMS_KEY_ARN, or AES_KEY_ID with AES_KEY) turns
|
|
10
|
-
* encryption on. With no key, the configuration is an error and the Prisma
|
|
11
|
-
* client refuses to start, unless FRIGG_ENCRYPTION_DISABLED=true opts out
|
|
12
|
-
* explicitly.
|
|
13
|
-
* - **Local** (`frigg start`, tests, scripts on a laptop): the stages dev,
|
|
14
|
-
* test and local skip encryption. Any other stage encrypts when a key is
|
|
15
|
-
* configured and stays plaintext, with a warning, when none is.
|
|
16
|
-
*
|
|
17
|
-
* Every module that encrypts or reports on encryption reads this one rule.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
const { logger } = require('./logger');
|
|
21
|
-
|
|
22
|
-
const LOCAL_BYPASS_STAGES = ['dev', 'test', 'local'];
|
|
23
|
-
const OPT_OUT_VAR = 'FRIGG_ENCRYPTION_DISABLED';
|
|
24
|
-
|
|
25
|
-
class EncryptionConfigurationError extends Error {
|
|
26
|
-
constructor(message) {
|
|
27
|
-
super(message);
|
|
28
|
-
this.name = 'EncryptionConfigurationError';
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function isSet(value) {
|
|
33
|
-
return typeof value === 'string' && value.trim() !== '';
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function isTrue(value) {
|
|
37
|
-
if (typeof value !== 'string') return false;
|
|
38
|
-
const normalized = value.trim().toLowerCase();
|
|
39
|
-
return normalized === 'true' || normalized === '1';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* True when the process runs in AWS Lambda for real. The Lambda runtime sets
|
|
44
|
-
* AWS_LAMBDA_FUNCTION_NAME, LAMBDA_TASK_ROOT and AWS_EXECUTION_ENV
|
|
45
|
-
* (AWS_Lambda_<runtime>). serverless-offline sets the first two as well, so
|
|
46
|
-
* IS_OFFLINE, IS_LOCAL and Jest mark a local run. Other AWS hosts (CloudShell,
|
|
47
|
-
* CodeBuild, ECS) set AWS_EXECUTION_ENV to other values and count as local:
|
|
48
|
-
* Frigg deploys its handlers to Lambda only, and CLI commands run from CI must
|
|
49
|
-
* not trip the deployed-stage check.
|
|
50
|
-
*
|
|
51
|
-
* @param {Object} [env=process.env]
|
|
52
|
-
* @returns {boolean}
|
|
53
|
-
*/
|
|
54
|
-
function isDeployedRuntime(env = process.env) {
|
|
55
|
-
const onAwsRuntime =
|
|
56
|
-
isSet(env.AWS_LAMBDA_FUNCTION_NAME) ||
|
|
57
|
-
isSet(env.LAMBDA_TASK_ROOT) ||
|
|
58
|
-
String(env.AWS_EXECUTION_ENV ?? '').startsWith('AWS_Lambda_');
|
|
59
|
-
if (!onAwsRuntime) return false;
|
|
60
|
-
|
|
61
|
-
const localRun =
|
|
62
|
-
isTrue(env.IS_OFFLINE) ||
|
|
63
|
-
isTrue(env.IS_LOCAL) ||
|
|
64
|
-
isSet(env.JEST_WORKER_ID);
|
|
65
|
-
return !localRun;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function missingKeyMessage(stage) {
|
|
69
|
-
return (
|
|
70
|
-
`[Frigg] No field-level encryption key is configured for stage "${stage}". ` +
|
|
71
|
-
'A deployed Frigg app will not write credentials and other sensitive fields in plaintext. ' +
|
|
72
|
-
"Fix: set encryption: { fieldLevelEncryptionMethod: 'kms' } in the app definition and redeploy " +
|
|
73
|
-
'(Frigg creates or discovers a KMS key and sets KMS_KEY_ARN), ' +
|
|
74
|
-
'or provide AES_KEY_ID and AES_KEY (32 characters) in the Lambda environment. ' +
|
|
75
|
-
`To run this stage without encryption on purpose, set ${OPT_OUT_VAR}=true ` +
|
|
76
|
-
"(or encryption: { fieldLevelEncryptionMethod: 'none' } in the app definition)."
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function incompleteAesMessage(stage) {
|
|
81
|
-
return (
|
|
82
|
-
`[Frigg] AES_KEY_ID is set but AES_KEY is not (stage "${stage}"). ` +
|
|
83
|
-
'Provide AES_KEY (32 characters) alongside AES_KEY_ID, ' +
|
|
84
|
-
"or use KMS with encryption: { fieldLevelEncryptionMethod: 'kms' }."
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Resolves the encryption configuration without side effects.
|
|
90
|
-
*
|
|
91
|
-
* @param {Object} [env=process.env]
|
|
92
|
-
* @returns {{
|
|
93
|
-
* enabled: boolean,
|
|
94
|
-
* method: 'kms'|'aes'|undefined,
|
|
95
|
-
* mode: 'kms'|'aes'|'none',
|
|
96
|
-
* runtime: 'deployed'|'local',
|
|
97
|
-
* stage: string,
|
|
98
|
-
* bypassed: boolean,
|
|
99
|
-
* optedOut: boolean,
|
|
100
|
-
* optOutIgnored: boolean,
|
|
101
|
-
* hasKMS: boolean,
|
|
102
|
-
* hasAES: boolean,
|
|
103
|
-
* error: string|null,
|
|
104
|
-
* }} `error` is set when a deployed runtime has no usable key.
|
|
105
|
-
*/
|
|
106
|
-
function resolveEncryptionConfig(env = process.env) {
|
|
107
|
-
const stage = env.STAGE || env.NODE_ENV || 'development';
|
|
108
|
-
const runtime = isDeployedRuntime(env) ? 'deployed' : 'local';
|
|
109
|
-
const hasKMS = isSet(env.KMS_KEY_ARN);
|
|
110
|
-
const hasAES = isSet(env.AES_KEY_ID);
|
|
111
|
-
const optOutRequested = isTrue(env[OPT_OUT_VAR]);
|
|
112
|
-
|
|
113
|
-
const base = {
|
|
114
|
-
enabled: false,
|
|
115
|
-
method: undefined,
|
|
116
|
-
mode: 'none',
|
|
117
|
-
runtime,
|
|
118
|
-
stage,
|
|
119
|
-
bypassed: false,
|
|
120
|
-
optedOut: false,
|
|
121
|
-
optOutIgnored: false,
|
|
122
|
-
hasKMS,
|
|
123
|
-
hasAES,
|
|
124
|
-
error: null,
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
if (runtime === 'local') {
|
|
128
|
-
if (LOCAL_BYPASS_STAGES.includes(String(stage).toLowerCase())) {
|
|
129
|
-
return { ...base, bypassed: true };
|
|
130
|
-
}
|
|
131
|
-
if (hasKMS || hasAES) {
|
|
132
|
-
const method = hasKMS ? 'kms' : 'aes';
|
|
133
|
-
return { ...base, enabled: true, method, mode: method };
|
|
134
|
-
}
|
|
135
|
-
return base;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const hasCompleteAES = hasAES && isSet(env.AES_KEY);
|
|
139
|
-
if (hasKMS || hasCompleteAES) {
|
|
140
|
-
const method = hasKMS ? 'kms' : 'aes';
|
|
141
|
-
return {
|
|
142
|
-
...base,
|
|
143
|
-
enabled: true,
|
|
144
|
-
method,
|
|
145
|
-
mode: method,
|
|
146
|
-
optOutIgnored: optOutRequested,
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (optOutRequested) {
|
|
151
|
-
return { ...base, optedOut: true };
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return {
|
|
155
|
-
...base,
|
|
156
|
-
error: hasAES ? incompleteAesMessage(stage) : missingKeyMessage(stage),
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const warned = new Set();
|
|
161
|
-
|
|
162
|
-
function warnOnce(key, message) {
|
|
163
|
-
if (warned.has(key)) return;
|
|
164
|
-
warned.add(key);
|
|
165
|
-
logger.warn(message);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Resolves the encryption configuration for use: throws when a deployed
|
|
170
|
-
* runtime has no usable key, and warns once per process (once per Lambda
|
|
171
|
-
* cold start) when encryption is explicitly disabled.
|
|
172
|
-
*
|
|
173
|
-
* @param {Object} [env=process.env]
|
|
174
|
-
* @returns {ReturnType<typeof resolveEncryptionConfig>}
|
|
175
|
-
* @throws {EncryptionConfigurationError}
|
|
176
|
-
*/
|
|
177
|
-
function getEncryptionConfig(env = process.env) {
|
|
178
|
-
const config = resolveEncryptionConfig(env);
|
|
179
|
-
|
|
180
|
-
if (config.error) {
|
|
181
|
-
throw new EncryptionConfigurationError(config.error);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (config.optedOut) {
|
|
185
|
-
warnOnce(
|
|
186
|
-
'opted-out',
|
|
187
|
-
`[Frigg] ${OPT_OUT_VAR}=true: field-level encryption is OFF for stage "${config.stage}". ` +
|
|
188
|
-
'Credentials and other sensitive fields are stored in PLAINTEXT. ' +
|
|
189
|
-
`Remove ${OPT_OUT_VAR} and configure a key (fieldLevelEncryptionMethod: 'kms') to encrypt them.`
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (config.optOutIgnored) {
|
|
194
|
-
warnOnce(
|
|
195
|
-
'opt-out-ignored',
|
|
196
|
-
`[Frigg] ${OPT_OUT_VAR}=true is ignored because an encryption key is configured; ` +
|
|
197
|
-
`fields are encrypted with ${config.method.toUpperCase()}. ` +
|
|
198
|
-
'Turning encryption off while a key is present would leave encrypted data unreadable.'
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (config.runtime === 'local' && !config.enabled && !config.bypassed) {
|
|
203
|
-
warnOnce(
|
|
204
|
-
'local-no-keys',
|
|
205
|
-
`No encryption keys configured (KMS_KEY_ARN or AES_KEY_ID) for local stage "${config.stage}". ` +
|
|
206
|
-
'Field-level encryption disabled for this local run. ' +
|
|
207
|
-
'A deployed stage without a key refuses to start.'
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return config;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** Test helper: let the once-per-process warnings fire again. */
|
|
215
|
-
function resetEncryptionConfigWarnings() {
|
|
216
|
-
warned.clear();
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
module.exports = {
|
|
220
|
-
EncryptionConfigurationError,
|
|
221
|
-
LOCAL_BYPASS_STAGES,
|
|
222
|
-
OPT_OUT_VAR,
|
|
223
|
-
getEncryptionConfig,
|
|
224
|
-
isDeployedRuntime,
|
|
225
|
-
resetEncryptionConfigWarnings,
|
|
226
|
-
resolveEncryptionConfig,
|
|
227
|
-
};
|