<# .SYNOPSIS Gives the EasyLife 365 app access to an existing secret in a key vault of your subscription. .DESCRIPTION EasyLife 365 setup script. Use case: identity/credential-renewal-secret (a renewed client secret). Version: v1. Source: https://docs.easylife365.cloud/downloads/identity/credential-renewal-secret/v1/grant.ps1 The script finds the key vault -VaultName in the subscription you are signed in to, checks that it uses Azure role-based access control (on access policies it stops) and that the secret -SecretName exists. Then it assigns the role "Key Vault Secrets Officer" to the service principal of the EasyLife 365 app (-AppId), on that secret only. It creates nothing else and never reads the secret's value. You can run it again: a role that is assigned already is left as it is and listed as skipped. The script never changes the settings of a resource and never deletes anything. Run it as someone who can give access on the secret: Owner, User Access Administrator or Role Based Access Control Administrator. Needs PowerShell 7 or Windows PowerShell 5.1 with the modules Az.Accounts and Az.Resources, signed in with Connect-AzAccount. Azure Cloud Shell has everything. .PARAMETER VaultName The key vault. .PARAMETER SecretName The secret: 1 to 127 letters, digits and hyphens. A name that starts with a hyphen works as it is, unless it is also the start of a parameter name of this script (for example -S or -App) or of a common parameter such as -Verbose (for example -V or -E): then quote it, -SecretName '-S'. .PARAMETER AppId The application (client) ID of the EasyLife 365 app in your tenant. EasyLife 365 Admin shows it in the command. .PARAMETER Subscription Optional. The subscription ID or name that holds the vault. Default: the current subscription. .EXAMPLE ./grant.ps1 -VaultName contoso-renewal -SecretName app-secret -AppId 00000000-0000-0000-0000-000000000000 #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'The script talks to the person who runs it.')] param( [Parameter(Mandatory = $true)][string]$VaultName, [Parameter(Mandatory = $true)][string]$SecretName, [Parameter(Mandatory = $true)][string]$AppId, [string]$Subscription ) $ErrorActionPreference = 'Stop' $role = 'Key Vault Secrets Officer' $apiVersion = '2023-07-01' if ($VaultName -notmatch '^[A-Za-z][A-Za-z0-9-]{1,22}[A-Za-z0-9]$' -or $VaultName.Contains('--')) { throw '-VaultName must be 3 to 24 letters, digits and hyphens, start with a letter, end with a letter or digit, and have no two hyphens in a row.' } if ($SecretName -notmatch '^[A-Za-z0-9-]{1,127}$') { throw '-SecretName must be 1 to 127 letters, digits and hyphens.' } if ($AppId -notmatch '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$') { throw '-AppId must be the application (client) ID, a GUID such as 00000000-0000-0000-0000-000000000000.' } $context = Get-AzContext if (-not $context) { throw 'Sign in first with Connect-AzAccount.' } if ($Subscription) { $context = Set-AzContext -Subscription $Subscription -Scope Process } Write-Host "Subscription: $($context.Subscription.Name) ($($context.Subscription.Id))" $servicePrincipal = Get-AzADServicePrincipal -ApplicationId $AppId -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $servicePrincipal) { throw "Stopped. Nothing was changed. There is no service principal for the app $AppId in this tenant. Check that you're signed in to the right tenant, and that an administrator has consented to the EasyLife 365 app." } Write-Host "EasyLife 365 app: $($servicePrincipal.DisplayName), app ID $AppId, service principal $($servicePrincipal.Id)" $vault = Get-AzResource -ResourceType 'Microsoft.KeyVault/vaults' -Name $VaultName | Select-Object -First 1 if (-not $vault) { throw "Stopped. Nothing was changed. There is no key vault $VaultName in this subscription. Pass -Subscription with the subscription that holds it." } # EasyLife 365 works with Azure role-based access control only. A vault on access policies is never changed here. $vaultProperties = (Get-AzResource -ResourceId $vault.ResourceId -ExpandProperties).Properties if ($vaultProperties.enableRbacAuthorization -ne $true) { throw "Stopped. Nothing was changed. The key vault $VaultName uses access policies. EasyLife 365 works with Azure role-based access control only. Switch the vault's permission model to Azure role-based access control (Access configuration in the Azure portal), or use another vault, then run the script again." } # Read through Azure Resource Manager: the metadata only, never the value, and with no data role needed. $existing = Invoke-AzRestMethod -Method GET -Path "$($vault.ResourceId)/secrets/$SecretName`?api-version=$apiVersion" if ($existing.StatusCode -eq 404) { throw "Stopped. Nothing was changed. The key vault $VaultName has no secret $SecretName. Check the name, or create the secret first." } if ($existing.StatusCode -ne 200) { throw "Azure didn't answer whether the secret $SecretName exists (HTTP $($existing.StatusCode)): $($existing.Content)" } $scope = "$($vault.ResourceId)/secrets/$SecretName" $assignment = Get-AzRoleAssignment -ObjectId $servicePrincipal.Id -RoleDefinitionName $role -Scope $scope | Where-Object { $_.Scope -eq $scope } | Select-Object -First 1 Write-Host '' if ($assignment) { Write-Host "Skipped: role $role for the EasyLife 365 app on the secret (assigned)" Write-Host 'Nothing changed: the access was in place already.' } else { New-AzRoleAssignment -ObjectId $servicePrincipal.Id -RoleDefinitionName $role -Scope $scope | Out-Null Write-Host "Created: role $role for the EasyLife 365 app on the secret" } Write-Host '' Write-Host "Secret: $scope" Write-Host 'Next: go back to EasyLife 365 Admin and test the connection. A new role can take a few minutes to apply.'