-
Notifications
You must be signed in to change notification settings - Fork 212
/
key_server.js
executable file
·302 lines (274 loc) · 8.96 KB
/
key_server.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
// Important! Must be required first to get proper hooks in place.
require('../lib/monitoring');
const { config } = require('../config');
const { CapabilityManager } = require('@fxa/payments/capability');
const { EligibilityManager } = require('@fxa/payments/eligibility');
const {
ContentfulClient,
ContentfulManager,
} = require('@fxa/shared/contentful');
const TracingProvider = require('fxa-shared/tracing/node-tracing');
const error = require('../lib/error');
const jwtool = require('fxa-jwtool');
const { StatsD } = require('hot-shots');
const { Container } = require('typedi');
const { StripeHelper } = require('../lib/payments/stripe');
const { PlayBilling } = require('../lib/payments/iap/google-play');
const { CurrencyHelper } = require('../lib/payments/currencies');
const {
AuthLogger,
AuthFirestore,
AppConfig,
ProfileClient,
} = require('../lib/types');
const { setupFirestore } = require('../lib/firestore-db');
const { AppleIAP } = require('../lib/payments/iap/apple-app-store/apple-iap');
const { AccountEventsManager } = require('../lib/account-events');
const { AccountDeleteManager } = require('../lib/account-delete');
const { gleanMetrics } = require('../lib/metrics/glean');
const Customs = require('../lib/customs');
const Profile = require('../lib/profile/client');
async function run(config) {
Container.set(AppConfig, config);
const statsd = config.statsd.enabled
? new StatsD({
...config.statsd,
errorHandler: (err) => {
// eslint-disable-next-line no-use-before-define
log.error('statsd.error', err);
},
})
: {
increment: () => {},
timing: () => {},
close: () => {},
};
Container.set(StatsD, statsd);
const log = require('../lib/log')({
...config.log,
statsd,
nodeTracer: TracingProvider.getCurrent(),
});
Container.set(AuthLogger, log);
if (!Container.has(AuthFirestore)) {
const authFirestore = setupFirestore(config);
Container.set(AuthFirestore, authFirestore);
}
const redis = require('../lib/redis')(
{ ...config.redis, ...config.redis.sessionTokens },
log
);
const DB = require('../lib/db')(
config,
log,
require('../lib/tokens')(log, config),
require('../lib/crypto/random').base32(config.signinUnblock.codeLength)
);
let database = null;
try {
database = await DB.connect(config, redis);
} catch (err) {
log.error('DB.connect', { err: { message: err.message } });
process.exit(1);
}
const oauthDb = require('../lib/oauth/db');
const push = require('../lib/push')(log, database, config, statsd);
const { pushboxApi } = require('../lib/pushbox');
const pushbox = pushboxApi(log, config, statsd);
const accountEventsManager = config.accountEvents.enabled
? new AccountEventsManager(database)
: {
recordEmailEvent: async () => Promise.resolve(),
recordSecurityEvent: async () => Promise.resolve(),
};
Container.set(AccountEventsManager, accountEventsManager);
// Set currencyHelper before stripe and paypal helpers, so they can use it.
try {
// eslint-disable-next-line
const currencyHelper = new CurrencyHelper(config);
Container.set(CurrencyHelper, currencyHelper);
} catch (err) {
log.error('Invalid currency configuration', {
err: { message: err.message },
});
process.exit(1);
}
/** @type {undefined | import('../lib/payments/stripe').StripeHelper} */
let stripeHelper = undefined;
if (config.subscriptions && config.subscriptions.stripeApiKey) {
if (
config.contentful &&
config.contentful.cdnUrl &&
config.contentful.graphqlUrl &&
config.contentful.apiKey &&
config.contentful.spaceId &&
config.contentful.environment &&
config.contentful.firestoreCacheCollectionName
) {
const contentfulClient = new ContentfulClient({
cdnApiUri: config.contentful.cdnUrl,
graphqlApiUri: config.contentful.graphqlUrl,
graphqlApiKey: config.contentful.apiKey,
graphqlSpaceId: config.contentful.spaceId,
graphqlEnvironment: config.contentful.environment,
firestoreCacheCollectionName:
config.contentful.firestoreCacheCollectionName,
});
const contentfulManager = new ContentfulManager(contentfulClient, statsd);
Container.set(ContentfulManager, contentfulManager);
const capabilityManager = new CapabilityManager(contentfulManager);
const eligibilityManager = new EligibilityManager(contentfulManager);
Container.set(CapabilityManager, capabilityManager);
Container.set(EligibilityManager, eligibilityManager);
}
const { createStripeHelper } = require('../lib/payments/stripe');
stripeHelper = createStripeHelper(log, config, statsd);
Container.set(StripeHelper, stripeHelper);
if (config.subscriptions.paypalNvpSigCredentials.enabled) {
const { PayPalClient } = require('@fxa/payments/paypal');
const { PayPalHelper } = require('../lib/payments/paypal/helper');
const paypalClient = new PayPalClient(
config.subscriptions.paypalNvpSigCredentials
);
Container.set(PayPalClient, paypalClient);
const paypalHelper = new PayPalHelper({ log });
Container.set(PayPalHelper, paypalHelper);
}
}
// Create PlayBilling if enabled by fetching it.
if (
config.subscriptions &&
config.subscriptions.playApiServiceAccount &&
config.subscriptions.playApiServiceAccount.enabled
) {
Container.get(PlayBilling);
}
// Create AppleIAP if enabled by fetching it.
if (config?.subscriptions?.appStore?.enabled) {
Container.get(AppleIAP);
}
// The AccountDeleteManager is dependent on some of the object set into
// Container above.
const accountDeleteManager = new AccountDeleteManager({
fxaDb: database,
oauthDb,
push,
pushbox,
statsd,
});
Container.set(AccountDeleteManager, accountDeleteManager);
const profile = new Profile(log, config, error, statsd);
Container.set(ProfileClient, profile);
const bounces = require('../lib/bounces')(config, database);
const senders = await require('../lib/senders')(log, config, bounces, statsd);
const serverPublicKeys = {
primary: jwtool.JWK.fromFile(config.publicKeyFile, {
algorithm: 'RS',
use: 'sig',
kty: 'RSA',
}),
secondary: config.oldPublicKeyFile
? jwtool.JWK.fromFile(config.oldPublicKeyFile, {
algorithm: 'RS',
use: 'sig',
kty: 'RSA',
})
: null,
};
const signer = require('../lib/signer')(config.secretKeyFile, config.domain);
const Password = require('../lib/crypto/password')(log, config);
const customs = new Customs(config.customsUrl, log, error, statsd);
const zendeskClient = require('../lib/zendesk-client').createZendeskClient(
config
);
const glean = gleanMetrics(config);
const routes = require('../lib/routes')(
log,
serverPublicKeys,
signer,
database,
senders.email,
Password,
config,
customs,
zendeskClient,
statsd,
profile,
stripeHelper,
redis,
glean,
push,
pushbox
);
const Server = require('../lib/server');
const server = await Server.create(
log,
error,
config,
routes,
database,
statsd,
glean
);
try {
await server.start();
log.info('server.start.1', {
msg: `running on ${server.info.uri}`,
});
} catch (err) {
log.error('server.start.1', {
msg: 'failed startup with error',
err: { message: err.message },
});
}
return {
server,
log: log,
async close() {
log.info('shutdown');
await server.stop();
statsd.close();
try {
senders.email.stop();
} catch (e) {
// XXX: simplesmtp module may quit early and set socket to `false`, stopping it may fail
log.warn('shutdown', { message: 'Mailer client already disconnected' });
}
await database.close();
},
};
}
async function main() {
try {
const server = await run(config.getProperties());
process.on('uncaughtException', (err) => {
server.log.fatal('uncaughtException', err);
process.exit(8);
});
process.on('unhandledRejection', (reason, promise) => {
server.log.fatal('promise.unhandledRejection', { error: reason });
});
const shutdown = async () => {
await server.close();
process.exit(); //XXX: because of openid dep ಠ_ಠ
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
server.log.on('error', shutdown);
if (config.get('env') !== 'prod') {
server.log.info('startConfig', { config: config.toString() });
}
} catch (err) {
console.error(err); // eslint-disable-line no-console
process.exit(8);
}
}
if (require.main === module) {
main();
} else {
module.exports = run;
}