Freenas API v2.0 swagger

JM2K69

Dabbler
Joined
Feb 17, 2019
Messages
22
I find the way to use the api v1.0. But I can not authenticate myself on the API v2 rest swagger. I always have error 401. Witch method can I use to make authenticate to the endpoint?

Thank’you.
 

JM2K69

Dabbler
Joined
Feb 17, 2019
Messages
22
I Use Powershell to create a powershell module it works great with API v1.0 but not with v2.0 have a look I use the same Server
API V1.0 :

API1.PNG

API v2.0
API2.PNG

I use the same object Disk
API3.PNG
 
Last edited:

ian351c

Patron
Joined
Oct 20, 2011
Messages
219
I'm much more of a Unix guy than a Windows guy, but a little time with Google tells me that you might be running into an issue I ran into using wget instead of curl. If I want to use wget, I have to manually specify the Authorization header rather than specify credentials because wget expects to see a challenge for authentication and FreeNAS doesn't present one. If you don't specify the authentication in your initial request, FreeNAS just says "go away!" rather than "who are you?". I was able to make the Swagger API work with Powershell by specifying the Authorization header rather than using the -Credentials option. Like so:

Code:
$headers = @{    'Authorization' = 'Basic <base64_encoded_password>'   }
Invoke-RestMethod -Uri "http://nas1.home/api/v2.0/ups/driver_choices" -Headers $headers


I can't quite figure out how to make it all work on one line, but hopefully that's enough to get you on the right track.

Edited to add:
Base64 encoding in Powershell: https://michlstechblog.info/blog/powershell-encode-and-decode-base64-strings/
This gave me the same result as using base64() in Unix, so it should work for you.
Example: (your login is root and password is opensesame)
Code:
$sStringToEncode = 'root:opensesame'
$sEncodedString=[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($sStringToEncode))
write-host "Encoded String:" $sEncodedString
Encoded String: cm9vdDpvcGVuc2VzYW1l
 
Last edited:

JM2K69

Dabbler
Joined
Feb 17, 2019
Messages
22
Thx I used but isn't very secure

Powershell 5.1 : works only v1.0
$Headers = @{ Authorization = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username, $Password))) }
try { $result = Invoke-RestMethod -Uri $Uri -Headers $Headers -Method Get -SessionVariable Freenas_S} catch {}

or
try {$result = Invoke-RestMethod -Uri $Uri -Method Get -SessionVariable Freenas_S -Credential (Get-Credential)}
catch { Write-Error "Error when try to connect to $Uri"
return }

This two method works and offer Basic Authentication .

Powershell 6.1.x : works v1.0 and v2.0

try {$result = Invoke-RestMethod -Uri $Uri -Authentication Basic -AllowUnencryptedAuthentication -Method Get -SessionVariable Freenas_S -Credential (Get-Credential)}
catch { Write-Error "Error when try to connect to $Uri"
return }

I don't understand Why ? in PowerShell 5.1 i must used this method:

$Headers = @{ Authorization = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username, $Password))) }
try { $result = Invoke-RestMethod -Uri $Uri -Headers $Headers -Method Get -SessionVariable Freenas_S} catch {}

Thank's for your help.
 

JM2K69

Dabbler
Joined
Feb 17, 2019
Messages
22
What is the way to download the JSON file or YAML for the v2.0 definitions for the swagger file, because I want to use with a other powershell project AUTOREST AZURE.
 

ian351c

Patron
Joined
Oct 20, 2011
Messages
219
I'm not sure what the difference is between the Invoke-RestMethod function in Powershell 5.1 vs 6.0 is, but my guess is that since you can specify the authentication method in 6.1, it creates and sends the authentication header for you instead of you having to create it yourself. Neither method is all that secure since base64 is not encryption (it probably looks exactly the same in a packet capture between 5.1 and 6.0). I really wish the iX folks would support API Keys for this, but from my digging into the files in /usr/local/www there's still a LOT of Django cruft in there that probably prevents much modernization of the GUI beyond skins (the shiny new angular GUI still talks to a very old Django middleware).

As far as the config file for the v2.0 API goes, I don't think there is one. At least I haven't been able to find it in all my digging. I'm not very familiar with how it all works (and I'm not really a web developer), but it looks like that config may be generated on the fly by the Django middleware. An iX developer might be able to tell us for sure though if there are any lurking on the forum...
 
Top