-
Notifications
You must be signed in to change notification settings - Fork 28
/
darwinSpecifics.ts
112 lines (96 loc) · 4.44 KB
/
darwinSpecifics.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
/**
*******************************************************
* *
* Copyright (C) Microsoft. All rights reserved. *
* *
*******************************************************
*/
/// <reference path="../../../typings/Q.d.ts" />
/// <reference path="../../../typings/tacoUtils.d.ts" />
/// <reference path="../../../typings/express.d.ts" />
/// <reference path="../../../typings/zip-stream.d.ts" />
"use strict";
import child_process = require ("child_process");
import express = require ("express");
import fs = require ("fs");
import https = require ("https");
import path = require ("path");
import Q = require ("q");
import certs = require ("./darwinCerts");
import HostSpecifics = require ("../hostSpecifics");
import RemoteBuildConf = require ("../remoteBuildConf");
import resources = require ("../../resources/resourceManager");
import utils = require ("taco-utils");
import Logger = utils.Logger;
import UtilHelper = utils.UtilHelper;
class DarwinSpecifics implements HostSpecifics.IHostSpecifics {
private static config: RemoteBuildConf;
public defaults(base: { [key: string]: any }): { [key: string]: any } {
var osxdefaults: { [key: string]: any } = {
writePidToFile: false,
lang: process.env.LANG && process.env.LANG.replace(/_/, "-").replace(/\..*/, "") || "en", // Convert "en_US.UTF8" to "en-US", similarly for other locales
suppressSetupMessage: false
};
Object.keys(osxdefaults).forEach(function (key: string): void {
if (!(key in base)) {
base[key] = osxdefaults[key];
}
});
return base;
}
// Note: we acquire dependencies for deploying and debugging here rather than in taco-remote-lib because it may require user intervention, and taco-remote-lib may be acquired unattended in future.
public initialize(conf: RemoteBuildConf): Q.Promise<any> {
DarwinSpecifics.config = conf;
if (process.getuid() === 0) {
Logger.logWarning(resources.getString("RunningAsRootError"));
process.exit(1);
}
return Q({});
}
public resetServerCert(conf: RemoteBuildConf): Q.Promise<any> {
return certs.resetServerCert(conf);
}
public generateClientCert(conf: RemoteBuildConf): Q.Promise<number> {
return certs.generateClientCert(conf);
}
public initializeServerCerts(conf: RemoteBuildConf): Q.Promise<HostSpecifics.ICertStore> {
return certs.initializeServerCerts(conf);
}
public getServerCerts(): Q.Promise<HostSpecifics.ICertStore> {
return certs.getServerCerts();
}
public removeAllCertsSync(conf: RemoteBuildConf): void {
certs.removeAllCertsSync(conf);
}
public downloadClientCerts(req: express.Request, res: express.Response): void {
Q.fcall<string>(certs.downloadClientCerts, DarwinSpecifics.config, req.params.pin).then(function (pfxFile: string): void {
res.sendFile(pfxFile);
}).catch<void>(function (error: { code?: number; id: string}): void {
if (error.code) {
res.status(error.code).send(resources.getStringForLanguage(req, error.id));
} else {
res.status(404).send(error);
}
}).finally((): void => {
certs.invalidatePIN(DarwinSpecifics.config, req.params.pin);
}).catch(function (err: Error): void {
Logger.logError(err.message);
}).done();
}
public getHttpsAgent(conf: RemoteBuildConf): Q.Promise<NodeJSHttp.Agent> {
if (conf.secure) {
conf.set("suppressSetupMessage", true);
return certs.generateClientCert(conf).then(function (pin: number): NodeJSHttp.Agent {
var pfxPath: string = path.join(conf.serverDir, "certs", "client", pin.toString(), "client.pfx");
var cert: Buffer = fs.readFileSync(pfxPath);
fs.unlinkSync(pfxPath);
// TODO: Remove the casting once we've get some complete/up-to-date .d.ts files. See https://github.com/Microsoft/TACO/issues/18
return new https.Agent(<https.RequestOptions> {strictSSL: true, pfx: cert });
});
} else {
return Q.resolve<NodeJSHttp.Agent>(null);
}
}
}
var darwinSpecifics: HostSpecifics.IHostSpecifics = new DarwinSpecifics();
export = darwinSpecifics;