Closed awill1988 closed 4 weeks ago
This whole code is unsafe in the presence of multiple hydra instances. We would need to change the database code to do an INSERT INTO ... ON UPDATE DO NOTHING RETURNING *
to properly synchronize.
This whole code is unsafe in the presence of multiple hydra instances. We would need to change the database code to do an
INSERT INTO ... ON UPDATE DO NOTHING RETURNING *
to properly synchronize.
While this is true, it doesn't really matter, because the whole thing will eventually settle. Even if we created 20 keys, it will be like if we rotated through 20 keys and just use the newest key. So the impact is really minimal in my view
That is:
SELECT * FROM HYDRA_JWK
INSERT INTO hydra_jwk ON CONFLICT DO NOTHING
SELECT * FROM hydra jwk
Postgres and other DBs which support RETURNING
could do 4+5 in one step as an optimization (optional, since this is a rare event).
I think that analysis is correct. So all we need really is a lock for when we generate the key and not for reading, which also solves the issue here
closing in favor of #3866, 🙏🏻 for taking over this issue @aeneasr
Update the locking behavior of
jwk.GetOrGenerateKeys
to acquire a read lock to fetch keys and only acquire the write lock when generating and persisting new keys.Related issue(s)
3863
Checklist
Further Comments
I can attempt to write up more unit tests if that is required, but since this is not a significant change I am wondering if the existing tests can cover it?
I am also not sure what would need to be written for documentation but I am happy to add it where it is preferred.
On the related issue there are some reproducible steps that can be used to demonstrate the benefit of this refactor, but I can specify steps or update here if needed.