Skip to content

Connect broken on UDMPro (at least when running version 3.0.16) #10

@kriggleman42

Description

@kriggleman42

Great set of routines, nicely done for UniFi access. :-)

I mostly looked at these routines because UniFi API is not well documented for UDMPro. When I tested your ps module, the connect-controller didn't display any message but didn't have an error either so I assumed that command had worked.
However, when I tried get-UnifiSite I received a 401 unauthorized.

Investigating a bit more I found reason for 401 was that Session variable didn't get set during connect-controller. Then I realized another change that occurred when we upgraded to UDMPro. $results.meta is no longer returned from a call to .../api/auth/login
Instead, status and other variables are returned including setting $results.unique_id which could be a different way to validate.
Note that because the call was successful, no error is triggered. Therefore it would be more robust of your code imho to provide some type of alert that connect didn't go as planned (and thus session variable hasn't been set).

Therefore, I'd recommend updating connect-controller lines 31 and after to something closer to this instead (which I've run on UDMPro and the following works):
if ($results.meta.rc -eq "ok" -or ($UDMPro -and $results.status -eq "ACTIVE")) {
if (!$refresh){
Write-Verbose "Successfully connected to Unifi controller."
}
else {
Write-Verbose "Successfully reconnected to Unifi controller."
}
# Set this as a script variable as it will be used for all other commands in the module.
$Script:Session = $MyWebSession
}
else {
Write-Verbose "Unexpected results from Unifi controller. Session not established." #I added for robustness!
}

Finally, the primary reason I was looking at your library was to see how in powershell you do set commands in Unifi API (which I'm doing in powershell). On the UDMPro they require that csrf token is set on session. Otherwise, I get "invalid csrf token" on any update UniFi API cmds. The read comands like get-UnifiSite work fine. Bottom line, my UniFi API update commands worked fine just like you have implemented until we upgraded to UDMPro controller but now they all fail with "invalid csrf token". I'm guessing you are not familiar with that issue but alerting you if anyone asks. If you happen to know how to address that would be wonderful if your routines handled that issue which I believe requires adding x-csrf-token to session state (only needed on UDMPro)

Thanks for your consideration.
Ken Riggleman

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions