-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathenvironment.js
314 lines (285 loc) · 10.6 KB
/
environment.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
303
304
305
306
307
308
309
310
311
312
313
314
// environment.js
// ------------------------------------------------------------------
//
// Tests for environment operations.
//
// Copyright 2018-2025 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/* global process, path, describe, faker, it, before, after, assert, require */
const common = require("./common"),
util = require("util");
describe("Environment", function () {
const resourceDir = "./test/resources",
dateVal = new Date().valueOf(),
contrivedNamePrefix = "apigee-edge-js-test-" + dateVal;
this.timeout(common.testTimeout);
this.slow(common.slowThreshold);
common.connectApigee(function (org) {
let environments = [];
before((done) => {
org.environments.get(function (e, result) {
assert.isNull(e, "error listing: " + JSON.stringify(e));
environments = result.filter((item) => item != "portal");
done();
});
});
describe("get", function () {
it("should get the list of environments", (done) => {
org.environments.get({}, function (e, result) {
assert.isNull(e, "error getting: " + JSON.stringify(e));
assert.isAtLeast(result.length, 1, "zero results");
done();
});
});
it("should get details for each env", (done) => {
let numDoneEnv = 0;
environments.forEach((env) => {
org.environments.get({ environment: env }, (e, result) => {
assert.isNull(e, "error getting: " + JSON.stringify(e));
assert.equal(result.name, env);
numDoneEnv++;
if (numDoneEnv == environments.length) {
done();
}
});
});
});
it("should fail to get details from a non-existent env", (done) => {
org.environments.get(
{ environment: faker.random.alphaNumeric(22) },
(e, result) => {
assert.isNotNull(e, "the expected error did not occur");
done();
},
);
});
});
describe("get vhosts", function () {
it("should get the vhosts for each environment", () => {
let fn = (p, env) =>
p.then((count) =>
org.environments
.getVhosts({ environment: env })
.then((result) => {
assert.isAtLeast(result.length, 1, "zero results");
})
.then(() => count + 1),
);
return environments.reduce(fn, Promise.resolve(0));
});
it("should fail to get vhosts from a non-existent env", function (done) {
org.environments.getVhosts(
{ environment: faker.random.alphaNumeric(22) },
function (e, result) {
assert.isNotNull(e, "the expected error did not occur");
done();
},
);
});
it("should inquire each vhost in each environment", () => {
let fn2 = (env) => (p, vhost) =>
p.then((count) =>
org.environments.getVhost({ env, vhost }).then(() => count + 1),
);
let fn1 = (p, env) =>
p.then((count) =>
org.environments
.getVhosts({ environment: env })
.then((vhosts) => {
assert.isAtLeast(vhosts.length, 1, "zero results");
return vhosts.reduce(fn2(env), Promise.resolve(0));
})
.then(() => count + 1),
);
return environments.reduce(fn1, Promise.resolve(0));
});
it("should fail to inquire a non-existent vhost in each environment", (done) => {
let numDone = 0;
const tick = () => {
if (++numDone == environments.length) {
done();
}
};
environments.forEach((env) => {
let fakeName = faker.random.alphaNumeric(22);
org.environments.getVhost({ env, vhost: fakeName }, (e, vhosts) => {
assert.isNotNull(e, "the expected error did not occur");
tick();
});
});
});
});
describe("create/delete vhosts", function () {
let selectedEnvironment;
const keyAlias = "alias-" + faker.random.alphaNumeric(8);
const keyStoreName1 =
contrivedNamePrefix + "-" + faker.random.alphaNumeric(10);
const keyStoreName2 =
contrivedNamePrefix + "-" + faker.random.alphaNumeric(10);
// const resolveHome = function (filepath) {
// if (filepath[0] === "~") {
// return path.join(process.env.HOME, filepath.slice(1));
// }
// return filepath;
// };
// Creation of vhosts works only with a Cert that has been signed by a commercial CA.
const certFile1 = path.resolve(
path.join(resourceDir, "20250115-0335.cert"), // apigee-edge-js-20231212.cert
);
const certFile2 = path.resolve(
path.join(resourceDir, "apigee-edge-js-wildcard-expired.cert"),
);
//const certFile = resolveHome( '~/dev/dinochiesa.net/keys/fullchain.pem');
//console.log('\n\n** certfile: ' + certFile + '\n');
before(() => {
// select one environment
do {
const ix = Math.floor(Math.random() * environments.length);
selectedEnvironment = environments[ix];
} while (selectedEnvironment == "portal");
//console.log(`selectedEnv: ${selectedEnvironment}`);
const options = {
environment: selectedEnvironment,
name: keyStoreName1,
};
return org.keystores.create(options).then((_r) => {
options.certificateFile = certFile1;
//options.keyFile = certFile.replace(new RegExp('fullchain\\.'), 'privkey.');
options.keyFile = certFile1.replace(new RegExp("\\.cert"), ".key");
options.alias = keyAlias;
return org.keystores.importCert(options).catch((e) => {
console.log("in before all, error: " + util.format(e));
throw e;
});
});
// TODO: create keystore with expired cert
});
after(() =>
org.keystores
.del({
environment: selectedEnvironment,
name: keyStoreName1,
})
.catch((e) => {
console.log("in after all, error: " + util.format(e));
throw e;
}),
);
it("should create a vhost w/ explicit port", () => {
const port = 443;
//const hostalias = faker.lorem.word() + '-' + faker.random.number() + '.apigee-edge-js.net';
// must be www.dinochiesa.net or dinochiesa.net ?
//const hostalias = "dinochiesa.net";
// to make this work I had to create a cert with letsencrypt manually
const hostalias = "user.dchiesa.demo.altostrat.com";
const options = {
env: selectedEnvironment,
vhost: contrivedNamePrefix + "-" + faker.random.alphaNumeric(8),
port,
aliases: [hostalias],
keyStore: keyStoreName1,
keyAlias,
};
return org.environments.createVhost(options).catch((e) => {
console.log("w/ explicit port, error: " + util.format(e.result));
throw e;
});
});
it("should create a vhost w/ no port", () => {
//const hostalias = faker.lorem.word() + '-' + faker.random.number() + '.apigee-edge-js.net';
//const hostalias = "apigee-js-test.dinochiesa.net";
const hostalias = "user.dchiesa.demo.altostrat.com";
const options = {
env: selectedEnvironment,
vhost: contrivedNamePrefix + "-" + faker.random.alphaNumeric(8),
aliases: [hostalias],
keyStore: keyStoreName1,
keyAlias,
};
return org.environments.createVhost(options).catch((e) => {
console.log("w/ no port, error: " + util.format(e.result));
throw e;
});
});
it("should fail to create a vhost with an expired cert", async () => {
//const hostalias = faker.lorem.word() + '-' + faker.random.number() + '.apigee-edge-js.net';
const hostalias = "apigee-js-test.dinochiesa.net";
const options = {
env: selectedEnvironment,
vhost: contrivedNamePrefix + "-" + faker.random.alphaNumeric(8),
aliases: [hostalias],
keyStore: keyStoreName2,
keyAlias,
};
try {
await org.environments.createVhost(options);
} catch (e) {
// TODO: be more explicit about the expected error
assert.isNotNull(e, "expected an error while creating");
return;
}
assert.isOk(false, "createVhost must throw");
});
it("should fail to create a vhost with invalid port", async () => {
const minPort = 4000,
maxPort = 8000;
const port = Math.floor(Math.random() * maxPort - minPort) + minPort;
const vhostName =
"apigee-edge-js-test-" +
faker.lorem.word() +
"-" +
faker.random.number();
const hostalias =
faker.lorem.word() +
"-" +
faker.random.number() +
".apigee-edge-js.net";
const options = {
env: selectedEnvironment,
vhost: vhostName,
port,
aliases: [hostalias],
keyStore: keyStoreName1,
keyAlias,
};
try {
await org.environments.createVhost(options);
} catch (e) {
// TODO: be more explicit about the expected error
assert.isNotNull(e, "expected an error while creating");
return;
}
assert.isOk(false, "createVhost must throw");
});
it("should delete previously created vhosts", () => {
let env = selectedEnvironment;
let fn = (p, vhost) =>
p.then((count) =>
org.environments.deleteVhost({ vhost, env }).then(() => count + 1),
);
return org.environments.getVhosts({ env }).then((vhosts) =>
vhosts
.filter((item) => item.match(new RegExp("^apigee-edge-js-test-.*")))
.reduce(fn, Promise.resolve(0))
.then((numDeleted) => {
if (numDeleted < 2) {
assert.fail(`deleted ${numDeleted} vhosts... not enough!`);
}
}),
);
});
});
});
});