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';
|
2026-07-08 14:45:00 +05:30
|
|
|
import * as constants from './constants.js';
|
|
|
|
|
import * as gpg from './gpg.js';
|
|
|
|
|
import {getBooleanInput} from './util.js';
|
2026-07-30 15:09:35 -04:00
|
|
|
import {escapeXmlText} from './xml.js';
|
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);
|
2026-07-17 20:10:58 -04:00
|
|
|
const usernameEnvVar = getInputWithDeprecatedAlias(
|
|
|
|
|
constants.INPUT_SERVER_USERNAME_ENV_VAR,
|
|
|
|
|
constants.INPUT_SERVER_USERNAME_DEPRECATED,
|
|
|
|
|
constants.INPUT_DEFAULT_SERVER_USERNAME
|
|
|
|
|
);
|
|
|
|
|
const passwordEnvVar = getInputWithDeprecatedAlias(
|
|
|
|
|
constants.INPUT_SERVER_PASSWORD_ENV_VAR,
|
|
|
|
|
constants.INPUT_SERVER_PASSWORD_DEPRECATED,
|
|
|
|
|
constants.INPUT_DEFAULT_SERVER_PASSWORD
|
|
|
|
|
);
|
2021-04-05 13:02:27 +03:00
|
|
|
const settingsDirectory =
|
2023-03-09 14:49:35 +02:00
|
|
|
core.getInput(constants.INPUT_SETTINGS_PATH) ||
|
|
|
|
|
path.join(os.homedir(), constants.M2_DIR);
|
|
|
|
|
const overwriteSettings = getBooleanInput(
|
|
|
|
|
constants.INPUT_OVERWRITE_SETTINGS,
|
|
|
|
|
true
|
|
|
|
|
);
|
2021-04-05 13:02:27 +03:00
|
|
|
const gpgPrivateKey =
|
2023-03-09 14:49:35 +02:00
|
|
|
core.getInput(constants.INPUT_GPG_PRIVATE_KEY) ||
|
|
|
|
|
constants.INPUT_DEFAULT_GPG_PRIVATE_KEY;
|
2026-07-17 20:10:58 -04:00
|
|
|
const gpgPassphraseEnvVar = getInputWithDeprecatedAlias(
|
|
|
|
|
constants.INPUT_GPG_PASSPHRASE_ENV_VAR,
|
|
|
|
|
constants.INPUT_GPG_PASSPHRASE_DEPRECATED,
|
|
|
|
|
gpgPrivateKey ? constants.INPUT_DEFAULT_GPG_PASSPHRASE : undefined
|
|
|
|
|
);
|
2021-04-05 13:02:27 +03:00
|
|
|
|
|
|
|
|
if (gpgPrivateKey) {
|
|
|
|
|
core.setSecret(gpgPrivateKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await createAuthenticationSettings(
|
|
|
|
|
id,
|
2026-07-17 20:10:58 -04:00
|
|
|
usernameEnvVar,
|
|
|
|
|
passwordEnvVar,
|
2021-04-05 13:02:27 +03:00
|
|
|
settingsDirectory,
|
|
|
|
|
overwriteSettings,
|
2026-07-17 20:10:58 -04:00
|
|
|
gpgPassphraseEnvVar
|
2021-04-05 13:02:27 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (gpgPrivateKey) {
|
|
|
|
|
core.info('Importing private gpg key');
|
|
|
|
|
const keyFingerprint = (await gpg.importKey(gpgPrivateKey)) || '';
|
|
|
|
|
core.saveState(constants.STATE_GPG_PRIVATE_KEY_FINGERPRINT, keyFingerprint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 20:10:58 -04:00
|
|
|
export function getInputWithDeprecatedAlias(
|
|
|
|
|
inputName: string,
|
|
|
|
|
deprecatedInputName: string,
|
|
|
|
|
defaultValue?: string
|
|
|
|
|
): string {
|
|
|
|
|
const value = core.getInput(inputName);
|
|
|
|
|
const deprecatedValue = core.getInput(deprecatedInputName);
|
|
|
|
|
|
|
|
|
|
if (deprecatedValue) {
|
|
|
|
|
core.warning(
|
|
|
|
|
`The '${deprecatedInputName}' input is deprecated and may be removed in a future release. Please use '${inputName}' instead.`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value || deprecatedValue || defaultValue || '';
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 13:02:27 +03:00
|
|
|
export async function createAuthenticationSettings(
|
2020-05-02 04:33:15 -07:00
|
|
|
id: string,
|
2026-07-17 20:10:58 -04:00
|
|
|
usernameEnvVar: string,
|
|
|
|
|
passwordEnvVar: string,
|
2021-04-05 13:02:27 +03:00
|
|
|
settingsDirectory: string,
|
|
|
|
|
overwriteSettings: boolean,
|
2026-07-17 20:10:58 -04:00
|
|
|
gpgPassphraseEnvVar: 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,
|
2026-07-17 20:10:58 -04:00
|
|
|
generate(id, usernameEnvVar, passwordEnvVar, gpgPassphraseEnvVar),
|
2021-04-05 13:02:27 +03:00
|
|
|
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,
|
2026-07-17 20:10:58 -04:00
|
|
|
usernameEnvVar: string,
|
|
|
|
|
passwordEnvVar: string,
|
|
|
|
|
gpgPassphraseEnvVar?: string | undefined
|
2019-12-19 11:28:11 -08:00
|
|
|
) {
|
2026-07-15 10:33:09 -04:00
|
|
|
// The maven-gpg-plugin reads the passphrase from the environment variable
|
|
|
|
|
// named by the `gpg.passphraseEnvName` property (default MAVEN_GPG_PASSPHRASE).
|
|
|
|
|
// Only configure it when the requested env var name differs from that default;
|
|
|
|
|
// otherwise the plugin already reads the right variable and no extra settings
|
|
|
|
|
// are needed. Writing `gpg.passphrase` to settings.xml is deprecated and fails
|
|
|
|
|
// when the plugin's `bestPractices` mode is enabled.
|
2026-07-30 15:09:35 -04:00
|
|
|
const includeGpgPassphraseProfile =
|
2026-07-17 20:10:58 -04:00
|
|
|
gpgPassphraseEnvVar &&
|
2026-07-30 15:09:35 -04:00
|
|
|
gpgPassphraseEnvVar !== constants.MAVEN_GPG_PASSPHRASE_DEFAULT_ENV;
|
|
|
|
|
|
|
|
|
|
const lines = [
|
|
|
|
|
'<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">',
|
|
|
|
|
' <interactiveMode>false</interactiveMode>',
|
|
|
|
|
' <servers>',
|
|
|
|
|
' <server>',
|
|
|
|
|
` <id>${escapeXmlText(id)}</id>`,
|
|
|
|
|
` <username>${escapeXmlText(`\${env.${usernameEnvVar}}`)}</username>`,
|
|
|
|
|
` <password>${escapeXmlText(`\${env.${passwordEnvVar}}`)}</password>`,
|
|
|
|
|
' </server>',
|
|
|
|
|
' </servers>'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (includeGpgPassphraseProfile) {
|
|
|
|
|
lines.push(
|
|
|
|
|
' <profiles>',
|
|
|
|
|
' <profile>',
|
|
|
|
|
` <id>${constants.GPG_PASSPHRASE_PROFILE_ID}</id>`,
|
|
|
|
|
' <properties>',
|
|
|
|
|
` <gpg.passphraseEnvName>${escapeXmlText(gpgPassphraseEnvVar)}</gpg.passphraseEnvName>`,
|
|
|
|
|
' </properties>',
|
|
|
|
|
' </profile>',
|
|
|
|
|
' </profiles>',
|
|
|
|
|
' <activeProfiles>',
|
|
|
|
|
` <activeProfile>${constants.GPG_PASSPHRASE_PROFILE_ID}</activeProfile>`,
|
|
|
|
|
' </activeProfiles>'
|
|
|
|
|
);
|
2020-05-02 04:33:15 -07:00
|
|
|
}
|
|
|
|
|
|
2026-07-30 15:09:35 -04:00
|
|
|
lines.push('</settings>');
|
|
|
|
|
return lines.join('\n');
|
2019-11-15 16:01:13 -08:00
|
|
|
}
|
|
|
|
|
|
2023-03-09 14:49:35 +02: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
|
|
|
}
|