-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathconfig.ts
282 lines (244 loc) · 9.45 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
/**
* 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 } 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";
export const Config = Symbol("Config");
export type Config = Omit<
ConfigSerialized,
| "blockedRepositories"
| "hostUrl"
| "chargebeeProviderOptionsFile"
| "stripeSecretsFile"
| "stripeConfigFile"
| "licenseFile"
> & {
hostUrl: GitpodHostUrl;
workspaceDefaults: WorkspaceDefaults;
chargebeeProviderOptions?: ChargebeeProviderOptions;
stripeSecrets?: { publishableKey: string; secretKey: string };
stripeConfig?: { usageProductPriceIds: { EUR: string; USD: string } };
builtinAuthProvidersConfigured: boolean;
blockedRepositories: { urlRegExp: RegExp; blockUser: boolean }[];
inactivityPeriodForRepos?: number;
};
export interface WorkspaceDefaults {
workspaceImage: string;
previewFeatureFlags: NamedWorkspaceFeatureFlag[];
defaultFeatureFlags: NamedWorkspaceFeatureFlag[];
timeoutDefault?: string;
timeoutExtended?: string;
}
export interface WorkspaceGarbageCollection {
disabled: boolean;
startDate: number;
chunkLimit: number;
minAgeDays: number;
minAgePrebuildDays: number;
contentRetentionPeriodDays: number;
contentChunkLimit: 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;
codeSync: CodeSyncConfig;
vsxRegistryUrl: string;
/**
* 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 };
/**
* List of repositories not allowed to be used for workspace starts.
* `blockUser` attribute to control handling of the user's account.
*/
blockedRepositories?: { urlRegExp: string; blockUser: boolean }[];
/**
* If a numeric value interpreted as days is set, repositories not beeing opened with Gitpod are
* considered inactive.
*/
inactivityPeriodForRepos?: number;
}
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: { usageProductPriceIds: { EUR: string; USD: string } } | 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");
}
const blockedRepositories: { urlRegExp: RegExp; blockUser: boolean }[] = [];
if (config.blockedRepositories) {
for (const { blockUser, urlRegExp } of config.blockedRepositories) {
blockedRepositories.push({
blockUser,
urlRegExp: new RegExp(urlRegExp),
});
}
}
let inactivityPeriodForRepos: number | undefined;
if (typeof config.inactivityPeriodForRepos === "number") {
if (config.inactivityPeriodForRepos >= 1) {
inactivityPeriodForRepos = config.inactivityPeriodForRepos;
}
}
return {
...config,
hostUrl,
authProviderConfigs,
builtinAuthProvidersConfigured,
chargebeeProviderOptions,
stripeSecrets,
stripeConfig,
license,
workspaceGarbageCollection: {
...config.workspaceGarbageCollection,
startDate: config.workspaceGarbageCollection.startDate
? new Date(config.workspaceGarbageCollection.startDate).getTime()
: Date.now(),
},
blockedRepositories,
inactivityPeriodForRepos,
};
}
}