Skip to content

Commit 932c597

Browse files
authored
Merge pull request #154 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 8e7dac8 + e81af7f commit 932c597

File tree

4 files changed

+77
-41
lines changed

4 files changed

+77
-41
lines changed

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Applications/Push-UploadApplication.ps1

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ function Push-UploadApplication {
1616

1717
$ChocoApp = (Get-CIPPAzDataTableEntity @Table -filter $Filter).JSON | ConvertFrom-Json
1818
$intuneBody = $ChocoApp.IntuneBody
19-
$tenants = if ($chocoapp.Tenant -eq 'AllTenants') {
20-
(Get-tenants).defaultDomainName
19+
$tenants = if ($ChocoApp.tenant -eq 'AllTenants') {
20+
(Get-Tenants -IncludeErrors).defaultDomainName
2121
} else {
22-
$chocoapp.Tenant
22+
$ChocoApp.tenant
2323
}
24-
if ($chocoApp.type -eq 'MSPApp') {
24+
if ($ChocoApp.type -eq 'MSPApp') {
2525
[xml]$Intunexml = Get-Content "AddMSPApp\$($ChocoApp.MSPAppName).app.xml"
2626
$intunewinFilesize = (Get-Item "AddMSPApp\$($ChocoApp.MSPAppName).intunewin")
2727
$Infile = "AddMSPApp\$($ChocoApp.MSPAppName).intunewin"
@@ -30,7 +30,7 @@ function Push-UploadApplication {
3030
$intunewinFilesize = (Get-Item 'AddChocoApp\IntunePackage.intunewin')
3131
$Infile = "AddChocoApp\$($intunexml.ApplicationInfo.FileName)"
3232
}
33-
$assignTo = $ChocoApp.AssignTo
33+
$assignTo = $ChocoApp.assignTo
3434
$AssignToIntent = $ChocoApp.InstallationIntent
3535
$Baseuri = 'https://graph.microsoft.com/beta/deviceAppManagement/mobileApps'
3636
$ContentBody = ConvertTo-Json @{
@@ -39,7 +39,7 @@ function Push-UploadApplication {
3939
sizeEncrypted = [int64]($intunewinFilesize).length
4040
}
4141
$ClearRow = Get-CIPPAzDataTableEntity @Table -Filter $Filter
42-
$RemoveCacheFile = if ($chocoapp.Tenant -ne 'AllTenants') {
42+
$RemoveCacheFile = if ($ChocoApp.tenant -ne 'AllTenants') {
4343
Remove-AzDataTableEntity -Force @Table -Entity $clearRow
4444
} else {
4545
$Table.Force = $true
@@ -63,24 +63,24 @@ function Push-UploadApplication {
6363
} | ConvertTo-Json
6464

6565
foreach ($tenant in $tenants) {
66-
Try {
67-
$ApplicationList = (New-graphGetRequest -Uri $baseuri -tenantid $Tenant) | Where-Object { $_.DisplayName -eq $ChocoApp.ApplicationName }
66+
try {
67+
$ApplicationList = (New-GraphGetRequest -Uri $baseuri -tenantid $tenant) | Where-Object { $_.DisplayName -eq $ChocoApp.Applicationname }
6868
if ($ApplicationList.displayname.count -ge 1) {
69-
Write-LogMessage -api 'AppUpload' -tenant $($Tenant) -message "$($ChocoApp.ApplicationName) exists. Skipping this application" -Sev 'Info'
69+
Write-LogMessage -api 'AppUpload' -tenant $tenant -message "$($ChocoApp.Applicationname) exists. Skipping this application" -Sev 'Info'
7070
continue
7171
}
72-
if ($chocoApp.type -eq 'WinGet') {
72+
if ($ChocoApp.type -eq 'WinGet') {
7373
Write-Host 'Winget!'
7474
Write-Host ($intuneBody | ConvertTo-Json -Compress)
7575
$NewApp = New-GraphPostRequest -Uri $baseuri -Body ($intuneBody | ConvertTo-Json -Compress) -Type POST -tenantid $tenant
7676
Start-Sleep -Milliseconds 200
77-
Write-LogMessage -api 'AppUpload' -tenant $($Tenant) -message "$($ChocoApp.ApplicationName) uploaded as WinGet app." -Sev 'Info'
77+
Write-LogMessage -api 'AppUpload' -tenant $tenant -message "$($ChocoApp.Applicationname) uploaded as WinGet app." -Sev 'Info'
7878
if ($AssignTo -ne 'On') {
7979
$intent = if ($AssignToIntent) { 'Uninstall' } else { 'Required' }
8080
Set-CIPPAssignedApplication -ApplicationId $NewApp.Id -Intent $intent -TenantFilter $tenant -groupName "$AssignTo" -AppType 'WinGet'
8181
}
82-
Write-LogMessage -api 'AppUpload' -tenant $($Tenant) -message "$($ChocoApp.ApplicationName) Successfully created" -Sev 'Info'
83-
exit 0
82+
Write-LogMessage -api 'AppUpload' -tenant $tenant -message "$($ChocoApp.Applicationname) Successfully created" -Sev 'Info'
83+
continue
8484
} else {
8585
$NewApp = New-GraphPostRequest -Uri $baseuri -Body ($intuneBody | ConvertTo-Json) -Type POST -tenantid $tenant
8686

@@ -109,23 +109,23 @@ function Push-UploadApplication {
109109
$CommitStateReq = New-graphGetRequest -Uri "$($BaseURI)/$($NewApp.id)/microsoft.graph.win32lobapp/contentVersions/1/files/$($ContentReq.id)" -tenantid $tenant
110110
Write-Host "Commit State Request: $($CommitStateReq | ConvertTo-Json -Depth 10)"
111111
if ($CommitStateReq.uploadState -like '*fail*') {
112-
Write-LogMessage -api 'AppUpload' -tenant $($Tenant) -message "$($ChocoApp.ApplicationName) Commit failed. Please check if app uploaded succesful" -Sev 'Warning'
112+
Write-LogMessage -api 'AppUpload' -tenant $tenant -message "$($ChocoApp.Applicationname) Commit failed. Please check if app uploaded succesful" -Sev 'Warning'
113113
break
114114
}
115115
Start-Sleep -Milliseconds 300
116116
} while ($CommitStateReq.uploadState -eq 'commitFilePending')
117117
$CommitFinalizeReq = New-graphPostRequest -Uri "$($BaseURI)/$($NewApp.id)" -tenantid $tenant -Body '{"@odata.type":"#microsoft.graph.win32lobapp","committedContentVersion":"1"}' -type PATCH
118118
Write-Host "Commit Finalize Request: $($CommitFinalizeReq | ConvertTo-Json -Depth 10)"
119-
Write-LogMessage -api 'AppUpload' -tenant $($Tenant) -message "Added Application $($chocoApp.ApplicationName)" -Sev 'Info'
119+
Write-LogMessage -api 'AppUpload' -tenant $tenant -message "Added Application $($ChocoApp.Applicationname)" -Sev 'Info'
120120
if ($AssignTo -ne 'On') {
121121
$intent = if ($AssignToIntent) { 'Uninstall' } else { 'Required' }
122122
Set-CIPPAssignedApplication -ApplicationId $NewApp.Id -Intent $intent -TenantFilter $tenant -groupName "$AssignTo" -AppType 'Win32Lob'
123123

124124
}
125-
Write-LogMessage -api 'AppUpload' -tenant $($Tenant) -message 'Successfully added Application' -Sev 'Info'
125+
Write-LogMessage -api 'AppUpload' -tenant $tenant -message 'Successfully added Application' -Sev 'Info'
126126
} catch {
127127
"Failed to add Application for $($Tenant): $($_.Exception.Message)"
128-
Write-LogMessage -api 'AppUpload' -tenant $($Tenant) -message "Failed adding Application $($ChocoApp.ApplicationName). Error: $($_.Exception.Message)" -LogData (Get-CippException -Exception $_) -Sev 'Error'
128+
Write-LogMessage -api 'AppUpload' -tenant $tenant -message "Failed adding Application $($ChocoApp.Applicationname). Error: $($_.Exception.Message)" -LogData (Get-CippException -Exception $_) -Sev 'Error'
129129
continue
130130
}
131131
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Administration/Tenant/Invoke-AddTenant.ps1

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,29 @@ function Invoke-AddTenant {
1010

1111
$APIName = $Request.Params.CIPPEndpoint
1212
Write-LogMessage -headers $Request.Headers -API $APINAME -message 'Accessed this API' -Sev 'Debug'
13+
$Action = $Request.Body.Action ?? $Request.Query.Action
14+
$TenantName = $Request.Body.TenantName ?? $Request.Query.TenantName
15+
$StatusCode = [HttpStatusCode]::OK
1316

14-
switch ($Request.Body.Action) {
17+
switch ($Action) {
1518
'ValidateDomain' {
1619
# Validate the onmicrosoft.com domain
17-
$Domain = "$($Request.Body.TenantName).onmicrosoft.com"
20+
$Domain = "$($TenantName).onmicrosoft.com"
1821
$DomainCheckUri = "https://api.partnercenter.microsoft.com/v1/domains/$Domain"
22+
Write-Information "Checking $Domain"
1923
try {
20-
$DomainCheckResponse = New-GraphPOSTRequest -type HEAD -uri $DomainCheckUri -scope 'https://api.partnercenter.microsoft.com/.default' -NoAuthCheck $true
21-
} catch {
22-
return @{
23-
Status = 'Error'
24+
$null = New-GraphPOSTRequest -type HEAD -uri $DomainCheckUri -scope 'https://api.partnercenter.microsoft.com/.default' -NoAuthCheck $true
25+
26+
$Body = @{
27+
Success = $false
2428
Message = "The domain '$Domain' is already in use."
2529
}
30+
} catch {
31+
$Body = @{
32+
Success = $true
33+
}
2634
}
27-
return @{
28-
Status = 'Success'
29-
Message = "The domain '$Domain' is available."
30-
}
35+
3136
}
3237
'AddTenant' {
3338
# Fetch the organization id for Tier 2 CSPs
@@ -41,6 +46,7 @@ function Invoke-AddTenant {
4146
state = 'Error'
4247
resultText = "Failed to retrieve organization profile: $($_.Exception.Message)"
4348
}
49+
$StatusCode = [HttpStatusCode]::BadRequest
4450
break
4551
}
4652
}
@@ -51,7 +57,7 @@ function Invoke-AddTenant {
5157
CommerceId = $null
5258
CompanyProfile = @{
5359
TenantId = $null
54-
Domain = '{0}.onmicrosoft.com' -f $Request.Body.TenantName
60+
Domain = '{0}.onmicrosoft.com' -f $TenantName
5561
CompanyName = $Request.Body.CompanyName
5662
Attributes = @{ ObjectType = 'CustomerCompanyProfile' }
5763
}
@@ -94,14 +100,15 @@ function Invoke-AddTenant {
94100

95101
$Body = @{
96102
state = 'Success'
97-
resultText = "Tenant created successfully. 'Username is $($Response.userCredentials.userName)@{0}.onmicrosoft.com'. Click copy to retrieve the password." -f $Request.Body.TenantName
103+
resultText = "Tenant created successfully. 'Username is $($Response.userCredentials.userName)@{0}.onmicrosoft.com'. Click copy to retrieve the password." -f $TenantName
98104
copyField = $Response.userCredentials.password
99105
}
100106
} catch {
101107
$Body = @{
102108
state = 'Error'
103109
resultText = "Failed to create tenant: $($_.Exception.Message)"
104110
}
111+
$StatusCode = [HttpStatusCode]::BadRequest
105112
}
106113
}
107114
'ValidateAddress' {
@@ -126,21 +133,21 @@ function Invoke-AddTenant {
126133
}
127134
} catch {
128135
return @{
129-
state = 'Error'
136+
state = 'Error'
130137
resultText = "Address validation failed: $($_.Exception.Message)"
131138
}
132139
}
133140
}
134141
default {
135142
return @{
136-
state = 'Error'
143+
state = 'Error'
137144
resultText = "Invalid action specified: $($Request.Body.Action)"
138145
}
139146
}
140147
}
141148

142149
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
143-
StatusCode = [HttpStatusCode]::InternalServerError
144-
Body = $Body
145-
})
150+
StatusCode = $StatusCode
151+
Body = $Body
152+
})
146153
}

Modules/CIPPCore/Public/Test-CIPPAccessPermissions.ps1

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function Test-CIPPAccessPermissions {
138138
$CPVRefreshList = [System.Collections.Generic.List[object]]::new()
139139
$CPVSuccess = $true
140140
foreach ($Tenant in $TenantList) {
141-
$LastRefresh = ($CpvRefresh | Where-Object { $_.RowKey -EQ $Tenant.customerId }).Timestamp.DateTime
141+
$LastRefresh = ($CpvRefresh | Where-Object { $_.RowKey -eq $Tenant.customerId }).Timestamp.DateTime
142142
if ($LastRefresh -lt $LastUpdate) {
143143
$CPVSuccess = $false
144144
$CPVRefreshList.Add([PSCustomObject]@{
@@ -157,6 +157,19 @@ function Test-CIPPAccessPermissions {
157157
$ErrorMessage = Get-CippException -Exception $_
158158
Write-LogMessage -Headers $User -API $APINAME -message "Permissions check failed: $($ErrorMessage.NormalizedError) " -Sev 'Error' -LogData $ErrorMessage
159159
$ErrorMessages.Add("We could not connect to the API to retrieve the permissions. There might be a problem with the secure application model configuration. The returned error is: $($ErrorMessage.NormalizedError)") | Out-Null
160+
161+
try {
162+
$MFAServicePolicy = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/policies/mfaServicePolicy' -tenantid $env:TenantID -AsApp $true -NoAuthCheck $true
163+
if ($MFAServicePolicy.rememberMfaOnTrustedDevice.isEnabled -eq $true -and $MFAServicePolicy.rememberMfaOnTrustedDevice.allowedNumberOfDays -gt 0) {
164+
$ErrorMessages.Add("MFA Service Policy has a session lifetime of $($MFAServicePolicy.rememberMfaOnTrustedDevice.allowedNumberOfDays) days. This may cause athentication issues for your service account.") | Out-Null
165+
$Links.Add([PSCustomObject]@{
166+
Text = 'Troubleshooting'
167+
Href = 'https://docs.cipp.app/troubleshooting/troubleshooting#multi-factor-authentication-troubleshooting'
168+
}
169+
) | Out-Null
170+
}
171+
} catch {}
172+
160173
$Success = $false
161174
}
162175

Modules/CippExtensions/Public/HIBP/Get-HIBPAuth.ps1

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
function Get-HIBPAuth {
2-
if ($env:AzureWebJobsStorage -eq 'UseDevelopmentStorage=true') {
3-
$DevSecretsTable = Get-CIPPTable -tablename 'DevSecrets'
4-
$Secret = (Get-CIPPAzDataTableEntity @DevSecretsTable -Filter "PartitionKey eq 'HIBP' and RowKey eq 'HIBP'").APIKey
2+
$Var = 'Ext_HIBP'
3+
$APIKey = Get-Item -Path "ENV:$Var" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Value
4+
if ($APIKey) {
5+
Write-Information 'Using cached API Key for HIBP'
56
} else {
6-
$null = Connect-AzAccount -Identity
7-
$VaultName = $ENV:WEBSITE_OWNER_NAME -like '3e625d35-bf18-4e55*' ? 'hibp-kv' : ($ENV:WEBSITE_DEPLOYMENT_ID -split '-')[0]
8-
$Secret = Get-AzKeyVaultSecret -VaultName $VaultName -Name 'HIBP' -AsPlainText
7+
if ($env:AzureWebJobsStorage -eq 'UseDevelopmentStorage=true') {
8+
$DevSecretsTable = Get-CIPPTable -tablename 'DevSecrets'
9+
$Secret = (Get-CIPPAzDataTableEntity @DevSecretsTable -Filter "PartitionKey eq 'HIBP' and RowKey eq 'HIBP'").APIKey
10+
} else {
11+
$null = Connect-AzAccount -Identity
12+
$VaultName = ($ENV:WEBSITE_DEPLOYMENT_ID -split '-')[0]
13+
try {
14+
$Secret = Get-AzKeyVaultSecret -VaultName $VaultName -Name 'HIBP' -AsPlainText -ErrorAction Stop
15+
} catch {
16+
$Secret = $null
17+
}
18+
19+
if ([string]::IsNullOrEmpty($Secret) -and $ENV:WEBSITE_OWNER_NAME -like '3e625d35-bf18-4e55*' -or $ENV:WEBSITE_OWNER_NAME -like '61e84181-ff2a-4ba3*') {
20+
$VaultName = 'hibp-kv'
21+
$Secret = Get-AzKeyVaultSecret -VaultName $VaultName -Name 'HIBP' -AsPlainText
22+
}
23+
}
24+
Set-Item -Path "ENV:$Var" -Value $APIKey -Force -ErrorAction SilentlyContinue
925
}
1026

1127
return @{

0 commit comments

Comments
 (0)