Comparing version 1.0.0 to 1.0.1
@@ -24,5 +24,42 @@ "use strict"; | ||
const CERT_TEST_ARN = 'arn:aws:acm-pca:us-west-2:123456789012:certificate-authority/4819f73f-af7c-4abf-8753-62e40512cac6/certificate/5e4c069a8eafc9bdb1fbc1c2a977160e'; | ||
const acmpca = new _awsSdk.default.ACMPCA(); | ||
acmpca.issueCertificate = jest.fn().mockImplementation(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
CertificateArn: CERT_TEST_ARN, | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}); | ||
} | ||
}; | ||
}); | ||
acmpca.getCertificate = jest.fn().mockImplementation(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}); | ||
} | ||
}; | ||
}); | ||
acmpca.getCertificateAuthorityCertificate = jest.fn().mockImplementation(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}); | ||
} | ||
}; | ||
}); | ||
acmpca.waitFor = jest.fn().mockImplementation((evt, param, cb) => { | ||
cb(null, { | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}); | ||
}); | ||
describe('PCA class', () => { | ||
it('should create an instance of the PCA', () => { | ||
const acmpca = new _awsSdk.default.ACMPCA(); | ||
const pca = new _PCA.default(acmpca, _pem.default, CA_TEST_ARN); | ||
@@ -32,3 +69,2 @@ expect(pca).toBeDefined(); | ||
it('should create a CSR and client key', async () => { | ||
const acmpca = new _awsSdk.default.ACMPCA(); | ||
const pca = new _PCA.default(acmpca, _pem.default, CA_TEST_ARN); | ||
@@ -42,13 +78,3 @@ const data = await pca.createCSR(CSR_TEST_DATA); | ||
it('should issue a certificate', async () => { | ||
const acmpca = new _awsSdk.default.ACMPCA(); | ||
const pca = new _PCA.default(acmpca, _pem.default, CA_TEST_ARN); | ||
acmpca.issueCertificate = jest.fn().mockImplementationOnce(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
CertificateArn: CERT_TEST_ARN | ||
}); | ||
} | ||
}; | ||
}); | ||
const csrData = await pca.createCSR(CSR_TEST_DATA); | ||
@@ -72,15 +98,7 @@ const issueData = await pca.issueCertificate(csrData.csr, { | ||
expect(issueData.CertificateArn).toBe(CERT_TEST_ARN); | ||
expect(issueData.Certificate).toContain('-----BEGIN CERTIFICATE-----'); | ||
expect(issueData.CertificateChain).toContain('-----BEGIN CERTIFICATE-----'); | ||
}); | ||
it('should fetch a certificate', async () => { | ||
const acmpca = new _awsSdk.default.ACMPCA(); | ||
const pca = new _PCA.default(acmpca, _pem.default, CA_TEST_ARN); | ||
acmpca.issueCertificate = jest.fn().mockImplementationOnce(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
CertificateArn: CERT_TEST_ARN | ||
}); | ||
} | ||
}; | ||
}); | ||
const csrData = await pca.createCSR(CSR_TEST_DATA); | ||
@@ -94,12 +112,2 @@ const issueData = await pca.issueCertificate(csrData.csr, { | ||
}); | ||
acmpca.getCertificate = jest.fn().mockImplementationOnce(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}); | ||
} | ||
}; | ||
}); | ||
const certData = await pca.getCertificate(issueData.CertificateArn); | ||
@@ -110,14 +118,3 @@ expect(certData.Certificate).toContain('-----BEGIN CERTIFICATE-----'); | ||
it('should get the Certificate Authority certificate', async () => { | ||
const acmpca = new _awsSdk.default.ACMPCA(); | ||
const pca = new _PCA.default(acmpca, _pem.default, CA_TEST_ARN); | ||
acmpca.getCertificateAuthorityCertificate = jest.fn().mockImplementationOnce(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}); | ||
} | ||
}; | ||
}); | ||
const certData = await pca.getCaCertificate(); | ||
@@ -124,0 +121,0 @@ expect(certData.Certificate).toContain('-----BEGIN CERTIFICATE-----'); |
@@ -73,3 +73,4 @@ "use strict"; | ||
/** | ||
* Wrapper around ACMPCA#issueCertificate() | ||
* Wrapper around ACMPCA#issueCertificate() and calls ACMPCA#waitFor() to ensure it has been | ||
* created so getCertificate() can be subsequently called | ||
* | ||
@@ -87,7 +88,23 @@ * Creates the certificate using the AWS PCA, storing it in AWS. | ||
async issueCertificate(csr, params) { | ||
return this.acmpca.issueCertificate(_objectSpread({ | ||
CertificateAuthorityArn: this.caArn | ||
}, params, { | ||
Csr: Buffer.from(csr, 'ascii') | ||
})).promise(); | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const caData = await this.acmpca.issueCertificate(_objectSpread({ | ||
CertificateAuthorityArn: this.caArn | ||
}, params, { | ||
Csr: Buffer.from(csr, 'ascii') | ||
})).promise(); | ||
this.acmpca.waitFor('certificateIssued', { | ||
CertificateAuthorityArn: this.caArn, | ||
CertificateArn: caData.CertificateArn | ||
}, (err, data) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(_objectSpread({}, caData, data)); | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
@@ -94,0 +111,0 @@ /** |
{ | ||
"name": "aws-pca", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A library to generate a certificate for HTTPS use using AWS Private Certificate Authority (PCA)", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# node-aws-pca | ||
[![npm version](https://badge.fury.io/js/aws-pca.svg)](https://badge.fury.io/js/aws-pca) | ||
A library to generate and fetch a certificate for HTTPS use using AWS Private Certificate Authority (PCA). | ||
@@ -47,3 +49,4 @@ | ||
// See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/ACMPCA.html#issueCertificate-property | ||
// no need to specify CertificateAuthorityArn since you specified it in the constructor | ||
// - no need to specify CertificateAuthorityArn since you specified it in the constructor | ||
// - also waits for the issue certificate task on AWS to complete (this takes around 5+ secs) | ||
@@ -58,6 +61,6 @@ const issueData = await pca.issueCertificate(csrData.csr, { | ||
// Get the server certificate | ||
// Note: You might have to wait a few seconds if you just issued the certificate | ||
// before you can fetch it as AWS PCA has not actually created it yet | ||
// issueData will have { CertificateArn, Certificate, CertificateChain } | ||
// You can technically stop here as you have the Certificate + CertificateChain data | ||
// Get the server certificate (not needed if you've just called issueCertificate() | ||
const certData = await pca.getCertificate(issueData.CertificateArn) | ||
@@ -64,0 +67,0 @@ |
@@ -23,6 +23,47 @@ /* eslint-env jest */ | ||
const acmpca = new AWS.ACMPCA() | ||
acmpca.issueCertificate = jest.fn().mockImplementation(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
CertificateArn: CERT_TEST_ARN, | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}) | ||
} | ||
} | ||
}) | ||
acmpca.getCertificate = jest.fn().mockImplementation(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}) | ||
} | ||
} | ||
}) | ||
acmpca.getCertificateAuthorityCertificate = jest.fn().mockImplementation(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}) | ||
} | ||
} | ||
}) | ||
acmpca.waitFor = jest.fn().mockImplementation((evt, param, cb) => { | ||
cb(null, { | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}) | ||
}) | ||
describe('PCA class', () => { | ||
it('should create an instance of the PCA', () => { | ||
const acmpca = new AWS.ACMPCA() | ||
const pca = new PCA(acmpca, pem, CA_TEST_ARN) | ||
@@ -34,4 +75,2 @@ | ||
it('should create a CSR and client key', async () => { | ||
const acmpca = new AWS.ACMPCA() | ||
const pca = new PCA(acmpca, pem, CA_TEST_ARN) | ||
@@ -49,16 +88,4 @@ | ||
it('should issue a certificate', async () => { | ||
const acmpca = new AWS.ACMPCA() | ||
const pca = new PCA(acmpca, pem, CA_TEST_ARN) | ||
acmpca.issueCertificate = jest.fn().mockImplementationOnce(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
CertificateArn: CERT_TEST_ARN | ||
}) | ||
} | ||
} | ||
}) | ||
const csrData = await pca.createCSR(CSR_TEST_DATA) | ||
@@ -85,19 +112,9 @@ | ||
expect(issueData.CertificateArn).toBe(CERT_TEST_ARN) | ||
expect(issueData.Certificate).toContain('-----BEGIN CERTIFICATE-----') | ||
expect(issueData.CertificateChain).toContain('-----BEGIN CERTIFICATE-----') | ||
}) | ||
it('should fetch a certificate', async () => { | ||
const acmpca = new AWS.ACMPCA() | ||
const pca = new PCA(acmpca, pem, CA_TEST_ARN) | ||
acmpca.issueCertificate = jest.fn().mockImplementationOnce(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
CertificateArn: CERT_TEST_ARN | ||
}) | ||
} | ||
} | ||
}) | ||
const csrData = await pca.createCSR(CSR_TEST_DATA) | ||
@@ -113,13 +130,2 @@ | ||
acmpca.getCertificate = jest.fn().mockImplementationOnce(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}) | ||
} | ||
} | ||
}) | ||
const certData = await pca.getCertificate(issueData.CertificateArn) | ||
@@ -132,19 +138,4 @@ | ||
it('should get the Certificate Authority certificate', async () => { | ||
const acmpca = new AWS.ACMPCA() | ||
const pca = new PCA(acmpca, pem, CA_TEST_ARN) | ||
acmpca.getCertificateAuthorityCertificate = jest | ||
.fn() | ||
.mockImplementationOnce(() => { | ||
return { | ||
promise: () => { | ||
return Promise.resolve({ | ||
Certificate: '-----BEGIN CERTIFICATE-----', | ||
CertificateChain: '-----BEGIN CERTIFICATE-----' | ||
}) | ||
} | ||
} | ||
}) | ||
const certData = await pca.getCaCertificate() | ||
@@ -151,0 +142,0 @@ |
@@ -62,3 +62,4 @@ export default class PCA { | ||
/** | ||
* Wrapper around ACMPCA#issueCertificate() | ||
* Wrapper around ACMPCA#issueCertificate() and calls ACMPCA#waitFor() to ensure it has been | ||
* created so getCertificate() can be subsequently called | ||
* | ||
@@ -74,9 +75,33 @@ * Creates the certificate using the AWS PCA, storing it in AWS. | ||
async issueCertificate (csr, params) { | ||
return this.acmpca | ||
.issueCertificate({ | ||
CertificateAuthorityArn: this.caArn, | ||
...params, | ||
Csr: Buffer.from(csr, 'ascii') | ||
}) | ||
.promise() | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const caData = await this.acmpca | ||
.issueCertificate({ | ||
CertificateAuthorityArn: this.caArn, | ||
...params, | ||
Csr: Buffer.from(csr, 'ascii') | ||
}) | ||
.promise() | ||
this.acmpca.waitFor( | ||
'certificateIssued', | ||
{ | ||
CertificateAuthorityArn: this.caArn, | ||
CertificateArn: caData.CertificateArn | ||
}, | ||
(err, data) => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
resolve({ | ||
...caData, | ||
...data | ||
}) | ||
} | ||
) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
}) | ||
} | ||
@@ -83,0 +108,0 @@ |
25959
13
538
81