Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate rsa private key and certificate content format #971

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 59 additions & 14 deletions src/lib/deploy/deploy-support.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

const fs = require('fs-extra');
const path = require('path');
const ram = require('../ram');
const debug = require('debug')('fun:deploy');
const promiseRetry = require('../retry');
const getProfile = require('../profile').getProfile;

const { green, red } = require('colors');
const { processApiParameters } = require('./deploy-support-api');
const { getCloudApiClient, getSlsClient, getMnsClient } = require('../client');
const {green, red} = require('colors');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint 是如何配置的?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我是在 Windows 上用 WebStorm 开发的, 未修改 .eslintrc 的内容, 对 deploy-support.js 使用 .eslintrc 的格式进行格式化的.
测试后发现有没有空格 eslint 都没有报错.
TIM截图20200714105210
TIM截图20200714105225

需要我回滚格式化代码的 commit 么?

const {processApiParameters} = require('./deploy-support-api');
const {getCloudApiClient, getSlsClient, getMnsClient} = require('../client');

const {
getOtsClient,
Expand Down Expand Up @@ -75,7 +76,9 @@ async function makeLogstore({
console.log(red(`\t\tretry ${times} times`));

retry(ex);
} else { exists = false; }
} else {
exists = false;
}
}
});

Expand Down Expand Up @@ -153,7 +156,9 @@ async function slsProjectExist(slsClient, projectName) {

console.log(red(`\tretry ${times} times`));
retry(ex);
} else { projectExist = false; }
} else {
projectExist = false;
}
}
});
return projectExist;
Expand Down Expand Up @@ -250,12 +255,50 @@ async function makeCustomDomain({
let privateKey = certConfig.PrivateKey;
let certificate = certConfig.Certificate;

if (privateKey && privateKey.endsWith('.pem')) {
certConfig.PrivateKey = await fs.readFile(privateKey, 'utf-8');
}
if (certificate && certificate.endsWith('.pem')) {
certConfig.Certificate = await fs.readFile(certificate, 'utf-8');
}
if (privateKey) {
//region resolve RSA private key content
let p = path.resolve(__dirname, privateKey);
// private key is provided by local file
if (fs.pathExistsSync(p)) {
certConfig.PrivateKey = fs.readFileSync(p, 'utf-8');
Copy link
Collaborator

@ChanDaoH ChanDaoH Jul 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs.readFileSync(p, 'utf-8') => await fs.readFile(p, 'utf-8')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

} // or it is hardcoded
//endregion

//region validate RSA private key content
let expectedPrefix = '-----BEGIN RSA PRIVATE KEY-----', expectedSuffix = '-----END RSA PRIVATE KEY-----';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写在 const 里面吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (!certConfig.PrivateKey.startsWith(expectedPrefix) || !certConfig.PrivateKey.endsWith(expectedSuffix)) {
throw new Error(red(`
Please provide a valid PEM encoded RSA private key for ${domainName}.
It's content MUST start with "${expectedPrefix}" AND end with "${expectedSuffix}".

See:
http://fileformats.archiveteam.org/wiki/PEM_encoded_RSA_private_key`));
}
//endregion
} // private key is not provided

if (certificate) {
//region resolve certificate content
let p = path.resolve(__dirname, certificate);
// certificate is provided by local file
if (fs.pathExistsSync(p)) {
certConfig.Certificate = fs.readFileSync(p, 'utf-8');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs.readFileSync(p, 'utf-8') => await fs.readFile(p, 'utf-8')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

} // or it is hardcoded
//endregion

//region validate certificate content
let expectedPrefix = '-----BEGIN CERTIFICATE-----', expectedSuffix = '-----END CERTIFICATE-----';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写在 const 里面吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (!certConfig.Certificate.startsWith(expectedPrefix) || !certConfig.Certificate.endsWith(expectedSuffix)) {
throw new Error(red(`
Please provide a valid PEM encoded certificate for ${domainName}.
It's content MUST start with "${expectedPrefix}" AND end with "${expectedSuffix}".

See:
http://fileformats.archiveteam.org/wiki/PEM_encoded_certificate`));
}
//endregion
} // certificate is not provided

Object.assign(options, {
certConfig
});
Expand Down Expand Up @@ -373,9 +416,11 @@ async function makeApi(group, {
'requestPath': requestPath
}, requestConfig);

const { apiRequestParameters,
const {
apiRequestParameters,
apiServiceParameters,
apiServiceParametersMap } = processApiParameters(requestParameters, serviceParameters, serviceParametersMap);
apiServiceParametersMap
} = processApiParameters(requestParameters, serviceParameters, serviceParametersMap);

const profile = await getProfile();

Expand Down Expand Up @@ -676,4 +721,4 @@ module.exports = {
makeApiTrigger, makeSlsProject, makeOtsInstance,
makeCustomDomain, makeLogstoreIndex, makeSlsAuto,
listCustomDomains
};
};
17 changes: 17 additions & 0 deletions test/fs-extra.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let fs = require('fs-extra');
let path = require('path');
const expect = require('expect.js');

describe('fs-extra module Tests', function () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个测试集的作用是?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 为了确认 fs-extra 的 pathExists 方法对相对路径的支持方式, 目前确认到的情况是需要使用 path.resolve 先对路径进行解析才行.
  • 为了确认 fs-extra 的 pathExists 方法对 PrivateKey 以及 Certificate 是硬编码的 PEM 内容时的返回结果.

需要添加注释? 还是删掉这个测试集?


it('should exists', function () {
let p = path.resolve(__dirname, '../LICENSE');
expect(fs.pathExistsSync(p)).to.be(true);
console.log(fs.readFileSync(p, 'utf-8'));
});

it('should not exists', function () {
let p = path.resolve(__dirname, '-----BEGIN RSA PRIVATE KEY-----');
expect(fs.pathExistsSync(p)).to.be(false);
});
});