This commit is contained in:
2025-05-27 00:13:37 -04:00
parent d70d17372d
commit 6e8eaddd24

View File

@@ -205,13 +205,13 @@ function Install-DattoRMM-Helper {
#endregion #endregion
# POST /getpw → read JSON body, call helper, return JSON # POST /getpw → read JSON body, call helper, return JSON
function returnRespondEmpty { function returnEmpty {
param($Context) param($Context)
$empty = [Text.Encoding]::UTF8.GetBytes("[]") $bytes = [Text.Encoding]::UTF8.GetBytes("[]")
$Context.Response.StatusCode = 500 $Context.Response.StatusCode = 500
$Context.Response.ContentType = 'application/json' $Context.Response.ContentType = 'application/json'
$Context.Response.ContentLength64 = $empty.Length $Context.Response.ContentLength64 = $bytes.Length
$Context.Response.OutputStream.Write($empty, 0, $empty.Length) $Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
$Context.Response.OutputStream.Close() $Context.Response.OutputStream.Close()
} }
@@ -219,47 +219,62 @@ function Handle-FetchSites {
param($Context) param($Context)
# 1) Read incoming JSON # 1) Read incoming JSON
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd() $raw = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
try { try {
$pw = (ConvertFrom-Json $body).password $pw = (ConvertFrom-Json $raw).password
} catch { } catch {
Write-Host "[Error][FetchSites] Invalid JSON body: $_" Write-Host "[Error][FetchSites] Invalid JSON: $_"
returnRespondEmpty $Context returnEmpty $Context; return
return
} }
# 2) Hit the n8n webhook for fresh creds # 2) Fetch your Datto API creds from the webhook
Write-Host "[Debug][FetchSites] Fetching API creds from webhook..." Write-Host "[Debug][FetchSites] calling webhook..."
$creds = Get-DattoApiCredentials -Password $pw
if (-not $creds) {
Write-Host "[Error][FetchSites] Failed to retrieve credentials."
returnRespondEmpty $Context
return
}
Write-Host "[Debug][FetchSites] Got ApiUrl=$($creds.ApiUrl), ApiKey startswith='$($creds.ApiKey.Substring(0,4))', Secret startswith='$($creds.ApiSecretKey.Substring(0,4))'"
# 3) Call your helper to get the site list
Write-Host "[Debug][FetchSites] Invoking Install-DattoRMM-Helper -FetchSitesOnly..."
try { try {
$sites = Install-DattoRMM-Helper ` $hdr = @{ "SVSMSPKit" = $pw }
-ApiUrl $creds.ApiUrl ` $resp = Invoke-RestMethod -Uri "https://automate.svstools.ca/webhook/svsmspkit" `
-ApiKey $creds.ApiKey ` -Headers $hdr -Method Get
-ApiSecretKey $creds.ApiSecretKey ` $apiUrl = $resp.ApiUrl
-FetchSitesOnly $apiKey = $resp.ApiKey
$apiSecretKey = $resp.ApiSecretKey
} catch { } catch {
Write-Host "[Error][FetchSites] Helper threw: $($_.Exception.Message)" Write-Host "[Error][FetchSites] webhook failed: $_"
returnRespondEmpty $Context returnEmpty $Context; return
return
} }
if (-not $sites) { # 3) Exchange for a bearer token
Write-Host "[Error][FetchSites] Helper returned no sites." Write-Host "[Debug][FetchSites] getting OAuth token..."
returnRespondEmpty $Context try {
return $securePublic = ConvertTo-SecureString -String 'public' -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential('public-client',$securePublic)
$tokenResp = Invoke-RestMethod -Uri "$apiUrl/auth/oauth/token" `
-Credential $creds `
-Method Post `
-ContentType 'application/x-www-form-urlencoded' `
-Body "grant_type=password&username=$apiKey&password=$apiSecretKey"
$token = $tokenResp.access_token
} catch {
Write-Host "[Error][FetchSites] token request failed: $_"
returnEmpty $Context; return
} }
# 4) Serialize and reply # 4) Pull the site list
$json = $sites | ConvertTo-Json -Depth 2 Write-Host "[Debug][FetchSites] fetching sites list..."
try {
$hdr = @{ Authorization = "Bearer $token" }
$sitesResp = Invoke-RestMethod -Uri "$apiUrl/api/v2/account/sites" `
-Method Get `
-Headers $hdr `
-ContentType 'application/json'
$siteList = $sitesResp.sites | ForEach-Object {
[PSCustomObject]@{ Name = $_.name; UID = $_.uid }
}
} catch {
Write-Host "[Error][FetchSites] site list failed: $_"
returnEmpty $Context; return
}
# 5) Return JSON array
$json = $siteList | ConvertTo-Json -Depth 2
$bytes = [Text.Encoding]::UTF8.GetBytes($json) $bytes = [Text.Encoding]::UTF8.GetBytes($json)
$Context.Response.ContentType = 'application/json' $Context.Response.ContentType = 'application/json'
$Context.Response.ContentLength64 = $bytes.Length $Context.Response.ContentLength64 = $bytes.Length
@@ -267,6 +282,7 @@ function Handle-FetchSites {
$Context.Response.OutputStream.Close() $Context.Response.OutputStream.Close()
} }
# Helper function to consistently return an empty JSON array # Helper function to consistently return an empty JSON array
function returnRespondEmpty { function returnRespondEmpty {
param($Context) param($Context)