/
fake-server.ts
226 lines (204 loc) · 5.88 KB
/
fake-server.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
import * as restify from 'restify';
import { AnnotatedIacIssue } from '../../src/lib/snyk-test/iac-test-result';
import { SEVERITY } from '../../src/lib/snyk-test/legacy';
interface FakeServer extends restify.Server {
_reqLog: restify.Request[];
_nextResponse?: restify.Response;
_nextStatusCode?: number;
popRequest: () => restify.Request;
popRequests: (num: number) => restify.Request[];
setNextResponse: (r: any) => void;
setNextStatusCodeAndResponse: (c: number, r: any) => void;
}
export function fakeServer(root, apikey) {
const server = restify.createServer({
name: 'snyk-mock-server',
version: '1.0.0',
handleUncaughtExceptions: true,
}) as FakeServer;
server._reqLog = [];
server.popRequest = () => {
return server._reqLog.pop()!;
};
server.popRequests = (num: number) => {
return server._reqLog.splice(server._reqLog.length - num, num);
};
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser({ mapParams: true }));
server.use(restify.plugins.bodyParser({ mapParams: true }));
server.use(function logRequest(req, res, next) {
server._reqLog.push(req);
next();
});
[root + '/verify/callback', root + '/verify/token'].map((url) => {
server.post(url, (req, res) => {
if (req.params.api && req.params.api === apikey) {
return res.send({
ok: true,
api: apikey,
});
}
if (req.params.token) {
return res.send({
ok: true,
api: apikey,
});
}
res.status(401);
res.send({
ok: false,
});
});
});
server.use((req, res, next) => {
if (!server._nextResponse && !server._nextStatusCode) {
return next();
}
const response = server._nextResponse;
delete server._nextResponse;
if (server._nextStatusCode) {
const code = server._nextStatusCode;
delete server._nextStatusCode;
res.send(code, response);
} else {
res.send(response);
}
});
server.get(root + '/vuln/:registry/:module', (req, res, next) => {
res.send({
vulnerabilities: [],
});
return next();
});
server.post(root + '/vuln/:registry', (req, res, next) => {
const vulnerabilities = [];
if (req.query.org && req.query.org === 'missing-org') {
res.status(404);
res.send({
code: 404,
userMessage: 'cli error message',
});
return next();
}
res.send({
vulnerabilities,
org: 'test-org',
isPrivate: true,
});
return next();
});
server.post(root + '/vuln/:registry/patches', (req, res, next) => {
res.send({
vulnerabilities: [],
});
return next();
});
server.post(root + '/test-dep-graph', (req, res, next) => {
if (req.query.org && req.query.org === 'missing-org') {
res.status(404);
res.send({
code: 404,
userMessage: 'cli error message',
});
return next();
}
res.send({
result: {
issuesData: {},
affectedPkgs: {},
},
meta: {
org: 'test-org',
isPublic: false,
},
});
return next();
});
server.post(root + '/docker-jwt/test-dep-graph', (req, res, next) => {
res.send({
result: {
issuesData: {},
affectedPkgs: {},
},
});
return next();
});
server.post(root + '/test-iac', (req, res, next) => {
if (req.query.org && req.query.org === 'missing-org') {
res.status(404);
res.send({
code: 404,
userMessage: 'cli error message',
});
return next();
}
// const cloudConfigResults = [{
// id: 'SNYK-CC-K8S-1',
// title: 'Reducing the admission of containers with dropped capabilities',
// description: 'The requiredDropCapabilities property (as part of the Pod Security Policy) provides a whitelist of capabilities that must be dropped from containers (https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities). These capabilities are removed from the default set, and must not be added. It’s recommended to drop all the capabilities (using ALL), or at least to drop NET_RAW (which allows a process to spy on packets on its network / to inject data onto the network).',
// cloudConfigPath: ['[DocId: 2]','input','spec','requiredDropCapabilities'],
// severity: 'high',
// isIgnored: false,
// type: 'k8s',
// subType: 'Deployment',
// }];
res.send({
result: {
projectType: 'k8sconfig',
cloudConfigResults: [],
},
meta: {
org: 'test-org',
isPublic: false,
},
});
return next();
});
server.get(
root + '/cli-config/feature-flags/:featureFlag',
(req, res, next) => {
const flag = req.params.featureFlag;
if ((req as any).params.org === 'no-flag') {
res.send({
ok: false,
userMessage: `Org ${
(req as any).org
} doesn\'t have \'${flag}\' feature enabled'`,
});
return next();
}
res.send({
ok: true,
});
return next();
},
);
server.get(root + '/authorization/:action', (req, res, next) => {
res.send({ result: { allowed: true } });
return next();
});
server.put(root + '/monitor/:registry/graph', (req, res, next) => {
res.send({
id: 'monitor',
uri: `${req.params.registry}/graph/some/project-id`,
isMonitored: true,
});
return next();
});
server.put(root + '/monitor/:registry', (req, res, next) => {
res.send({
id: 'monitor',
uri: `${req.params.registry}/some/project-id`,
isMonitored: true,
});
return next();
});
server.setNextResponse = (response) => {
server._nextResponse = response;
};
server.setNextStatusCodeAndResponse = (code, body) => {
server._nextStatusCode = code;
server._nextResponse = body;
};
return server;
}