Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19069,7 +19069,7 @@ const outputMap = {
cert: { key: 'certificate', tx: (v) => v },
key: { key: 'private_key', tx: (v) => v },
ca: { key: 'issuing_ca', tx: (v) => v },
ca_chain: { key: 'ca_chain', tx: (v) => v.join('\n') },
ca_chain: { key: 'ca_chain', tx: (v) => v.join('\n'), optional: true },
};

/**
Expand Down Expand Up @@ -19118,7 +19118,12 @@ async function getCertificates(pkiRequests, client) {
core.info(`✔ Successfully generated certificate (serial number ${body.data.serial_number})`);

Object.entries(outputMap).forEach(([key, value]) => {
const val = value.tx(body.data[value.key]);
const rawValue = body.data[value.key];
if (value.optional && rawValue == null) {
return;
}

const val = value.tx(rawValue);
results.push({
request: {
...pkiRequest,
Expand All @@ -19138,6 +19143,7 @@ module.exports = {
getCertificates,
};


/***/ }),

/***/ 8452:
Expand Down
11 changes: 8 additions & 3 deletions src/pki.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const outputMap = {
cert: { key: 'certificate', tx: (v) => v },
key: { key: 'private_key', tx: (v) => v },
ca: { key: 'issuing_ca', tx: (v) => v },
ca_chain: { key: 'ca_chain', tx: (v) => v.join('\n') },
ca_chain: { key: 'ca_chain', tx: (v) => v.join('\n'), optional: true },
};

/**
Expand Down Expand Up @@ -60,7 +60,12 @@ async function getCertificates(pkiRequests, client) {
core.info(`✔ Successfully generated certificate (serial number ${body.data.serial_number})`);

Object.entries(outputMap).forEach(([key, value]) => {
const val = value.tx(body.data[value.key]);
const rawValue = body.data[value.key];
if (value.optional && rawValue == null) {
return;
}

const val = value.tx(rawValue);
results.push({
request: {
...pkiRequest,
Expand All @@ -78,4 +83,4 @@ async function getCertificates(pkiRequests, client) {

module.exports = {
getCertificates,
};
};
85 changes: 85 additions & 0 deletions src/pki.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright IBM Corp. 2019, 2026
* SPDX-License-Identifier: MIT
*/

jest.mock('@actions/core');

const { getCertificates } = require('./pki');

describe('getCertificates', () => {
const pkiRequest = {
path: 'pki/issue/Test',
parameters: { common_name: 'test', ttl: '1h' },
envVarName: 'TEST',
outputVarName: 'test',
};

it('omits ca_chain output when Vault does not return one', async () => {
const client = {
post: jest.fn().mockResolvedValue({
body: JSON.stringify({
data: {
certificate: 'cert',
private_key: 'key',
issuing_ca: 'ca',
serial_number: '01:02',
},
}),
}),
};

const results = await getCertificates([pkiRequest], client);

expect(results).toEqual([
expect.objectContaining({
request: expect.objectContaining({
envVarName: 'TEST_CERT',
outputVarName: 'test_cert',
}),
value: 'cert',
}),
expect.objectContaining({
request: expect.objectContaining({
envVarName: 'TEST_KEY',
outputVarName: 'test_key',
}),
value: 'key',
}),
expect.objectContaining({
request: expect.objectContaining({
envVarName: 'TEST_CA',
outputVarName: 'test_ca',
}),
value: 'ca',
}),
]);
});

it('joins ca_chain output when Vault returns one', async () => {
const client = {
post: jest.fn().mockResolvedValue({
body: JSON.stringify({
data: {
certificate: 'cert',
private_key: 'key',
issuing_ca: 'ca',
ca_chain: ['root', 'intermediate'],
serial_number: '01:02',
},
}),
}),
};

const results = await getCertificates([pkiRequest], client);

expect(results).toHaveLength(4);
expect(results[3]).toEqual(expect.objectContaining({
request: expect.objectContaining({
envVarName: 'TEST_CA_CHAIN',
outputVarName: 'test_ca_chain',
}),
value: 'root\nintermediate',
}));
});
});
Loading