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