Repository navigation
Simulate Tesla's Owner API, its tokens and the car's sleep - #148
Conversation
Tesla's Owner API answers with the live state: the speed while driving, and the energy the charge put into the battery, kept until the next plug-in. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A scenario's brand may be tesla, and its vehicle a trim (Tesla's trim_badging). StateAt advances the simulation and gives the actual state, not only what was uploaded: what an awake Tesla answers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
teslaapi serves the refresh of the owner's token, rotated on each refresh and the one given then refused, the products, a vehicle's state and its vehicle_data, shaped as a real Model Y's. A car neither driving nor charging falls asleep after its last activity or vehicle_data read, reports itself offline and answers vehicle_data with a 408; reading its state does not keep it awake. A 429, the Owner API closed and a revocation are set on demand under /sim/tesla/. runsten-simulator serves it for the scenarios of brand tesla (scenarios/model3.yaml), and the Tesla client is tested against it over the scenario's day. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Tesla scenario and simulation state internal/simulator/scenario/*, scenarios/model3.yaml |
Scenario validation accepts Tesla and passes vehicle trim into the simulation. Simulation.StateAt returns vehicle state at a requested time. A Model 3 scenario and related tests are added. |
Vehicle speed and charging state internal/simulator/vehicle/vehicle.go, internal/simulator/vehicle/vehicle_test.go |
Vehicle state records driving speed and charge energy. Driving updates speed, and charging records delivered energy until the next plug-in. |
Owner API authentication and endpoints internal/simulator/teslaapi/auth.go, internal/simulator/teslaapi/handler.go, internal/simulator/teslaapi/handler_test.go |
The handler implements token issuance, rotation, and authorization; vehicle and product routes; sleep behavior; and controls for rate limiting, account closure, and token revocation. Tests cover these behaviors and API responses. |
Vehicle-data response rendering internal/simulator/teslaapi/render.go, internal/simulator/teslaapi/vehicle_data.json, internal/simulator/teslaapi/handler_test.go |
The renderer builds vehicle-data responses from simulator state and a JSON template. It populates charge, drive, configuration, climate, and timestamp fields, including odometer and range conversions. |
Command wiring and simulator validation cmd/runsten-simulator/*, internal/tesla/simulator_test.go, internal/archtest/deps_test.go, .golangci.yml, AGENTS.md |
The command routes Tesla scenarios to the Tesla handler and reads Tesla sleep and token-lifetime settings. Fleet and end-to-end tests, package dependency rules, and simulator documentation are updated. |
Possibly related issues
- Issue 131 includes a simulator checklist that this change addresses; its broader collector, schema, and real-car work remains outside this change.
Possibly related PRs
- runsten-app/runsten#137: This change tests the
internal/teslaclient against the new simulator.
Priority:
Merge Risk: 
Minimal · up to 14364
-
No concrete blocking issue remains; the Tesla simulator is ready to merge after normal checks.
Security architecture risk: 
Low · up to 14364
-
The change is confined to a development simulator. A refresh-token revocation inconsistency after restart can weaken authentication-recovery testing. The new fault controls also rely on deployment isolation rather than caller authentication; that isolation is not established by the available evidence.
Retained concerns
- Low · reliability · inferred: Refresh-token revocation is inconsistent after recovery. Issue an unused token at generation 1, restart the simulator, then revoke once: the handler returns to generation 1 and accepts that pre-restart token because validation rejects only lower generations. Restart deliberately resets token-consumption state, but a later revoke still promises to refuse previously issued refresh tokens. This weakens the simulator's authentication-failure contract and can mask client recovery defects; it does not establish access to real Tesla accounts.
Security review details
-
Security Blast Radius
- inferred — A caller able to reach the Tesla control routes can mint synthetic refresh tokens, revoke refresh credentials, or change faults for every Tesla vehicle assigned to that handler. The inspected wiring bounds these effects to one simulated account; it does not supply real-car credentials or command authority.
Trust Boundaries and Controls
- observed — Bearer middleware protects the Owner API submux, but not simulator controls. Tests assert rejection of missing, forged, wrong-kind, and expired access tokens. These are simulated protocol controls, not a production credential boundary: development token generation and its signing key are intentionally available in source.
Resilience and Maintainability Implications
- observed — Fault controls provide explicit recovery operations: a zero duration lifts rate limiting, closed=false reopens the account, and refresh-token issues a replacement credential after revocation. These operations mutate shared handler state under its mutex, but that state is not durable.
Hardening Proposals
- proposed — For shared or remotely reachable test environments, isolate the simulator listener or separately authorize the mutable control routes. This is a deployment-hardening proposal, not evidence of an exposed production service.
Pre-merge checks: 
4
-
Full details: Description check
-
Explanation
The description explains the purpose, implementation scope, known limits, linked issue, and test results. It is mostly complete and covers the required information, although it uses
task unit-goinstead of the template's exacttask unitcheckbox format.
Full details: Linked Issues check
-
Explanation
The PR implements the coding requirements in
#147.internal/simulator/teslaapiprovides token refresh and rotation, signed access-token validation, products, vehicle, andvehicle_dataendpoints. It returns 408 while a parked car sleeps, keeps the car awake on successfulvehicle_datareads, does not wake it with vehicle-state reads, and wakes it during driving or charging. The control API provides 429 withRetry-After, Owner API closed 403, refresh-token revocation, and refresh-token issuance.scenarios/model3.yamldefines abrand: teslaModel 3 drive and AC charge.internal/tesla/simulator_test.goexercises the scenario, sleep, charging, token rotation and expiry, and required faults. The simulator command serves the Tesla API with configurable sleep and token TTL.
Full details: Out of Scope Changes check
-
Explanation
The changes remain within
#147. Scenario-model extensions support Tesla state and charging data. Vehicle speed and charge-energy fields support the simulator responses. Command wiring, dependency rules, documentation, and tests support serving and validating the simulator. No unrelated product or web-interface changes are shown.
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment @coderabbitai help to get the list of available commands.
The simulator now serves Tesla's Owner API, so the collector's Tesla step (the sleep window, the state-conditioned read, the token's rotation) can be built and tested end to end. Its tokens rotate and its cars fall asleep the way a real Tesla's do, without anyone needing a car.
Changes
internal/simulator/teslaapi): the refresh of the owner's token (client_id=ownerapi, form or JSON);products;vehicles/{id};vehicle_data, filled from a real Model Y response (anonymised) with the live state: miles,charging_state,shift_state, speed, power,charge_energy_added, AC phases, DC fast charger. Errors: 401invalid bearer token, 404not_found.invalid_grant). This is the strictest assumption, so a client that keeps the old token gets caught. Tokens are signed, so they survive a restart of the simulator.SIM_TESLA_SLEEP_AFTERafter its last activity or its lastvehicle_dataread (10 min by default; the real delay is unknown). Readingvehicles/{id}does not keep it awake. Asleep, it reports itselfoffline, as observed, andvehicle_dataanswers 408./sim/tesla/:rate-limit?for=(429 withRetry-After),owner-api?closed=(the 403 that points to the Fleet API),revoke, andrefresh-token(a new token, as Tesla Auth gives one).brand: teslaandtrim;StateAtfor the live state;scenarios/model3.yaml(a Model 3 driving home to work and back, then an AC charge at 11 kW).runsten-simulatorserves it (SIM_TESLA_SLEEP_AFTER,SIM_TESLA_TOKEN_TTL). The Tesla client is tested against it over the scenario's day: refresh and rotation, sleep and 408, the drive, the charge, 429, 403, revocation, expiry.AGENTS.mdupdated.Known limits: the position stays at the start of a trip until the car arrives (the vehicle model does not move it while driving). The sleep delay and the shape of a 429 are assumptions, to check on a real car.
No screenshots: nothing changes in the web interface.
Tests
task unit-go: format, lint (0 issues), unit tests, govulncheck and build, all passed.task unit-webnot run: nothing changed inweb/.productsandvehicle_dataanswer as expected.Closes #147
🤖 Generated with Claude Code
Summary by CodeRabbit