Skip to content

User API Keys

Each NetBox user attaches their own RIR credential to one or more RIRConfig rows. Keys are encrypted at rest and never returned by the REST API after creation.

Why per-user keys

The plugin assumes a multi-tenant operations model. RIR audit trails attribute every change to the credential that submitted it, so the plugin records, on every synced or written object:

  • synced_by -- the RIRUserKey whose call produced the row (on RIROrganization, RIRContact, RIRNetwork).
  • submitted_by -- the RIRUserKey that submitted a write (on RIRTicket).

Per-user keys also avoid the operational hazard of a shared secret, and make rotation a per-user concern.

Which key an operation uses

  • Anything a user initiates runs only with that user's own key -- the Sync, Reassign, Reallocate, Remove, and Delete actions, and the background work an edit sets off (automatic reassignment when a prefix gets a site and tenant, automatic removal when a prefix is deleted or deactivated). If that user has no key for the config, nothing is sent to the RIR; the UI reports an error, or, for edit-driven work, a skipped sync log row names the user and the config.
  • Work no user initiates -- the scheduled sync, or automation running outside any request -- uses the config's default key (RIRConfig.default_key). If none is set, it is skipped. Its changes are attributed in NetBox's change log to the default key's owner.
  • A deactivated user's keys are never used, whether as their own key or as a config's default key. Deactivating a user stops their keys the same way deleting the user does: work that would need one is skipped and the log names the deactivated user. This includes jobs queued before the deactivation: a prefix sync, reassign or remove job whose key's owner is deactivated by the time it runs makes no RIR call and writes a skipped sync log row instead, and a queued manual RIR Sync fails with a job message naming the deactivated user. A deactivated user's key cannot be chosen as a config's default key.

A user's edit never falls back to the default key, so every write at the RIR is attributable to the person who made it. Background jobs run as the acting user, so NetBox's job list and changelog record who triggered them.

Adding a key

  1. Go to RIR Manager > User Keys and click Add.
  2. Pick a NetBox user (admins can create keys for other users).
  3. Pick the RIR Config. The pair (user, config) must be unique.
  4. Paste the API key into the password input.
  5. Save.

The form widget uses forms.PasswordInput, so the value is masked while typing. The serializer marks api_key as write_only, so it is never echoed back over the REST API.

Encryption at rest

The RIRUserKey.api_key column is an EncryptedCharField. Saving it transparently runs the value through Fernet encryption with a key derived from the plugin's encryption_key setting (or NetBox's SECRET_KEY when the setting is empty). Reading it (via the ORM) transparently decrypts.

Implementation details:

  • KDF: HKDF-SHA256, salt netbox-rir-manager, info api-key-encryption, output 32 bytes.
  • Cipher: Fernet (AES-128-CBC + HMAC-SHA256 + IV).
  • Storage prefix: $FERNET$<token>. Values without the prefix are returned as-is, which preserves backward compatibility with rows written before the field was added.
  • Decryption failure mode: InvalidToken returns the stored value verbatim instead of raising. This means a misconfigured encryption_key will not crash the application, but write operations will fail at the RIR with confusing errors. See the warning below.

The implementation is netbox_rir_manager.fields.EncryptedCharField.

Rotation

Rotating a single user's RIR API key:

  1. Update the key with the RIR.
  2. Edit the RIRUserKey row in NetBox and paste the new value.
  3. Save. The new ciphertext replaces the old.

Rotating the encryption_key setting:

  1. Pre-flight: write a one-shot Django shell snippet that loops over every RIRUserKey, reads api_key (which decrypts under the old key), and save()s it back (which re-encrypts under the same old key). This guarantees no plaintext rows remain.
  2. Update encryption_key in configuration.py.
  3. Restart NetBox and the RQ worker.
  4. Have each user re-enter their key. There is no way to recover the old ciphertext under the new key.

Lost keys are unrecoverable

Changing or losing encryption_key (or SECRET_KEY when the plugin's setting is empty) makes every previously encrypted API key unrecoverable. Treat the encryption key like a database password: store it in your secrets manager and back it up alongside the database.

REST API

GET  /api/plugins/rir-manager/user-keys/
POST /api/plugins/rir-manager/user-keys/
GET  /api/plugins/rir-manager/user-keys/{id}/
PATCH /api/plugins/rir-manager/user-keys/{id}/
DELETE /api/plugins/rir-manager/user-keys/{id}/

api_key is write_only. List and retrieve responses omit it entirely.

Example create:

curl -X POST https://netbox.example.com/api/plugins/rir-manager/user-keys/ \
    -H "Authorization: Token <netbox-token>" \
    -H "Content-Type: application/json" \
    -d '{
      "user": 1,
      "rir_config": 1,
      "api_key": "API-XXXX-XXXX-XXXX-XXXX"
    }'

Filtering

The list view filters by user and rir_config_id. Search matches user.username.

Permissions

  • Managing keys uses the standard RIRUserKey permissions: view_riruserkey to list and view keys, add_riruserkey, change_riruserkey and delete_riruserkey to manage them. The key form lets the editor pick any user; see User keys to limit users to their own keys.
  • Sync and write actions, in the web UI and the REST API, require the add permission on the model they act on (for example add_rirnetwork for Reassign, add_rirconfig for Sync), within that permission's constraints, as well as the user's own key on the config. A key alone is not enough, and a permission without a key sends nothing to the RIR. Through the REST API, the token must also have write access.

See Reference: Permissions for the full list.