Skip to content

Commit 9a9ab52

Browse files
asranjaRoshanKumarMicrosoft
authored andcommitted
MSI Support tasks - 2 (#7404)
* MSI Support tasks - 2 * azure kry vault fix * monitor l0 test fixes * scheme order changes
1 parent af0a757 commit 9a9ab52

File tree

10 files changed

+54
-18
lines changed

10 files changed

+54
-18
lines changed

Tasks/AzureKeyVaultV1/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"loc.messages.AccessDeniedError": "%s. Specified Azure endpoint needs to have Get, List secret management permissions on the selected key vault. To set these permissions, download ProvisionKeyVaultPermissions.ps1 script from build/release logs and execute it OR set them from Azure portal.",
3737
"loc.messages.GetSecretValueFailed": "Get secret value failed for: %s. Error: %s.",
3838
"loc.messages.ConflictingVariableFound": "Variable with name %s is defined in both environment and key vault",
39-
"loc.messages.GetSecretFailedBecauseOfInvalidCharacters": "Cannot find the secret with name: %s. Secret name must be a string 1-127 characters in length containing only 0-9, a-z, A-Z, and -",
39+
"loc.messages.GetSecretFailedBecauseOfInvalidCharacters": "Secret not found: %s. Secret name must be a string 1-127 characters in length containing only -, 0-9, a-z and A-Z.",
4040
"loc.messages.UploadingAttachment": "Uploading %s as attachment",
4141
"loc.messages.CouldNotWriteToFile": "Could not save content to file. Failed with an error %s",
4242
"loc.messages.CouldNotMaskSecret": "%s value has regular expressions hence could not mask completely",

Tasks/AzureKeyVaultV1/models/KeyVaultTaskParameters.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class KeyVaultTaskParameters {
1212
public vaultCredentials: msRestAzure.ApplicationTokenCredentials;
1313
public keyVaultUrl: string;
1414
public servicePrincipalId: string;
15+
public scheme: string;
1516

1617
constructor() {
1718
var connectedService = tl.getInput("ConnectedServiceName", true);
@@ -20,20 +21,22 @@ export class KeyVaultTaskParameters {
2021
this.secretsFilter = tl.getDelimitedInput("SecretsFilter", ",", true);
2122
var azureKeyVaultDnsSuffix = tl.getEndpointDataParameter(connectedService, "AzureKeyVaultDnsSuffix", true);
2223

23-
this.servicePrincipalId = tl.getEndpointAuthorizationParameter(connectedService, 'serviceprincipalid', false);
24+
this.servicePrincipalId = tl.getEndpointAuthorizationParameter(connectedService, 'serviceprincipalid', true);
2425
this.keyVaultUrl = util.format("https://%s.%s", this.keyVaultName, azureKeyVaultDnsSuffix);
26+
this.scheme = tl.getEndpointAuthorizationScheme(connectedService, false);
2527
this.vaultCredentials = this.getVaultCredentials(connectedService, azureKeyVaultDnsSuffix);
2628
}
2729

2830
private getVaultCredentials(connectedService: string, azureKeyVaultDnsSuffix: string): msRestAzure.ApplicationTokenCredentials {
2931
var vaultUrl = util.format("https://%s", azureKeyVaultDnsSuffix);
3032

31-
var servicePrincipalKey: string = tl.getEndpointAuthorizationParameter(connectedService, 'serviceprincipalkey', false);
33+
var servicePrincipalKey: string = tl.getEndpointAuthorizationParameter(connectedService, 'serviceprincipalkey', true);
3234
var tenantId: string = tl.getEndpointAuthorizationParameter(connectedService, 'tenantid', false);
3335
var armUrl: string = tl.getEndpointUrl(connectedService, true);
3436
var envAuthorityUrl: string = tl.getEndpointDataParameter(connectedService, 'environmentAuthorityUrl', true);
3537
envAuthorityUrl = (envAuthorityUrl != null) ? envAuthorityUrl : "https://login.windows.net/";
36-
var credentials = new msRestAzure.ApplicationTokenCredentials(this.servicePrincipalId, tenantId, servicePrincipalKey, vaultUrl, envAuthorityUrl, vaultUrl, false);
38+
var msiClientId = tl.getEndpointDataParameter(connectedService, 'msiclientId', true);
39+
var credentials = new msRestAzure.ApplicationTokenCredentials(this.servicePrincipalId, tenantId, servicePrincipalKey, vaultUrl, envAuthorityUrl, vaultUrl, false, this.scheme , msiClientId);
3740
return credentials;
3841
}
3942
}

Tasks/AzureKeyVaultV1/npm-shrinkwrap.json

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tasks/AzureKeyVaultV1/operations/KeyVault.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,21 @@ export class KeyVault {
5555
this.taskParameters.keyVaultName,
5656
this.taskParameters.keyVaultUrl);
5757

58-
var scriptContentFormat = `$ErrorActionPreference=\"Stop\";
59-
Login-AzureRmAccount -SubscriptionId %s;
60-
$spn=(Get-AzureRmADServicePrincipal -SPN %s);
61-
$spnObjectId=$spn.Id;
62-
Set-AzureRmKeyVaultAccessPolicy -VaultName %s -ObjectId $spnObjectId -PermissionsToSecrets get,list;`;
58+
let scriptContentFormat;
59+
if(this.taskParameters.scheme === "ManagedServiceIdentity") {
60+
scriptContentFormat = `$ErrorActionPreference=\"Stop\";
61+
Login-AzureRmAccount -SubscriptionId %s;
62+
$vmMetadata = Invoke-RestMethod -Headers @{"Metadata"="true"} -URI http://169.254.169.254/metadata/instance?api-version=2017-08-01 -Method get
63+
$vm = Get-AzureRmVM -ResourceGroupName $vmMetadata.compute.resourceGroupName -Name $vmMetadata.compute.name
64+
$spn=(Get-AzureRmADServicePrincipal -SPN %s);
65+
Set-AzureRmKeyVaultAccessPolicy -VaultName %s -ObjectId $vm.Identity.PrincipalId -PermissionsToSecrets get,list;`;
66+
} else {
67+
scriptContentFormat = `$ErrorActionPreference=\"Stop\";
68+
Login-AzureRmAccount -SubscriptionId %s;
69+
$spn=(Get-AzureRmADServicePrincipal -SPN %s);
70+
$spnObjectId=$spn.Id;
71+
Set-AzureRmKeyVaultAccessPolicy -VaultName %s -ObjectId $spnObjectId -PermissionsToSecrets get,list;`;
72+
}
6373

6474
this.provisionKeyVaultSecretsScript = util.format(scriptContentFormat, this.taskParameters.subscriptionId, this.taskParameters.servicePrincipalId, this.taskParameters.keyVaultName);
6575
}

Tasks/AzureKeyVaultV1/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"version": {
1515
"Major": 1,
1616
"Minor": 0,
17-
"Patch": 19
17+
"Patch": 20
1818
},
1919
"demands": [],
2020
"minimumAgentVersion": "2.0.0",

Tasks/AzureKeyVaultV1/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"version": {
1515
"Major": 1,
1616
"Minor": 0,
17-
"Patch": 19
17+
"Patch": 20
1818
},
1919
"demands": [],
2020
"minimumAgentVersion": "2.0.0",

Tasks/AzureMonitorAlertsV0/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
"loc.messages.CreatedRule": "Created rule : '%s'",
2727
"loc.messages.UpdatedRule": "Updated rule : '%s'",
2828
"loc.messages.Couldnotfetchaccesstoken": "Could not fetch access token for Azure. Status Code: %s (%s) %s.",
29-
"loc.messages.SPNExpiredCheck": "Check if the SPN is valid and not expired."
29+
"loc.messages.SPNExpiredCheck": "Check if the SPN is valid and not expired.",
30+
"loc.messages.MSINotSupported": "Managed Service Identity(MSI) authentication is not supported for this task."
3031
}

Tasks/AzureMonitorAlertsV0/azuremonitoralerts.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ async function run() {
2020
let alertRules: IAzureMetricAlertRulesList = JSON.parse(tl.getInput("AlertRules", true));
2121
let notifyServiceOwners: boolean = tl.getInput("NotifyServiceOwners") && tl.getInput("NotifyServiceOwners").toLowerCase() === "true" ? true : false;
2222
let notifyEmails: string = tl.getInput("NotifyEmails");
23+
24+
let endpointScheme = tl.getEndpointAuthorizationScheme(connectedServiceName, true);
25+
if (endpointScheme === "ManagedServiceIdentity") {
26+
throw tl.loc("MSINotSupported");
27+
}
28+
2329
let endpoint = await initializeAzureRMEndpointData(connectedServiceName);
2430

2531
let resourceId: string = `/subscriptions/${endpoint["subscriptionId"]}/resourceGroups/${resourceGroupName}/providers/${resourceType}/${resourceName}`

Tasks/AzureMonitorAlertsV0/task.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": {
1414
"Major": 0,
1515
"Minor": 1,
16-
"Patch": 0
16+
"Patch": 1
1717
},
1818
"minimumAgentVersion": "2.111.0",
1919
"instanceNameFormat": "Configure Azure Alerts : $(ResourceName)",
@@ -129,6 +129,7 @@
129129
"CreatedRule": "Created rule : '%s'",
130130
"UpdatedRule": "Updated rule : '%s'",
131131
"Couldnotfetchaccesstoken": "Could not fetch access token for Azure. Status Code: %s (%s) %s.",
132-
"SPNExpiredCheck": "Check if the SPN is valid and not expired."
132+
"SPNExpiredCheck": "Check if the SPN is valid and not expired.",
133+
"MSINotSupported": "Managed Service Identity(MSI) authentication is not supported for this task."
133134
}
134135
}

Tasks/AzureMonitorAlertsV0/task.loc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": {
1414
"Major": 0,
1515
"Minor": 1,
16-
"Patch": 0
16+
"Patch": 1
1717
},
1818
"minimumAgentVersion": "2.111.0",
1919
"instanceNameFormat": "ms-resource:loc.instanceNameFormat",
@@ -131,6 +131,7 @@
131131
"CreatedRule": "ms-resource:loc.messages.CreatedRule",
132132
"UpdatedRule": "ms-resource:loc.messages.UpdatedRule",
133133
"Couldnotfetchaccesstoken": "ms-resource:loc.messages.Couldnotfetchaccesstoken",
134-
"SPNExpiredCheck": "ms-resource:loc.messages.SPNExpiredCheck"
134+
"SPNExpiredCheck": "ms-resource:loc.messages.SPNExpiredCheck",
135+
"MSINotSupported": "ms-resource:loc.messages.MSINotSupported"
135136
}
136137
}

0 commit comments

Comments
 (0)