-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathapp.ts
192 lines (176 loc) · 6.03 KB
/
app.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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { isIPv4, isIPv6 } from "net";
import {
IDeltaService,
IDocumentStorage,
IProducer,
ITenantManager,
IThrottler,
ICache,
IDocumentRepository,
ITokenRevocationManager,
IRevokedTokenChecker,
IClusterDrainingChecker,
IFluidAccessTokenGenerator,
IReadinessCheck,
} from "@fluidframework/server-services-core";
import { TypedEventEmitter } from "@fluidframework/common-utils";
import { ICollaborationSessionEvents } from "@fluidframework/server-lambdas";
import { json, urlencoded } from "body-parser";
import compression from "compression";
import cookieParser from "cookie-parser";
import express from "express";
import shajs from "sha.js";
import { Provider } from "nconf";
import { DriverVersionHeaderName, IAlfredTenant } from "@fluidframework/server-services-client";
import {
alternativeMorganLoggerMiddleware,
bindTelemetryContext,
bindTimeoutContext,
jsonMorganLoggerMiddleware,
} from "@fluidframework/server-services-utils";
import { RestLessServer, IHttpServerConfig } from "@fluidframework/server-services";
import { BaseTelemetryProperties, HttpProperties } from "@fluidframework/server-services-telemetry";
import { catch404, getIdFromRequest, getTenantIdFromRequest, handleError } from "../utils";
import { IDocumentDeleteService } from "./services";
import * as alfredRoutes from "./routes";
export function create(
config: Provider,
tenantManager: ITenantManager,
tenantThrottlers: Map<string, IThrottler>,
clusterThrottlers: Map<string, IThrottler>,
singleUseTokenCache: ICache,
storage: IDocumentStorage,
appTenants: IAlfredTenant[],
deltaService: IDeltaService,
producer: IProducer,
documentRepository: IDocumentRepository,
documentDeleteService: IDocumentDeleteService,
startupCheck: IReadinessCheck,
tokenRevocationManager?: ITokenRevocationManager,
revokedTokenChecker?: IRevokedTokenChecker,
collaborationSessionEventEmitter?: TypedEventEmitter<ICollaborationSessionEvents>,
clusterDrainingChecker?: IClusterDrainingChecker,
enableClientIPLogging?: boolean,
readinessCheck?: IReadinessCheck,
fluidAccessTokenGenerator?: IFluidAccessTokenGenerator,
) {
// Maximum REST request size
const requestSize = config.get("alfred:restJsonSize");
const enableLatencyMetric = config.get("alfred:enableLatencyMetric") ?? false;
const httpServerConfig: IHttpServerConfig = config.get("system:httpServer");
// Express app configuration
const app: express.Express = express();
// initialize RestLess server translation
const restLessMiddleware: () => express.RequestHandler = () => {
const restLessServer = new RestLessServer({ requestSizeLimit: requestSize });
return (req, res, next) => {
restLessServer
.translate(req, res)
.then(() => next())
.catch(next);
};
};
app.use(restLessMiddleware());
// Running behind iisnode
app.set("trust proxy", 1);
app.use(compression());
app.use(bindTelemetryContext());
if (httpServerConfig?.connectionTimeoutMs) {
// If connectionTimeoutMs configured and not 0, bind timeout context.
app.use(bindTimeoutContext(httpServerConfig.connectionTimeoutMs));
}
const loggerFormat = config.get("logger:morganFormat");
if (loggerFormat === "json") {
app.use(
jsonMorganLoggerMiddleware(
"alfred",
(tokens, req, res) => {
const additionalProperties: Record<string, any> = {
[HttpProperties.driverVersion]: tokens.req(
req,
res,
DriverVersionHeaderName,
),
[BaseTelemetryProperties.tenantId]: getTenantIdFromRequest(req.params),
[BaseTelemetryProperties.documentId]: getIdFromRequest(req.params),
};
if (enableClientIPLogging === true) {
const hashedClientIP = req.ip
? shajs("sha256").update(`${req.ip}`).digest("hex")
: "";
additionalProperties.hashedClientIPAddress = hashedClientIP;
const clientIPAddress = req.ip ? req.ip : "";
if (isIPv4(clientIPAddress)) {
additionalProperties.clientIPType = "IPv4";
} else if (isIPv6(clientIPAddress)) {
additionalProperties.clientIPType = "IPv6";
} else {
additionalProperties.clientIPType = "";
}
const XAzureClientIP = "x-azure-clientip";
const hashedAzureClientIP = req.headers[XAzureClientIP]
? shajs("sha256").update(`${req.headers[XAzureClientIP]}`).digest("hex")
: "";
additionalProperties.hashedAzureClientIPAddress = hashedAzureClientIP;
const XAzureSocketIP = "x-azure-socketip";
const hashedAzureSocketIP = req.headers[XAzureSocketIP]
? shajs("sha256").update(`${req.headers[XAzureSocketIP]}`).digest("hex")
: "";
additionalProperties.hashedAzureSocketIPAddress = hashedAzureSocketIP;
}
if (req.body?.isEphemeralContainer !== undefined) {
additionalProperties.isEphemeralContainer = req.body.isEphemeralContainer;
}
const customHeadersToLog = (config.get("customHeadersToLog") as string[]) ?? [];
if (customHeadersToLog) {
customHeadersToLog.forEach((header) => {
const lowerCaseHeader = header.toLowerCase();
if (req.headers[lowerCaseHeader]) {
additionalProperties[lowerCaseHeader] =
req.headers[lowerCaseHeader];
}
});
}
return additionalProperties;
},
enableLatencyMetric,
),
);
} else {
app.use(alternativeMorganLoggerMiddleware(loggerFormat));
}
app.use(cookieParser());
app.use(json({ limit: requestSize }));
app.use(urlencoded({ limit: requestSize, extended: false }));
// Bind routes
const routes = alfredRoutes.create(
config,
tenantManager,
tenantThrottlers,
clusterThrottlers,
singleUseTokenCache,
deltaService,
storage,
producer,
appTenants,
documentRepository,
documentDeleteService,
startupCheck,
tokenRevocationManager,
revokedTokenChecker,
collaborationSessionEventEmitter,
clusterDrainingChecker,
readinessCheck,
fluidAccessTokenGenerator,
);
app.use(routes.api);
// Catch 404 and forward to error handler
app.use(catch404());
// Error handlers
app.use(handleError());
return app;
}