-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathConfig.js
More file actions
326 lines (284 loc) · 12 KB
/
Config.js
File metadata and controls
326 lines (284 loc) · 12 KB
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
'use strict';
const assert = require('assert');
const { EventEmitter } = require('events');
const joi = require('joi');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const extensions = require('../extensions');
const { backbeatConfigJoi } = require('./config.joi');
const locationTypeMatch = {
'location-mem-v1': 'mem',
'location-file-v1': 'file',
'location-azure-v1': 'azure',
'location-do-spaces-v1': 'aws_s3',
'location-aws-s3-v1': 'aws_s3',
'location-wasabi-v1': 'aws_s3',
'location-gcp-v1': 'gcp',
'location-scality-ring-s3-v1': 'aws_s3',
'location-scality-artesca-s3-v1': 'aws_s3',
'location-ceph-radosgw-s3-v1': 'aws_s3',
'location-dmf-v1': 'tlp',
'location-azure-archive-v1': 'tlp',
'location-miria-v1': 'tlp',
};
// NOTE: currently only s3 connector (s3c 7.4.3 and above) are supported
const ingestionTypeMatch = {
'location-scality-ring-s3-v1': 'scality_s3',
};
class Config extends EventEmitter {
constructor() {
super();
/*
* By default, the config file is "config.json" at the root.
* It can be overridden using the BACKBEAT_CONFIG_FILE environment var.
*/
// Note: used to allow access to the class itself from an instance.
this.Config = Config;
this._basePath = `${__dirname}/../conf`;
if (process.env.BACKBEAT_CONFIG_FILE !== undefined) {
this._configPath = process.env.BACKBEAT_CONFIG_FILE;
} else {
this._configPath = path.join(this._basePath, 'config.json');
}
let config;
try {
const data = fs.readFileSync(this._configPath,
{ encoding: 'utf-8' });
config = JSON.parse(data);
} catch (err) {
throw new Error(`could not parse config file: ${err.message}`);
}
this._parseConfig(config);
}
/**
* Parses Backbeat's configuration
* @param {Object} config backbeat configuration
* @returns {undefined}
*/
_parseConfig(config) {
const parsedConfig = joi.attempt(config, backbeatConfigJoi);
if (parsedConfig.extensions) {
Object.keys(parsedConfig.extensions).forEach(extName => {
const index = extensions[extName];
if (!index) {
throw new Error(`configured extension ${extName}: ` +
'not found in extensions directory');
}
if (index.configValidator) {
const extConfig = parsedConfig.extensions[extName];
const validatedConfig =
index.configValidator(this, extConfig);
parsedConfig.extensions[extName] = validatedConfig;
}
});
}
const lifecycleConfig = parsedConfig.extensions?.lifecycle;
const backbeatSupportsTransition = lifecycleConfig?.supportedLifecycleRules?.includes('Transition');
const replicationConfig = parsedConfig.extensions?.replication;
if (backbeatSupportsTransition && !replicationConfig.dataMoverTopic) {
throw new Error('dataMoverTopic is required when lifecycle transitions is supported');
}
const destination = parsedConfig.extensions?.replication?.destination;
this.bootstrapList = destination?.bootstrapList?.map(endpoint => {
if (!endpoint.servers) {
return endpoint;
}
const transport = destination.sites?.[endpoint.site]?.transport || destination.transport || 'http';
const defaultPort = transport === 'https' ? 443 : 80;
const servers = endpoint.servers.map(server => server.includes(':') ? server : `${server}:${defaultPort}`);
return { ...endpoint, servers };
}) ?? [];
const shouldRestrictToSite = process.env.BOOTSTRAP_SITE_NAME;
if (shouldRestrictToSite) {
this.bootstrapList = this.bootstrapList.filter(item => item.site === shouldRestrictToSite);
}
// NOTE: used to store ingestion bucket information
this.ingestionBuckets = [];
// whitelist IP, CIDR for health checks
const defaultHealthChecks = ['127.0.0.1/8', '::1'];
const healthChecks = parsedConfig.server.healthChecks;
healthChecks.allowFrom =
healthChecks.allowFrom.concat(defaultHealthChecks);
if (parsedConfig.redis &&
typeof parsedConfig.redis.sentinels === 'string') {
const redisConf = { sentinels: [], name: parsedConfig.redis.name };
parsedConfig.redis.sentinels.split(',').forEach(item => {
const [host, port] = item.split(':');
redisConf.sentinels.push({ host,
port: Number.parseInt(port, 10) });
});
parsedConfig.redis = redisConf;
}
// default to standalone configuration if sentinel not setup
if (!parsedConfig.redis || !parsedConfig.redis.sentinels) {
this.redis = Object.assign({}, parsedConfig.redis,
{ host: '127.0.0.1', port: 6379 });
}
// additional certs checks
if (parsedConfig.certFilePaths) {
parsedConfig.https = this._parseCertFilePaths(
parsedConfig.certFilePaths);
}
if (parsedConfig.internalCertFilePaths) {
parsedConfig.internalHttps = this._parseCertFilePaths(
parsedConfig.internalCertFilePaths);
}
if (process.env.MONGODB_HOSTS) {
parsedConfig.queuePopulator.mongo.replicaSetHosts =
process.env.MONGODB_HOSTS;
}
if (process.env.MONGODB_RS) {
parsedConfig.queuePopulator.mongo.replicatSet =
process.env.MONGODB_RS;
}
if (process.env.MONGODB_DATABASE) {
parsedConfig.queuePopulator.mongo.database =
process.env.MONGODB_DATABASE;
}
if (process.env.MONGODB_AUTH_USERNAME &&
process.env.MONGODB_AUTH_PASSWORD) {
parsedConfig.queuePopulator.mongo.authCredentials = {
username: process.env.MONGODB_AUTH_USERNAME,
password: process.env.MONGODB_AUTH_PASSWORD,
};
}
// Overwrite extension configs if configured
// We can specify the list of extensions that should be handled by this
// instance of the queuePopulator
const configuredExtensions = process.env.BACKBEAT_QUEUEPOPULATOR_EXTENSIONS;
if (configuredExtensions) {
const allowedExtensions = configuredExtensions.split(',');
const filteredExtensions = Object.entries(parsedConfig.extensions)
.filter(entry => allowedExtensions.includes(entry[0]));
parsedConfig.extensions = Object.fromEntries(filteredExtensions);
}
// config is validated, safe to assign directly to the config object
Object.assign(this, parsedConfig);
this.transientLocations = {};
this._setTimeOptions();
}
_setTimeOptions() {
// NOTE: EXPIRE_ONE_DAY_EARLIER and TRANSITION_ONE_DAY_EARLIER are deprecated in favor of
// TIME_PROGRESSION_FACTOR which decreases the weight attributed to a day in order to, amongst other things,
// expedite the lifecycle of objects.
// moves lifecycle expiration deadlines 1 day earlier, mostly for testing
const expireOneDayEarlier = process.env.EXPIRE_ONE_DAY_EARLIER === 'true';
// moves lifecycle transition deadlines 1 day earlier, mostly for testing
const transitionOneDayEarlier = process.env.TRANSITION_ONE_DAY_EARLIER === 'true';
// decreases the weight attributed to a day in order to expedite the lifecycle of objects.
const timeProgressionFactor = Number.parseInt(process.env.TIME_PROGRESSION_FACTOR, 10) || 1;
const isIncompatible = (expireOneDayEarlier || transitionOneDayEarlier) && (timeProgressionFactor > 1);
assert(!isIncompatible, 'The environment variables "EXPIRE_ONE_DAY_EARLIER" or ' +
'"TRANSITION_ONE_DAY_EARLIER" are not compatible with the "TIME_PROGRESSION_FACTOR" variable.');
this.timeOptions = {
expireOneDayEarlier,
transitionOneDayEarlier,
timeProgressionFactor,
};
}
getTimeOptions() {
return this.timeOptions;
}
_parseCertFilePaths(certFilePaths) {
const { key, cert, ca } = certFilePaths;
const makePath = value =>
(value.startsWith('/') ?
value : `${this._basePath}/${value}`);
const https = {};
if (key && cert) {
const keypath = makePath(key);
const certpath = makePath(cert);
fs.accessSync(keypath, fs.F_OK | fs.R_OK);
fs.accessSync(certpath, fs.F_OK | fs.R_OK);
https.cert = fs.readFileSync(certpath, 'ascii');
https.key = fs.readFileSync(keypath, 'ascii');
}
if (ca) {
const capath = makePath(ca);
fs.accessSync(capath, fs.F_OK | fs.R_OK);
https.ca = fs.readFileSync(capath, 'ascii');
}
return https;
}
getBasePath() {
return this._basePath;
}
getConfigPath() {
return this._configPath;
}
setBootstrapList(locationConstraints) {
this.bootstrapList = Object.keys(locationConstraints).map(key => ({
site: key,
type: locationTypeMatch[locationConstraints[key].locationType],
}));
this.emit('bootstrap-list-update');
}
getBootstrapList() {
return this.bootstrapList;
}
setIngestionBuckets(locationConstraints, buckets, log) {
const ingestionBuckets = [];
buckets.forEach(bucket => {
const { name, ingestion, locationConstraint } = bucket;
const locationInfo = locationConstraints[locationConstraint];
if (!locationInfo ||
!locationInfo.details ||
!locationInfo.locationType) {
log.debug('ingestion bucket missing information', {
bucket,
locationInfo,
});
return;
}
// if location is not an enabled backbeat ingestion location, skip
const locationType = ingestionTypeMatch[locationInfo.locationType];
if (!locationType) {
return;
}
const ingestionBucketDetails = Object.assign(
{},
locationInfo.details,
{
locationType,
zenkoBucket: name,
ingestion,
locationConstraint,
}
);
ingestionBuckets.push(ingestionBucketDetails);
});
this.ingestionBuckets = ingestionBuckets;
}
getIngestionBuckets() {
return this.ingestionBuckets;
}
setIsTransientLocation(locationName, isTransient) {
this.transientLocations[locationName] = isTransient;
}
getIsTransientLocation(locationName) {
return this.transientLocations[locationName] || false;
}
getPublicInstanceId() {
return this.publicInstanceId;
}
setPublicInstanceId(instanceId) {
this.publicInstanceId = crypto.createHash('sha256')
.update(instanceId)
.digest('hex');
}
/**
* returns the queue processor's site specific destination configuration
* @param {string} site - site name
* @returns {object} site specific destination configuration
*/
getReplicationSiteDestConfig(site) {
const destConfig = this.extensions.replication.destination;
return {
transport: destConfig.sites?.[site]?.transport || destConfig.transport,
auth: destConfig.sites?.[site]?.auth || destConfig.auth,
replicationEndpoint: this.bootstrapList.find(endpoint => endpoint.site === site),
};
}
}
module.exports = new Config();