Skip to content

Commit 613c0af

Browse files
authored
fix: lint and tests (#43)
Integrating many modules together in individual PRs is hard. I’m not surprised that I missed something.
1 parent 7a14870 commit 613c0af

File tree

6 files changed

+85
-82
lines changed

6 files changed

+85
-82
lines changed

modules/decrypt-node/src/parse_header_stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class ParseHeaderStream extends PortableTransformWithType {
7878
const verify = getVerify ? getVerify() : void 0
7979
const verifyInfo: VerifyInfo = { headerInfo, getDecipher, verify, dispose }
8080
this.emit('VerifyInfo', verifyInfo)
81-
this.emit('MessageHeader', headerInfo)
81+
this.emit('MessageHeader', headerInfo.messageHeader)
8282

8383
// The header is parsed, pass control
8484
const readPos = rawHeader.byteLength + headerIv.byteLength + headerAuthTag.byteLength

modules/encrypt-node/test/encrypt.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616
/* eslint-env mocha */
1717

1818
// import { expect } from 'chai'
19-
import 'mocha'
20-
import {
21-
NodeDecryptionMaterial, // eslint-disable-line no-unused-vars
22-
NodeAlgorithmSuite, NodeEncryptionMaterial, NodeCryptographicMaterialsManager, KeyringNode, EncryptedDataKey,
23-
KeyringTraceFlag, AlgorithmSuiteIdentifier
24-
} from '@aws-crypto/material-management-node'
19+
// import 'mocha'
20+
// import {
21+
// NodeDecryptionMaterial, // eslint-disable-line no-unused-vars
22+
// NodeAlgorithmSuite, NodeEncryptionMaterial, NodeCryptographicMaterialsManager, KeyringNode, EncryptedDataKey,
23+
// KeyringTraceFlag, AlgorithmSuiteIdentifier
24+
// } from '@aws-crypto/material-management-node'
2525

26-
import * as fs from 'fs'
26+
// import * as fs from 'fs'
2727

28-
import { encryptStream, getEncryptionInfo } from '../src/encrypt_stream'
28+
// import { encryptStream, getEncryptionInfo } from '../src/encrypt_stream'
2929

30-
import { getFramedEncryptStream } from '../src/framed_encrypt_stream'
31-
import { SignatureStream } from '../src/signature_stream'
32-
import { encrypt } from '../src/encrypt'
30+
// import { getFramedEncryptStream } from '../src/framed_encrypt_stream'
31+
// import { SignatureStream } from '../src/signature_stream'
32+
// import { encrypt } from '../src/encrypt'
3333

34-
const never = () => { throw new Error('never') }
34+
// const never = () => { throw new Error('never') }

modules/kms-keyring/src/kms_keyring.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ export function KmsKeyringClass<S extends SupportedAlgorithmSuites, Client exten
7373
/* Precondition: This is an abstract class. (But TypeScript does not have a clean way to model this) */
7474
needs(this.constructor !== KmsKeyring, 'new KmsKeyring is not allowed')
7575
/* Precondition: A noop KmsKeyring is not allowed. */
76-
needs(!discovery && !generatorKeyId && !keyIds.length, 'Noop keyring is not allowed: Set a keyId or discovery')
76+
needs(!(!discovery && !generatorKeyId && !keyIds.length), 'Noop keyring is not allowed: Set a keyId or discovery')
7777
/* Precondition: A keyring can be either a Discovery or have keyIds configured. */
78-
needs(discovery && (generatorKeyId || keyIds.length), 'A keyring can be either a Discovery or have keyIds configured.')
78+
needs(!(discovery && (generatorKeyId || keyIds.length)), 'A keyring can be either a Discovery or have keyIds configured.')
7979
/* Precondition: All KMS key arns must be valid. */
8080
needs(!generatorKeyId || !!regionFromKmsKeyArn(generatorKeyId), 'Malformed arn.')
8181
needs(keyIds.every(keyarn => !!regionFromKmsKeyArn(keyarn)), 'Malformed arn.')

modules/kms-keyring/test/kms_keyring.constructor.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,15 @@ describe('KmsKeyring: constructor', () => {
5454
expect(test.isDiscovery).to.equal(true)
5555
})
5656

57-
it('Precondition: A noop KmsKeyring is not allowed. You must explicitly set discovery or keyIds.', () => {
57+
it('Precondition: A noop KmsKeyring is not allowed.', () => {
5858
class TestKmsKeyring extends KmsKeyringClass(Keyring as KeyRingConstructible<NodeAlgorithmSuite>) {}
5959
const clientProvider: any = () => {}
6060
expect(() => new TestKmsKeyring({ clientProvider })).to.throw()
61+
})
6162

63+
it('Precondition: A keyring can be either a Discovery or have keyIds configured.', () => {
64+
class TestKmsKeyring extends KmsKeyringClass(Keyring as KeyRingConstructible<NodeAlgorithmSuite>) {}
65+
const clientProvider: any = () => {}
6266
const generatorKeyId = 'arn:aws:kms:us-east-1:123456789012:alias/example-alias'
6367
const keyIds = ['arn:aws:kms:us-east-1:123456789012:alias/example-alias']
6468
const discovery = true

modules/raw-keyring/src/raw_aes_encrypted_data_keys.ts

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -26,73 +26,72 @@
2626
* The AAD (encryption context) is the same as the message.
2727
*/
2828

29-
import { concatBuffers, uInt32BE } from '@aws-crypto/serialize'
30-
import {
31-
AlgorithmSuite, // eslint-disable-line no-unused-vars
32-
EncryptedDataKey, // eslint-disable-line no-unused-vars
33-
needs
34-
} from '@aws-crypto/material-management'
35-
36-
export function rawAesEncryptedDataKeyFactory (
37-
toUtf8: (input: Uint8Array) => string,
38-
fromUtf8: (input: any) => Uint8Array
39-
) {
40-
return { rawAesEncryptedDataKey }
41-
42-
function rawAesEncryptedDataKey (
43-
keyNamespace: string,
44-
keyName: string,
45-
iv: Uint8Array,
46-
ciphertext: Uint8Array,
47-
authTag: Uint8Array
48-
): EncryptedDataKey {
49-
const ivLength = iv.byteLength
50-
const authTagBitLength = authTag.byteLength * 8
51-
const encryptedDataKey = concatBuffers(ciphertext, authTag)
52-
const providerId = keyNamespace
53-
const rawInfo = concatBuffers(fromUtf8(keyName), uInt32BE(authTagBitLength), uInt32BE(ivLength), iv)
54-
const providerInfo = toUtf8(rawInfo)
55-
return new EncryptedDataKey({ encryptedDataKey, providerId, providerInfo, rawInfo })
56-
}
57-
}
58-
59-
export function rawAesEncryptedPartsFactory (fromUtf8: (input: any) => Uint8Array) {
60-
return { rawAesEncryptedParts }
61-
62-
function rawAesEncryptedParts (suite: AlgorithmSuite, keyName: string, { encryptedDataKey, rawInfo }: EncryptedDataKey) {
63-
if (!(rawInfo instanceof Uint8Array)) throw new Error('Malformed Encrypted Data Key.')
64-
// see above for format, slice off the "string part"
65-
rawInfo = rawInfo.slice(fromUtf8(keyName).byteLength)
66-
/* Uint8Array is a view on top of the underlying ArrayBuffer.
29+
import { concatBuffers, uInt32BE } from '@aws-crypto/serialize'
30+
import {
31+
AlgorithmSuite, // eslint-disable-line no-unused-vars
32+
EncryptedDataKey, // eslint-disable-line no-unused-vars
33+
needs
34+
} from '@aws-crypto/material-management'
35+
36+
export function rawAesEncryptedDataKeyFactory (
37+
toUtf8: (input: Uint8Array) => string,
38+
fromUtf8: (input: any) => Uint8Array
39+
) {
40+
return { rawAesEncryptedDataKey }
41+
42+
function rawAesEncryptedDataKey (
43+
keyNamespace: string,
44+
keyName: string,
45+
iv: Uint8Array,
46+
ciphertext: Uint8Array,
47+
authTag: Uint8Array
48+
): EncryptedDataKey {
49+
const ivLength = iv.byteLength
50+
const authTagBitLength = authTag.byteLength * 8
51+
const encryptedDataKey = concatBuffers(ciphertext, authTag)
52+
const providerId = keyNamespace
53+
const rawInfo = concatBuffers(fromUtf8(keyName), uInt32BE(authTagBitLength), uInt32BE(ivLength), iv)
54+
const providerInfo = toUtf8(rawInfo)
55+
return new EncryptedDataKey({ encryptedDataKey, providerId, providerInfo, rawInfo })
56+
}
57+
}
58+
59+
export function rawAesEncryptedPartsFactory (fromUtf8: (input: any) => Uint8Array) {
60+
return { rawAesEncryptedParts }
61+
62+
function rawAesEncryptedParts (suite: AlgorithmSuite, keyName: string, { encryptedDataKey, rawInfo }: EncryptedDataKey) {
63+
if (!(rawInfo instanceof Uint8Array)) throw new Error('Malformed Encrypted Data Key.')
64+
// see above for format, slice off the "string part"
65+
rawInfo = rawInfo.slice(fromUtf8(keyName).byteLength)
66+
/* Uint8Array is a view on top of the underlying ArrayBuffer.
6767
* This means that raw underlying memory stored in the ArrayBuffer
6868
* may be larger than the Uint8Array. This is especially true of
6969
* the Node.js Buffer object. The offset and length *must* be
7070
* passed to the DataView otherwise I will get unexpected results.
7171
*/
72-
const dataView = new DataView(
73-
rawInfo.buffer,
74-
rawInfo.byteOffset,
75-
rawInfo.byteLength
76-
)
77-
/* See above:
72+
const dataView = new DataView(
73+
rawInfo.buffer,
74+
rawInfo.byteOffset,
75+
rawInfo.byteLength
76+
)
77+
/* See above:
7878
* uInt32BE(authTagBitLength),uInt32BE(ivLength), iv
7979
*/
80-
const tagLengthBits = dataView.getUint32(0, false) // big endian
81-
const ivLength = dataView.getUint32(4, false) // big endian
82-
/* Precondition: The ivLength must match the algorith suite specification. */
83-
needs(ivLength === suite.ivLength, 'Malformed providerInfo')
84-
/* Precondition: The tagLength must match the algorith suite specification. */
85-
needs(tagLengthBits === suite.tagLength, 'Malformed providerInfo')
86-
/* Precondition: The byteLength of rawInfo should match the encoded length. */
87-
needs(rawInfo.byteLength === 4 + 4 + ivLength, 'Malformed providerInfo')
88-
const tagLength = tagLengthBits / 8
89-
/* Precondition: The encryptedDataKey byteLength must match the algorith suite specification and encoded length. */
90-
needs(encryptedDataKey.byteLength === tagLength + suite.keyLengthBytes, 'Malformed providerInfo')
91-
const iv = rawInfo.slice(-ivLength)
92-
const authTag = encryptedDataKey.slice(-tagLength)
93-
const ciphertext = encryptedDataKey.slice(0, -tagLength)
94-
95-
return { authTag, ciphertext, iv }
96-
}
97-
}
98-
80+
const tagLengthBits = dataView.getUint32(0, false) // big endian
81+
const ivLength = dataView.getUint32(4, false) // big endian
82+
/* Precondition: The ivLength must match the algorith suite specification. */
83+
needs(ivLength === suite.ivLength, 'Malformed providerInfo')
84+
/* Precondition: The tagLength must match the algorith suite specification. */
85+
needs(tagLengthBits === suite.tagLength, 'Malformed providerInfo')
86+
/* Precondition: The byteLength of rawInfo should match the encoded length. */
87+
needs(rawInfo.byteLength === 4 + 4 + ivLength, 'Malformed providerInfo')
88+
const tagLength = tagLengthBits / 8
89+
/* Precondition: The encryptedDataKey byteLength must match the algorith suite specification and encoded length. */
90+
needs(encryptedDataKey.byteLength === tagLength + suite.keyLengthBytes, 'Malformed providerInfo')
91+
const iv = rawInfo.slice(-ivLength)
92+
const authTag = encryptedDataKey.slice(-tagLength)
93+
const ciphertext = encryptedDataKey.slice(0, -tagLength)
94+
95+
return { authTag, ciphertext, iv }
96+
}
97+
}

modules/raw-keyring/src/raw_aes_material.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
/* Here I am reusing the Material implementation and interface from material-management.
16+
/* Here I am reusing the Material implementation and interface from material-management.
1717
* This is because there are many security guarantees that this implementations offer
1818
* that map to the current implementation of raw AES keyrings.
1919
* The KeyringTrace is an unfortunate case because there is no mapping.
@@ -55,7 +55,7 @@ export class NodeRawAesMaterial implements
5555
/* NodeRawAesMaterial need to set a flag, this is an abuse of TraceFlags
5656
* because the material is not generated.
5757
* but CryptographicMaterial force a flag to be set.
58-
*/
58+
*/
5959
const setFlags = KeyringTraceFlag.WRAPPING_KEY_GENERATED_DATA_KEY
6060
decorateCryptographicMaterial<NodeRawAesMaterial>(this, setFlags)
6161
Object.setPrototypeOf(this, NodeRawAesMaterial.prototype)
@@ -87,7 +87,7 @@ export class WebCryptoRawAesMaterial implements
8787
/* WebCryptoRawAesMaterial need to set a flag, this is an abuse of TraceFlags
8888
* because the material is not generated.
8989
* but CryptographicMaterial force a flag to be set.
90-
*/
90+
*/
9191
const setFlag = KeyringTraceFlag.WRAPPING_KEY_GENERATED_DATA_KEY
9292
decorateCryptographicMaterial<WebCryptoRawAesMaterial>(this, setFlag)
9393
decorateWebCryptoMaterial<WebCryptoRawAesMaterial>(this, 0)

0 commit comments

Comments
 (0)