Skip to content

Commit d54b03c

Browse files
committed
Feature: Add support for Azure SQL / Managed Identity based
1 parent 94a298f commit d54b03c

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"type": "Microsoft.Web/connections",
3+
"apiVersion": "2018-07-01-preview",
4+
"name": "##NAME##",
5+
"location": "[parameters('logicAppLocation')]",
6+
"kind": "V1",
7+
"properties": {
8+
"alternativeParameterValues": {},
9+
"api": {
10+
"id": "[subscriptionResourceId('Microsoft.Web/locations/managedApis', parameters('logicAppLocation'), 'sql')]"
11+
},
12+
"customParameterValues": {},
13+
"displayName": "##NAME##",
14+
"parameterValueSet": {
15+
"name": "oauthMI",
16+
"values": {}
17+
}
18+
}
19+
}

PsLogicAppExtractor/internal/tasks/All/All.task.ps1

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,110 @@ Task -Name "Set-Arm.Connections.ManagedApis.AzureBlob.ManagedIdentity.AsArmObjec
399399
Out-TaskFileArm -InputObject $armObj
400400
}
401401

402+
#Original file: Set-Arm.Connections.ManagedApis.AzureSql.ManagedIdentity.AsArmObject.task.ps1
403+
$parm = @{
404+
Description = @"
405+
Loops all `$connections children
406+
-Validates that is of the type SQL and is using the Managed Identity authentication scheme
407+
--Creates a new resource in the ARM template, for the ApiConnection object
408+
--Makes sure the ARM Parameters logicAppLocation exists
409+
--The type is based on the Managed Identity authentication
410+
--Name & Displayname is extracted from the ConnectionName property
411+
Requires an authenticated session, either Az.Accounts or az cli
412+
"@
413+
Alias = "Arm.Set-Arm.Connections.ManagedApis.AzureSql.ManagedIdentity.AsArmObject"
414+
}
415+
416+
Task -Name "Set-Arm.Connections.ManagedApis.AzureSql.ManagedIdentity.AsArmObject" @parm -Action {
417+
Set-TaskWorkDirectory
418+
419+
# We can either use the az cli or the Az modules
420+
$tools = Get-PSFConfigValue -FullName PsLogicAppExtractor.Execution.Tools
421+
422+
$found = $false
423+
$conType = "sql"
424+
425+
$armObj = Get-TaskWorkObject
426+
427+
foreach ($connectionObj in $armObj.resources[0].properties.parameters.'$connections'.value.PsObject.Properties) {
428+
if ($connectionObj.Value.id -like "*managedApis/sql*") {
429+
430+
# This should only handle Managed Identity Servicebus connections
431+
if ($connectionObj.Value.connectionProperties.authentication.type -ne "ManagedServiceIdentity") { continue }
432+
433+
$found = $true
434+
435+
# Fetch the details from the connection object
436+
$uri = "{0}?api-version=2018-07-01-preview" -f $($connectionObj.Value.connectionId)
437+
438+
if ($tools -eq "AzCli") {
439+
$resObj = az rest --url $uri | ConvertFrom-Json
440+
}
441+
else {
442+
$resObj = Invoke-AzRestMethod -Path $uri -Method Get | Select-Object -ExpandProperty content | ConvertFrom-Json
443+
}
444+
445+
# Use the display name as the name of the resource
446+
$conName = $resObj.Properties.DisplayName
447+
$resName = $resObj.Properties.DisplayName #fallback default value
448+
449+
# Fetch base template
450+
$pathArms = "$(Get-PSFConfigValue -FullName PsLogicAppExtractor.ModulePath.Base)\internal\arms"
451+
$apiObj = Get-Content -Path "$pathArms\API.Sql.Managed.json" -Raw | ConvertFrom-Json
452+
453+
# Set the names of the parameters
454+
$Prefix = Get-PSFConfigValue -FullName PsLogicAppExtractor.prefixsuffix.connection.prefix
455+
$idPreSuf = Format-Name -Type "Connection" -Value "$($connectionObj.Name)"
456+
457+
<#! Needs work if we want to extract it from the first action that uses the connection
458+
# $nsPreSuf = Format-Name -Type "Connection" -Prefix $Prefix -Suffix "_Server" -Value "$($connectionObj.Name)"
459+
# $armObj = Add-ArmParameter -InputObject $armObj -Name "$nsPreSuf" `
460+
# -Type "string" `
461+
# -Value "$resName" `
462+
# -Description "The name/address of the Azure SQL Server (instance). ($($connectionObj.Name))"
463+
#>
464+
465+
$armObj = Add-ArmParameter -InputObject $armObj -Name "$idPreSuf" `
466+
-Type "string" `
467+
-Value $conName `
468+
-Description "The name / id of the ManagedApi connection object that is being utilized by the Logic App. Will be for the trigger and other actions that depend on connections."
469+
470+
# Update the api object properties
471+
$apiObj.Name = "[parameters('$idPreSuf')]"
472+
$apiObj.properties.displayName = "[parameters('$idPreSuf')]"
473+
474+
# Append the new resource to the ARM template
475+
$armObj.resources += $apiObj
476+
477+
if ($null -eq $armObj.resources[0].dependsOn) {
478+
# Create the dependsOn array if it does not exist
479+
$armObj.resources[0] | Add-Member -MemberType NoteProperty -Name "dependsOn" -Value @()
480+
}
481+
482+
# Add the new resource to the dependsOn array, so that the deployment will work
483+
$armObj.resources[0].dependsOn += "[resourceId('Microsoft.Web/connections', parameters('$idPreSuf'))]"
484+
485+
# Adjust the connection object to depend on the same name
486+
$connectionObj.Value.connectionId = "[resourceId('Microsoft.Web/connections', parameters('$idPreSuf'))]"
487+
$connectionObj.Value.connectionName = "[parameters('$idPreSuf')]"
488+
$connectionObj.Value.id = "[format('/subscriptions/{0}/providers/Microsoft.Web/locations/{1}/managedApis/$conType', subscription().subscriptionId, parameters('logicAppLocation'))]"
489+
}
490+
491+
}
492+
493+
if ($found) {
494+
# We need the location parameter
495+
if ($null -eq $armObj.parameters.logicAppLocation) {
496+
$armObj = Add-ArmParameter -InputObject $armObj -Name "logicAppLocation" `
497+
-Type "string" `
498+
-Value "[resourceGroup().location]" `
499+
-Description "Location of the Logic App. Best practice recommendation is to make this depending on the Resource Group and its location."
500+
}
501+
}
502+
503+
Out-TaskFileArm -InputObject $armObj
504+
}
505+
402506
#Original file: Set-Arm.Connections.ManagedApis.Dataverse.ServicePrincipal.Advanced.AsArmObject.task.ps1
403507
$parm = @{
404508
Description = @"
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
$parm = @{
2+
Description = @"
3+
Loops all `$connections children
4+
-Validates that is of the type SQL and is using the Managed Identity authentication scheme
5+
--Creates a new resource in the ARM template, for the ApiConnection object
6+
--Makes sure the ARM Parameters logicAppLocation exists
7+
--The type is based on the Managed Identity authentication
8+
--Name & Displayname is extracted from the ConnectionName property
9+
Requires an authenticated session, either Az.Accounts or az cli
10+
"@
11+
Alias = "Arm.Set-Arm.Connections.ManagedApis.AzureSql.ManagedIdentity.AsArmObject"
12+
}
13+
14+
Task -Name "Set-Arm.Connections.ManagedApis.AzureSql.ManagedIdentity.AsArmObject" @parm -Action {
15+
Set-TaskWorkDirectory
16+
17+
# We can either use the az cli or the Az modules
18+
$tools = Get-PSFConfigValue -FullName PsLogicAppExtractor.Execution.Tools
19+
20+
$found = $false
21+
$conType = "sql"
22+
23+
$armObj = Get-TaskWorkObject
24+
25+
foreach ($connectionObj in $armObj.resources[0].properties.parameters.'$connections'.value.PsObject.Properties) {
26+
if ($connectionObj.Value.id -like "*managedApis/sql*") {
27+
28+
# This should only handle Managed Identity Servicebus connections
29+
if ($connectionObj.Value.connectionProperties.authentication.type -ne "ManagedServiceIdentity") { continue }
30+
31+
$found = $true
32+
33+
# Fetch the details from the connection object
34+
$uri = "{0}?api-version=2018-07-01-preview" -f $($connectionObj.Value.connectionId)
35+
36+
if ($tools -eq "AzCli") {
37+
$resObj = az rest --url $uri | ConvertFrom-Json
38+
}
39+
else {
40+
$resObj = Invoke-AzRestMethod -Path $uri -Method Get | Select-Object -ExpandProperty content | ConvertFrom-Json
41+
}
42+
43+
# Use the display name as the name of the resource
44+
$conName = $resObj.Properties.DisplayName
45+
$resName = $resObj.Properties.DisplayName #fallback default value
46+
47+
# Fetch base template
48+
$pathArms = "$(Get-PSFConfigValue -FullName PsLogicAppExtractor.ModulePath.Base)\internal\arms"
49+
$apiObj = Get-Content -Path "$pathArms\API.Sql.Managed.json" -Raw | ConvertFrom-Json
50+
51+
# Set the names of the parameters
52+
$Prefix = Get-PSFConfigValue -FullName PsLogicAppExtractor.prefixsuffix.connection.prefix
53+
$idPreSuf = Format-Name -Type "Connection" -Value "$($connectionObj.Name)"
54+
55+
<#! Needs work if we want to extract it from the first action that uses the connection
56+
# $nsPreSuf = Format-Name -Type "Connection" -Prefix $Prefix -Suffix "_Server" -Value "$($connectionObj.Name)"
57+
# $armObj = Add-ArmParameter -InputObject $armObj -Name "$nsPreSuf" `
58+
# -Type "string" `
59+
# -Value "$resName" `
60+
# -Description "The name/address of the Azure SQL Server (instance). ($($connectionObj.Name))"
61+
#>
62+
63+
$armObj = Add-ArmParameter -InputObject $armObj -Name "$idPreSuf" `
64+
-Type "string" `
65+
-Value $conName `
66+
-Description "The name / id of the ManagedApi connection object that is being utilized by the Logic App. Will be for the trigger and other actions that depend on connections."
67+
68+
# Update the api object properties
69+
$apiObj.Name = "[parameters('$idPreSuf')]"
70+
$apiObj.properties.displayName = "[parameters('$idPreSuf')]"
71+
72+
# Append the new resource to the ARM template
73+
$armObj.resources += $apiObj
74+
75+
if ($null -eq $armObj.resources[0].dependsOn) {
76+
# Create the dependsOn array if it does not exist
77+
$armObj.resources[0] | Add-Member -MemberType NoteProperty -Name "dependsOn" -Value @()
78+
}
79+
80+
# Add the new resource to the dependsOn array, so that the deployment will work
81+
$armObj.resources[0].dependsOn += "[resourceId('Microsoft.Web/connections', parameters('$idPreSuf'))]"
82+
83+
# Adjust the connection object to depend on the same name
84+
$connectionObj.Value.connectionId = "[resourceId('Microsoft.Web/connections', parameters('$idPreSuf'))]"
85+
$connectionObj.Value.connectionName = "[parameters('$idPreSuf')]"
86+
$connectionObj.Value.id = "[format('/subscriptions/{0}/providers/Microsoft.Web/locations/{1}/managedApis/$conType', subscription().subscriptionId, parameters('logicAppLocation'))]"
87+
}
88+
89+
}
90+
91+
if ($found) {
92+
# We need the location parameter
93+
if ($null -eq $armObj.parameters.logicAppLocation) {
94+
$armObj = Add-ArmParameter -InputObject $armObj -Name "logicAppLocation" `
95+
-Type "string" `
96+
-Value "[resourceGroup().location]" `
97+
-Description "Location of the Logic App. Best practice recommendation is to make this depending on the Resource Group and its location."
98+
}
99+
}
100+
101+
Out-TaskFileArm -InputObject $armObj
102+
}

0 commit comments

Comments
 (0)