Skip to content

Commit

Permalink
Merge pull request #1148 from JohnDuprey/dev
Browse files Browse the repository at this point in the history
Bugfixes
  • Loading branch information
JohnDuprey authored Oct 11, 2024
2 parents 4b39166 + 97b8b9e commit c58304c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function Get-CippAuditLogSearches {

if ($ReadyToProcess.IsPresent) {
$AuditLogSearchesTable = Get-CippTable -TableName 'AuditLogSearches'
$PendingQueries = Get-CIPPAzDataTableEntity @AuditLogSearchesTable -Filter "Tenant eq '$TenantFilter' and CippStatus eq 'Pending'"
$15MinutesAgo = (Get-Date).AddMinutes(-15).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
$PendingQueries = Get-CIPPAzDataTableEntity @AuditLogSearchesTable -Filter "Tenant eq '$TenantFilter' and (CippStatus eq 'Pending' or (CippStatus eq 'Processing' and Timestamp le datetime'$15MinutesAgo'))" | Sort-Object Timestamp

$BulkRequests = foreach ($PendingQuery in $PendingQueries) {
@{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
function Push-ExecAddMultiTenantApp($QueueItem, $TriggerMetadata) {
function Push-ExecAddMultiTenantApp {
<#
.FUNCTIONALITY
Entrypoint
#>
[CmdletBinding()]
param($Item)
try {
$Queueitem = $QueueItem | ConvertTo-Json -Depth 10 | ConvertFrom-Json
Write-Host "$($Queueitem | ConvertTo-Json -Depth 10)"
$ServicePrincipalList = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/servicePrincipals?`$select=AppId,id,displayName&`$top=999" -tenantid $Queueitem.Tenant
if ($Queueitem.AppId -Notin $ServicePrincipalList.appId) {
$PostResults = New-GraphPostRequest 'https://graph.microsoft.com/beta/servicePrincipals' -type POST -tenantid $queueitem.tenant -body "{ `"appId`": `"$($Queueitem.appId)`" }"
Write-LogMessage -message "Added $($Queueitem.AppId) to tenant $($Queueitem.Tenant)" -tenant $Queueitem.Tenant -API 'Add Multitenant App' -sev Info
$Item = $Item | ConvertTo-Json -Depth 10 | ConvertFrom-Json
Write-Host "$($Item | ConvertTo-Json -Depth 10)"
$ServicePrincipalList = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/servicePrincipals?`$select=AppId,id,displayName&`$top=999" -tenantid $Item.Tenant
if ($Item.AppId -Notin $ServicePrincipalList.appId) {
$PostResults = New-GraphPostRequest 'https://graph.microsoft.com/beta/servicePrincipals' -type POST -tenantid $Item.tenant -body "{ `"appId`": `"$($Item.appId)`" }"
Write-LogMessage -message "Added $($Item.AppId) to tenant $($Item.Tenant)" -tenant $Item.Tenant -API 'Add Multitenant App' -sev Info
} else {
Write-LogMessage -message "This app already exists in tenant $($Queueitem.Tenant). We're adding the required permissions." -tenant $Queueitem.Tenant -API 'Add Multitenant App' -sev Info
Write-LogMessage -message "This app already exists in tenant $($Item.Tenant). We're adding the required permissions." -tenant $Item.Tenant -API 'Add Multitenant App' -sev Info
}
Add-CIPPApplicationPermission -RequiredResourceAccess ($queueitem.applicationResourceAccess) -ApplicationId $queueitem.AppId -Tenantfilter $Queueitem.Tenant
Add-CIPPDelegatedPermission -RequiredResourceAccess ($queueitem.DelegateResourceAccess) -ApplicationId $queueitem.AppId -Tenantfilter $Queueitem.Tenant
Add-CIPPApplicationPermission -RequiredResourceAccess ($Item.applicationResourceAccess) -ApplicationId $Item.AppId -Tenantfilter $Item.Tenant
Add-CIPPDelegatedPermission -RequiredResourceAccess ($Item.DelegateResourceAccess) -ApplicationId $Item.AppId -Tenantfilter $Item.Tenant
} catch {
Write-LogMessage -message "Error adding application to tenant $($Queueitem.Tenant) - $($_.Exception.Message)" -tenant $Queueitem.Tenant -API 'Add Multitenant App' -sev Error
Write-LogMessage -message "Error adding application to tenant $($Item.Tenant) - $($_.Exception.Message)" -tenant $Item.Tenant -API 'Add Multitenant App' -sev Error
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
function Push-ExecApplicationCopy($QueueItem, $TriggerMetadata) {
function Push-ExecApplicationCopy {
<#
.FUNCTIONALITY
Entrypoint
#>
[CmdletBinding()]
param($Item)
try {
$Queueitem = $QueueItem | ConvertTo-Json -Depth 10 | ConvertFrom-Json
Write-Host "$($Queueitem | ConvertTo-Json -Depth 10)"
New-CIPPApplicationCopy -App $queueitem.AppId -Tenant $Queueitem.Tenant
Write-Host "$($Item | ConvertTo-Json -Depth 10)"
New-CIPPApplicationCopy -App $Item.AppId -Tenant $Item.Tenant
} catch {
Write-LogMessage -message "Error adding application to tenant $($Queueitem.Tenant) - $($_.Exception.Message)" -tenant $Queueitem.Tenant -API 'Add Multitenant App' -sev Error
Write-LogMessage -message "Error adding application to tenant $($Item.Tenant) - $($_.Exception.Message)" -tenant $Item.Tenant -API 'Add Multitenant App' -sev Error
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,34 @@ function Invoke-ExecAddMultiTenantApp {
$ApplicationResourceAccess = @{ ResourceAppId = '00000003-0000-0000-c000-000000000000'; resourceAccess = $ApplicationResources }

$Results = try {
if ($request.body.CopyPermissions -eq $true) {
if ($Request.Body.CopyPermissions -eq $true) {
$Command = 'ExecApplicationCopy'
} else {
$Command = 'ExecAddMultiTenantApp'
}
if ('allTenants' -in $Request.body.SelectedTenants.defaultDomainName) {
if ('allTenants' -in $Request.Body.SelectedTenants.defaultDomainName) {
$TenantFilter = (Get-Tenants).defaultDomainName
} else {
$TenantFilter = $Request.body.SelectedTenants.defaultDomainName
$TenantFilter = $Request.Body.SelectedTenants.defaultDomainName
}

$TenantCount = ($TenantFilter | Measure-Object).Count
$Queue = New-CippQueueEntry -Name 'Application Approval' -TotalTasks $TenantCount
foreach ($Tenant in $TenantFilter) {
try {
Push-OutputBinding -Name QueueItem -Value ([pscustomobject]@{
FunctionName = $Command
Tenant = $tenant
appId = $Request.body.appid
applicationResourceAccess = $ApplicationResourceAccess
delegateResourceAccess = $DelegateResourceAccess
})
$InputObject = @{
OrchestratorName = 'ExecMultiTenantAppOrchestrator'
Batch = @([pscustomobject]@{
FunctionName = $Command
Tenant = $tenant
AppId = $Request.Body.AppId
applicationResourceAccess = $ApplicationResourceAccess
delegateResourceAccess = $DelegateResourceAccess
QueueId = $Queue.RowKey
})
SkipLog = $true
}
$null = Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress)
"Queued application to tenant $Tenant. See the logbook for deployment details"
} catch {
"Error queuing application to tenant $Tenant - $($_.Exception.Message)"
Expand Down

0 comments on commit c58304c

Please sign in to comment.