-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetup-POC.ps1
194 lines (154 loc) · 6.31 KB
/
Setup-POC.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[Guid]
$TenantId,
[Parameter(Mandatory = $true)]
[string]
$AppName,
[Parameter(Mandatory = $true)]
[securestring]
$CertificatePassword
)
$ErrorActionPreference = "Stop"
function Create-Certificate {
param (
[Parameter(Mandatory = $true)]
[string]
$Subject,
[Parameter(Mandatory = $true)]
[securestring]
$CertificatePassword
)
$ErrorActionPreference = "Stop"
$certName = "$($Subject.ToLower() -replace "[^a-zA-Z0-9]", "_")-$(New-Guid)"
$certSubject = "CN=$Subject"
$certStoreLocation = "Cert:\CurrentUser\My"
$certFile = "$PSScriptRoot\$certName.pfx"
New-SelfSignedCertificate -Subject $certSubject -CertStoreLocation $certStoreLocation -KeyExportPolicy Exportable -KeySpec Signature -KeyAlgorithm RSA -KeyLength 2048 | Export-PfxCertificate -FilePath $certFile -Password $CertificatePassword | Out-Null
Get-ChildItem $certStoreLocation | Where-Object { $_.Subject -match $certSubject } | Remove-Item
$newCert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certFile, $CertificatePassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)
return @{
Object = $newCert
Path = $certFile
PrivateKey = $newCert.PrivateKey.ExportPkcs8PrivateKeyPem()
Thumbprint = $newCert.Thumbprint
Base64Value = [System.Convert]::ToBase64String($newCert.GetRawCertData())
Base64Thumbprint = [System.Convert]::ToBase64String($newCert.GetCertHash())
Base64UrlThumbprint = [System.Convert]::ToBase64String($newCert.GetCertHash()) -replace '\+', '-' -replace '/', '_' -replace '=', ''
StartDate = $newCert.NotBefore
EndDate = $newCert.NotAfter
}
}
function Get-AzureDevOpsPermissions {
$devops = Get-MgServicePrincipal -Filter "DisplayName eq 'Azure DevOps'"
$permission = $devops.Oauth2PermissionScopes | Where-Object { $_.Value -eq 'user_impersonation' } | Select-Object -First 1
return @{
ResourceAppId = $devops.AppId
ResourceAccessId = $permission.Id
}
}
function Register-AzureApplication {
param (
[string]
$AppName,
[Guid]
$ResourceAppId,
[Guid]
$ResourceAccessId
)
$app = Get-MgApplication -Filter "DisplayName eq '$AppName'"
if($null -eq $app) {
$app = New-MgApplication -DisplayName $AppName
New-MgServicePrincipal -AppId $app.AppId -DisplayName $AppName | Out-Null
}else{
Write-Warning "Application '$AppName' already exists. Updating..."
}
$params = @{
requiredResourceAccess = @(
@{
resourceAppId = $ResourceAppId
resourceAccess = @(
@{
id = $ResourceAccessId
type = "Scope"
}
)
}
)
}
Update-MgApplication -ApplicationId $app.Id -BodyParameter $params
return $app
}
function Update-AzureApplication {
param (
[Parameter(Mandatory = $true)]
[Guid]
$AppId,
[Parameter(Mandatory = $true)]
$Cert
)
$params = @{
keyCredentials = @(
@{
customKeyIdentifier = [System.Text.Encoding]::ASCII.GetBytes($Cert.Base64Thumbprint)
endDateTime = $Cert.EndDate
keyId = [System.Guid]::NewGuid()
startDateTime = $Cert.StartDate
type = "AsymmetricX509Cert"
usage = "Verify"
key = [System.Text.Encoding]::ASCII.GetBytes($Cert.Base64Value)
}
)
}
Update-MgApplication -ApplicationId $AppId -BodyParameter $params
}
function Update-PostmanCollection {
param (
[Parameter(Mandatory = $true)]
[Guid]
$TenantId,
[Parameter(Mandatory = $true)]
[string]
$AppName,
[Parameter(Mandatory = $true)]
[Guid]
$AppId,
[Parameter(Mandatory = $true)]
[string]
$Scope,
[Parameter(Mandatory = $true)]
[string]
$CertificatePrivateKey,
[Parameter(Mandatory = $true)]
[string]
$CertificateThumbprint
)
$collection = Get-Content "$PSScriptRoot\template.postman_collection.json" -Encoding utf8 | ConvertFrom-Json -Depth 10
$collection.info.name = "$($collection.info.name) - $AppName"
$collection.variable | ForEach-Object {
$variable = $_
switch ($variable.key) {
"az_devops_scope" { $variable.value = $Scope; Break }
"cert_privatey_key" { $variable.value = $CertificatePrivateKey; Break }
"cert_thumbprint" { $variable.value = $CertificateThumbprint; Break }
"tenant_id" { $variable.value = $TenantId; Break }
"client_id" { $variable.value = $AppId; Break }
"account_name" { $variable.value = $AppName; Break }
}
}
$collectionName = $AppName.ToLower() -replace "[^a-zA-Z0-9]", "_"
$collection | ConvertTo-Json -Depth 10 | Out-File "$PSScriptRoot\$collectionName.postman_collection.json" -Encoding utf8 -Force
}
Connect-MgGraph -TenantId $TenantId -Scopes "Application.ReadWrite.All", "DelegatedPermissionGrant.ReadWrite.All" -NoWelcome
Write-Host "Creating certificate..."
$cert = Create-Certificate -Subject $AppName -CertificatePassword $CertificatePassword
Write-Host "Getting Azure DevOps permissions..."
$devopsPermissions = Get-AzureDevOpsPermissions
Write-Host "Creating application '$AppName' in Azure AD..."
$app = Register-AzureApplication -AppName $AppName -ResourceAppId $devopsPermissions.ResourceAppId -ResourceAccessId $devopsPermissions.ResourceAccessId
Write-Host "Adding certificate to application..."
Update-AzureApplication -AppId $app.Id -Cert $cert
Write-Host "Updating Postman collection..."
Update-PostmanCollection -TenantId $TenantId -AppName $AppName -AppId $app.AppId -Scope "$($devopsPermissions.ResourceAppId)/.default" -CertificatePrivateKey $cert.PrivateKey -CertificateThumbprint $cert.Base64UrlThumbprint
Write-Host "Done." -ForegroundColor Green