From e2b787c056b9cf235a3831532ba877cc9580b9db Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:22:00 -0600 Subject: [PATCH 01/15] chore: add database test fixture to insert non-unique linked_ids --- .../fixtures/000189_workspace_app_order.up.sql | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql new file mode 100644 index 0000000000000..3e41155957fe7 --- /dev/null +++ b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql @@ -0,0 +1,14 @@ +-- This is a deleted user that shares the same username and linked_id as the existing user below. +-- Any future migrations need to handle this case. +INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) + VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', true) ON CONFLICT DO NOTHING; +INSERT INTO public.organization_members VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; +INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) + VALUES('a0061a8e-7db7-4585-838c-3116a003dd21', 'github', '100', ''); + + +INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) + VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING; +INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; +INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) + VALUES('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'github', '100', ''); From 43356eda79773526a86cf7db261f8a386c08a653 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:23:18 -0600 Subject: [PATCH 02/15] mend --- .../testdata/fixtures/000189_workspace_app_order.up.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql index 3e41155957fe7..d3fd503b4b79d 100644 --- a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql +++ b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql @@ -1,13 +1,13 @@ -- This is a deleted user that shares the same username and linked_id as the existing user below. -- Any future migrations need to handle this case. -INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', true) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) VALUES('a0061a8e-7db7-4585-838c-3116a003dd21', 'github', '100', ''); -INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) From 2791347d65301f1929b574450da0e132e9721c6a Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:25:48 -0600 Subject: [PATCH 03/15] mend --- coderd/database/migrations/migrate_test.go | 2 +- .../testdata/fixtures/000189_workspace_app_order.up.sql | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/coderd/database/migrations/migrate_test.go b/coderd/database/migrations/migrate_test.go index c475c1fa5f026..a01ee8e7f3ef3 100644 --- a/coderd/database/migrations/migrate_test.go +++ b/coderd/database/migrations/migrate_test.go @@ -352,7 +352,7 @@ func TestMigrateUpWithFixtures(t *testing.T) { for _, table := range tables { var count int - err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM "+table).Scan(&count) + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM"+table).Scan(&count) require.NoError(t, err) if tt.useStats { diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql index d3fd503b4b79d..5424c294bd221 100644 --- a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql +++ b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql @@ -1,13 +1,13 @@ -- This is a deleted user that shares the same username and linked_id as the existing user below. -- Any future migrations need to handle this case. -INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, deleted) VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', true) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) VALUES('a0061a8e-7db7-4585-838c-3116a003dd21', 'github', '100', ''); -INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, deleted) VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) From 549fee96a5ef68186356fffafdad8fe6fcac7c7c Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:28:41 -0600 Subject: [PATCH 04/15] rename test fixute --- ...kspace_app_order.up.sql => 000189_duplicate_user_links.up.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename coderd/database/migrations/testdata/fixtures/{000189_workspace_app_order.up.sql => 000189_duplicate_user_links.up.sql} (100%) diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql similarity index 100% rename from coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql rename to coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql From 7b082eff683d48b0fa6c77eef7a748223f40fd45 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:30:01 -0600 Subject: [PATCH 05/15] add extra case --- .../testdata/fixtures/000189_duplicate_user_links.up.sql | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql b/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql index 5424c294bd221..0fb1d0efd4aca 100644 --- a/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql +++ b/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql @@ -12,3 +12,8 @@ INSERT INTO public.users(id, email, username, hashed_password, created_at, updat INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) VALUES('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'github', '100', ''); + +-- Additionally, there is no unique constraint on user_id. So also add another user_link for the same user. +-- This has happened on a production database. +INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) +VALUES('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'oidc', 'foo', ''); From 90e7164c2f0d630081b4404a5bbaaac66a4bcb52 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:33:01 -0600 Subject: [PATCH 06/15] accidental file change --- coderd/database/migrations/migrate_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/database/migrations/migrate_test.go b/coderd/database/migrations/migrate_test.go index a01ee8e7f3ef3..c475c1fa5f026 100644 --- a/coderd/database/migrations/migrate_test.go +++ b/coderd/database/migrations/migrate_test.go @@ -352,7 +352,7 @@ func TestMigrateUpWithFixtures(t *testing.T) { for _, table := range tables { var count int - err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM"+table).Scan(&count) + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM "+table).Scan(&count) require.NoError(t, err) if tt.useStats { From fdf04ed29008e7ce8042b931e8f20db1b7b05d9b Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 13 Feb 2024 11:06:10 -0600 Subject: [PATCH 07/15] move migration closer to source Added to the migration that added the delete field to users --- ...00189_duplicate_user_links.up.sql => 000048_userdelete.up.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename coderd/database/migrations/testdata/fixtures/{000189_duplicate_user_links.up.sql => 000048_userdelete.up.sql} (100%) diff --git a/coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql b/coderd/database/migrations/testdata/fixtures/000048_userdelete.up.sql similarity index 100% rename from coderd/database/migrations/testdata/fixtures/000189_duplicate_user_links.up.sql rename to coderd/database/migrations/testdata/fixtures/000048_userdelete.up.sql From b1e066e83654c9715342b3def9b90961fc6a4938 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:22:00 -0600 Subject: [PATCH 08/15] chore: add database test fixture to insert non-unique linked_ids --- .../fixtures/000189_workspace_app_order.up.sql | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql new file mode 100644 index 0000000000000..3e41155957fe7 --- /dev/null +++ b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql @@ -0,0 +1,14 @@ +-- This is a deleted user that shares the same username and linked_id as the existing user below. +-- Any future migrations need to handle this case. +INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) + VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', true) ON CONFLICT DO NOTHING; +INSERT INTO public.organization_members VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; +INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) + VALUES('a0061a8e-7db7-4585-838c-3116a003dd21', 'github', '100', ''); + + +INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) + VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING; +INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; +INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) + VALUES('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'github', '100', ''); From cce73c60c0ea2815dcc6fdb706157e8c7cc84fc0 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:23:18 -0600 Subject: [PATCH 09/15] mend --- .../testdata/fixtures/000189_workspace_app_order.up.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql index 3e41155957fe7..d3fd503b4b79d 100644 --- a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql +++ b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql @@ -1,13 +1,13 @@ -- This is a deleted user that shares the same username and linked_id as the existing user below. -- Any future migrations need to handle this case. -INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', true) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) VALUES('a0061a8e-7db7-4585-838c-3116a003dd21', 'github', '100', ''); -INSERT INTO public.users(id, email, username, password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) From c0dc8a636980643caa3e9d6ce3e0706d2730c60d Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:25:48 -0600 Subject: [PATCH 10/15] mend --- coderd/database/migrations/migrate_test.go | 2 +- .../testdata/fixtures/000189_workspace_app_order.up.sql | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/coderd/database/migrations/migrate_test.go b/coderd/database/migrations/migrate_test.go index c475c1fa5f026..a01ee8e7f3ef3 100644 --- a/coderd/database/migrations/migrate_test.go +++ b/coderd/database/migrations/migrate_test.go @@ -352,7 +352,7 @@ func TestMigrateUpWithFixtures(t *testing.T) { for _, table := range tables { var count int - err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM "+table).Scan(&count) + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM"+table).Scan(&count) require.NoError(t, err) if tt.useStats { diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql index d3fd503b4b79d..5424c294bd221 100644 --- a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql +++ b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql @@ -1,13 +1,13 @@ -- This is a deleted user that shares the same username and linked_id as the existing user below. -- Any future migrations need to handle this case. -INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, deleted) VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', true) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) VALUES('a0061a8e-7db7-4585-838c-3116a003dd21', 'github', '100', ''); -INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, is_deleted) +INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, deleted) VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING; INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) From 53a37ee6b2c6db62901f0e80cc8345540b925979 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:28:41 -0600 Subject: [PATCH 11/15] rename test fixute --- .../fixtures/000189_workspace_app_order.up.sql | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql diff --git a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql b/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql deleted file mode 100644 index 5424c294bd221..0000000000000 --- a/coderd/database/migrations/testdata/fixtures/000189_workspace_app_order.up.sql +++ /dev/null @@ -1,14 +0,0 @@ --- This is a deleted user that shares the same username and linked_id as the existing user below. --- Any future migrations need to handle this case. -INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, deleted) - VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', true) ON CONFLICT DO NOTHING; -INSERT INTO public.organization_members VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; -INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) - VALUES('a0061a8e-7db7-4585-838c-3116a003dd21', 'github', '100', ''); - - -INSERT INTO public.users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, deleted) - VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING; -INSERT INTO public.organization_members VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'bb640d07-ca8a-4869-b6bc-ae61ebb2fda1', '2022-11-02 13:05:21.447595+02', '2022-11-02 13:05:21.447595+02', '{}') ON CONFLICT DO NOTHING; -INSERT INTO public.user_links(user_id, login_type, linked_id, oauth_access_token) - VALUES('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'github', '100', ''); From c751ccff42b1b6c9dea52fa6ab069f88e8643b1a Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:33:01 -0600 Subject: [PATCH 12/15] accidental file change --- coderd/database/migrations/migrate_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/database/migrations/migrate_test.go b/coderd/database/migrations/migrate_test.go index a01ee8e7f3ef3..c475c1fa5f026 100644 --- a/coderd/database/migrations/migrate_test.go +++ b/coderd/database/migrations/migrate_test.go @@ -352,7 +352,7 @@ func TestMigrateUpWithFixtures(t *testing.T) { for _, table := range tables { var count int - err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM"+table).Scan(&count) + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM "+table).Scan(&count) require.NoError(t, err) if tt.useStats { From 5ee1017039d5a8fa6b3c93218d4e2fd03e906d6f Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:42:47 -0600 Subject: [PATCH 13/15] chore: creaet unit test to exercise failed email change bug Changing emails on github fails if another deleted user exists with the same link. --- coderd/userauth_test.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/coderd/userauth_test.go b/coderd/userauth_test.go index e502b7b4cc780..6c3346d05d4c2 100644 --- a/coderd/userauth_test.go +++ b/coderd/userauth_test.go @@ -603,6 +603,8 @@ func TestUserOAuth2Github(t *testing.T) { require.Equal(t, http.StatusUnauthorized, resp.StatusCode) }) + + // The bug only is exercised when a deleted user with the same linked_id exists. t.Run("ChangedEmail", func(t *testing.T) { t.Parallel() @@ -627,7 +629,7 @@ func TestUserOAuth2Github(t *testing.T) { coderEmail, } - client := coderdtest.New(t, &coderdtest.Options{ + owner := coderdtest.New(t, &coderdtest.Options{ Auditor: auditor, GithubOAuth2Config: &coderd.GithubOAuth2Config{ AllowSignups: true, @@ -650,9 +652,19 @@ func TestUserOAuth2Github(t *testing.T) { }, }, }) + coderdtest.CreateFirstUser(t, owner) + + ctx := testutil.Context(t, testutil.WaitLong) + // Create the user, then delete the user, then create again. + // This causes the email change to fail. + client := codersdk.New(owner.URL) - ctx := testutil.Context(t, testutil.WaitMedium) - // This should register the user + client, _ = fake.Login(t, client, jwt.MapClaims{}) + deleted, err := client.User(ctx, "me") + err = owner.DeleteUser(ctx, deleted.ID) + require.NoError(t, err) + + // Create the user again. client, _ = fake.Login(t, client, jwt.MapClaims{}) user, err := client.User(ctx, "me") require.NoError(t, err) @@ -666,7 +678,8 @@ func TestUserOAuth2Github(t *testing.T) { client, _ = fake.Login(t, client, jwt.MapClaims{}) user, err = client.User(ctx, "me") require.NoError(t, err) - require.Equal(t, user.ID, userID) + + require.Equal(t, user.ID, userID, "user_id is different, a new user was likely created") require.Equal(t, user.Email, *gmailEmail.Email) // Entirely change emails. @@ -681,7 +694,8 @@ func TestUserOAuth2Github(t *testing.T) { client, _ = fake.Login(t, client, jwt.MapClaims{}) user, err = client.User(ctx, "me") require.NoError(t, err) - require.Equal(t, user.ID, userID) + + require.Equal(t, user.ID, userID, "user_id is different, a new user was likely created") require.Equal(t, user.Email, newEmail) }) } From f14d37f40f268f85a65c9806fb78d5ddeb66d1b6 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 12 Feb 2024 15:52:39 -0600 Subject: [PATCH 14/15] fix: do not use user_link for deleted accounts --- coderd/database/dbmem/dbmem.go | 4 ++++ coderd/database/queries.sql.go | 6 +++++- coderd/database/queries/user_links.sql | 8 ++++++-- coderd/userauth_test.go | 2 ++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/coderd/database/dbmem/dbmem.go b/coderd/database/dbmem/dbmem.go index a124dad513937..0e77bacd3b216 100644 --- a/coderd/database/dbmem/dbmem.go +++ b/coderd/database/dbmem/dbmem.go @@ -3787,6 +3787,10 @@ func (q *FakeQuerier) GetUserLinkByLinkedID(_ context.Context, id string) (datab defer q.mutex.RUnlock() for _, link := range q.userLinks { + user, err := q.getUserByIDNoLock(link.UserID) + if err == nil && user.Deleted { + continue + } if link.LinkedID == id { return link, nil } diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index c4ba338c7fe52..f2ef7b697ed00 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -7088,11 +7088,15 @@ func (q *sqlQuerier) InsertTemplateVersionVariable(ctx context.Context, arg Inse const getUserLinkByLinkedID = `-- name: GetUserLinkByLinkedID :one SELECT - user_id, login_type, linked_id, oauth_access_token, oauth_refresh_token, oauth_expiry, oauth_access_token_key_id, oauth_refresh_token_key_id, debug_context + user_links.user_id, user_links.login_type, user_links.linked_id, user_links.oauth_access_token, user_links.oauth_refresh_token, user_links.oauth_expiry, user_links.oauth_access_token_key_id, user_links.oauth_refresh_token_key_id, user_links.debug_context FROM user_links +INNER JOIN + users ON user_links.user_id = users.id WHERE linked_id = $1 + AND + deleted = false ` func (q *sqlQuerier) GetUserLinkByLinkedID(ctx context.Context, linkedID string) (UserLink, error) { diff --git a/coderd/database/queries/user_links.sql b/coderd/database/queries/user_links.sql index b2bfc7d593cbc..9fc0e6f9d7598 100644 --- a/coderd/database/queries/user_links.sql +++ b/coderd/database/queries/user_links.sql @@ -1,10 +1,14 @@ -- name: GetUserLinkByLinkedID :one SELECT - * + user_links.* FROM user_links +INNER JOIN + users ON user_links.user_id = users.id WHERE - linked_id = $1; + linked_id = $1 + AND + deleted = false; -- name: GetUserLinkByUserIDLoginType :one SELECT diff --git a/coderd/userauth_test.go b/coderd/userauth_test.go index 6c3346d05d4c2..ca6c2fb3e9c38 100644 --- a/coderd/userauth_test.go +++ b/coderd/userauth_test.go @@ -661,6 +661,8 @@ func TestUserOAuth2Github(t *testing.T) { client, _ = fake.Login(t, client, jwt.MapClaims{}) deleted, err := client.User(ctx, "me") + require.NoError(t, err) + err = owner.DeleteUser(ctx, deleted.ID) require.NoError(t, err) From b0c3cbb1ece35ab2e9eaaec7173516526080a641 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 13 Feb 2024 07:49:38 -0600 Subject: [PATCH 15/15] Add links to existing issues --- coderd/userauth_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/coderd/userauth_test.go b/coderd/userauth_test.go index ca6c2fb3e9c38..f4e12cec8b2cb 100644 --- a/coderd/userauth_test.go +++ b/coderd/userauth_test.go @@ -605,6 +605,9 @@ func TestUserOAuth2Github(t *testing.T) { }) // The bug only is exercised when a deleted user with the same linked_id exists. + // Still related open issues: + // - https://github.com/coder/coder/issues/12116 + // - https://github.com/coder/coder/issues/12115 t.Run("ChangedEmail", func(t *testing.T) { t.Parallel()