Skip to content

Commit

Permalink
MSI Support tasks - 2 (#7404)
Browse files Browse the repository at this point in the history
* MSI Support tasks - 2

* azure kry vault fix

* monitor l0 test fixes

* scheme order changes
  • Loading branch information
asranja authored and RoshanKumarMicrosoft committed Jun 8, 2018
1 parent af0a757 commit 9a9ab52
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"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.",
"loc.messages.GetSecretValueFailed": "Get secret value failed for: %s. Error: %s.",
"loc.messages.ConflictingVariableFound": "Variable with name %s is defined in both environment and key vault",
"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 -",
"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.",
"loc.messages.UploadingAttachment": "Uploading %s as attachment",
"loc.messages.CouldNotWriteToFile": "Could not save content to file. Failed with an error %s",
"loc.messages.CouldNotMaskSecret": "%s value has regular expressions hence could not mask completely",
Expand Down
9 changes: 6 additions & 3 deletions Tasks/AzureKeyVaultV1/models/KeyVaultTaskParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class KeyVaultTaskParameters {
public vaultCredentials: msRestAzure.ApplicationTokenCredentials;
public keyVaultUrl: string;
public servicePrincipalId: string;
public scheme: string;

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

this.servicePrincipalId = tl.getEndpointAuthorizationParameter(connectedService, 'serviceprincipalid', false);
this.servicePrincipalId = tl.getEndpointAuthorizationParameter(connectedService, 'serviceprincipalid', true);
this.keyVaultUrl = util.format("https://%s.%s", this.keyVaultName, azureKeyVaultDnsSuffix);
this.scheme = tl.getEndpointAuthorizationScheme(connectedService, false);
this.vaultCredentials = this.getVaultCredentials(connectedService, azureKeyVaultDnsSuffix);
}

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

var servicePrincipalKey: string = tl.getEndpointAuthorizationParameter(connectedService, 'serviceprincipalkey', false);
var servicePrincipalKey: string = tl.getEndpointAuthorizationParameter(connectedService, 'serviceprincipalkey', true);
var tenantId: string = tl.getEndpointAuthorizationParameter(connectedService, 'tenantid', false);
var armUrl: string = tl.getEndpointUrl(connectedService, true);
var envAuthorityUrl: string = tl.getEndpointDataParameter(connectedService, 'environmentAuthorityUrl', true);
envAuthorityUrl = (envAuthorityUrl != null) ? envAuthorityUrl : "https://login.windows.net/";
var credentials = new msRestAzure.ApplicationTokenCredentials(this.servicePrincipalId, tenantId, servicePrincipalKey, vaultUrl, envAuthorityUrl, vaultUrl, false);
var msiClientId = tl.getEndpointDataParameter(connectedService, 'msiclientId', true);
var credentials = new msRestAzure.ApplicationTokenCredentials(this.servicePrincipalId, tenantId, servicePrincipalKey, vaultUrl, envAuthorityUrl, vaultUrl, false, this.scheme , msiClientId);
return credentials;
}
}
18 changes: 16 additions & 2 deletions Tasks/AzureKeyVaultV1/npm-shrinkwrap.json

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

20 changes: 15 additions & 5 deletions Tasks/AzureKeyVaultV1/operations/KeyVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,21 @@ export class KeyVault {
this.taskParameters.keyVaultName,
this.taskParameters.keyVaultUrl);

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

this.provisionKeyVaultSecretsScript = util.format(scriptContentFormat, this.taskParameters.subscriptionId, this.taskParameters.servicePrincipalId, this.taskParameters.keyVaultName);
}
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureKeyVaultV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 19
"Patch": 20
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureKeyVaultV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 19
"Patch": 20
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"loc.messages.CreatedRule": "Created rule : '%s'",
"loc.messages.UpdatedRule": "Updated rule : '%s'",
"loc.messages.Couldnotfetchaccesstoken": "Could not fetch access token for Azure. Status Code: %s (%s) %s.",
"loc.messages.SPNExpiredCheck": "Check if the SPN is valid and not expired."
"loc.messages.SPNExpiredCheck": "Check if the SPN is valid and not expired.",
"loc.messages.MSINotSupported": "Managed Service Identity(MSI) authentication is not supported for this task."
}
6 changes: 6 additions & 0 deletions Tasks/AzureMonitorAlertsV0/azuremonitoralerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ async function run() {
let alertRules: IAzureMetricAlertRulesList = JSON.parse(tl.getInput("AlertRules", true));
let notifyServiceOwners: boolean = tl.getInput("NotifyServiceOwners") && tl.getInput("NotifyServiceOwners").toLowerCase() === "true" ? true : false;
let notifyEmails: string = tl.getInput("NotifyEmails");

let endpointScheme = tl.getEndpointAuthorizationScheme(connectedServiceName, true);
if (endpointScheme === "ManagedServiceIdentity") {
throw tl.loc("MSINotSupported");
}

let endpoint = await initializeAzureRMEndpointData(connectedServiceName);

let resourceId: string = `/subscriptions/${endpoint["subscriptionId"]}/resourceGroups/${resourceGroupName}/providers/${resourceType}/${resourceName}`
Expand Down
5 changes: 3 additions & 2 deletions Tasks/AzureMonitorAlertsV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 0,
"Minor": 1,
"Patch": 0
"Patch": 1
},
"minimumAgentVersion": "2.111.0",
"instanceNameFormat": "Configure Azure Alerts : $(ResourceName)",
Expand Down Expand Up @@ -129,6 +129,7 @@
"CreatedRule": "Created rule : '%s'",
"UpdatedRule": "Updated rule : '%s'",
"Couldnotfetchaccesstoken": "Could not fetch access token for Azure. Status Code: %s (%s) %s.",
"SPNExpiredCheck": "Check if the SPN is valid and not expired."
"SPNExpiredCheck": "Check if the SPN is valid and not expired.",
"MSINotSupported": "Managed Service Identity(MSI) authentication is not supported for this task."
}
}
5 changes: 3 additions & 2 deletions Tasks/AzureMonitorAlertsV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 0,
"Minor": 1,
"Patch": 0
"Patch": 1
},
"minimumAgentVersion": "2.111.0",
"instanceNameFormat": "ms-resource:loc.instanceNameFormat",
Expand Down Expand Up @@ -131,6 +131,7 @@
"CreatedRule": "ms-resource:loc.messages.CreatedRule",
"UpdatedRule": "ms-resource:loc.messages.UpdatedRule",
"Couldnotfetchaccesstoken": "ms-resource:loc.messages.Couldnotfetchaccesstoken",
"SPNExpiredCheck": "ms-resource:loc.messages.SPNExpiredCheck"
"SPNExpiredCheck": "ms-resource:loc.messages.SPNExpiredCheck",
"MSINotSupported": "ms-resource:loc.messages.MSINotSupported"
}
}

0 comments on commit 9a9ab52

Please sign in to comment.