Repository navigation
Conversation
The devices list's CSV export moves into NervesHub.Devices.DeviceCSV, which both writes the file and reads it back, so every export imports again as it was: identifier, description, tags and, when asked for, certificates. - GET /api/orgs/:org/products/:product/devices/export streams the same CSV as the web UI's export, filtered and sorted as the devices list is, for user tokens and product API keys alike. include_certificates=true adds the certificates column, in the API and from the UI's export menu. - POST .../devices/import reads the CSV as its csv format, sent as text/csv, and the devices list gets an Import devices dialog for it. A file it can't read is refused with each problem before anything is created. - The deployment group's CSV import finds the identifier column among others, so it takes the export too. Closes #2433
Rummager review of
|
| Before | After |
|---|---|
![]() |
![]() |
Before, the download button in the header starts the export right away, so there is no menu to show. After, there is a new upload (Import devices) button next to it, and clicking download opens a menu with "Export as CSV" and "Export with certificates", each with a description.
Import devices dialog with file chosen
| Before | After |
|---|---|
| none | ![]() |
New dialog at /devices/import showing the explanatory text, the chosen smartkiosk-devices.csv as a row with a spreadsheet icon and a remove button, and the Import button; this dialog does not exist before the PR.
Import dialog listing file problems
| Before | After |
|---|---|
| none | ![]() |
After clicking Import on a CSV with bad rows, the new problems block reads "Nothing was imported. Fix the file and try again:" and lists the problems by row: a tag with a space, a repeated identifier and an empty identifier.
Import button on empty product
| Before | After |
|---|---|
![]() |
![]() |
On a product with no devices, the header now has the Import devices (upload) button next to Add device, and the export menu stays hidden.
rummager · prompts 885379605730 · automated, verify before acting.
u7d-bot
left a comment
There was a problem hiding this comment.
Inline findings for 8ee7585a3c. The summary is in the rummager comment.
| case rows do | ||
| [] -> {:error, [{"entries", "must have at least one entry"}]} | ||
| rows -> {:ok, rows |> Enum.map(&parse_row(&1, columns)) |> refuse_repeated_identifiers()} | ||
| end |
There was a problem hiding this comment.
[WARNING] The CSV import caps bytes at 32 MB but not rows, so one request can expand into millions of in-memory entries and background inserts. Security · review
A 32 MB body of rows like a1,b c is roughly 5 million rows; each becomes a cell map, a result tuple, an entry in the seen identifier map and, when invalid, a {path, message} problem. All of that is held at once in the request or LiveView process, then either rendered whole into the 422 JSON, kept whole in the :problems assign (only 8 are shown), or copied into the Task that inserts every device serially. Any member or API key with device:create can repeat this concurrently, so refuse a file over a fixed row count (for example {"entries", "must have at most N entries"}) before mapping the rows, and truncate the problems list returned.
| defp record_string(attrs, key) do | ||
| case attrs[key] do | ||
| value when is_binary(value) and value != "" -> {:ok, value} | ||
| _ -> {:error, "has a certificate record without a #{key}"} |
There was a problem hiding this comment.
[WARNING] With key interpolated, this error reads "has a certificate record without a aki", which is ungrammatical. Prose, comments and docs · definite
This text appears in the import dialog and the API's 422 responses. Dropping the article works for every key (serial, aki, not_before, not_after). The assertion in test/nerves_hub/devices/device_csv_test.exs line 197 also needs updating.
| _ -> {:error, "has a certificate record without a #{key}"} | |
| _ -> {:error, "has a certificate record without #{key}"} |
| case Tag.cast(tags) do | ||
| {:ok, tags} -> {:ok, tags} | ||
| {:error, message: message} -> {:error, {:invalid_manifest, [{"tags", message}]}} | ||
| :error -> {:error, {:invalid_manifest, [{"tags", "must be comma-separated tags"}]}} | ||
| end |
There was a problem hiding this comment.
[SUGGESTION] cast_import_tags/1 repeats the same Tag.cast error handling as the private tags/1 in NervesHub.Devices.DeviceCSV. Code quality · definite
Both copies turn {:error, message: m} and :error into the same messages, so if one changes and the other doesn't, the tags header and the CSV tags column will give different errors. Expose one function, for example DeviceCSV.cast_tags/1 returning {:ok, tags} | {:error, message}, and have both callers wrap its result in their own error shape.






The devices list's CSV export gets an API endpoint, an option to include certificates, and an import that reads the file back. A product's devices can be exported and imported into another product or instance, with their descriptions, tags and certificates.
Closes #2433. nh gets
nh device exportand CSV import in nerves-hub/nh#39.One module writes and reads the CSV
NervesHub.Devices.DeviceCSVreplacesProducts.devices_export_reducer/4. It writes the export and parses the import, so a file it writes always imports again. The columns are unchanged:identifier,description,tags,product,org, thencertificatesonly when asked for. Each certificate is a PEM block with one blank line between them. A certificate saved before DERs were kept is written as a JSON object of its details, as before. Importing that object saves the record without a DER, using thefrom_jsonpathDeviceCertificatealready had.Both exports start from
Devices.export_query/3, which takes a user or a product API key, so tag-limited roles see only their devices. Both are streamed byNervesHubWeb.Helpers.DeviceCSVDownload.API
GET /api/orgs/:org/products/:product/devices/exporttakes the devices list'sfilters[...],sortandsort_direction, plusinclude_certificates, which defaults to false. It streams every matching device, without paging. A bad filter, sort, advanced query orinclude_certificatesis refused with a 422 before streaming starts.Accept: text/csv, which the:apipipeline refuses with a 406. Its errors are still JSON. It sits ahead of the API scope so that/devices/:identifierdoesn't take it.POST .../devices/importtakes the CSV as a newcsvformat. It's sent astext/csv, which implies the format, and is read from the raw body, up to 32 MB (413 beyond that). Columns are matched by name in any order.identifieris required,productandorgare ignored, and any other column is refused. As with Trust&Go, a file it can't read gets a 422 naming each problem before anything is created. That includes an identifier repeated in the file and a PEM certificate without an authority key identifier. Each device is created with its certificates in one transaction, in the background, and the notification counts them.APISpecAssertionsnow checks a non-JSON response by its content type instead of failing on it.Web UI
include_certificates, like the API.device:create) opens a dialog at/devices/import. It reads the CSV and lists what's wrong with a file it can't read. It shows on an empty product too, which is where an import is most useful.identifiercolumn, so it refused the export. It now finds that column among others.Behaviour to know about
devices_identifier_index). An import only creates devices the instance doesn't already have; the others count as failed. A round trip on the same instance needs the originals hard-deleted. Within an instance,moveis the tool.tagsheader on any import, Trust&Go included, is now refused with a 422. Before, every device failed in the background.exportorimportis now shadowed by those routes, asnewalready was.Verified
DeviceCSV, including a round trip with PEM and JSON certificates, and one into another org.text/csvaccept handling.mix deps.unlock --check-unusedflags:comeonin, which fails on main too.