Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for token authentication in MSDeploy #19130

Merged
merged 4 commits into from
Oct 17, 2023
Merged
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
26 changes: 23 additions & 3 deletions Tasks/AzureRmWebAppDeploymentV3/azurermwebappdeployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { addReleaseAnnotation } from './operations/ReleaseAnnotationUtility';
import { PackageUtility } from 'azure-pipelines-tasks-webdeployment-common/packageUtility';
import { isInputPkgIsFolder, canUseWebDeploy } from 'azure-pipelines-tasks-webdeployment-common/utility';
import { DeployUsingMSDeploy } from 'azure-pipelines-tasks-webdeployment-common/deployusingmsdeploy';
import { shouldUseMSDeployTokenAuth} from 'azure-pipelines-tasks-webdeployment-common/msdeployutility';

async function main() {
let zipDeploymentID: string;
Expand Down Expand Up @@ -86,7 +87,7 @@ async function main() {

if(canUseWebDeploy(taskParams.UseWebDeploy)) {
tl.debug("Performing the deployment of webapp.");
if(!tl.osType().match(/^Win/)){
if(tl.getPlatform() !== tl.Platform.Windows){
throw Error(tl.loc("PublishusingwebdeployoptionsaresupportedonlywhenusingWindowsagent"));
}

Expand All @@ -95,13 +96,32 @@ async function main() {
}

var msDeployPublishingProfile = await appServiceUtility.getWebDeployPublishingProfile();
let authType = "Basic";

if (await appServiceUtility.isSitePublishingCredentialsEnabled())
{
tl.debug("Using Basic authentication.")
}
else if (shouldUseMSDeployTokenAuth())
{
tl.debug("Basic authentication is disabled, using token based authentication.");
authType = "Bearer";
msDeployPublishingProfile.userPWD = await appServiceUtility.getAuthToken();
msDeployPublishingProfile.userName = "user"; // arbitrary but not empty
}
else
{
//deployment would fail in this case
throw new Error(tl.loc("BasicAuthNotSupported"));
}

if (webPackage.toString().toLowerCase().endsWith('.war')) {
await DeployWar(webPackage, taskParams, msDeployPublishingProfile, kuduService, appServiceUtility);
await DeployWar(webPackage, taskParams, msDeployPublishingProfile, kuduService, appServiceUtility, authType);
}
else {
await DeployUsingMSDeploy(webPackage, taskParams.WebAppName, msDeployPublishingProfile, taskParams.RemoveAdditionalFilesFlag,
taskParams.ExcludeFilesFromAppDataFlag, taskParams.TakeAppOfflineFlag, taskParams.VirtualApplication, taskParams.SetParametersFile,
taskParams.AdditionalArguments, isFolderBasedDeployment, taskParams.UseWebDeploy);
taskParams.AdditionalArguments, isFolderBasedDeployment, taskParams.UseWebDeploy, authType);
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ export class AzureAppServiceUtility {
}
}

public async isSitePublishingCredentialsEnabled(): Promise<boolean> {
const utility = new AzureAppServiceUtilityCommon(this._appService);
return await utility.isSitePublishingCredentialsEnabled();
}

public async getAuthToken(): Promise<string> {
const token = await this._appService._client.getCredentials().getToken();
tl.setSecret(token);
return token;
}

public async getKuduService(): Promise<Kudu> {

const utility = new AzureAppServiceUtilityCommon(this._appService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { sleepFor } from 'azure-pipelines-tasks-azure-arm-rest/webClient';

import { DeployUsingMSDeploy } from 'azure-pipelines-tasks-webdeployment-common/deployusingmsdeploy';

export async function DeployWar(webPackage, taskParams: TaskParameters, msDeployPublishingProfile, kuduService: Kudu, appServiceUtility: AzureAppServiceUtility): Promise<void> {
export async function DeployWar(webPackage, taskParams: TaskParameters, msDeployPublishingProfile, kuduService: Kudu, appServiceUtility: AzureAppServiceUtility, authType: string): Promise<void> {
// get list of files before deploying to the web app.
await appServiceUtility.pingApplication();
var listOfFilesBeforeDeployment: any = await kuduService.listDir('/site/wwwroot/webapps/');
Expand All @@ -33,7 +33,7 @@ export async function DeployWar(webPackage, taskParams: TaskParameters, msDeploy
while (retryCount > 0) {
await DeployUsingMSDeploy(webPackage, taskParams.WebAppName, msDeployPublishingProfile, taskParams.RemoveAdditionalFilesFlag,
taskParams.ExcludeFilesFromAppDataFlag, taskParams.TakeAppOfflineFlag, taskParams.VirtualApplication, taskParams.SetParametersFile,
taskParams.AdditionalArguments, false, taskParams.UseWebDeploy);
taskParams.AdditionalArguments, false, taskParams.UseWebDeploy, authType);

// verify if the war file has expanded
// if not expanded, deploy using msdeploy once more, to make it work.
Expand Down
129 changes: 8 additions & 121 deletions Tasks/AzureRmWebAppDeploymentV3/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Tasks/AzureRmWebAppDeploymentV3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"azure-pipelines-task-lib": "^4.4.0",
"agent-base": "^6.0.2",
"azure-pipelines-tasks-azure-arm-rest": "3.223.3",
"azure-pipelines-tasks-webdeployment-common": "^4.222.2",
"azure-pipelines-tasks-webdeployment-common": "4.230.1",
"decompress-zip": "^0.3.3",
"ltx": "2.8.0",
"moment": "^2.29.4",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureRmWebAppDeploymentV3/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 3,
"Minor": 230,
"Patch": 1
"Patch": 2
},
"releaseNotes": "What's new in Version 3.0: <br/>&nbsp;&nbsp;Supports File Transformations (XDT) <br/>&nbsp;&nbsp;Supports Variable Substitutions(XML, JSON) <br/>Click [here](https://aka.ms/azurermwebdeployreadme) for more information.",
"minimumAgentVersion": "2.104.1",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureRmWebAppDeploymentV3/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 3,
"Minor": 230,
"Patch": 1
"Patch": 2
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"minimumAgentVersion": "2.104.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,21 @@ export class WindowsWebAppWebDeployProvider extends AzureRmWebAppDeploymentProvi
var deleteApplicationSetting = ParameterParser.parse(removeRunFromZipAppSetting)
await this.appServiceUtility.updateAndMonitorAppSettings(null, deleteApplicationSetting);

if(deployUtility.canUseWebDeploy(this.taskParams.UseWebDeploy)) {
let deploymentMethodtelemetry = '{"deploymentMethod":"Web Deploy"}';
if (deployUtility.canUseWebDeploy(this.taskParams.UseWebDeploy)) {
const webDeployUtility = new WebDeployUtility(this.appServiceUtility);
const deploymentMethodtelemetry = '{"deploymentMethod":"Web Deploy"}';
console.log("##vso[telemetry.publish area=TaskDeploymentMethod;feature=AzureWebAppDeployment]" + deploymentMethodtelemetry);

tl.debug("Performing the deployment of webapp.");

if(!tl.osType().match(/^Win/)) {
if (tl.getPlatform() !== tl.Platform.Windows) {
throw Error(tl.loc("PublishusingwebdeployoptionsaresupportedonlywhenusingWindowsagent"));
}

var msDeployPublishingProfile = await this.appServiceUtility.getWebDeployPublishingProfile();
await WebDeployUtility.publishUsingWebDeploy(this.taskParams,
WebDeployUtility.constructWebDeployArguments(this.taskParams, msDeployPublishingProfile), this.appServiceUtility
);


await webDeployUtility.publishUsingWebDeploy(this.taskParams);
}
else {
let deploymentMethodtelemetry = '{"deploymentMethod":"Zip API"}';
const deploymentMethodtelemetry = '{"deploymentMethod":"Zip API"}';
console.log("##vso[telemetry.publish area=TaskDeploymentMethod;feature=AzureWebAppDeployment]" + deploymentMethodtelemetry);

tl.debug("Initiated deployment via kudu service for webapp package : ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ export class AzureAppServiceUtility {

}

public async isSitePublishingCredentialsEnabled(): Promise<boolean> {
const utility = new AzureAppServiceUtilityCommon(this._appService);
return await utility.isSitePublishingCredentialsEnabled();
}

public async getAuthToken(): Promise<string> {
const token = await this._appService._client.getCredentials().getToken();
tl.setSecret(token);
return token;
}

public async getPhysicalPath(virtualApplication: string): Promise<string> {

if(!virtualApplication) {
Expand Down
Loading