5
5
*/
6
6
7
7
import { MigrationInterface , QueryRunner } from "typeorm" ;
8
+ import * as crypto from "crypto" ;
9
+ import { v4 as uuidv4 } from 'uuid' ;
8
10
9
11
import { BUILTIN_WORKSPACE_PROBE_USER_ID } from "../../user-db" ;
10
12
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
+
11
29
export class Baseline1592203031938 implements MigrationInterface {
12
30
13
31
public async up ( queryRunner : QueryRunner ) : Promise < any > {
@@ -85,7 +103,25 @@ export class Baseline1592203031938 implements MigrationInterface {
85
103
}
86
104
const existsIdentity = ( await queryRunner . query ( `SELECT COUNT(1) AS cnt FROM d_b_identity WHERE userId = 'builtin-user-workspace-probe-0000000'` ) ) [ 0 ] . cnt == 1 ;
87
105
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 ( ) } ')` )
89
125
}
90
126
}
91
127
}
@@ -94,4 +130,4 @@ export class Baseline1592203031938 implements MigrationInterface {
94
130
// this is a one-way idempotent 'migration', no rollback possible for a nonempty DB
95
131
}
96
132
97
- }
133
+ }
0 commit comments