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

Removed SPN ObjectId Fetching step #14382

Merged
merged 2 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 0 additions & 17 deletions Tasks/PackerBuildV1/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe('PackerBuild Suite V1', function() {
delete process.env["__packer_exists__"] ;
t-dedah marked this conversation as resolved.
Show resolved Hide resolved
delete process.env["__packer_fix_fails__"] ;
delete process.env["__packer_validate_fails__"] ;
delete process.env["__spnObjectId_not_exists__"] ;

done();
});
Expand Down Expand Up @@ -243,22 +242,6 @@ describe('PackerBuild Suite V1', function() {
done();
});

it('Should fetch SPN object id if service endpoint does not contain it', (done:MochaDone) => {
process.env["__spnObjectId_not_exists__"] = "true";
let tp = path.join(__dirname, 'L0Windows.js');
let tr : ttm.MockTestRunner = new ttm.MockTestRunner(tp);
tr.run();
process.env["__spnObjectId_not_exists__"] = "false";

runValidations(() => {
assert(tr.invokedToolCount == 4, 'should have invoked tool four times. actual: ' + tr.invokedToolCount);
assert(tr.succeeded, 'task should have succeeded');
assert(tr.stdout.indexOf("loc_mock_FetchingSPNDetailsRemotely") != -1, "SPN object should be fetched");
assert(tr.stdout.indexOf("loc_mock_FetchedSPNDetailsRemotely") != -1, "SPN object should be fetched");
}, tr, done);

});

it('Should cleanup temp template folder', (done:MochaDone) => {
let tp = path.join(__dirname, 'L0Windows.js');
let tr : ttm.MockTestRunner = new ttm.MockTestRunner(tp);
Expand Down
1 change: 0 additions & 1 deletion Tasks/PackerBuildV1/Tests/L0Linux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "oId";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
Expand Down
1 change: 0 additions & 1 deletion Tasks/PackerBuildV1/Tests/L0LinuxCustomImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "oId";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
Expand Down
1 change: 0 additions & 1 deletion Tasks/PackerBuildV1/Tests/L0LinuxInstallPacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "oId";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
Expand Down
1 change: 0 additions & 1 deletion Tasks/PackerBuildV1/Tests/L0LinuxManaged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
Expand Down
1 change: 0 additions & 1 deletion Tasks/PackerBuildV1/Tests/L0Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "oId";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
Expand Down
5 changes: 0 additions & 5 deletions Tasks/PackerBuildV1/Tests/L0Windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "oId";
process.env["ENDPOINT_URL_AzureRMSpn"] = "https://management.azure.com/";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
process.env["RELEASE_RELEASENAME"] = "Release-1";
process.env["SYSTEM_DEFAULTWORKINGDIRECTORY"] = DefaultWorkingDirectory;

if(process.env["__spnObjectId_not_exists__"] === "true") {
delete process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"];
}

// provide answers for task mock
let a: any = <any>{
"which": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "oId";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
Expand Down
1 change: 0 additions & 1 deletion Tasks/PackerBuildV1/Tests/L0WindowsCustomImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "oId";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
Expand Down
1 change: 0 additions & 1 deletion Tasks/PackerBuildV1/Tests/L0WindowsFail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "oId";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
Expand Down
1 change: 0 additions & 1 deletion Tasks/PackerBuildV1/Tests/L0WindowsInstallPacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "oId";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
Expand Down
5 changes: 0 additions & 5 deletions Tasks/PackerBuildV1/Tests/L0WindowsManaged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,13 @@ process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_SERVICEPRINCIPALKEY"] = "spKey";
process.env["ENDPOINT_AUTH_PARAMETER_AzureRMSpn_TENANTID"] = "tenant";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONNAME"] = "sName";
process.env["ENDPOINT_DATA_AzureRMSpn_SUBSCRIPTIONID"] = "sId";
process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"] = "oId";
process.env["ENDPOINT_URL_AzureRMSpn"] = "https://management.azure.com/";
process.env["ENDPOINT_DATA_AzureRMSpn_ENVIRONMENTAUTHORITYURL"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_ACTIVEDIRECTORYSERVICEENDPOINTRESOURCEID"] = "https://login.windows.net/";
process.env["ENDPOINT_DATA_AzureRMSpn_GRAPHURL"] = "https://graph.windows.net/";
process.env["RELEASE_RELEASENAME"] = "Release-1";
process.env["SYSTEM_DEFAULTWORKINGDIRECTORY"] = DefaultWorkingDirectory;

if(process.env["__spnObjectId_not_exists__"] === "true") {
delete process.env["ENDPOINT_DATA_AzureRMSpn_SPNOBJECTID"];
}

// provide answers for task mock
let a: any = <any>{
"which": {
Expand Down
33 changes: 0 additions & 33 deletions Tasks/PackerBuildV1/src/azureSpnTemplateVariablesProvider.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
"use strict";

import azureGraph = require('azure-pipelines-tasks-azure-arm-rest-v2/azure-graph');
import msRestAzure = require("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common");

import * as tl from "azure-pipelines-task-lib/task";
import * as constants from "./constants";
import * as definitions from "./definitions"
import TaskParameters from "./taskParameters"

// Provider for all template variables related to azure SPN. Reads service endpoint to get all necessary details.
export default class AzureSpnTemplateVariablesProvider implements definitions.ITemplateVariablesProvider {
Expand Down Expand Up @@ -37,37 +33,8 @@ export default class AzureSpnTemplateVariablesProvider implements definitions.IT
this._spnVariables.set(constants.TemplateVariableClientSecretName, tl.getEndpointAuthorizationParameter(connectedService, 'serviceprincipalkey', false));
this._spnVariables.set(constants.TemplateVariableTenantIdName, tl.getEndpointAuthorizationParameter(connectedService, 'tenantid', false));


var spnObjectId = tl.getEndpointDataParameter(connectedService, "spnObjectId", true);
// if we are creating windows VM and SPN object-id is not available in service endpoint, fetch it from Graph endpoint
// NOP for nix
if(!spnObjectId && taskParameters.osType.toLowerCase().match(/^win/)) {
spnObjectId = await this.getServicePrincipalObjectId(await taskParameters.graphCredentialsPromise);
}

this._spnVariables.set(constants.TemplateVariableObjectIdName, spnObjectId);

return this._spnVariables;
}

private async getServicePrincipalObjectId(graphCredentials: msRestAzure.ApplicationTokenCredentials): Promise<string> {
console.log(tl.loc("FetchingSPNDetailsRemotely", graphCredentials.getClientId()));
var client = new azureGraph.GraphManagementClient(graphCredentials);
var servicePrincipal = null;
try {
servicePrincipal = await client.servicePrincipals.GetServicePrincipal(null);
} catch (error) {
throw tl.loc("FailedToFetchSPNDetailsRemotely", error.message);
}

var spnObjectId: string = "";
if(!!servicePrincipal && !!servicePrincipal.objectId) {
spnObjectId = servicePrincipal.objectId;
}

console.log(tl.loc("FetchedSPNDetailsRemotely", spnObjectId));
return spnObjectId;
}

private _spnVariables: Map<string, string>;
}
5 changes: 1 addition & 4 deletions Tasks/PackerBuildV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 176,
"Minor": 183,
"Patch": 0
},
"demands": [],
Expand Down Expand Up @@ -341,9 +341,6 @@
"ParsingAdditionalBuilderParameters": "Parsing additional builder parameters json.",
"ParsingTemplateFileContentFailed": "Unable to parse json content from template file %s with error: %s.",
"ParsingCustomTemplateParameters": "Parsing custom template parameters json.",
"FetchingSPNDetailsRemotely": "Fetching SPN details for app ID %s from the Azure AD graph service connection...",
"FetchedSPNDetailsRemotely": "Fetched SPN details successfully. ObjectId: %s",
"FailedToFetchSPNDetailsRemotely": "Could not fetch SPN details from the graph service connection. Error: %s.",
"GetArtifactItemsNotSupported": "Get artifact items not supported, invalid code path",
"CouldNotFetchAccessTokenforMSIDueToMSINotConfiguredProperlyStatusCode": "Could not fetch access token for Managed Service Principal. Please configure Managed Service Identity (MSI) for virtual machine 'https://aka.ms/azure-msi-docs'. Status code: %s, status message: %s",
"CouldNotFetchAccessTokenforMSIStatusCode": "Could not fetch access token for Managed Service Principal. Status code: %s, status message: %s",
Expand Down
5 changes: 1 addition & 4 deletions Tasks/PackerBuildV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 176,
"Minor": 183,
"Patch": 0
},
"demands": [],
Expand Down Expand Up @@ -341,9 +341,6 @@
"ParsingAdditionalBuilderParameters": "ms-resource:loc.messages.ParsingAdditionalBuilderParameters",
"ParsingTemplateFileContentFailed": "ms-resource:loc.messages.ParsingTemplateFileContentFailed",
"ParsingCustomTemplateParameters": "ms-resource:loc.messages.ParsingCustomTemplateParameters",
"FetchingSPNDetailsRemotely": "ms-resource:loc.messages.FetchingSPNDetailsRemotely",
"FetchedSPNDetailsRemotely": "ms-resource:loc.messages.FetchedSPNDetailsRemotely",
"FailedToFetchSPNDetailsRemotely": "ms-resource:loc.messages.FailedToFetchSPNDetailsRemotely",
"GetArtifactItemsNotSupported": "ms-resource:loc.messages.GetArtifactItemsNotSupported",
"CouldNotFetchAccessTokenforMSIDueToMSINotConfiguredProperlyStatusCode": "ms-resource:loc.messages.CouldNotFetchAccessTokenforMSIDueToMSINotConfiguredProperlyStatusCode",
"CouldNotFetchAccessTokenforMSIStatusCode": "ms-resource:loc.messages.CouldNotFetchAccessTokenforMSIStatusCode",
Expand Down