- Notifications
You must be signed in to change notification settings - Fork1.1k
fix(coderd): ensure that user API keys are deleted when a user is#7270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
19 changes: 18 additions & 1 deletioncoderd/apikey_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletionscoderd/database/dbfake/databasefake.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletionscoderd/database/dump.sql
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
9 changes: 9 additions & 0 deletionscoderd/database/migrations/000120_trigger_delete_user_apikey.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| BEGIN; | ||
| DROP TRIGGER IF EXISTS trigger_update_users ON users; | ||
| DROP FUNCTION IF EXISTS delete_deleted_user_api_keys; | ||
| DROP TRIGGER IF EXISTS trigger_insert_apikeys ON api_keys; | ||
| DROP FUNCTION IF EXISTS insert_apikey_fail_if_user_deleted; | ||
| COMMIT; |
55 changes: 55 additions & 0 deletionscoderd/database/migrations/000120_trigger_delete_user_apikey.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| BEGIN; | ||
| -- We need to delete all existing API keys for soft-deleted users. | ||
| DELETE FROM | ||
| api_keys | ||
| WHERE | ||
| user_id | ||
| IN ( | ||
| SELECT id FROM users WHERE deleted | ||
| ); | ||
| -- When we soft-delete a user, we also want to delete their API key. | ||
| CREATE FUNCTION delete_deleted_user_api_keys() RETURNS trigger | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| BEGIN | ||
| IF (NEW.deleted) THEN | ||
| DELETE FROM api_keys | ||
| WHERE user_id = OLD.id; | ||
| END IF; | ||
| RETURN NEW; | ||
| END; | ||
| $$; | ||
| CREATE TRIGGER trigger_update_users | ||
| AFTER INSERT OR UPDATE ON users | ||
| FOR EACH ROW | ||
| WHEN (NEW.deleted = true) | ||
| EXECUTE PROCEDURE delete_deleted_user_api_keys(); | ||
| -- When we insert a new api key, we want to fail if the user is soft-deleted. | ||
| CREATE FUNCTION insert_apikey_fail_if_user_deleted() RETURNS trigger | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| BEGIN | ||
| IF (NEW.user_id IS NOT NULL) THEN | ||
| IF (SELECT deleted FROM users WHERE id = NEW.user_id LIMIT 1) THEN | ||
| RAISE EXCEPTION 'Cannot create API key for deleted user'; | ||
| END IF; | ||
| END IF; | ||
| RETURN NEW; | ||
| END; | ||
| $$; | ||
| CREATE TRIGGER trigger_insert_apikeys | ||
| BEFORE INSERT ON api_keys | ||
| FOR EACH ROW | ||
| EXECUTE PROCEDURE insert_apikey_fail_if_user_deleted(); | ||
| COMMIT; |
30 changes: 30 additions & 0 deletionscoderd/userauth_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletioncoderd/users_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.