jaredhendrickson13 / pfsense-api

The missing REST API package for pfSense
https://pfrest.org/
Apache License 2.0
651 stars 95 forks source link

Missing group of api endpoints in v2 #501

Closed droid-sheep closed 5 days ago

droid-sheep commented 1 week ago

Describe the bug

After freshly installing the pfrest pkg (pfSense-2.7.2-pkg-RESTAPI.pkg) some endpoint groups are not available (HTTP 404). Following groups of endpoints are not available (referencing https://pfrest.org/api-docs/#/):

To Reproduce Steps to reproduce the behavior:

  1. Install pkg via pfsense command line: pkg-static -C /dev/null add https://github.com/jaredhendrickson13/pfsense-api/releases/latest/download/pfSense-2.7.2-pkg-RESTAPI.pkg
  2. Switch Authentication to KEY based only
  3. Create a REST Key under keys tab. Hashing = SHA256, Length = 16
  4. Call via REST API Client (i used Advanced Rest client):
    GET https://<fw>/api/v2/system/crls
    Headers:
    X-API-Key: <API Key>

    Response:

    <html>
    <head><title>404 Not Found</title></head>
    <body>
    <center><h1>404 Not Found</h1></center>
    <hr><center>nginx</center>
    </body>
    </html>

Expected behavior Response defined in api docs and not a HTTP 404 error.

Screenshots or Response -/-

pfSense Version & Package Version:

Affected Endpoints:

Additional context -/-

jaredhendrickson13 commented 1 week ago

These are all present for me after installing. Try running pfsense-restapi buildendpoints from the command line to force the endpoints to be rebuilt.

droid-sheep commented 1 week ago

When i execute your mentioned command in the web debug command line i get this response:

Building endpoints...
Fatal error: Uncaught RESTAPI\Responses\ServerError: Cannot parse FloatField `cpu_load_avg` from internal because its internal value is not
                    a numeric value. Consider changing this field to a StringField. in /usr/local/pkg/RESTAPI/Fields/FloatField.inc:143
Stack trace:
#0 /usr/local/pkg/RESTAPI/Core/Field.inc(886): RESTAPI\Fields\FloatField->_from_internal('0,18 0,22 0,23')
#1 /usr/local/pkg/RESTAPI/Core/Model.inc(827): RESTAPI\Core\Field->from_internal('0,18 0,22 0,23')
#2 /usr/local/pkg/RESTAPI/Core/Model.inc(810): RESTAPI\Core\Model->from_internal_object(Array)
#3 /usr/local/pkg/RESTAPI/Core/Model.inc(354): RESTAPI\Core\Model->from_internal()
#4 /usr/local/pkg/RESTAPI/Core/Model.inc(296): RESTAPI\Core\Model->construct_from_internal(NULL, NULL)
#5 /usr/local/pkg/RESTAPI/Models/SystemStatus.inc(162): RESTAPI\Core\Model->__construct(NULL, NULL, Array)
#6 /usr/local/pkg/RESTAPI/Core/Endpoint.inc(272): RESTAPI\Models\SystemStatus->__construct()
#7 /usr/local/pkg/RESTAPI/Endpoints/StatusSystemEndpoint.inc(21): RESTAPI\Core\Endpoint->__construct()
#8 /usr/local/pkg/RESTAPI/.resources/scripts/manage.php(40): RESTAPI\Endpoints\StatusSystemEndpoint->__construct()
#9 /usr/local/pkg/RESTAPI/.resources/scripts/manage.php(412): build_endpoints()
#10 {main}
  thrown in /usr/local/pkg/RESTAPI/Fields/FloatField.inc on line 143
PHP ERROR: Type: 1, File: /usr/local/pkg/RESTAPI/Fields/FloatField.inc, Line: 143, Message: Uncaught RESTAPI\Responses\ServerError: Cannot parse FloatField `cpu_load_avg` from internal because its internal value is not
                    a numeric value. Consider changing this field to a StringField. in /usr/local/pkg/RESTAPI/Fields/FloatField.inc:143
Stack trace:
#0 /usr/local/pkg/RESTAPI/Core/Field.inc(886): RESTAPI\Fields\FloatField->_from_internal('0,18 0,22 0,23')
#1 /usr/local/pkg/RESTAPI/Core/Model.inc(827): RESTAPI\Core\Field->from_internal('0,18 0,22 0,23')
#2 /usr/local/pkg/RESTAPI/Core/Model.inc(810): RESTAPI\Core\Model->from_internal_object(Array)
#3 /usr/local/pkg/RESTAPI/Core/Model.inc(354): RESTAPI\Core\Model->from_internal()
#4 /usr/local/pkg/RESTAPI/Core/Model.inc(296): RESTAPI\Core\Model->construct_from_internal(NULL, NULL)
#5 /usr/local/pkg/RESTAPI/Models/SystemStatus.inc(162): RESTAPI\Core\Model->__construct(NULL, NULL, Array)
#6 /usr/local/pkg/RESTAPI/Core/Endpoint.inc(272): RESTAPI\Models\SystemStatus->__construct()
#7 /usr/local/pkg/RESTAPI/Endpoints/StatusSystemEndpoint.inc(21): RESTAPI\Core\Endpoint->__construct()
#8 /usr/local/pkg/RESTAPI/.resources/scripts/manage.php(40): RESTAPI\Endpoints\StatusSystemEndpoint->__construct()
#9 /usr/local/pkg/RESTAPI/.resources/scripts/manage.php(412): build_endpoints()
#10 {main}
  thrown

Also no change in the 404 Endpoints and still 404

droid-sheep commented 1 week ago

@jaredhendrickson13 Okay now i found something! Executing the pfsense-restapi buildendpoints command in the virtual machine shell gives me:

Building endpoints... done.
Building privileges... done.

After that (quick look) all endpoints work now. I don't know why the command doesn't work in the web debug shell. Also maybe there is a difference if i execute the install command in the web php prompt or in a "real" shell.

Maybe further investigate this or add an disclaimer to the install section that users shouldn't use the web debug shell.

I leave it up to you if you want to close this issue or investigate. Thank you for the rebuild command, it solved my issue.

jaredhendrickson13 commented 1 week ago

Actually, the error you received previously does show a valid issue which could be preventing the endpoints from building correctly. The CPU load averages on your system are/were formatted with commas as the decimal separator instead of points as PHP expects them, which is valid for certain regions around the world. Regardless, the logic of the underlying models probably shouldn't prevent the endpoints from being built, so I'll work on a fix for those things.

Thanks!