Skip to content

Commit

Permalink
fix: add newlines if they are missing (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
rethab committed Jul 13, 2021
1 parent 889c36d commit 094fe45
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type Options = {
cwd?: string;
};

const begin = "-----BEGIN RSA PRIVATE KEY-----";
const end = "-----END RSA PRIVATE KEY-----";

export function getPrivateKey(options: Options = {}): string | null {
const env = options.env || process.env;
const cwd = options.cwd || process.cwd();
Expand All @@ -31,11 +34,17 @@ export function getPrivateKey(options: Options = {}): string | null {
privateKey = Buffer.from(privateKey, "base64").toString();
}

const begin = "-----BEGIN RSA PRIVATE KEY-----";
const end = "-----END RSA PRIVATE KEY-----";
if (privateKey.includes(begin) && privateKey.includes(end)) {
// Full key with new lines
return privateKey.replace(/\\n/g, "\n");
// newlines are escaped
if (privateKey.indexOf("\\n") !== -1) {
privateKey = privateKey.replace(/\\n/g, "\n");
}

// newlines are missing
if (privateKey.indexOf("\n") === -1) {
privateKey = addNewlines(privateKey);
}
return privateKey;
}

throw new Error(
Expand Down Expand Up @@ -65,4 +74,10 @@ export function getPrivateKey(options: Options = {}): string | null {
return null;
}

function addNewlines(privateKey: string): string {
const middleLength = privateKey.length - begin.length - end.length - 2;
const middle = privateKey.substr(begin.length + 1, middleLength);
return `${begin}\n${middle.trim().replace(/\s+/g, "\n")}\n${end}`;
}

getPrivateKey.VERSION = VERSION;
29 changes: 28 additions & 1 deletion test/get-private-key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ const readdirSync = fs.readdirSync as jest.Mock;
const readFileSync = fs.readFileSync as jest.Mock;

const PRIVATE_KEY =
"-----BEGIN RSA PRIVATE KEY-----\n ... \n-----END RSA PRIVATE KEY-----";
"-----BEGIN RSA PRIVATE KEY-----\n7HjkPK\nKLm395\nAIBII\n-----END RSA PRIVATE KEY-----";

const PRIVATE_KEY_NO_NEWLINES =
"-----BEGIN RSA PRIVATE KEY----- 7HjkPK KLm395 AIBII -----END RSA PRIVATE KEY-----";

const PRIVATE_KEY_NO_NEWLINES_MULTIPLE_SPACES =
"-----BEGIN RSA PRIVATE KEY----- 7HjkPK KLm395 AIBII -----END RSA PRIVATE KEY-----";

const PRIVATE_KEY_ESCAPED_NEWLINES =
"-----BEGIN RSA PRIVATE KEY-----\\n7HjkPK\\nKLm395\\nAIBII\\n-----END RSA PRIVATE KEY-----";

describe("getPrivateKey", () => {
beforeEach(() => {
Expand Down Expand Up @@ -123,6 +132,24 @@ describe("getPrivateKey", () => {
expect(result).toEqual(PRIVATE_KEY);
});

it("PRIVATE_KEY contains no newlines", () => {
process.env.PRIVATE_KEY = PRIVATE_KEY_NO_NEWLINES;
const result = getPrivateKey();
expect(result).toEqual(PRIVATE_KEY);
});

it("PRIVATE_KEY contains consecutive spaces", () => {
process.env.PRIVATE_KEY = PRIVATE_KEY_NO_NEWLINES_MULTIPLE_SPACES;
const result = getPrivateKey();
expect(result).toEqual(PRIVATE_KEY);
});

it("PRIVATE_KEY contains escaped newlines", () => {
process.env.PRIVATE_KEY = PRIVATE_KEY_ESCAPED_NEWLINES;
const result = getPrivateKey();
expect(result).toEqual(PRIVATE_KEY);
});

it("PRIVATE_KEY invalid", () => {
process.env.PRIVATE_KEY = "invalid";
expect(() => getPrivateKey()).toThrow(
Expand Down

0 comments on commit 094fe45

Please sign in to comment.