From cd16d8406ca2d5c848284505b1ee05cea265f7db Mon Sep 17 00:00:00 2001 From: Anthony Whitford Date: Tue, 16 Feb 2021 13:46:27 -0800 Subject: [PATCH] Completed the attribute support for servers. (#95) * Completed the attribute support for servers, and password is optional. Resolves #92. * Updated oracleServers.xml template to match new servers.xml template. * Revised tests to match code and template changes. * Added documentation for server attributes. * Added additional use cases to exercise server attribute handling. --- README.md | 12 +++ index.test.js | 11 ++- settings.js | 41 ++++++----- settings.test.js | 143 +++++++++++++++++++++++------------- templates/oracleServers.xml | 4 + templates/servers.xml | 4 + 6 files changed, 139 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index 170fed4b..47fa985b 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,18 @@ steps: servers: '[{"id": "serverId", "username": "username", "password": "password"}]' ``` +All `server` attributes may be specified: + * `id` _(required)_ + * `username` + * `password` + * `privateKey` + * `passphrase` + * `filePermissions` + * `directoryPermissions` + * `configuration` + +Please refer to the [servers](http://maven.apache.org/settings.html#Servers) documentation for more information. + ## ```settings.xml``` with servers section and additional configuration ``` yml diff --git a/index.test.js b/index.test.js index 26133816..67963b47 100644 --- a/index.test.js +++ b/index.test.js @@ -63,11 +63,11 @@ afterAll(() => { test('run with all feature', () => { - process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword", "configuration": {"props1": "value1"}}]'; - process.env['INPUT_ORACLESERVERS'] = '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]'; - process.env['INPUT_GITHUBSERVER'] = true; + process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword", "configuration": {"props1": "value1"}}]'; + process.env['INPUT_ORACLESERVERS'] = '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]'; + process.env['INPUT_GITHUBSERVER'] = true; - process.env['INPUT_MIRRORS'] = '[{"id": "mirrorId", "name": "mirror Name", "mirrorOf": "mirror Off *", "url": "mirror url"}]'; + process.env['INPUT_MIRRORS'] = '[{"id": "mirrorId", "name": "mirror Name", "mirrorOf": "mirror Off *", "url": "mirror url"}]'; process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]' process.env['INPUT_APACHESNAPSHOTS'] = true; @@ -80,7 +80,7 @@ test('run with all feature', () => { expect(settingsStatus.isFile()).toBeTruthy(); expect(settingsStatus.size).toBeGreaterThan(0); - const settingsBody = fs.readFileSync(settingsPath).toString().replace(/^ $/mg, ''); + const settingsBody = fs.readFileSync(settingsPath).toString().replace(/^\s*$(?:\r\n?|\n)/gm, ''); expect(settingsBody).toBe(` false @@ -214,7 +214,6 @@ test('run with all feature', () => { github \${env.GITHUB_ACTOR} \${env.GITHUB_TOKEN} - diff --git a/settings.js b/settings.js index 06e8543e..418e9e01 100644 --- a/settings.js +++ b/settings.js @@ -54,7 +54,7 @@ function jsonToXml(templateXml, xmlTag, json) { for (const key in json) { const keyXml = templateXml.createElement(key); const value = json[key]; - if ( value instanceof Object) { + if (value instanceof Object) { jsonToXml(templateXml, keyXml, value); } else { keyXml.textContent = value; @@ -63,28 +63,32 @@ function jsonToXml(templateXml, xmlTag, json) { } } -function fillServer(templateXml, templateName, id, username, password, configurations) { +function fillServer(templateXml, templateName, id, username, password, privateKey, passphrase, filePermissions, directoryPermissions, configurations) { - if (!id || ((!username || !password) && !configurations) ) { - core.setFailed(templateName + ' must contain id, (username and password) or configuration'); + if (!id || (!username && !configurations)) { + core.setFailed(templateName + ' must contain id, and username or configuration'); return; } const serverXml = getTemplate(templateName + '.xml') serverXml.getElementsByTagName('id')[0].textContent = id; - const usernameTag = serverXml.getElementsByTagName('username')[0]; - if (username) { - usernameTag.textContent = username; - } else { - serverXml.documentElement.removeChild(usernameTag); - } - - const passwordTag = serverXml.getElementsByTagName('password')[0]; - if (password) { - passwordTag.textContent = password; - } else { - serverXml.documentElement.removeChild(passwordTag); + const serverTags = { + 'username': username, + 'password': password, + 'privateKey': privateKey, + 'passphrase': passphrase, + 'filePermissions': filePermissions, + 'directoryPermissions': directoryPermissions + }; + for (const tag in serverTags) { + const serverTag = serverXml.getElementsByTagName(tag)[0]; + const tagValue = serverTags[tag]; + if (tagValue) { + serverTag.textContent = tagValue; + } else { + serverXml.documentElement.removeChild(serverTag); + } } const configurationTag = serverXml.getElementsByTagName('configuration')[0]; @@ -109,7 +113,10 @@ function fillServers(template, templateName) { } JSON.parse(servers).forEach((server) => - fillServer(template, templateName, server.id, server.username, server.password, server.configuration)); + fillServer(template, templateName, server.id, server.username, + server.password, server.privateKey, server.passphrase, + server.filePermissions, server.directoryPermissions, + server.configuration)); } function fillMirror(template, id, name, mirrorOf, url) { diff --git a/settings.test.js b/settings.test.js index 006c8e67..e5db41c4 100644 --- a/settings.test.js +++ b/settings.test.js @@ -38,6 +38,14 @@ const settingsPath = path.join(testHomePath, '.m2', 'settings.xml'); var consoleOutput = []; +function stringAsXml(str) { + return new DOMParser().parseFromString(str, 'text/xml'); +} + +function xmlAsString(xml) { + return new XMLSerializer().serializeToString(xml).replace(/^\s*$(?:\r\n?|\n)/gm, ''); +} + beforeAll(() => { if (!fs.existsSync(testHomePath)) { fs.mkdirSync(testHomePath); @@ -51,7 +59,7 @@ beforeAll(() => { }); beforeEach(() => { - xmlTestProfile = new DOMParser().parseFromString(` + xmlTestProfile = stringAsXml(` _properties_ @@ -119,7 +127,7 @@ test('xml should be write', () => { test('fillServers do nothing if no params', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); settings.fillServers(xml, 'servers'); @@ -130,54 +138,46 @@ test('fillServers do nothing if no params', () => { test('fillServers one server', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]'; settings.fillServers(xml, 'servers'); - const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, ''); - - expect(xmlStr).toBe(` + expect(xmlAsString(xml)).toBe(` id1 username1 password1 - `); }); test('fillServers with username and configuration', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]'; settings.fillServers(xml, 'servers'); - const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '') - - expect(xmlStr).toBe(` + expect(xmlAsString(xml)).toBe(` id1 username - prop1Valueprop2Value `); }); test('fillServers with username, password and configuration', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "password": "password", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]'; settings.fillServers(xml, 'servers'); - const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '') - - expect(xmlStr).toBe(` + expect(xmlAsString(xml)).toBe(` id1 username @@ -186,73 +186,115 @@ test('fillServers with username, password and configuration', () => { `); }); +test('fillServers with username, privateKey', () => { + + const xml = stringAsXml(""); + + process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "privateKey": "${user.home}/.ssh/id_rsa"}]'; + + settings.fillServers(xml, 'servers'); + + expect(xmlAsString(xml)).toBe(` + + id1 + username + \${user.home}/.ssh/id_rsa +`); +}); + +test('fillServers with username, privateKey, and passphrase', () => { + + const xml = stringAsXml(""); + + process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "privateKey": "${user.home}/.ssh/id_rsa", "passphrase": "secret"}]'; + + settings.fillServers(xml, 'servers'); + + expect(xmlAsString(xml)).toBe(` + + id1 + username + \${user.home}/.ssh/id_rsa + secret +`); +}); + +test('fillServers with all attributes', () => { + + const xml = stringAsXml(""); + + process.env['INPUT_SERVERS'] = '[{"id": "server001", "username": "my_login", "password": "my_password", "privateKey": "${user.home}/.ssh/id_dsa", "passphrase": "some_passphrase", "filePermissions": "664", "directoryPermissions": "775", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"} }]'; + + settings.fillServers(xml, 'servers'); + + expect(xmlAsString(xml)).toBe(` + + server001 + my_login + my_password + \${user.home}/.ssh/id_dsa + some_passphrase + 664 + 775 + prop1Valueprop2Value +`); +}); + test('fillServers with configuration', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]'; settings.fillServers(xml, 'servers'); - const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '') - - expect(xmlStr).toBe(` + expect(xmlAsString(xml)).toBe(` id1 - - prop1Valueprop2Value `); }); test('fillServers with configuration subLevel', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": {"prop11": "value11", "prop12": "value12"}, "prop2": "value2"}}]'; settings.fillServers(xml, 'servers'); - const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '') - - expect(xmlStr).toBe(` + expect(xmlAsString(xml)).toBe(` id1 - - value11value12value2 `); }); test('fillServers two servers', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"},\ {"id": "id2", "username": "username2", "password":"password2"}]'; settings.fillServers(xml, 'servers'); - const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, ''); - - expect(xmlStr).toBe(` + expect(xmlAsString(xml)).toBe(` id1 username1 password1 - id2 username2 password2 - `); }); test('fill servers incorrect fields', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_SERVERS'] = '[{"idx": "id1"}]'; @@ -263,22 +305,20 @@ test('fill servers incorrect fields', () => { expect(xmlStr).toBe(''); expect(consoleOutput).toEqual( expect.arrayContaining([ - expect.stringMatching(/::error::servers must contain id, \(username and password\) or configuration/) + expect.stringMatching(/::error::servers must contain id, and username or configuration/) ]) ); }); test('fill oracleServers', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_ORACLESERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]'; settings.fillServers(xml, 'oracleServers'); - const xmlStr = new XMLSerializer().serializeToString(xml); - - expect(xmlStr).toBe(` + expect(xmlAsString(xml)).toBe(` id1 username1 @@ -305,27 +345,24 @@ test('fill oracleServers', () => { test('fillServers github', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_GITHUBSERVER'] = 'true'; settings.fillServerForGithub(xml); - const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, ''); - - expect(xmlStr).toBe(` + expect(xmlAsString(xml)).toBe(` github \${env.GITHUB_ACTOR} \${env.GITHUB_TOKEN} - `); expect(consoleOutput).toEqual([]); }); test('fillMirrors do nothing if no params', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); settings.fillMirrors(xml); @@ -337,7 +374,7 @@ test('fillMirrors do nothing if no params', () => { test('fillMirrors one mirror', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_MIRRORS'] = '[{"id": "id1", "name": "name", "mirrorOf":"mirrorOf", "url":"url"}]'; @@ -357,7 +394,7 @@ test('fillMirrors one mirror', () => { test('fillMirrors two mirrors', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_MIRRORS'] = '[{"id": "id1", "name": "name1", "mirrorOf":"mirrorOf1", "url":"url1"},{"id": "id2", "name": "name2", "mirrorOf":"mirrorOf2", "url":"url2"}]'; @@ -383,7 +420,7 @@ test('fillMirrors two mirrors', () => { test('fillMirrors incorrect fields', () => { - const xml = new DOMParser().parseFromString(""); + const xml = stringAsXml(""); process.env['INPUT_MIRRORS'] = '[{"idx": "id1"}]'; @@ -403,7 +440,7 @@ test('addApacheSnapshots', () => { process.env['INPUT_APACHESNAPSHOTS'] = "true"; - const xml = new DOMParser().parseFromString(''); + const xml = stringAsXml(''); settings.addApacheSnapshots(xml); @@ -445,7 +482,7 @@ test('addSonatypeSnapshots', () => { process.env['INPUT_SONATYPESNAPSHOTS'] = "true"; - const xml = new DOMParser().parseFromString(''); + const xml = stringAsXml(''); settings.addSonatypeSnapshots(xml); @@ -487,7 +524,7 @@ test('addOracleRepo', () => { process.env['INPUT_ORACLEREPO'] = "true"; - const xml = new DOMParser().parseFromString(''); + const xml = stringAsXml(''); settings.addOracleRepo(xml); @@ -529,7 +566,7 @@ test('fillProperties', () => { process.env['INPUT_PROPERTIES'] = '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]'; - const xml = new DOMParser().parseFromString(''); + const xml = stringAsXml(''); settings.fillProperties(xml); diff --git a/templates/oracleServers.xml b/templates/oracleServers.xml index 62fdcf05..bcc228b1 100644 --- a/templates/oracleServers.xml +++ b/templates/oracleServers.xml @@ -3,6 +3,10 @@ + + + + ANY diff --git a/templates/servers.xml b/templates/servers.xml index e7f69638..7caabf6b 100644 --- a/templates/servers.xml +++ b/templates/servers.xml @@ -3,5 +3,9 @@ + + + +