From 78c66457afcec04f7ad9e91a5e0758bc217aca69 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Wed, 25 Jun 2025 00:34:44 -0400 Subject: [PATCH] =?UTF-8?q?Handle-FetchSites=20function=20you=20should=20r?= =?UTF-8?q?eplace=20the=20hard-coded=20Invoke-RestMethod=20block=20(lines?= =?UTF-8?q?=20~458=E2=80=93472?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- StackMonkey.ps1 | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/StackMonkey.ps1 b/StackMonkey.ps1 index e90979b..b88e104 100644 --- a/StackMonkey.ps1 +++ b/StackMonkey.ps1 @@ -441,36 +441,28 @@ function Handle-FetchSites { # 2) Fetch your Datto API creds from the webhook Write-LogHybrid "Calling webhook for Datto credentials…" "Info" "FetchSites" - <# - $resp = Get-DattoApiCredentials -Password $N8nPassword - if (-not $resp) { - Write-LogHelper "Failed to retrieve Datto API credentials from n8n" Error DattoAuth - throw - } - # now exactly the same values are populated: - $Global:ApiUrl = $resp.ApiUrl - $Global:ApiKey = $resp.ApiKey - $Global:ApiSecretKey = $resp.ApiSecretKey - Write-LogHelper "Fetched Datto API credentials from n8n" Success DattoAuth -#> #could replace the following 15 lines - - try { - $hdr = @{ "SVSMSPKit" = $pw } - $resp = Invoke-RestMethod -Uri $Global:DattoWebhookUrl -Headers $hdr -Method GET - - # store for later RMM calls - $Global:ApiUrl = $resp.ApiUrl - $Global:ApiKey = $resp.ApiKey - $Global:ApiSecretKey = $resp.ApiSecretKey - - Write-LogHybrid "Fetched and stored API credentials." "Success" "FetchSites" - } catch { - Write-LogHybrid "Webhook call failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent +try { + $creds = Get-DattoApiCredentials -Password $pw + if (-not $creds) { + Write-LogHybrid "Webhook returned no credentials" Error FetchSites returnRespondEmpty $Context 403 return } + # reuse the same globals from the entrypoint + $Global:ApiUrl = $creds.ApiUrl + $Global:ApiKey = $creds.ApiKey + $Global:ApiSecretKey = $creds.ApiSecretKey + + Write-LogHybrid "Fetched and stored API credentials." Success FetchSites +} catch { + Write-LogHybrid "Credential-fetch error: $($_.Exception.Message)" Error FetchSites -LogToEvent + returnRespondEmpty $Context 500 + return +} + + # 3) Exchange for a bearer token Write-LogHybrid "Requesting OAuth token" "Info" "FetchSites" try {