-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[server, db] AuthProviderEntry: Introduce oauthRevision to avoid repe…
…ated materialization of encrypted data
- Loading branch information
Showing
10 changed files
with
241 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,94 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the Gitpod Enterprise Source Code License, | ||
* See License.enterprise.txt in the project root folder. | ||
*/ | ||
|
||
import * as chai from 'chai'; | ||
import { suite, test, timeout } from 'mocha-typescript'; | ||
import { testContainer } from './test-container'; | ||
import { TypeORM } from './typeorm/typeorm'; | ||
import { AuthProviderEntryDB } from '.'; | ||
import { DBAuthProviderEntry } from './typeorm/entity/db-auth-provider-entry'; | ||
import { DeepPartial } from '@gitpod/gitpod-protocol/lib/util/deep-partial'; | ||
const expect = chai.expect; | ||
|
||
@suite @timeout(5000) | ||
export class AuthProviderEntryDBSpec { | ||
|
||
typeORM = testContainer.get<TypeORM>(TypeORM); | ||
db = testContainer.get<AuthProviderEntryDB>(AuthProviderEntryDB); | ||
|
||
async before() { | ||
await this.clear(); | ||
} | ||
|
||
async after() { | ||
await this.clear(); | ||
} | ||
|
||
protected async clear() { | ||
const connection = await this.typeORM.getConnection(); | ||
const manager = connection.manager; | ||
await manager.clear(DBAuthProviderEntry); | ||
} | ||
|
||
protected authProvider(ap: DeepPartial<DBAuthProviderEntry> = {}): DBAuthProviderEntry { | ||
const ownerId = "1234"; | ||
const host = "github.com"; | ||
return { | ||
id: "0049b9d2-005f-43c2-a0ae-76377805d8b8", | ||
host, | ||
ownerId, | ||
status: 'verified', | ||
type: "GitHub", | ||
oauthRevision: undefined, | ||
deleted: false, | ||
...ap, | ||
oauth: { | ||
callBackUrl: "example.org/some/callback", | ||
authorizationUrl: "example.org/some/auth", | ||
settingsUrl: "example.org/settings", | ||
configURL: "example.org/config", | ||
clientId: "clientId", | ||
clientSecret: "clientSecret", | ||
tokenUrl: "example.org/get/token", | ||
scope: "scope", | ||
scopeSeparator: ",", | ||
...ap.oauth, | ||
authorizationParams: {}, | ||
}, | ||
}; | ||
} | ||
|
||
@test public async storeEmtpyOAuthRevision() { | ||
const ap = this.authProvider(); | ||
await this.db.storeAuthProvider(ap, false); | ||
|
||
const aap = await this.db.findByHost(ap.host); | ||
expect(aap, "AuthProvider").to.deep.equal(ap); | ||
} | ||
|
||
@test public async findAll() { | ||
const ap1 = this.authProvider({ id: "1", oauthRevision: "rev1" }); | ||
const ap2 = this.authProvider({ id: "2", oauthRevision: "rev2" }); | ||
await this.db.storeAuthProvider(ap1, false); | ||
await this.db.storeAuthProvider(ap2, false); | ||
|
||
const all = await this.db.findAll(); | ||
expect(all, "findAll([])").to.deep.equal([ap1, ap2]); | ||
expect(await this.db.findAll([ap1.oauthRevision!, ap2.oauthRevision!]), "findAll([ap1, ap2])").to.be.empty; | ||
expect(await this.db.findAll([ap1.oauthRevision!]), "findAll([ap1])").to.deep.equal([ap2]); | ||
} | ||
|
||
@test public async oauthRevision() { | ||
const ap = this.authProvider({ id: "1" }); | ||
await this.db.storeAuthProvider(ap, true); | ||
|
||
const loadedAp = await this.db.findByHost(ap.host); | ||
expect(loadedAp, "findByHost()").to.deep.equal(ap); | ||
expect(loadedAp?.oauthRevision, "findByHost()").to.equal("e05ea6fab8efcaba4b3246c2b2d3931af897c3bc2c1cf075c31614f0954f9840"); | ||
} | ||
} | ||
|
||
module.exports = AuthProviderEntryDBSpec |
This file contains 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
This file contains 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
52 changes: 52 additions & 0 deletions
52
components/gitpod-db/src/typeorm/migration/1643986994402-OAuthRevision.ts
This file contains 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,52 @@ | ||
/** | ||
* Copyright (c) 2021 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 { AuthProviderEntry } from "@gitpod/gitpod-protocol"; | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
import { dbContainerModule } from "../../container-module"; | ||
import { columnExists, indexExists } from "./helper/helper"; | ||
import { Container } from 'inversify'; | ||
import { AuthProviderEntryDB } from "../../auth-provider-entry-db"; | ||
import { UserDB } from "../../user-db"; | ||
|
||
const TABLE_NAME = "d_b_auth_provider_entry"; | ||
const COLUMN_NAME: keyof AuthProviderEntry = "oauthRevision"; | ||
const INDEX_NAME = "ind_oauthRevision"; | ||
|
||
export class OAuthRevision1643986994402 implements MigrationInterface { | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
// create new column | ||
if (!(await columnExists(queryRunner, TABLE_NAME, COLUMN_NAME))) { | ||
await queryRunner.query(`ALTER TABLE ${TABLE_NAME} ADD COLUMN ${COLUMN_NAME} varchar(128) NOT NULL DEFAULT ''`); | ||
} | ||
|
||
// create index on said column | ||
if (!(await indexExists(queryRunner, TABLE_NAME, INDEX_NAME))) { | ||
await queryRunner.query(`CREATE INDEX ${INDEX_NAME} ON ${TABLE_NAME} (${COLUMN_NAME})`); | ||
} | ||
|
||
// to update all oauthRevisions we need to load all providers (to decrypt them) and | ||
// write them back using the DB implementation (which does the calculation for us) | ||
const container = new Container(); | ||
container.load(dbContainerModule); | ||
|
||
container.get<UserDB>(UserDB); // initializes encryptionProvider as side effect | ||
const db = container.get<AuthProviderEntryDB>(AuthProviderEntryDB); | ||
const allProviders = await db.findAll([]); | ||
const writes: Promise<AuthProviderEntry>[] = []; | ||
for (const provider of allProviders) { | ||
writes.push(db.storeAuthProvider(provider, true)); | ||
} | ||
await Promise.all(writes); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE ${TABLE_NAME} DROP INDEX ${INDEX_NAME}`); | ||
await queryRunner.query(`ALTER TABLE ${TABLE_NAME} DROP COLUMN ${COLUMN_NAME}`); | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
Oops, something went wrong.