-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompose.ts
717 lines (656 loc) · 18.9 KB
/
compose.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
import { Readable } from 'stream';
import * as _ from 'lodash';
import * as path from 'path';
import {
InternalInconsistencyError,
ServiceError,
ValidationError,
} from './errors';
import {
DEFAULT_SCHEMA_VERSION,
SchemaError,
SchemaVersion,
validate,
} from './schemas';
import {
BuildConfig,
Composition,
Dict,
ImageDescriptor,
ListOrDict,
Network,
Service,
StringOrList,
Volume,
} from './types';
export function defaultComposition(
image?: string,
dockerfile?: string,
): string {
let context: string;
if (image) {
context = `image: ${image}`;
} else {
if (dockerfile) {
context = `build: {context: ".", dockerfile: "${dockerfile}"}`;
} else {
context = 'build: "."';
}
}
return `# This file has been auto-generated.
version: '${DEFAULT_SCHEMA_VERSION}'
networks: {}
volumes:
resin-data: {}
services:
main:
${context}
privileged: true
tty: true
restart: always
network_mode: host
volumes:
- type: volume
source: resin-data
target: /data
labels:
io.resin.features.kernel-modules: 1
io.resin.features.firmware: 1
io.resin.features.dbus: 1
io.resin.features.supervisor-api: 1
io.resin.features.resin-api: 1
`;
}
/**
* Validates, normalises and returns the input composition. If the composition
* does not have the expected structure and discrepancies can't be resolved,
* validation errors are thrown. The input composition is mutated in-place.
*
* @param inputCompositionObject The input composition as a plain JS object
*/
export function normalize(inputCompositionObject: any): Composition;
/**
* Validates, normalises and returns the input composition. If the composition
* does not have the expected structure and discrepancies can't be resolved,
* validation errors are thrown. The input composition is mutated in-place.
*
* The context for the composition is the project directory which contains the composition
* and describes additional context for the composition. E.g. environment varialbe files
* This context is read and expanded into the composition.
*
* To access this context (files) a callback function fileResolverCb is needed as argument,
* that reads a filePath:string and creates an promisfied Readable from this file.
* Callback has to validate that no symbolic links are used outside project folder.
* Using on filesystem files it should call fs.realpath to validate the filePath.
* Using a tar archive which contains the file should add additional validation for
* the file resinding in the archive (e.g. symbolic links by default are references in
* tar archives)
*
* Drops env_file propertie from composition to indicate that the expand has taken place.
*
* @param inputCompositionObject The input composition as a plain JS object
* @param fileResolverCb Callback to access filePath and returning a Readable
* Callback tries to read the filePath as file and create a Readable for it.
*/
export async function normalize(
inputCompositionObject: any,
fileResolverCb: (path: string) => Promise<Readable>,
): Promise<Composition>;
export function normalize(
inputCompositionObject: any,
fileResolverCb?: (path: string) => Promise<Readable>,
): Composition | Promise<Composition> {
if (fileResolverCb === undefined) {
return normalizeObjectToComposition(inputCompositionObject);
} else {
const composition = normalizeObjectToComposition(inputCompositionObject);
return expandContext(composition, fileResolverCb);
}
}
function normalizeObjectToComposition(
inputCompositionObject: any,
): Composition {
if (!_.isObject(inputCompositionObject)) {
throw new ValidationError('Invalid composition format');
}
let version: SchemaVersion;
let c = inputCompositionObject as {
version: any;
[key: string]: any;
};
if (_.isUndefined(c.version)) {
version = SchemaVersion.v1;
} else {
if (!_.isString(c.version)) {
c.version = `${c.version}`;
}
switch (c.version) {
case '2':
case '2.0':
case '2.1':
case '2.2':
case '2.3':
case '2.4':
version = DEFAULT_SCHEMA_VERSION;
break;
default:
throw new ValidationError('Unsupported composition version');
}
}
preflight(version, c);
try {
validate(version, c);
} catch (e) {
if (e instanceof SchemaError) {
throw new ValidationError(e);
}
throw e;
}
switch (version) {
case SchemaVersion.v1:
// FIXME: perform attribute migration
c = { version: DEFAULT_SCHEMA_VERSION, services: c };
case DEFAULT_SCHEMA_VERSION:
// Normalise volumes
if (c.volumes) {
c.volumes = _.mapValues(c.volumes, normalizeVolume);
}
// Normalise networks
if (c.networks) {
c.networks = _.mapValues(c.networks, normalizeNetwork);
}
// Normalise services
const services: Dict<any> = c.services || {};
const serviceNames = _.keys(services);
const volumeNames = _.keys(c.volumes);
const networkNames = _.keys(c.networks);
c.services = _(services)
.map((service, serviceName) => {
try {
const normalizedService = normalizeService(
service,
serviceNames,
volumeNames,
networkNames,
);
return [serviceName, normalizedService];
} catch (err) {
if (err instanceof ValidationError) {
throw new ServiceError(serviceName, err);
}
throw err;
}
})
.fromPairs()
.value();
}
c.version = DEFAULT_SCHEMA_VERSION;
return c as Composition;
}
function preflight(_version: SchemaVersion, data: any) {
// Convert `null` networks to empty objects
if (_.isObject(data.networks)) {
data.networks = _.mapValues(data.networks, (n) => n || {});
}
// Convert `null` volumes to empty objects
if (_.isObject(data.volumes)) {
data.volumes = _.mapValues(data.volumes, (v) => v || {});
}
}
function normalizeService(
service: any,
serviceNames: string[],
volumeNames: string[],
networkNames: string[],
): Service {
if (!service.image && !service.build) {
throw new ValidationError('You must specify either an image or a build');
}
if (service.build) {
service.build = normalizeServiceBuild(service.build, networkNames);
}
if (service.depends_on) {
if (!_.isArray(service.depends_on)) {
// Try to convert long-form into list-of-strings
service.depends_on = _.map(service.depends_on, (dep, serviceName) => {
if (_.includes(['service_started', 'service-started'], dep.condition)) {
return serviceName;
}
throw new ValidationError(
'Only "service_started" type of service dependency is supported',
);
});
}
if (_.uniq(service.depends_on).length !== service.depends_on.length) {
throw new ValidationError('Service dependencies must be unique');
}
_.forEach(service.depends_on, (dep) => {
if (!_.includes(serviceNames, dep)) {
throw new ValidationError(`Unknown service dependency: ${dep}`);
}
});
}
if (service.environment) {
service.environment = normalizeKeyValuePairs(service.environment);
}
if (service.env_file) {
service.env_file = normalizeAndValidateFilePath(service.env_file);
}
if (service.extra_hosts) {
if (!_.isArray(service.extra_hosts)) {
// At this point we know that the extra_hosts entry is an object, so cast to
// keep TS happy
service.extra_hosts = normalizeExtraHostObject(
service.extra_hosts as any,
);
}
}
if (service.labels) {
service.labels = normalizeKeyValuePairs(service.labels);
validateLabels(service.labels);
}
if (service.ports) {
service.ports = normalizeArrayOfStrings(service.ports);
}
if (service.volumes) {
const [volumes, tmpfs, labels] = normalizeServiceVolumes(
service.volumes,
volumeNames,
);
service.volumes = volumes;
if (service.tmpfs) {
if (typeof service.tmpfs === 'string') {
service.tmpfs = [service.tmpfs].concat(tmpfs);
} else {
service.tmpfs = service.tmpfs.concat(tmpfs);
}
}
if (labels.length > 0) {
service.labels = {
...labels.reduce((o, l) => ({ ...o, [l]: 1 }), {}),
...service.labels,
};
}
}
if (service.scale) {
throw new ValidationError('service.scale is not allowed');
}
return service;
}
function normalizeArrayOfStrings(value: any[]): string[] {
return _.map(value, String);
}
function normalizeServiceBuild(
serviceBuild: any,
networkNames: string[],
): BuildConfig {
if (typeof serviceBuild === 'string') {
serviceBuild = { context: serviceBuild };
}
if (serviceBuild.args) {
serviceBuild.args = normalizeKeyValuePairs(serviceBuild.args);
}
if (serviceBuild.labels) {
serviceBuild.labels = normalizeKeyValuePairs(serviceBuild.labels);
validateLabels(serviceBuild.labels);
}
if (serviceBuild.extra_hosts && !_.isArray(serviceBuild.extra_hosts)) {
serviceBuild.extra_hosts = normalizeExtraHostObject(
serviceBuild.extra_hosts as any,
);
}
if (serviceBuild.isolation) {
throw new ValidationError('service.build.isolation is not allowed');
}
if (
serviceBuild.network &&
serviceBuild.network !== 'host' &&
serviceBuild.network !== 'none'
) {
if (networkNames.indexOf(serviceBuild.network) === -1) {
throw new ValidationError(
`Missing network definition for '${serviceBuild.network}'`,
);
}
}
return serviceBuild;
}
function normalizeServiceVolumes(
serviceVolumes: any[],
volumeNames: string[],
): [string[], string[], string[]] {
const tmpfs: string[] = [];
const volumes: string[] = [];
const mounts: string[] = [];
const labels: string[] = [];
serviceVolumes.forEach((volume) => {
const ref = normalizeServiceVolume(volume);
validateServiceVolume(ref, volumeNames);
switch (ref.type) {
case 'bind':
mounts.push(ref.source!);
break;
case 'tmpfs':
if (ref.target) {
tmpfs.push(ref.target);
}
break;
case 'volume':
volumes.push(
`${ref.source}:${ref.target}${ref.read_only ? ':ro' : ''}`,
);
break;
}
});
appliedBindMountsByLabel.forEach(([label, appliedBindMounts]) => {
if (_.every(appliedBindMounts, (m) => mounts.indexOf(m) !== -1)) {
labels.push(label);
}
});
return [volumes, tmpfs, labels];
}
interface VolumeRef {
type: string;
source?: string;
target?: string;
read_only?: boolean;
bind?: {
propagation?: string;
};
volume?: {
nocopy?: boolean;
};
tmpfs?: {
size?: number;
};
}
const appliedBindMountsByLabel: Array<[string, string[]]> = [
['io.balena.features.balena-socket', ['/var/run/docker.sock']],
['io.balena.features.balena-socket', ['/var/run/balena-engine.sock']],
['io.balena.features.dbus', ['/run/dbus']],
['io.balena.features.sysfs', ['/sys']],
['io.balena.features.procfs', ['/proc']],
['io.balena.features.kernel-modules', ['/lib/modules']],
['io.balena.features.firmware', ['/lib/firmware']],
[
'io.balena.features.journal-logs',
['/var/log/journal', '/run/log/journal', '/etc/machine-id'],
],
];
const allowedBindMounts = _.flatMap(appliedBindMountsByLabel, (b) => b[1]);
function normalizeServiceVolume(serviceVolume: any): VolumeRef {
let ref: VolumeRef = { type: 'volume', read_only: false };
if (typeof serviceVolume === 'string') {
const parts = serviceVolume.split(':');
if (parts.length < 2) {
throw new ValidationError(`Invalid volume: '${serviceVolume}'`);
}
ref.source = parts[0];
ref.target = parts[1];
if (path.parse(ref.source).dir !== '') {
ref.type = 'bind';
}
if (parts[2] === 'ro') {
ref.read_only = true;
}
} else {
ref = serviceVolume;
}
return ref;
}
function validateServiceVolume(
serviceVolume: VolumeRef,
volumeNames: string[],
) {
switch (serviceVolume.type) {
case 'bind':
if (!serviceVolume.source) {
throw new ValidationError('Missing bind mount source');
}
if (!serviceVolume.target) {
throw new ValidationError('Missing bind mount target');
}
if (allowedBindMounts.indexOf(serviceVolume.source) === -1) {
// not a well-known bind mount but an arbitrary one
throw new ValidationError('Bind mounts are not allowed');
}
break;
case 'tmpfs':
if (serviceVolume.source) {
throw new ValidationError('Tmpfs mount can not have a source');
}
if (!serviceVolume.target) {
throw new ValidationError('Tmpfs mount missing target');
}
if (serviceVolume.read_only) {
throw new ValidationError('Tmpfs can not be read only');
}
if (serviceVolume.tmpfs) {
throw new ValidationError('Tmpfs options are not allowed');
}
break;
case 'volume':
if (!serviceVolume.source) {
throw new ValidationError('Missing volume source');
}
if (volumeNames.indexOf(serviceVolume.source) === -1) {
throw new ValidationError(
`Missing volume definition for '${serviceVolume.source}'`,
);
}
if (serviceVolume.volume) {
throw new ValidationError('Volume options are not allowed');
}
break;
}
}
function validateLabels(labels: Dict<string>) {
_.keys(labels).forEach((name) => {
if (!/^[a-zA-Z0-9.-]+$/.test(name)) {
throw new ValidationError(
`Invalid label name: "${name}". ` +
'Label names must only contain alphanumeric ' +
'characters, periods "." and dashes "-".',
);
}
});
}
function normalizeNetwork(network: Network): Network {
if (network.labels) {
network.labels = normalizeKeyValuePairs(network.labels);
validateLabels(network.labels);
}
return network;
}
function normalizeVolume(volume: Volume): Volume {
if (volume.labels) {
volume.labels = normalizeKeyValuePairs(volume.labels);
validateLabels(volume.labels);
}
return volume;
}
function normalizeExtraHostObject(extraHostsObject: Dict<string>): string[] {
return _.map(extraHostsObject, (ip, host) => `${host}:${ip}`);
}
/**
* Parses a composition and returns a list of image descriptors that can
* be used to pull or build a service image. The given composition version
* must be equal to `DEFAULT_SCHEMA_VERSION`, or an exception is thrown.
* Normalise the composition before passing it to this function.
*/
export function parse(c: Composition): ImageDescriptor[] {
if (c.version !== DEFAULT_SCHEMA_VERSION) {
throw new Error('Unsupported composition version');
}
return _.toPairs(c.services).map(([name, service]) => {
return createImageDescriptor(name, service);
});
}
function createImageDescriptor(
serviceName: string,
service: Service,
): ImageDescriptor {
if (service.image && !service.build) {
return { serviceName, image: service.image };
}
if (!service.build) {
// should not get here
throw new InternalInconsistencyError();
}
const build: BuildConfig = service.build;
// TODO(robertgzr): could probably move this into normalizeServiceBuild
if (service.image) {
build.tag = service.image;
}
return { serviceName, image: build };
}
function normalizeKeyValuePairs(
obj?: ListOrDict,
sep: string = '=',
): Dict<string> {
if (!obj) {
return {};
}
if (!_.isArray(obj)) {
return _(obj)
.toPairs()
.map(([key, value]) => {
return [key, value ? ('' + value).trim() : ''];
})
.fromPairs()
.value();
}
return _(obj)
.map((val) => {
const parts = val.split(sep);
return [parts.shift()!, parts.join('=')];
})
.map(([key, value]) => {
return [key.trim(), value ? value.trim() : ''];
})
.fromPairs()
.value();
}
function normalizeAndValidateFilePath(envFile: StringOrList): StringOrList {
// use a set to store only unique normalized file paths
const normalizedEnvFilePaths: Set<string> = new Set();
if (!Array.isArray(envFile)) {
envFile = [envFile];
}
for (let envFilePath of envFile) {
envFilePath = path.normalize(envFilePath);
if (path.isAbsolute(envFilePath)) {
throw new ValidationError(
`Absolute filepath not allowed: ${envFilePath}`,
);
}
if (envFilePath.startsWith('..')) {
throw new ValidationError(
`Directory traversing not allowed : ${envFilePath}`,
);
}
if (envFilePath.includes('*')) {
throw new ValidationError(`Wildcards not allowed : ${envFilePath}`);
}
normalizedEnvFilePaths.add(envFilePath);
}
// spread set and return as array
return [...normalizedEnvFilePaths];
}
async function expandContext(
composition: Composition,
fileResolverCb: (path: string) => Promise<Readable>,
): Promise<Composition> {
// read all env_file delcared file paths from the composition
const expandedEnvironmentFiles: Dict<Dict<string>> =
await readEnvFilesFromComposition(composition, fileResolverCb);
// assign all normalized envrionment variables to the services in the composition
assignExpandedEnvFilesToComposition(composition, expandedEnvironmentFiles);
return composition;
}
async function readEnvFilesFromComposition(
composition: Composition,
fileResolverCb: (path: string) => Promise<Readable>,
): Promise<Dict<Dict<string>>> {
const envFileVariables: Dict<Dict<string>> = {};
for (const service of Object.values(composition.services)) {
let envFilePaths = service.env_file;
if (!!envFilePaths) {
if (!Array.isArray(envFilePaths)) {
envFilePaths = [envFilePaths];
}
for (const envFilePath of envFilePaths) {
if (!(envFilePath in envFileVariables)) {
envFileVariables[envFilePath] = await readAndNormalizeExpandEnvFile(
envFilePath,
fileResolverCb,
);
}
}
}
}
return envFileVariables;
}
function assignExpandedEnvFilesToComposition(
composition: Composition,
envFileVariables: Dict<Dict<string>>,
): Composition {
// Apply all read env_files content to the services referncing the env_files
for (const service of Object.values(composition.services)) {
let envFilePaths = service.env_file;
if (!!envFilePaths) {
service.environment = service.environment ?? {};
if (!Array.isArray(envFilePaths)) {
envFilePaths = [envFilePaths];
}
for (const envFilePath of envFilePaths) {
for (const [key, value] of Object.entries(
envFileVariables[envFilePath],
)) {
if (!(key in service.environment)) {
service.environment[key] = value;
}
}
}
}
// delete the env_file property as it has been translated into composition environments
delete service.env_file;
}
return composition;
}
async function readAndNormalizeExpandEnvFile(
envFile: string,
fileResolverCb: (path: string) => Promise<Readable>,
): Promise<Dict<string>> {
const readline = require('readline');
const { once } = require('events');
const intermediateEnv: Dict<string> = {};
let readableError;
// instantiate readable from callback to add eventlistener to it
const readable = await fileResolverCb(envFile);
const lineReader = readline.createInterface({
input: readable,
crlfDelay: Infinity,
});
// get error from stream reader and close linereader
// no race condition as the lineReader is paused until an event listener is registered
readable.on('error', (readError) => {
readableError = readError;
lineReader.close();
});
// process each line on event
// now readable will be evaluated
// read all lines in a buffer dictionary to later merge them into existing environment
lineReader.on('line', (line: string) => {
for (const [key, val] of Object.entries(normalizeKeyValuePairs([line]))) {
intermediateEnv[key] = val;
}
});
// wait until all lines read or stream error occured
await once(lineReader, 'close');
// populate stream errors
if (readableError !== undefined) {
throw readableError;
}
return intermediateEnv;
}