|
6 | 6 | DecryptionMaterial,
|
7 | 7 | SupportedAlgorithmSuites,
|
8 | 8 | EncryptedDataKey,
|
| 9 | + needs, |
9 | 10 | } from '@aws-crypto/material-management'
|
10 | 11 |
|
11 | 12 | export interface RawKeyRing<S extends SupportedAlgorithmSuites> {
|
@@ -47,17 +48,37 @@ export function _onDecrypt<
|
47 | 48 | /* Check for early return (Postcondition): If there are not EncryptedDataKeys for this keyring, do nothing. */
|
48 | 49 | if (!edks.length) return material
|
49 | 50 |
|
| 51 | + const cmkErrors: Error[] = [] |
| 52 | + |
50 | 53 | for (const edk of edks) {
|
51 | 54 | try {
|
52 | 55 | return await this._unwrapKey(material, edk)
|
53 | 56 | } catch (e) {
|
54 |
| - // there should be some debug here? or wrap? |
55 |
| - // Failures decrypt should not short-circuit the process |
56 |
| - // If the caller does not have access they may have access |
57 |
| - // through another Keyring. |
| 57 | + /* Failures onDecrypt should not short-circuit the process |
| 58 | + * If the caller does not have access they may have access |
| 59 | + * through another Keyring. |
| 60 | + */ |
| 61 | + cmkErrors.push(e) |
58 | 62 | }
|
59 | 63 | }
|
60 | 64 |
|
| 65 | + /* Postcondition: An EDK must provide a valid data key or _unwrapKey must not have raised any errors. |
| 66 | + * If I have a data key, |
| 67 | + * decrypt errors can be ignored. |
| 68 | + * However, if I was unable to decrypt a data key AND I have errors, |
| 69 | + * these errors should bubble up. |
| 70 | + * Otherwise, the only error customers will see is that |
| 71 | + * the material does not have an unencrypted data key. |
| 72 | + * So I return a concatenated Error message |
| 73 | + */ |
| 74 | + needs( |
| 75 | + material.hasValidKey() || (!material.hasValidKey() && !cmkErrors.length), |
| 76 | + cmkErrors.reduce( |
| 77 | + (m, e, i) => `${m} Error #${i + 1} \n ${e.stack} \n`, |
| 78 | + `Unable to decrypt data key ${this.keyName} ${this.keyNamespace}.\n ` |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
61 | 82 | return material
|
62 | 83 | }
|
63 | 84 | }
|
|
0 commit comments