@@ -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 ,
@@ -192,6 +191,7 @@ import { UsageService } from "../orgs/usage-service";
192
191
import { UserService } from "../user/user-service" ;
193
192
import { SSHKeyService } from "../user/sshkey-service" ;
194
193
import { StartWorkspaceOptions , WorkspaceService } from "./workspace-service" ;
194
+ import { EnvVarService } from "../user/env-var-service" ;
195
195
196
196
// shortcut
197
197
export const traceWI = ( ctx : TraceContext , wi : Omit < LogContext , "userId" > ) => TraceContext . setOWI ( ctx , wi ) ; // userId is already taken care of in WebsocketConnectionManager
@@ -234,6 +234,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
234
234
@inject ( IAnalyticsWriter ) private readonly analytics : IAnalyticsWriter ,
235
235
@inject ( AuthorizationService ) private readonly authorizationService : AuthorizationService ,
236
236
@inject ( SSHKeyService ) private readonly sshKeyservice : SSHKeyService ,
237
+ @inject ( EnvVarService ) private readonly envVarService : EnvVarService ,
237
238
238
239
@inject ( TeamDB ) private readonly teamDB : TeamDB ,
239
240
@inject ( OrganizationService ) private readonly organizationService : OrganizationService ,
@@ -2278,104 +2279,23 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
2278
2279
// Get all environment variables (unfiltered)
2279
2280
async getAllEnvVars ( ctx : TraceContext ) : Promise < UserEnvVarValue [ ] > {
2280
2281
const user = await this . checkUser ( "getAllEnvVars" ) ;
2281
- const result : UserEnvVarValue [ ] = [ ] ;
2282
- for ( const value of await this . userDB . getEnvVars ( user . id ) ) {
2283
- if ( ! ( await this . resourceAccessGuard . canAccess ( { kind : "envVar" , subject : value } , "get" ) ) ) {
2284
- continue ;
2285
- }
2286
- result . push ( {
2287
- id : value . id ,
2288
- name : value . name ,
2289
- value : value . value ,
2290
- repositoryPattern : value . repositoryPattern ,
2291
- } ) ;
2292
- }
2293
- return result ;
2282
+ return this . envVarService . getAllEnvVars ( user . id , user . id ) ;
2294
2283
}
2295
2284
2296
2285
async setEnvVar ( ctx : TraceContext , variable : UserEnvVarValue ) : Promise < void > {
2297
2286
traceAPIParams ( ctx , { variable : censor ( variable , "value" ) } ) ; // filter content because of PII
2298
2287
2299
2288
// Note: this operation is per-user only, hence needs no resource guard
2300
2289
const user = await this . checkAndBlockUser ( "setEnvVar" ) ;
2301
- const userId = user . id ;
2302
-
2303
- // validate input
2304
- const validationError = UserEnvVar . validate ( variable ) ;
2305
- if ( validationError ) {
2306
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , validationError ) ;
2307
- }
2308
-
2309
- variable . repositoryPattern = UserEnvVar . normalizeRepoPattern ( variable . repositoryPattern ) ;
2310
- const existingVars = ( await this . userDB . getEnvVars ( user . id ) ) . filter ( ( v ) => ! v . deleted ) ;
2311
-
2312
- const existingVar = existingVars . find (
2313
- ( v ) => v . name == variable . name && v . repositoryPattern == variable . repositoryPattern ,
2314
- ) ;
2315
- if ( ! ! existingVar ) {
2316
- // overwrite existing variable rather than introduce a duplicate
2317
- variable . id = existingVar . id ;
2318
- }
2319
-
2320
- if ( ! variable . id ) {
2321
- // this is a new variable - make sure the user does not have too many (don't DOS our database using gp env)
2322
- const varCount = existingVars . length ;
2323
- if ( varCount > this . config . maxEnvvarPerUserCount ) {
2324
- throw new ApplicationError (
2325
- ErrorCodes . PERMISSION_DENIED ,
2326
- `cannot have more than ${ this . config . maxEnvvarPerUserCount } environment variables` ,
2327
- ) ;
2328
- }
2329
- }
2330
-
2331
- const envvar : UserEnvVar = {
2332
- id : variable . id || uuidv4 ( ) ,
2333
- name : variable . name ,
2334
- repositoryPattern : variable . repositoryPattern ,
2335
- value : variable . value ,
2336
- userId,
2337
- } ;
2338
- await this . guardAccess (
2339
- { kind : "envVar" , subject : envvar } ,
2340
- typeof variable . id === "string" ? "update" : "create" ,
2341
- ) ;
2342
- this . analytics . track ( { event : "envvar-set" , userId } ) ;
2343
-
2344
- await this . userDB . setEnvVar ( envvar ) ;
2290
+ return this . envVarService . setEnvVar ( user . id , user . id , variable ) ;
2345
2291
}
2346
2292
2347
2293
async deleteEnvVar ( ctx : TraceContext , variable : UserEnvVarValue ) : Promise < void > {
2348
2294
traceAPIParams ( ctx , { variable : censor ( variable , "value" ) } ) ;
2349
2295
2350
2296
// Note: this operation is per-user only, hence needs no resource guard
2351
2297
const user = await this . checkAndBlockUser ( "deleteEnvVar" ) ;
2352
- const userId = user . id ;
2353
-
2354
- if ( ! variable . id && variable . name && variable . repositoryPattern ) {
2355
- variable . repositoryPattern = UserEnvVar . normalizeRepoPattern ( variable . repositoryPattern ) ;
2356
- const existingVars = ( await this . userDB . getEnvVars ( user . id ) ) . filter ( ( v ) => ! v . deleted ) ;
2357
- const existingVar = existingVars . find (
2358
- ( v ) => v . name == variable . name && v . repositoryPattern == variable . repositoryPattern ,
2359
- ) ;
2360
- variable . id = existingVar ?. id ;
2361
- }
2362
-
2363
- if ( ! variable . id ) {
2364
- throw new ApplicationError (
2365
- ErrorCodes . NOT_FOUND ,
2366
- `cannot delete '${ variable . name } ' in scope '${ variable . repositoryPattern } '` ,
2367
- ) ;
2368
- }
2369
-
2370
- const envvar : UserEnvVar = {
2371
- ...variable ,
2372
- id : variable . id ! ,
2373
- userId,
2374
- } ;
2375
- await this . guardAccess ( { kind : "envVar" , subject : envvar } , "delete" ) ;
2376
- this . analytics . track ( { event : "envvar-deleted" , userId } ) ;
2377
-
2378
- await this . userDB . deleteEnvVar ( envvar ) ;
2298
+ return this . envVarService . deleteEnvVar ( user . id , user . id , variable ) ;
2379
2299
}
2380
2300
2381
2301
async hasSSHPublicKey ( ctx : TraceContext ) : Promise < boolean > {
0 commit comments