| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | import * as path from 'path'; | 
					
						
							|  |  |  | import * as core from '@actions/core'; | 
					
						
							|  |  |  | import * as io from '@actions/io'; | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | import * as fs from 'fs'; | 
					
						
							|  |  |  | import * as os from 'os'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { create as xmlCreate } from 'xmlbuilder2'; | 
					
						
							| 
									
										
										
										
											2020-07-15 20:16:31 -06:00
										 |  |  | import * as constants from './constants'; | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  | import * as gpg from './gpg'; | 
					
						
							|  |  |  | import { getBooleanInput } from './util'; | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  | export async function configureAuthentication() { | 
					
						
							|  |  |  |   const id = core.getInput(constants.INPUT_SERVER_ID); | 
					
						
							|  |  |  |   const username = core.getInput(constants.INPUT_SERVER_USERNAME); | 
					
						
							|  |  |  |   const password = core.getInput(constants.INPUT_SERVER_PASSWORD); | 
					
						
							|  |  |  |   const settingsDirectory = | 
					
						
							| 
									
										
										
										
											2022-01-16 17:33:29 +01:00
										 |  |  |     core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), constants.M2_DIR); | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  |   const overwriteSettings = getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, true); | 
					
						
							|  |  |  |   const gpgPrivateKey = | 
					
						
							|  |  |  |     core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY; | 
					
						
							|  |  |  |   const gpgPassphrase = | 
					
						
							|  |  |  |     core.getInput(constants.INPUT_GPG_PASSPHRASE) || | 
					
						
							|  |  |  |     (gpgPrivateKey ? constants.INPUT_DEFAULT_GPG_PASSPHRASE : undefined); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (gpgPrivateKey) { | 
					
						
							|  |  |  |     core.setSecret(gpgPrivateKey); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await createAuthenticationSettings( | 
					
						
							|  |  |  |     id, | 
					
						
							|  |  |  |     username, | 
					
						
							|  |  |  |     password, | 
					
						
							|  |  |  |     settingsDirectory, | 
					
						
							|  |  |  |     overwriteSettings, | 
					
						
							|  |  |  |     gpgPassphrase | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (gpgPrivateKey) { | 
					
						
							|  |  |  |     core.info('Importing private gpg key'); | 
					
						
							|  |  |  |     const keyFingerprint = (await gpg.importKey(gpgPrivateKey)) || ''; | 
					
						
							|  |  |  |     core.saveState(constants.STATE_GPG_PRIVATE_KEY_FINGERPRINT, keyFingerprint); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function createAuthenticationSettings( | 
					
						
							| 
									
										
										
										
											2020-05-02 04:33:15 -07:00
										 |  |  |   id: string, | 
					
						
							|  |  |  |   username: string, | 
					
						
							|  |  |  |   password: string, | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  |   settingsDirectory: string, | 
					
						
							|  |  |  |   overwriteSettings: boolean, | 
					
						
							| 
									
										
										
										
											2020-05-02 04:33:15 -07:00
										 |  |  |   gpgPassphrase: string | undefined = undefined | 
					
						
							| 
									
										
										
										
											2019-11-28 12:40:45 -08:00
										 |  |  | ) { | 
					
						
							| 
									
										
										
										
											2022-01-16 17:33:29 +01:00
										 |  |  |   core.info(`Creating ${constants.MVN_SETTINGS_FILE} with server-id: ${id}`); | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  |   // when an alternate m2 location is specified use only that location (no .m2 directory)
 | 
					
						
							|  |  |  |   // otherwise use the home/.m2/ path
 | 
					
						
							| 
									
										
										
										
											2020-05-02 04:33:15 -07:00
										 |  |  |   await io.mkdirP(settingsDirectory); | 
					
						
							|  |  |  |   await write( | 
					
						
							|  |  |  |     settingsDirectory, | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  |     generate(id, username, password, gpgPassphrase), | 
					
						
							|  |  |  |     overwriteSettings | 
					
						
							| 
									
										
										
										
											2020-05-02 04:33:15 -07:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2019-12-10 09:26:51 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | // only exported for testing purposes
 | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  | export function generate( | 
					
						
							| 
									
										
										
										
											2020-05-02 04:33:15 -07:00
										 |  |  |   id: string, | 
					
						
							|  |  |  |   username: string, | 
					
						
							|  |  |  |   password: string, | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  |   gpgPassphrase?: string | undefined | 
					
						
							| 
									
										
										
										
											2019-12-19 11:28:11 -08:00
										 |  |  | ) { | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  |   const xmlObj: { [key: string]: any } = { | 
					
						
							| 
									
										
										
										
											2020-05-02 04:33:15 -07:00
										 |  |  |     settings: { | 
					
						
							|  |  |  |       '@xmlns': 'http://maven.apache.org/SETTINGS/1.0.0', | 
					
						
							|  |  |  |       '@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', | 
					
						
							|  |  |  |       '@xsi:schemaLocation': | 
					
						
							|  |  |  |         'http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd', | 
					
						
							|  |  |  |       servers: { | 
					
						
							|  |  |  |         server: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             id: id, | 
					
						
							|  |  |  |             username: `\${env.${username}}`, | 
					
						
							|  |  |  |             password: `\${env.${password}}` | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (gpgPassphrase) { | 
					
						
							|  |  |  |     const gpgServer = { | 
					
						
							|  |  |  |       id: 'gpg.passphrase', | 
					
						
							|  |  |  |       passphrase: `\${env.${gpgPassphrase}}` | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     xmlObj.settings.servers.server.push(gpgServer); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  |   return xmlCreate(xmlObj).end({ | 
					
						
							|  |  |  |     headless: true, | 
					
						
							|  |  |  |     prettyPrint: true, | 
					
						
							|  |  |  |     width: 80 | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  | async function write(directory: string, settings: string, overwriteSettings: boolean) { | 
					
						
							| 
									
										
										
										
											2022-01-16 17:33:29 +01:00
										 |  |  |   const location = path.join(directory, constants.MVN_SETTINGS_FILE); | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  |   const settingsExists = fs.existsSync(location); | 
					
						
							|  |  |  |   if (settingsExists && overwriteSettings) { | 
					
						
							|  |  |  |     core.info(`Overwriting existing file ${location}`); | 
					
						
							|  |  |  |   } else if (!settingsExists) { | 
					
						
							|  |  |  |     core.info(`Writing to ${location}`); | 
					
						
							| 
									
										
										
										
											2019-12-19 08:52:26 -08:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2021-04-05 13:02:27 +03:00
										 |  |  |     core.info( | 
					
						
							|  |  |  |       `Skipping generation ${location} because file already exists and overwriting is not required` | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2019-12-04 23:54:21 -05:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-12-19 08:52:26 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return fs.writeFileSync(location, settings, { | 
					
						
							|  |  |  |     encoding: 'utf-8', | 
					
						
							|  |  |  |     flag: 'w' | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | } |