Skip to content

Commit 9a83e92

Browse files
authored
[server] delete duplicate auth provider (#17651)
1 parent 2f59294 commit 9a83e92

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

components/gitpod-db/src/auth-provider-entry.spec.db.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ export class AuthProviderEntryDBSpec {
104104
}
105105

106106
@test public async findByOrgId() {
107-
const ap1 = this.authProvider({ id: "1", organizationId: "O1" });
108-
const ap2 = this.authProvider({ id: "2", organizationId: "O1" });
109-
const ap3 = this.authProvider({ id: "3", organizationId: "O2" });
107+
const ap1 = this.authProvider({ id: "1", organizationId: "O1", host: "H1" });
108+
const ap2 = this.authProvider({ id: "2", organizationId: "O1", host: "H2" });
109+
const ap3 = this.authProvider({ id: "3", organizationId: "O2", host: "H1" });
110110

111111
await this.db.storeAuthProvider(ap1, false);
112112
await this.db.storeAuthProvider(ap2, false);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { MigrationInterface, QueryRunner } from "typeorm";
8+
9+
export class AuthProviderUniqueHostName1684328022688 implements MigrationInterface {
10+
public async up(queryRunner: QueryRunner): Promise<void> {
11+
// delete any duplicates
12+
await queryRunner.query(`
13+
DELETE FROM d_b_auth_provider_entry
14+
WHERE id NOT IN (
15+
SELECT id FROM (
16+
SELECT MIN(id) AS id
17+
FROM d_b_auth_provider_entry
18+
GROUP BY host
19+
) AS t
20+
)
21+
`);
22+
// create constraint
23+
await queryRunner.query(`
24+
ALTER TABLE d_b_auth_provider_entry
25+
ADD CONSTRAINT unique_host_by_org UNIQUE (host, organizationId)
26+
`);
27+
}
28+
29+
public async down(queryRunner: QueryRunner): Promise<void> {}
30+
}

components/server/src/auth/auth-provider-service.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,7 @@ export class AuthProviderService {
3333
*/
3434
async getAllAuthProviders(exceptOAuthRevisions: string[] = []): Promise<AuthProviderParams[]> {
3535
const all = await this.authProviderDB.findAll(exceptOAuthRevisions);
36-
const transformed = all.map(this.toAuthProviderParams.bind(this));
37-
38-
// as a precaution, let's remove duplicates
39-
const unique = new Map<string, AuthProviderParams>();
40-
for (const current of transformed) {
41-
const duplicate = unique.get(current.host);
42-
if (duplicate) {
43-
log.warn(`Duplicate dynamic Auth Provider detected.`, { rawResult: all, duplicate: current.host });
44-
continue;
45-
}
46-
unique.set(current.host, current);
47-
}
48-
return Array.from(unique.values());
36+
return all.map((provider) => this.toAuthProviderParams(provider));
4937
}
5038

5139
async getAllAuthProviderHosts(): Promise<string[]> {

0 commit comments

Comments
 (0)