Skip to content

[server] delete duplicate auth provider #17651

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions components/gitpod-db/src/auth-provider-entry.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export class AuthProviderEntryDBSpec {
}

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

await this.db.storeAuthProvider(ap1, false);
await this.db.storeAuthProvider(ap2, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/

import { MigrationInterface, QueryRunner } from "typeorm";

export class AuthProviderUniqueHostName1684328022688 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
// delete any duplicates
await queryRunner.query(`
DELETE FROM d_b_auth_provider_entry
WHERE id NOT IN (
SELECT id FROM (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the middle SELECT id FROM (...) AS t seems to be redundant.
anyways, TIL how to delete duplicates 👍🏻

SELECT MIN(id) AS id
FROM d_b_auth_provider_entry
GROUP BY host
) AS t
)
`);
// create constraint
await queryRunner.query(`
ALTER TABLE d_b_auth_provider_entry
ADD CONSTRAINT unique_host_by_org UNIQUE (host, organizationId)
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {}
}
14 changes: 1 addition & 13 deletions components/server/src/auth/auth-provider-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,7 @@ export class AuthProviderService {
*/
async getAllAuthProviders(exceptOAuthRevisions: string[] = []): Promise<AuthProviderParams[]> {
const all = await this.authProviderDB.findAll(exceptOAuthRevisions);
const transformed = all.map(this.toAuthProviderParams.bind(this));

// as a precaution, let's remove duplicates
const unique = new Map<string, AuthProviderParams>();
for (const current of transformed) {
const duplicate = unique.get(current.host);
if (duplicate) {
log.warn(`Duplicate dynamic Auth Provider detected.`, { rawResult: all, duplicate: current.host });
continue;
}
unique.set(current.host, current);
}
return Array.from(unique.values());
return all.map((provider) => this.toAuthProviderParams(provider));
}

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