openwisp-controller icon indicating copy to clipboard operation
openwisp-controller copied to clipboard

[bug] Changing org shared_secret prevents devices from re-registering after losing stored credentials

Open asmodehn opened this issue 5 months ago • 12 comments

Describe the bug

Changing an organization's shared_secret does not affect already-registered devices (they have UUID+KEY stored locally). However, if a device subsequently loses its stored UUID+KEY (factory reset, reflash, manual config clear), it becomes permanently unable to re-register.

The agent computes key = md5(mac_address + "+" + shared_secret) using the new secret; the server looks up by key and doesn't find it (the stored key used the old secret); it falls back to creating a new device, which fails on unique_together constraints for (mac_address, organization).

There is no fallback matching by (mac_address, organization) when key lookup fails, and no mechanism to update existing device keys when shared_secret changes.

Root cause

  1. Deterministic key — generate_key(): md5("{}+{}".format(mac_address, shared_secret)). Changing shared_secret produces a different key for the same mac address.

  2. Key set once at creation — save(): only generates key if self.key is empty; never updates it later.

  3. Lookup by key only — DeviceRegisterView.post(): Device.objects.get(key=key) on line 402. If key doesn't match, falls to init_object() (line 421) which creates a new device — which then fails on unique_together.

  4. Unique constraints — unique_together = (mac_address, organization), (hardware_id, organization), plus _validate_unique_name() for (name, organization).

  5. Agent erases shared_secret — openwisp.agent: computes key ~line 195, then post-registration (~lines 294-298) stores UUID+KEY and clears shared_secret. On restarts, if UUID+KEY already exist, registration is skipped (~line 308).

  6. Default settings: CONSISTENT_REGISTRATION=True, REGISTRATION_SELF_CREATION=True, DEVICE_NAME_UNIQUE=True, HARDWARE_ID_ENABLED=False.

Steps To Reproduce

  1. Register a device normally (e.g. mac_address=AA:BB:CC:DD:EE:FF, org shared_secret="old_secret"). Server stores key = md5("AA:BB:CC:DD:EE:FF+old_secret").
  2. Change the org's shared_secret to "new_secret" in the admin interface (Organization → Config Settings).
  3. On the device, clear the stored UUID and KEY and restore shared_secret:
    uci set openwisp.http.uuid=""
    uci set openwisp.http.key=""
    uci set openwisp.http.shared_secret="new_secret"
    uci commit openwisp
    /etc/init.d/openwisp restart
    
    This simulates a device that lost its credentials (e.g. after reflash) while using the current org shared_secret.
  4. The agent boots and attempts registration:
    • Computes key = md5("AA:BB:CC:DD:EE:FF+new_secret") — different from the stored key.
    • Sends POST to /controller/register/ with the new key.
    • Server does Device.objects.get(key=...) → DoesNotExist.
    • Server tries to create a new device with same mac_address, name, and organization.
    • Fails with Device with this Name and Organization already exists. or config device with this Mac address and Organization already exists.

Expected behavior

One of:

  1. When key lookup fails during re-registration, the server should fall back to matching by (mac_address, organization). If a device with matching mac and org is found, the server should update the existing device's key to the newly computed one and proceed with registration — rather than attempting to create a duplicate and failing.

  2. The server should update all existing device keys in the organization when shared_secret is changed.

  3. At minimum, prevent changing shared_secret in the admin UI when the organization already has registered devices, or show a clear warning about the consequences.

System Information

Applies to base openwisp-controller (no settings overridden: CONSISTENT_REGISTRATION, DEVICE_NAME_UNIQUE, REGISTRATION_SELF_CREATION all at defaults).

asmodehn avatar Jun 12 '26 13:06 asmodehn

We should communicate better that changing the shared secret has consequences. We could also automatically update keys on all devices but it won't be trivial.

I don't think this is a bug. It's more of a UX issue, but the system is working as designed.

nemesifier avatar Jun 12 '26 14:06 nemesifier

@nemesifier Could the key be generated based on the organization uuid instead of the shared_secret ?

md5("{}+{}".format(mac_address, organization_uuid))

In this case the device key would remain static.

cbeaujoin avatar Jun 12 '26 15:06 cbeaujoin

A related issue that I am noticing now: It seems that if :

  • a device is registered for an organization A
  • that device attempts to register to another organisation B (via another shared secret) Then the device will be re-registered on the first organization, not the one with the shared secret provided by the device. Maybe also behaving as intended ?

Anyway I am currently confused... I am wondering what is the process to:

  • keep an old device configuration around
  • register the same device again, fresh.
  • then slowly, manually recover data from the old config, before deleting it.

I am trying to find a process to safely "move" (somehow) devices from one organisation to another (I want to change the shared secret), but it doesn't seem straight forward. Lots of things are attached to an organisation.

asmodehn avatar Jun 12 '26 16:06 asmodehn

@asmodehn pratical examples.

Scenario A:

  1. Register a device which ships in its firmware the shared secret of organization OpenWISP
  2. Move the device to organization Stellar
  3. Perform factory-reset of the device, which ships the shared secret of OpenWISP
  4. The device will regenerate its key and since the existing object is related to Stellar, it will continue reusing the device object assigned to Stellar

Scenario B:

If after step 3 you delete the device object, at step 4 the device will register again with OpenWISP (it creates a new device because the key does not exist).

If we regenerate and change the keys of existing devices when the organization is changed, only scenario B remains possible.

nemesifier avatar Jun 12 '26 17:06 nemesifier

@nemesifier thank you for the pratical examples.

We are aware that this would be a major effort and that it is not the priority. But is it conceivable to redesign/improve that part of the system in the future ?

cbeaujoin avatar Jun 15 '26 12:06 cbeaujoin

Our current usecase (slightly different from these two scenarios, since we produce the firmware builds ourselves) is the following:

  • We have a bunch of devices registered on one organization A
  • that organization A shared secret has been compromised/shared too broadly -> what can we do ?

We produce a new firmware build with shared_secret of organization B. Then, since the device key and the related orga determine where the device ends up registering (no matter the shared_secret sent by the device) we can either :

  1. Edit the orga A shared_secret -> fails as described earlier
  2. Create another organization B and move all devices to not lose their configuration. Doable, but quite cumbersome since many related objects (groups, locations, firmwares, etc.) may be linked to the organization A, and need to be deleted, shared system-wide or duplicated into the target organization B.

If we do not move devices however, then re-registering devices will still register to the organization A, unless the previous ones are deleted, and configuration lost. The aim is to eventually get rid of organization A so that shared_secret cannot be used to register a new device.

=> Moving devices from organization A to organization B ( and fixing errors on related objects during the move ) seems to be the right call, to have devices (with updated firmware build) re-register to organization B and not lose any device information. => Having the shared_secret editable seems, to me, more trouble than it is worth...

asmodehn avatar Jun 15 '26 16:06 asmodehn

If the problem is the shared secret only and nothing else, then I would do the following:

  • change the shared secret both on openwisp and in the firmware
  • delete the "key" field on each device and save (will generate a new key from the new shared secret) while simultaneously re-register the device (erase uuid and key from its /etc/config/openwisp, place the new shared secret)
  • mass upgrade all the devices so they have the new shared secret

nemesifier avatar Jun 15 '26 17:06 nemesifier

@nemesifier thank you for the pratical examples.

We are aware that this would be a major effort and that it is not the priority. But is it conceivable to redesign/improve that part of the system in the future ?

@cbeaujoin-stellar I am always eager to improve openwisp.

In this particular case, we can definitely improve the UX part of changing the shared secret as the UI doesn't clearly communicate that changing this aspect has strong impact on the system.

Apart from that, I am not sure we should go and also try to fix everything "auto-magically" because I am not 100% this would work reliably in all cases.

nemesifier avatar Jun 15 '26 19:06 nemesifier

If the problem is the shared secret only and nothing else, then I would do the following:

* change the shared secret both on openwisp and in the firmware

* delete the "key" field on each device and save (will generate a new key from the new shared secret) while simultaneously re-register the device (erase uuid and key from its /etc/config/openwisp, place the new shared secret)

* mass upgrade all the devices so they have the new shared secret

Oh indeed, I can confirm that with this log on the device :

openwisp: Registering device...
openwisp: Registration failed! Device with this Name and Organization already exists. Device with this Mac address and Organization already exists.

Deleting the device key on Openwisp allows the registration to proceed

openwisp: Registering device...
openwisp: Existing device registered successfully as <hostname>, id: <device_id>

It didn't cross my mind that the device key could be deleted safely in the first place...

asmodehn avatar Jun 16 '26 09:06 asmodehn

I went through the entire discussion and had one small suggestion. Would it make sense to add a fallback lookup by (mac_address, organization) when the key lookup fails? If the device is authenticated using the current shared_secret, the server could update the existing device's key and continue registration instead of attempting to create a duplicate device. This would only improve the recovery (factory reset/reflash) scenario without changing the normal registration flow.

sameerrandive-dev avatar Jul 10 '26 18:07 sameerrandive-dev

One thing I noticed with "moving the device to organization B after registration":

When needing to re-register the device with B shared_secret, it fails with:

2026-08-07T09:49:59+00:00 <hostname> openwisp: Registering device...
2026-08-07T09:49:59+00:00 <hostname> openwisp: Registration failed! Device with this Name and Organization already exists. Device with this Mac address and Organization already exists.

I have to use the shared_secret from its original organization (A) (but this might be gone from config and record other than the device key in the DB...)

So anyway, this is another way to resolve https://github.com/openwisp/openwisp-controller/issues/1396#issuecomment-4716872657 which does NOT create a new device...

asmodehn avatar Aug 07 '26 10:08 asmodehn

One thing I noticed with "moving the device to organization B after registration":

When needing to re-register the device with B shared_secret, it fails with:

2026-08-07T09:49:59+00:00 <hostname> openwisp: Registering device...
2026-08-07T09:49:59+00:00 <hostname> openwisp: Registration failed! Device with this Name and Organization already exists. Device with this Mac address and Organization already exists.

I have to use the shared_secret from its original organization (A) (but this might be gone from config and record other than the device key in the DB...)

So anyway, this is another way to resolve #1396 (comment) which does NOT create a new device...

If you want to do that, you'd have to update the key of the device before you reset it with the new shared secret.

Hence, changing the shared secret requires updating the keys of the devices in the DB and in their local configs.

nemesifier avatar Aug 07 '26 13:08 nemesifier