pfsense-api icon indicating copy to clipboard operation
pfsense-api copied to clipboard

Get the original XML string of the config.xml file

Open devvtecc opened this issue 2 years ago • 3 comments

I wonder how to get the original content (XML-string) of the config file, e.g. for backup purposes.

I converted the json result of the current /system/config GET endpoint to XML and compared it with the original XML string of the config.xml file from a backup via WebUI... There are differences - and these differences could cause that a restore (via WebUI), of a config.xml file generated via conversion from Json to XML, will not be possible without issuse - in my opinion.

The differences (without guarantee of completeness) that I've noticed...

  • original XML nodes do have innerText wrapped with <![CDATA[innerText]]>, jsonToXmlConverted do NOT! example: pfsense/system/group original: <description><![CDATA[All Users]]></description> jsonToXmlConverted: <description>All Users</description> => This issue happens very often Unfortunately there is no reference to elements with CData wrapper in the Json result, so it might be difficult to convert this back to xml properly.

  • spaces between colons in innerText example: pfsense/dhcpdv6/lan/range original: <from>::1000</from> jsonToXmlConverted: <from>: : 1000</from>

  • line break inserted example: pfsense/cron/item original: <minute>1,31</minute> jsonToXmlConverted: <minute>1,\n31</minute>

  • space inserted example: pfsense/installedpackages/package/website/ original: <website>https://github.com/jaredhendrickson13/pfsense-api</website> jsonToXmlConverted: <website>https: //github.com/jaredhendrickson13/pfsense-api</website>

=> I don't think it's a good idea to manually restore such an config file with these differences.

Generally I like to use the API on Json basis to read and change specific settings. But for the backup of the overall configuration I would think it much easier and especially safer to be able to get the original XML-string of the config.xml via GET as an alternative to the Json result.

Additionally, it would also be desirable to be able to make a restore via XML-string (e.g. taken from a manually saved config.xml file) - via PUT endpoint.

So I would like to see an endpoint like /system/config-xml that provides the described functionality.

I welcome an implementation or alternative suggestions on how to work around the issues described.

Many thanks for the hard work on this project so far!

devvtecc avatar Jan 16 '22 16:01 devvtecc

Hey!

The /api/v1/system/config endpoint is really not intended to restore a full configuration backup, or be converted to XML to restore a backup. It is intended to have a way to read the entire configuration and apply specific changes to the configuration that may not be configurable via API. This was requested for some very custom implementations. Seems like the terminology in the documentation right now doesn't make that super clear. I will update that.

As far as pulling the XML configuration from the API, you can do so using the /api/v1/diagnostics/command_prompt endpoint to do this. For example:

curl -s -d '{"client-id": "admin", "client-token": "pfsense", "shell_cmd": "cat /cf/conf/config.xml"}' -X POST https://127.0.0.1/api/v1/diagnostics/command_prompt

An endpoint to replicate the Diagnostics > Backup & Restore page has been in the works, but there are some hoops to jump through to make things work nicely with auto configuration backups and HA setups. In the meantime, backup restores can be applied via command line using the /api/v1/diagnostics/command_prompt endpoint.

Hope this helps.

Thanks!

jaredhendrickson13 avatar Jan 16 '22 17:01 jaredhendrickson13

Thank you very much for this quick reply.

Your workaround works perfectly to get the XML string of the config file. Tested and working!

Thanks a lot!

devvtecc avatar Jan 16 '22 18:01 devvtecc

Hi @jaredhendrickson13,

From what I can read, you were working on the backup & restore page, is there any update on that?

If this is available somewhere, I'd be happy to try helping on this subject as this would definitively be a functionality that I'm looking for for my automation of pfSense.

Thanks a lot for the API!

triat avatar Jun 02 '22 06:06 triat