Skip to content

Commit 5ec4ad6

Browse files
introducing hibp
1 parent 41eb644 commit 5ec4ad6

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Extensions/Invoke-ExecExtensionTest.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ Function Invoke-ExecExtensionTest {
5959
$token = Get-SherwebAuthentication
6060
$Results = [pscustomobject]@{'Results' = 'Successfully Connected to Sherweb' }
6161
}
62+
'HIBP' {
63+
$ConnectionTest = Get-HIBPConnectionTest
64+
$Results = [pscustomobject]@{'Results' = 'Successfully Connected to HIBP' }
65+
}
6266
}
6367
} catch {
64-
$Results = [pscustomobject]@{'Results' = "Failed to connect: $($_.Exception.Message) $($_.InvocationInfo.ScriptLineNumber)" }
68+
$Results = [pscustomobject]@{'Results' = "Failed to connect: $($_.Exception.Message). Line $($_.InvocationInfo.ScriptLineNumber)" }
6569
}
6670

6771
# Associate values to output bindings by calling 'Push-OutputBinding'.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using namespace System.Net
2+
3+
Function Invoke-ListBreachesAccount {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
CIPP.Core.Read
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $TriggerMetadata.FunctionName
14+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
15+
16+
$Results = Get-HIBPRequest "breachedaccount/$($Request.query.account)?truncateResponse=false"
17+
# Associate values to output bindings by calling 'Push-OutputBinding'.
18+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
19+
StatusCode = [HttpStatusCode]::OK
20+
Body = @($results)
21+
})
22+
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using namespace System.Net
2+
3+
Function Invoke-ListBreachesTenant {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
CIPP.Core.Read
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $TriggerMetadata.FunctionName
14+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
15+
$users = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users?`$select=UserPrincipalName,mail" -tenantid $Request.query.TenantFilter
16+
$usersResults = foreach ($user in $users) {
17+
$Results = Get-HIBPRequest "breachedaccount/$($user.UserPrincipalName)?truncateResponse=true"
18+
if ($null -eq $Results) {
19+
$Results = 'No breaches found.'
20+
}
21+
[PSCustomObject]@{
22+
user = $user.UserPrincipalName
23+
breaches = $Results
24+
}
25+
}
26+
27+
28+
# Associate values to output bindings by calling 'Push-OutputBinding'.
29+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
30+
StatusCode = [HttpStatusCode]::OK
31+
Body = @($usersResults)
32+
})
33+
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
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
5+
} else {
6+
$null = Connect-AzAccount -Identity
7+
$VaultName = ($ENV:WEBSITE_DEPLOYMENT_ID -split '-')[0]
8+
$Secret = Get-AzKeyVaultSecret -VaultName $VaultName -Name 'HIBP' -AsPlainText
9+
}
10+
11+
return @{
12+
'User-Agent' = "CIPP-$($ENV:TenantId)"
13+
'Accept' = 'application/json'
14+
'api-version' = '3'
15+
'hibp-api-key' = $Secret
16+
}
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function Get-HIBPConnectionTest {
2+
$uri = 'https://haveibeenpwned.com/api/v3/subscription/status'
3+
try {
4+
Invoke-RestMethod -Uri $uri -Headers (Get-HIBPAuth)
5+
} catch {
6+
throw "Failed to connect to HIBP: $($_.Exception.Message)"
7+
}
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function Get-HIBPRequest {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter()]$endpoint
5+
6+
)
7+
$uri = "https://haveibeenpwned.com/api/v3/$endpoint"
8+
try {
9+
Invoke-RestMethod -Uri $uri -Headers (Get-HIBPAuth)
10+
} catch {
11+
#If the error is a 404, it means no breach has been found. Return an empty object.
12+
if ($_.Exception.Response.StatusCode -eq 404) {
13+
return @()
14+
}
15+
throw "Failed to connect to HIBP: $($_.Exception.Message)"
16+
}
17+
}

0 commit comments

Comments
 (0)