Skip to content

Commit 6f318a7

Browse files
committed
Update d_b_identity
1 parent 6273c09 commit 6f318a7

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

components/gitpod-db/src/typeorm/migration/1592203031938-Baseline.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,27 @@
55
*/
66

77
import { MigrationInterface, QueryRunner } from "typeorm";
8+
import * as crypto from "crypto";
9+
import { v4 as uuidv4 } from 'uuid';
810

911
import { BUILTIN_WORKSPACE_PROBE_USER_ID } from "../../user-db";
1012

13+
// From encryption-key.json
14+
const encryption_key = "4uGh1q8y2DYryJwrVMHs0kWXJlqvHWWt/KJuNi04edI=";
15+
16+
function encrypt(data: string, key: Buffer) {
17+
const iv = crypto.randomBytes(16);
18+
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
19+
const encrypted = cipher.update(new Buffer(data, 'utf8'));
20+
const finalEncrypted = Buffer.concat([encrypted, cipher.final()]);
21+
return {
22+
data: finalEncrypted.toString('base64'),
23+
keyParams: {
24+
iv: iv.toString('base64')
25+
}
26+
};
27+
}
28+
1129
export class Baseline1592203031938 implements MigrationInterface {
1230

1331
public async up(queryRunner: QueryRunner): Promise<any> {
@@ -85,7 +103,25 @@ export class Baseline1592203031938 implements MigrationInterface {
85103
}
86104
const existsIdentity = (await queryRunner.query(`SELECT COUNT(1) AS cnt FROM d_b_identity WHERE userId = 'builtin-user-workspace-probe-0000000'`))[0].cnt == 1;
87105
if (!existsIdentity) {
88-
await queryRunner.query(`INSERT IGNORE INTO d_b_identity (authProviderId, authId, authName, userId) VALUES ('Public-GitHub', '12345678', 'builtin-workspace-prober', 'builtin-user-workspace-probe-0000000')`)
106+
await queryRunner.query(`INSERT IGNORE INTO d_b_identity (authProviderId, authId, authName, userId, tokens) VALUES ('Public-GitHub', '12345678', 'builtin-workspace-prober', 'builtin-user-workspace-probe-0000000', '[]')`)
107+
}
108+
const existsToken = (await queryRunner.query(`SELECT COUNT(1) AS cnt FROM d_b_token_entry WHERE authId = '12345678'`))[0].cnt == 1;
109+
if (!existsToken) {
110+
const encriptedData = encrypt(
111+
JSON.stringify(JSON.stringify({
112+
value: '',
113+
scopes: []
114+
})),
115+
new Buffer(encryption_key, 'base64')
116+
);
117+
const token = JSON.stringify({
118+
...encriptedData,
119+
keyMetadata: {
120+
name: "general",
121+
version: 1
122+
}
123+
});
124+
await queryRunner.query(`INSERT IGNORE INTO d_b_token_entry (authProviderId, authId, token, uid) VALUES ('Public-GitHub', '12345678', '${token}', '${uuidv4()}')`)
89125
}
90126
}
91127
}
@@ -94,4 +130,4 @@ export class Baseline1592203031938 implements MigrationInterface {
94130
// this is a one-way idempotent 'migration', no rollback possible for a nonempty DB
95131
}
96132

97-
}
133+
}

0 commit comments

Comments
 (0)