From f9d70ef1624169744c8abc9b67f0b737e13b1799 Mon Sep 17 00:00:00 2001 From: Aasim Malladi Date: Fri, 14 Aug 2020 15:24:04 -0700 Subject: [PATCH 1/3] Added cleanup task to MavenAuthenticate to remove creds --- .../resources.resjson/en-US/resources.resjson | 3 ++- Tasks/MavenAuthenticateV0/cleanup.ts | 26 +++++++++++++++++++ Tasks/MavenAuthenticateV0/task.json | 13 +++++++--- Tasks/MavenAuthenticateV0/task.loc.json | 13 +++++++--- 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 Tasks/MavenAuthenticateV0/cleanup.ts diff --git a/Tasks/MavenAuthenticateV0/Strings/resources.resjson/en-US/resources.resjson b/Tasks/MavenAuthenticateV0/Strings/resources.resjson/en-US/resources.resjson index c3755f4ae749..b7b832b46ce3 100644 --- a/Tasks/MavenAuthenticateV0/Strings/resources.resjson/en-US/resources.resjson +++ b/Tasks/MavenAuthenticateV0/Strings/resources.resjson/en-US/resources.resjson @@ -14,5 +14,6 @@ "loc.messages.Info_SettingsXmlRead": "Adding authentication to settings file %s.", "loc.messages.Info_CreatingSettingsXml": "Creating new settings.xml at path %s.", "loc.messages.Info_WritingToSettingsXml": "Writing new settings.xml with added authentication.", - "loc.messages.Error_InvalidServiceConnection": "The service connection for %s is invalid." + "loc.messages.Error_InvalidServiceConnection": "The service connection for %s is invalid.", + "loc.messages.Error_FailedCleanupM2": "Failed to delete credentials from the user settings.xml file: %s" } \ No newline at end of file diff --git a/Tasks/MavenAuthenticateV0/cleanup.ts b/Tasks/MavenAuthenticateV0/cleanup.ts new file mode 100644 index 000000000000..0797f220ce42 --- /dev/null +++ b/Tasks/MavenAuthenticateV0/cleanup.ts @@ -0,0 +1,26 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import * as tl from 'azure-pipelines-task-lib/task'; + +async function run() { + try { + tl.setResourcePath(path.join(__dirname, 'task.json')); + + const userM2SettingsXmlPath: string = tl.getTaskVariable('userM2SettingsXmlPath'); + const backupUserM2SettingsFilePath: string = tl.getTaskVariable('backupUserM2SettingsFilePath'); + + if (userM2SettingsXmlPath && tl.exist(userM2SettingsXmlPath)) { + fs.unlinkSync(userM2SettingsXmlPath); + tl.debug('Deleted user m2 settings.xml file: ' + userM2SettingsXmlPath); + + if (backupUserM2SettingsFilePath && tl.exist(backupUserM2SettingsFilePath)) { + fs.renameSync(backupUserM2SettingsFilePath, userM2SettingsXmlPath); + tl.debug('Deleted user m2 settings.xml file: ' + backupUserM2SettingsFilePath); + } + } + } catch (err) { + tl.warning(tl.loc('Error_FailedCleanupM2', err)); + } +} + +run(); diff --git a/Tasks/MavenAuthenticateV0/task.json b/Tasks/MavenAuthenticateV0/task.json index 55d0754beb2e..cb6f8f9d2899 100644 --- a/Tasks/MavenAuthenticateV0/task.json +++ b/Tasks/MavenAuthenticateV0/task.json @@ -9,8 +9,8 @@ "author": "Microsoft Corporation", "version": { "Major": 0, - "Minor": 167, - "Patch": 2 + "Minor": 174, + "Patch": 0 }, "runsOn": [ "Agent", @@ -54,6 +54,12 @@ "target": "mavenauth.js" } }, + "postjobexecution": { + "Node": { + "target": "cleanup.js", + "argumentFormat": "" + } + }, "messages": { "Warning_FeedEntryAlreadyExists": "The settings for the feed or repository '%s' already exists in the users settings.xml file.", "Warning_NoEndpointsToAuth": "No repositories were selected to authenticate, please check your task configuration.", @@ -63,6 +69,7 @@ "Info_SettingsXmlRead": "Adding authentication to settings file %s.", "Info_CreatingSettingsXml": "Creating new settings.xml at path %s.", "Info_WritingToSettingsXml": "Writing new settings.xml with added authentication.", - "Error_InvalidServiceConnection": "The service connection for %s is invalid." + "Error_InvalidServiceConnection": "The service connection for %s is invalid.", + "Error_FailedCleanupM2": "Failed to delete credentials from the user settings.xml file: %s" } } \ No newline at end of file diff --git a/Tasks/MavenAuthenticateV0/task.loc.json b/Tasks/MavenAuthenticateV0/task.loc.json index ac85678ad4d4..3dd1bc29a2ea 100644 --- a/Tasks/MavenAuthenticateV0/task.loc.json +++ b/Tasks/MavenAuthenticateV0/task.loc.json @@ -9,8 +9,8 @@ "author": "Microsoft Corporation", "version": { "Major": 0, - "Minor": 167, - "Patch": 2 + "Minor": 174, + "Patch": 0 }, "runsOn": [ "Agent", @@ -54,6 +54,12 @@ "target": "mavenauth.js" } }, + "postjobexecution": { + "Node": { + "target": "cleanup.js", + "argumentFormat": "" + } + }, "messages": { "Warning_FeedEntryAlreadyExists": "ms-resource:loc.messages.Warning_FeedEntryAlreadyExists", "Warning_NoEndpointsToAuth": "ms-resource:loc.messages.Warning_NoEndpointsToAuth", @@ -63,6 +69,7 @@ "Info_SettingsXmlRead": "ms-resource:loc.messages.Info_SettingsXmlRead", "Info_CreatingSettingsXml": "ms-resource:loc.messages.Info_CreatingSettingsXml", "Info_WritingToSettingsXml": "ms-resource:loc.messages.Info_WritingToSettingsXml", - "Error_InvalidServiceConnection": "ms-resource:loc.messages.Error_InvalidServiceConnection" + "Error_InvalidServiceConnection": "ms-resource:loc.messages.Error_InvalidServiceConnection", + "Error_FailedCleanupM2": "ms-resource:loc.messages.Error_FailedCleanupM2" } } \ No newline at end of file From 12e76f7b7055430ff83a40e26dc793443c3f3b1a Mon Sep 17 00:00:00 2001 From: Aasim Malladi Date: Fri, 14 Aug 2020 16:23:53 -0700 Subject: [PATCH 2/3] Fixed mavenauth.ts --- Tasks/MavenAuthenticateV0/cleanup.ts | 9 ++++----- Tasks/MavenAuthenticateV0/mavenauth.ts | 10 ++++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Tasks/MavenAuthenticateV0/cleanup.ts b/Tasks/MavenAuthenticateV0/cleanup.ts index 0797f220ce42..79b5b5cd2e70 100644 --- a/Tasks/MavenAuthenticateV0/cleanup.ts +++ b/Tasks/MavenAuthenticateV0/cleanup.ts @@ -1,4 +1,3 @@ -import * as fs from 'fs'; import * as path from 'path'; import * as tl from 'azure-pipelines-task-lib/task'; @@ -10,12 +9,12 @@ async function run() { const backupUserM2SettingsFilePath: string = tl.getTaskVariable('backupUserM2SettingsFilePath'); if (userM2SettingsXmlPath && tl.exist(userM2SettingsXmlPath)) { - fs.unlinkSync(userM2SettingsXmlPath); + tl.rmRF(userM2SettingsXmlPath); tl.debug('Deleted user m2 settings.xml file: ' + userM2SettingsXmlPath); - + if (backupUserM2SettingsFilePath && tl.exist(backupUserM2SettingsFilePath)) { - fs.renameSync(backupUserM2SettingsFilePath, userM2SettingsXmlPath); - tl.debug('Deleted user m2 settings.xml file: ' + backupUserM2SettingsFilePath); + tl.mv(backupUserM2SettingsFilePath, userM2SettingsXmlPath); + tl.debug('Restored old user m2 settings.xml file: ' + backupUserM2SettingsFilePath); } } } catch (err) { diff --git a/Tasks/MavenAuthenticateV0/mavenauth.ts b/Tasks/MavenAuthenticateV0/mavenauth.ts index d7848f962b06..52cd00990545 100644 --- a/Tasks/MavenAuthenticateV0/mavenauth.ts +++ b/Tasks/MavenAuthenticateV0/mavenauth.ts @@ -6,6 +6,7 @@ import { emitTelemetry } from 'artifacts-common/telemetry'; const M2FolderName: string = ".m2"; const SettingsXmlName: string = "settings.xml"; +const backupSettingsXmlName: string = "_settings.xml"; tl.setResourcePath(path.join(__dirname, 'task.json')); @@ -36,11 +37,16 @@ async function run(): Promise { } let userSettingsXmlPath: string = path.join(userM2FolderPath, SettingsXmlName); + let backupSettingsXmlPath: string = path.join(userM2FolderPath, backupSettingsXmlName); let settingsJson: any; + + tl.setTaskVariable('userM2SettingsXmlPath', userSettingsXmlPath); - if(tl.exist(userSettingsXmlPath)) { + if (tl.exist(userSettingsXmlPath)) { tl.debug(tl.loc("Info_SettingsXmlRead", userSettingsXmlPath)); - settingsJson = await util.readXmlFileAsJson(userSettingsXmlPath) + tl.cp(userSettingsXmlPath, backupSettingsXmlPath); + tl.setTaskVariable("backupUserM2SettingsFilePath", backupSettingsXmlPath); + settingsJson = await util.readXmlFileAsJson(userSettingsXmlPath); } else { tl.debug(tl.loc("Info_CreatingSettingsXml", userSettingsXmlPath)); From e16b58020462b6e06f25d2fbb7450dcc60d4898d Mon Sep 17 00:00:00 2001 From: Aasim Malladi Date: Mon, 17 Aug 2020 12:16:46 -0700 Subject: [PATCH 3/3] Updated milestone --- Tasks/MavenAuthenticateV0/task.json | 2 +- Tasks/MavenAuthenticateV0/task.loc.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tasks/MavenAuthenticateV0/task.json b/Tasks/MavenAuthenticateV0/task.json index cb6f8f9d2899..3f77f11c845e 100644 --- a/Tasks/MavenAuthenticateV0/task.json +++ b/Tasks/MavenAuthenticateV0/task.json @@ -9,7 +9,7 @@ "author": "Microsoft Corporation", "version": { "Major": 0, - "Minor": 174, + "Minor": 175, "Patch": 0 }, "runsOn": [ diff --git a/Tasks/MavenAuthenticateV0/task.loc.json b/Tasks/MavenAuthenticateV0/task.loc.json index 3dd1bc29a2ea..bc62098426fa 100644 --- a/Tasks/MavenAuthenticateV0/task.loc.json +++ b/Tasks/MavenAuthenticateV0/task.loc.json @@ -9,7 +9,7 @@ "author": "Microsoft Corporation", "version": { "Major": 0, - "Minor": 174, + "Minor": 175, "Patch": 0 }, "runsOn": [