Skip to content

Export devices as CSV and import the export - #39

Open
lawik wants to merge 1 commit into
mainfrom
feature/device-export
Open

lawik wants to merge 1 commit into
mainfrom
feature/device-export

Conversation

@lawik

@lawik lawik commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

NervesHub is adding a devices CSV export to its API, and a CSV import that reads the file back, in nerves-hub/nerves_hub_web#3427. This PR adds both to nh, so a product's devices can be copied to another product or instance with their descriptions, tags and certificates:

nh device export --include-certificates > devices.csv
nh device import devices.csv --product thermostat   # on the other instance

nh device export

  • Streams GET .../devices/export to standard output, or to a file with -f/--file. A file is written beside the destination and renamed once complete, so a failed export leaves nothing behind. With a file, nh then says how many devices it exported, as JSON with -o json.
  • --filter, --query and --sort are the same flags, with the same checks, as nh device list. They're factored out of device list without changing it.
  • --include-certificates adds the certificates column.
  • It sends Accept: text/csv, application/json;q=0.9. A server without the export only answers JSON, so offering JSON lets it answer 404 instead of 406. nh reports "needs a release newer than v2.5.4" when that 404 comes for a product that exists, or when a 200 isn't CSV.

nh device import

  • Takes the export's CSV as well as a Trust&Go manifest. The format comes from the file: a .csv file, or content that doesn't start like JSON, is CSV. --format csv|microchip_trust_and_go overrides it.
  • The CSV is sent as text/csv with Format: csv. --tag adds tags on top of each row's own.
  • As for Trust&Go, nh checks every row the way the server reads it and sends nothing if any has a problem. --dry-run only checks.
    • Headers are matched trimmed and in any case. A row with fewer cells than the header reads the rest as empty, and a row of empty cells is skipped.
    • nh refuses unknown, repeated or missing columns, empty identifiers, tags with spaces, and certificates that aren't PEM or the export's JSON records.
    • It also refuses certificates without an authority key identifier, and identifiers or certificates repeated across rows.
  • NervesHub 2.5.4 and earlier answer a CSV import with 201 and then drop it. So before sending a CSV, nh checks that the server has the export, which shipped with the CSV import: it sends a filter no server knows, which a new server refuses with 422. On an old server, nh sends nothing and says why.

Identifiers are unique across a whole NervesHub instance, deleted devices included. An import therefore only creates devices the instance doesn't already have. The help text and README say so and point to nh device move for moving devices between products.

Verified

  • gofmt, go vet, go build and go test -race ./... pass, with new tests for both commands and the CSV reader.
  • End to end against a local server running Export devices from the API and import the export back nerves_hub_web#3427:
    • nh device export --include-certificates -f and a dry run.
    • Hard-deleting the devices, then nh device import into another product.
    • Re-exporting gave the same file apart from the product and org columns, including a multi-line quoted description, PEM certificates and a DER-less certificate record.

nh device export writes a product's devices as the CSV NervesHub's new
GET .../devices/export sends: identifier, description, tags, product and
org, plus certificates with --include-certificates. --filter, --query and
--sort work as for nh device list, and --file writes the file whole or not
at all.

nh device import now takes that CSV as well as a Trust&Go manifest, reading
the format from the file or --format. It checks every row the way the
server reads it before sending, and checks first that the server can import
a CSV, since NervesHub 2.5.4 and earlier accept one and then drop it.
@u7d-bot

u7d-bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Rummager review of 8a42db5c70

Sensitive areas

  • Authentication: device identity certificates in bulk export and import, client-side X.509 and authority key identifier checks
  • Personal data: bulk export of device inventory, descriptions, tags and certificates to local files

Pass with warnings: 1 warning, 4 suggestions.

  • Correctness: 1 finding, inline at cmd/device_export.go:160.
  • Code quality: 3 findings, inline at cmd/device_import.go:227, cmd/device_import.go:307-310.
    • [SUGGESTION] cmd/device_import.go:90-91 The argument errors still say "manifest file" now that import also takes a CSV export. Someone running nh device import to bring in a CSV gets "Manifest file missing", which suggests the command only takes Trust&Go manifests. Reword both messages to match the new import <file> usage, and update the assertion in cmd/device_import_test.go:238 to the new text.
      		"File to import missing",
      		"too many arguments: provide a single file to import",
      
  • Prose, comments and docs: 1 finding, inline at cmd/device_import.go:64-67.
  • No findings: Security.

The inline comments are in this review.

Screenshots: no UI changes in this PR.

rummager · prompts 885379605730 · automated, verify before acting.

@u7d-bot u7d-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline findings for 8a42db5c70. The summary is in the rummager comment.

Comment thread cmd/device_import.go
Comment on lines +64 to +67
nh checks every entry first, the way the server reads it, and sends nothing
unless every entry is good, since older NervesHub servers accept a file
they can't read and then drop it without saying so. --dry-run does the check
and stops; it needs no login, org or product.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[WARNING] The rewritten help no longer says why nh checks every entry first: one malformed entry can make NervesHub drop the whole file without saying so. Prose, comments and docs · likely

The new reason is that "older NervesHub servers accept a file they can't read". That describes the old-server CSV case, and nh handles that case with the SupportsDeviceExport probe, not with the row checks. The per-entry check exists because the server abandons the whole import over one bad entry and never reports it. That fact was in the old help text and is now gone.

Suggested change
nh checks every entry first, the way the server reads it, and sends nothing
unless every entry is good, since older NervesHub servers accept a file
they can't read and then drop it without saying so. --dry-run does the check
and stops; it needs no login, org or product.
nh checks every entry first, the way the server reads it, and sends nothing
unless every entry is good: a malformed entry can make NervesHub abandon the
whole file without saying so. --dry-run does the check and stops; it needs
no login, org or product.

Comment thread cmd/device_export.go
if err != nil {
return fmt.Errorf("creating temporary file: %w", err)
}
tmpName := tmp.Name()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[SUGGESTION] The export's temporary file is not registered with interrupt.RemoveOnExit, so a second Ctrl-C or the grace timeout leaves a hidden .nh-export-* file next to the destination. Correctness · likely

When interrupt.Run gives up on the work, it calls os.Exit without running the cleanup on lines 164-179. Only files registered with interrupt.RemoveOnExit are removed on that path (signedTempPath registers its file this way). That breaks the PR's promise that a failed export leaves nothing behind. Add defer interrupt.RemoveOnExit(tmpName)() after this line and import github.com/nerves-hub/nh/internal/interrupt.

Comment thread cmd/device_import.go
if strings.EqualFold(filepath.Ext(path), ".csv") {
return api.DeviceImportFormatCSV
}
trimmed := bytes.TrimSpace(bytes.TrimPrefix(data, []byte("\xef\xbb\xbf")))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[SUGGESTION] detectImportFormat writes the UTF-8 BOM bytes out by hand, while internal/api already defines them as utf8BOM. Code quality · definite

The same constant now lives in two packages. Export it from api (for example as api.UTF8BOM) and use that here, so the format detection and the CSV parser cannot drift apart.

Comment thread cmd/device_import.go
Comment on lines +307 to +310
entries := "entries"
if isCSV {
entries = "rows"
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[SUGGESTION] renderDeviceImport picks the rows/entries noun with its own branch instead of calling the new importEntries helper. Code quality · definite

Two helpers now choose the word for an import's entries and already disagree ("manifest entries" in importEntries, "entries" here). Keep one source, for example entries := importEntries(r.Format), and drop this branch.

Suggested change
entries := "entries"
if isCSV {
entries = "rows"
}
entries := importEntries(r.Format)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants