REST API Issue? DomainSettings...
Problem reported by TJ - 10/29/2019 at 5:59 PM
Known
Hi SM devs/guys, I'm trying to automate some things with SM using Powershell 5.1 via the REST API, I can do most of the things I'm attempting, but when I try and add white-listed domains, the command succeeds (i.e. no error), but the white-listed domains aren't added.

Below is a cut down version of the Powershell script (use Visual Studio Code), switch out the url, username and password and step through the code. What am I doing wrong, or is this a bug in the API?

Thanks, TJ

# working vars, params & constants
$vendorTrustedDomains = 'trusteddomain1.com, trusteddomain2.com '
New-Variable -Name 'SMbaseurl' -Value 'https://mail.mydomain.com/api/v1'; -Option Constant
$emailLoginUsername = 'admin@mydomain.com'
$emailLoginPwd  ='MyPassword123'

$global:Header = ""
$vendorTrustedDomains = $vendorTrustedDomains.split(',').Trim() | Where-Object {$_}

# Functions
function AddTrustedEmailDomain($trustedVendorDomains)
{
    # Update Mailing list system messages
    $SMurl = "$SMbaseurl/settings/domain/domain"

    $response = Invoke-RestMethod -Method GET -Header $Header -ContentType "application/json" -uri $SMurl

    if ($response.success = $true)
    {
        $domainSettings = $response
        foreach ($trustedVendorDomain in $trustedVendorDomains)
        {
            if (-not $domainSettings.domainSettings.whitelistDomains.Contains($trustedVendorDomain))
            {
                #add new vendor domain to whitelist - NOTE use of -InputObject for adding a single value!
                write-host "Vendor Domain ($trustedVendorDomain) not setup on  Domain as a trusted domain. Adding it now..."
                #type:1 = domain
                $vendorDomain = @{  "id" = ""
                                    "value" = "$trustedVendorDomain"
                                    "type" = "1"
                                    "description" = ""
                                } | ConvertTo-Json
                $domainSettings.domainSettings.whitelistDomains += (ConvertFrom-Json -InputObject $vendorDomain)   
            }   
        }

        $domainSettings = $domainSettings | ConvertTo-Json -Depth 10

        $SMurl = "$SMbaseurl/settings/domain/domain"
        $response = Invoke-RestMethod -Method POST -Header $Header -ContentType "application/json" -uri $SMurl -Body $domainSettings 
        if ($response.success = $true)
        {
            return 1
        }
        else {
            return -1
        }
       
    }
    else {
        return -1
    }
}


# Adding certificate exception and TLS 1.2 
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

#-----------------
#Authenticate and get an authorization token...
#-----------------
$SMurl = "$SMbaseurl/login-settings/"
$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("${emailLoginUsername}:${emailLoginPwd}"))}

$SMurl = "$SMbaseurl/auth/authenticate-user"
$response = Invoke-RestMethod -Method POST -Header $Header -ContentType "application/json" -uri $SMurl -Body $params
#Write-Host $response.accessToken
if ($response.resultCode = 200)
{
    $accesstoken = $response.accessToken
    $refreshtoken = $response.refreshToken

    $Header = @{Authorization = "Bearer "+$accesstoken}
}
else {
    exit
}

# Now lets give it a go...
AddTrustedEmailDomain($vendorTrustedDomains)

Employee Replied
Employee Post
TJ,

What build of SmarterMail are you running?
TJ Replied
7188  
Employee Replied
Employee Post
There were some issues with 7188 that were fixed in the latest release of 7236.  I would recommend doing a minor upgrade to 7236 first and re-testing your code with it.  You can follow this knowledge base article to do the minor upgrade:

TJ Replied
I just upgraded to SmarterMail Enterprise Build 7236 (Oct 24, 2019) and the issue is still there- the API accepts the post and returns success, but does not update the whitelisted domain list... :(

TJ
Employee Replied
Employee Post
TJ,

Thanks for letting me know.  I've submitted this as a bug to our developers for you.
TJ Replied
Thanks, appreciate it...

TJ

TJ Replied
Is there anyway of tracking when/if this bug is fixed? Its not logged under my account as a ticket, so just curious how I will know when its fixed...

Thanks, TJ

Reply to Thread

Enter the verification text