netbox-docker-plugin icon indicating copy to clipboard operation
netbox-docker-plugin copied to clipboard

:bug: Fix race condition in Container API

Open linkdd opened this issue 1 year ago • 1 comments

Decision Record

When creating or updating a container via the API, here's what happens:

sequenceDiagram
    actor Client
    participant PluginAPI
    participant Netbox
    participant Agent

    Client ->>PluginAPI: PATCH with operation=recreate, binds=[...]
    PluginAPI->>Netbox: update Container
    Netbox->>Agent: Webhook Call
    PluginAPI->>Netbox: update binds, ...
    Agent->>Netbox: write back binds=[]

Because the creation/update is not in an atomic transaction, the Agent writes back to Netbox potentially invalid data.

To alleviate this, the Client needs to first PATCH with the data for Netbox, then PATCH operation=recreate (just as you would do in the Netbox UI).

We can skip that part if we use Django transactions.

Changes

  • [x] :bug: Use transaction.atomic() for container creation/update in API
  • [ ] :white_check_mark: Add test suite

linkdd avatar May 10 '24 11:05 linkdd

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
2177 2143 98% 90%

New Files

No new covered files...

Modified Files

No covered modified files...

updated for commit: c34d54d by action🐍

github-actions[bot] avatar May 10 '24 11:05 github-actions[bot]