-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathconfig.ts
326 lines (273 loc) · 10.8 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* Copyright (c) 2020 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 { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";
import { AuthProviderParams, normalizeAuthProviderParams } from "./auth/auth-provider";
import { NamedWorkspaceFeatureFlag, StripeConfig } from "@gitpod/gitpod-protocol";
import { RateLimiterConfig } from "./auth/rate-limiter";
import { CodeSyncConfig } from "./code-sync/code-sync-service";
import { ChargebeeProviderOptions, readOptionsFromFile } from "@gitpod/gitpod-payment-endpoint/lib/chargebee";
import * as fs from "fs";
import * as yaml from "js-yaml";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { filePathTelepresenceAware } from "@gitpod/gitpod-protocol/lib/env";
import { WorkspaceClasses, WorkspaceClassesConfig } from "./workspace/workspace-classes";
export const Config = Symbol("Config");
export type Config = Omit<
ConfigSerialized,
"hostUrl" | "chargebeeProviderOptionsFile" | "stripeSecretsFile" | "stripeConfigFile" | "licenseFile"
> & {
hostUrl: GitpodHostUrl;
workspaceDefaults: WorkspaceDefaults;
chargebeeProviderOptions?: ChargebeeProviderOptions;
stripeSecrets?: { publishableKey: string; secretKey: string };
stripeConfig?: StripeConfig;
builtinAuthProvidersConfigured: boolean;
inactivityPeriodForReposInDays?: number;
};
export interface WorkspaceDefaults {
workspaceImage: string;
previewFeatureFlags: NamedWorkspaceFeatureFlag[];
defaultFeatureFlags: NamedWorkspaceFeatureFlag[];
timeoutDefault?: string;
timeoutExtended?: string;
}
export interface WorkspaceGarbageCollection {
disabled: boolean;
startDate: number;
/** The number of seconds between a run and the next */
intervalSeconds: number;
/** The maximum amount of workspaces that are marked as 'softDeleted' in one go */
chunkLimit: number;
/** The minimal age of a workspace before it is marked as 'softDeleted' (= hidden for the user) */
minAgeDays: number;
/** The minimal age of a prebuild (incl. workspace) before it's content is deleted (+ marked as 'softDeleted') */
minAgePrebuildDays: number;
/** The minimal number of days a workspace has to stay in 'softDeleted' before it's content is deleted */
contentRetentionPeriodDays: number;
/** The maximum amount of workspaces whose content is deleted in one go */
contentChunkLimit: number;
/** The minimal number of days a workspace has to stay in 'contentDeleted' before it's purged from the DB */
purgeRetentionPeriodDays: number;
/** The maximum amount of workspaces which are purged in one go */
purgeChunkLimit: number;
}
/**
* This is the config shape as found in the configuration file, e.g. server-configmap.yaml
*/
export interface ConfigSerialized {
version: string;
hostUrl: string;
installationShortname: string;
devBranch?: string;
insecureNoDomain: boolean;
// Use one or other - licenseFile reads from a file and populates license
license?: string;
licenseFile?: string;
workspaceHeartbeat: {
intervalSeconds: number;
timeoutSeconds: number;
};
workspaceDefaults: Omit<WorkspaceDefaults, "ideImage">;
session: {
maxAgeMs: number;
secret: string;
};
githubApp?: {
enabled: boolean;
appId: number;
baseUrl?: string;
webhookSecret: string;
authProviderId: string;
certPath: string;
marketplaceName: string;
};
definitelyGpDisabled: boolean;
workspaceGarbageCollection: WorkspaceGarbageCollection;
enableLocalApp: boolean;
authProviderConfigs: AuthProviderParams[];
authProviderConfigFiles: string[];
disableDynamicAuthProviderLogin: boolean;
/**
* The maximum number of environment variables a user can have configured in their list at any given point in time.
* Note: This limit should be so high that no regular user ever reaches it.
*/
maxEnvvarPerUserCount: number;
/** maxConcurrentPrebuildsPerRef is the maximum number of prebuilds we allow per ref type at any given time */
maxConcurrentPrebuildsPerRef: number;
incrementalPrebuilds: {
repositoryPasslist: string[];
commitHistory: number;
};
blockNewUsers: {
enabled: boolean;
passlist: string[];
};
makeNewUsersAdmin: boolean;
/** defaultBaseImageRegistryWhitelist is the list of registryies users get acces to by default */
defaultBaseImageRegistryWhitelist: string[];
runDbDeleter: boolean;
oauthServer: {
enabled: boolean;
jwtSecret: string;
};
/**
* The configuration for the rate limiter we (mainly) use for the websocket API
*/
rateLimiter: RateLimiterConfig;
/**
* The address content service clients connect to
* Example: content-service:8080
*/
contentServiceAddr: string;
/**
* The address content service clients connect to
* Example: image-builder:8080
*/
imageBuilderAddr: string;
/**
* The address usage service clients connect to
* Example: usage:8080
*/
usageServiceAddr: string;
/**
* The address ide service clients connect to
* Example: ide-service:9001
*/
ideServiceAddr: string;
codeSync: CodeSyncConfig;
vsxRegistryUrl: string;
/*
* The maximum event loop lag allowed before the liveness endpoint should return
* an error code.
*/
maximumEventLoopLag: number;
/**
* Payment related options
*/
chargebeeProviderOptionsFile?: string;
stripeSecretsFile?: string;
stripeConfigFile?: string;
enablePayment?: boolean;
/**
* Number of prebuilds that can be started in the last 1 minute.
* Key '*' specifies the default rate limit for a cloneURL, unless overriden by a specific cloneURL.
*/
prebuildLimiter: { [cloneURL: string]: number } & { "*": number };
/**
* If a numeric value interpreted as days is set, repositories not beeing opened with Gitpod are
* considered inactive.
*/
inactivityPeriodForReposInDays?: number;
/**
* Supported workspace classes
*/
workspaceClasses: WorkspaceClassesConfig;
/**
* configuration for twilio
*/
twilioConfig?: {
serviceID: string;
accountSID: string;
authToken: string;
};
}
export namespace ConfigFile {
export function fromFile(path: string | undefined = process.env.CONFIG_PATH): Config {
if (!path) {
throw new Error("config load error: CONFIG_PATH not set!");
}
try {
const configStr = fs.readFileSync(filePathTelepresenceAware(path), { encoding: "utf-8" }).toString();
const configSerialized: ConfigSerialized = JSON.parse(configStr);
return loadAndCompleteConfig(configSerialized);
} catch (err) {
log.error("config parse error", err);
process.exit(1);
}
}
function loadAndCompleteConfig(config: ConfigSerialized): Config {
const hostUrl = new GitpodHostUrl(config.hostUrl);
let authProviderConfigs: AuthProviderParams[] = [];
const rawProviderConfigs = config.authProviderConfigs;
if (rawProviderConfigs) {
/* Add raw provider data */
authProviderConfigs.push(...rawProviderConfigs);
}
const rawProviderConfigFiles = config.authProviderConfigFiles;
if (rawProviderConfigFiles) {
/* Add providers from files */
const authProviderConfigFiles: AuthProviderParams[] = rawProviderConfigFiles.map<AuthProviderParams>(
(providerFile) => {
const rawProviderData = fs.readFileSync(filePathTelepresenceAware(providerFile), "utf-8");
return yaml.load(rawProviderData) as AuthProviderParams;
},
);
authProviderConfigs.push(...authProviderConfigFiles);
}
authProviderConfigs = normalizeAuthProviderParams(authProviderConfigs);
const builtinAuthProvidersConfigured = authProviderConfigs.length > 0;
const chargebeeProviderOptions = readOptionsFromFile(
filePathTelepresenceAware(config.chargebeeProviderOptionsFile || ""),
);
let stripeSecrets: { publishableKey: string; secretKey: string } | undefined;
if (config.enablePayment && config.stripeSecretsFile) {
try {
stripeSecrets = JSON.parse(
fs.readFileSync(filePathTelepresenceAware(config.stripeSecretsFile), "utf-8"),
);
} catch (error) {
log.error("Could not load Stripe secrets", error);
}
}
let stripeConfig: StripeConfig | undefined;
if (config.enablePayment && config.stripeConfigFile) {
try {
stripeConfig = JSON.parse(fs.readFileSync(filePathTelepresenceAware(config.stripeConfigFile), "utf-8"));
} catch (error) {
log.error("Could not load Stripe config", error);
}
}
let license = config.license;
const licenseFile = config.licenseFile;
if (licenseFile) {
license = fs.readFileSync(filePathTelepresenceAware(licenseFile), "utf-8");
}
let inactivityPeriodForReposInDays: number | undefined;
if (typeof config.inactivityPeriodForReposInDays === "number") {
if (config.inactivityPeriodForReposInDays >= 1) {
inactivityPeriodForReposInDays = config.inactivityPeriodForReposInDays;
}
}
const twilioConfigPath = "/twilio-config/config.json";
let twilioConfig: Config["twilioConfig"];
if (fs.existsSync(filePathTelepresenceAware(twilioConfigPath))) {
try {
twilioConfig = JSON.parse(fs.readFileSync(filePathTelepresenceAware(twilioConfigPath), "utf-8"));
} catch (error) {
log.error("Could not load Twilio config", error);
}
}
WorkspaceClasses.validate(config.workspaceClasses);
return {
...config,
hostUrl,
authProviderConfigs,
builtinAuthProvidersConfigured,
chargebeeProviderOptions,
stripeSecrets,
stripeConfig,
twilioConfig,
license,
workspaceGarbageCollection: {
...config.workspaceGarbageCollection,
startDate: config.workspaceGarbageCollection.startDate
? new Date(config.workspaceGarbageCollection.startDate).getTime()
: Date.now(),
},
inactivityPeriodForReposInDays,
};
}
}