@@ -36,7 +36,6 @@ import {
36
36
StartWorkspaceResult ,
37
37
Token ,
38
38
User ,
39
- UserEnvVar ,
40
39
UserEnvVarValue ,
41
40
UserInfo ,
42
41
WhitelistedRepository ,
@@ -194,6 +193,7 @@ import { UsageService } from "../orgs/usage-service";
194
193
import { UserService } from "../user/user-service" ;
195
194
import { WorkspaceService } from "./workspace-service" ;
196
195
import { SSHKeyService } from "../user/sshkey-service" ;
196
+ import { EnvVarService } from "../user/env-var-service" ;
197
197
198
198
// shortcut
199
199
export const traceWI = ( ctx : TraceContext , wi : Omit < LogContext , "userId" > ) => TraceContext . setOWI ( ctx , wi ) ; // userId is already taken care of in WebsocketConnectionManager
@@ -237,6 +237,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
237
237
@inject ( IAnalyticsWriter ) private readonly analytics : IAnalyticsWriter ,
238
238
@inject ( AuthorizationService ) private readonly authorizationService : AuthorizationService ,
239
239
@inject ( SSHKeyService ) private readonly sshKeyservice : SSHKeyService ,
240
+ @inject ( EnvVarService ) private readonly envVarService : EnvVarService ,
240
241
241
242
@inject ( TeamDB ) private readonly teamDB : TeamDB ,
242
243
@inject ( OrganizationService ) private readonly organizationService : OrganizationService ,
@@ -2344,104 +2345,23 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
2344
2345
// Get all environment variables (unfiltered)
2345
2346
async getAllEnvVars ( ctx : TraceContext ) : Promise < UserEnvVarValue [ ] > {
2346
2347
const user = await this . checkUser ( "getAllEnvVars" ) ;
2347
- const result : UserEnvVarValue [ ] = [ ] ;
2348
- for ( const value of await this . userDB . getEnvVars ( user . id ) ) {
2349
- if ( ! ( await this . resourceAccessGuard . canAccess ( { kind : "envVar" , subject : value } , "get" ) ) ) {
2350
- continue ;
2351
- }
2352
- result . push ( {
2353
- id : value . id ,
2354
- name : value . name ,
2355
- value : value . value ,
2356
- repositoryPattern : value . repositoryPattern ,
2357
- } ) ;
2358
- }
2359
- return result ;
2348
+ return this . envVarService . getAllEnvVars ( user . id , user . id ) ;
2360
2349
}
2361
2350
2362
2351
async setEnvVar ( ctx : TraceContext , variable : UserEnvVarValue ) : Promise < void > {
2363
2352
traceAPIParams ( ctx , { variable : censor ( variable , "value" ) } ) ; // filter content because of PII
2364
2353
2365
2354
// Note: this operation is per-user only, hence needs no resource guard
2366
2355
const user = await this . checkAndBlockUser ( "setEnvVar" ) ;
2367
- const userId = user . id ;
2368
-
2369
- // validate input
2370
- const validationError = UserEnvVar . validate ( variable ) ;
2371
- if ( validationError ) {
2372
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , validationError ) ;
2373
- }
2374
-
2375
- variable . repositoryPattern = UserEnvVar . normalizeRepoPattern ( variable . repositoryPattern ) ;
2376
- const existingVars = ( await this . userDB . getEnvVars ( user . id ) ) . filter ( ( v ) => ! v . deleted ) ;
2377
-
2378
- const existingVar = existingVars . find (
2379
- ( v ) => v . name == variable . name && v . repositoryPattern == variable . repositoryPattern ,
2380
- ) ;
2381
- if ( ! ! existingVar ) {
2382
- // overwrite existing variable rather than introduce a duplicate
2383
- variable . id = existingVar . id ;
2384
- }
2385
-
2386
- if ( ! variable . id ) {
2387
- // this is a new variable - make sure the user does not have too many (don't DOS our database using gp env)
2388
- const varCount = existingVars . length ;
2389
- if ( varCount > this . config . maxEnvvarPerUserCount ) {
2390
- throw new ApplicationError (
2391
- ErrorCodes . PERMISSION_DENIED ,
2392
- `cannot have more than ${ this . config . maxEnvvarPerUserCount } environment variables` ,
2393
- ) ;
2394
- }
2395
- }
2396
-
2397
- const envvar : UserEnvVar = {
2398
- id : variable . id || uuidv4 ( ) ,
2399
- name : variable . name ,
2400
- repositoryPattern : variable . repositoryPattern ,
2401
- value : variable . value ,
2402
- userId,
2403
- } ;
2404
- await this . guardAccess (
2405
- { kind : "envVar" , subject : envvar } ,
2406
- typeof variable . id === "string" ? "update" : "create" ,
2407
- ) ;
2408
- this . analytics . track ( { event : "envvar-set" , userId } ) ;
2409
-
2410
- await this . userDB . setEnvVar ( envvar ) ;
2356
+ return this . envVarService . setEnvVar ( user . id , user . id , variable ) ;
2411
2357
}
2412
2358
2413
2359
async deleteEnvVar ( ctx : TraceContext , variable : UserEnvVarValue ) : Promise < void > {
2414
2360
traceAPIParams ( ctx , { variable : censor ( variable , "value" ) } ) ;
2415
2361
2416
2362
// Note: this operation is per-user only, hence needs no resource guard
2417
2363
const user = await this . checkAndBlockUser ( "deleteEnvVar" ) ;
2418
- const userId = user . id ;
2419
-
2420
- if ( ! variable . id && variable . name && variable . repositoryPattern ) {
2421
- variable . repositoryPattern = UserEnvVar . normalizeRepoPattern ( variable . repositoryPattern ) ;
2422
- const existingVars = ( await this . userDB . getEnvVars ( user . id ) ) . filter ( ( v ) => ! v . deleted ) ;
2423
- const existingVar = existingVars . find (
2424
- ( v ) => v . name == variable . name && v . repositoryPattern == variable . repositoryPattern ,
2425
- ) ;
2426
- variable . id = existingVar ?. id ;
2427
- }
2428
-
2429
- if ( ! variable . id ) {
2430
- throw new ApplicationError (
2431
- ErrorCodes . NOT_FOUND ,
2432
- `cannot delete '${ variable . name } ' in scope '${ variable . repositoryPattern } '` ,
2433
- ) ;
2434
- }
2435
-
2436
- const envvar : UserEnvVar = {
2437
- ...variable ,
2438
- id : variable . id ! ,
2439
- userId,
2440
- } ;
2441
- await this . guardAccess ( { kind : "envVar" , subject : envvar } , "delete" ) ;
2442
- this . analytics . track ( { event : "envvar-deleted" , userId } ) ;
2443
-
2444
- await this . userDB . deleteEnvVar ( envvar ) ;
2364
+ return this . envVarService . deleteEnvVar ( user . id , user . id , variable ) ;
2445
2365
}
2446
2366
2447
2367
async hasSSHPublicKey ( ctx : TraceContext ) : Promise < boolean > {
0 commit comments