Skip to content

Commit d8f0ae3

Browse files
authored
Merge pull request openiap#179 from skadefro/master
bump
2 parents 562dad8 + 7d3fa0f commit d8f0ae3

16 files changed

Lines changed: 423 additions & 198 deletions

OpenFlow/src/DatabaseConnection.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export class DatabaseConnection extends events.EventEmitter {
169169
} catch (error) {
170170
console.error(error);
171171
}
172+
Logger.instanse.info("supports_watch: " + Config.supports_watch);
172173
if (Config.supports_watch) {
173174
let collections = await DatabaseConnection.toArray(this.db.listCollections());
174175
collections = collections.filter(x => x.name.indexOf("system.") === -1);
@@ -1097,8 +1098,7 @@ export class DatabaseConnection extends events.EventEmitter {
10971098
if (!user.HasRoleName("customer admins") && !user.HasRoleName("admins")) throw new Error("Access denied (not admin) to customer with id " + user2.customerid);
10981099
}
10991100
customer = await this.getbyid<Customer>(user2.customerid, "users", jwt, span)
1100-
if (customer == null) throw new Error("Access denied to customer with id " + user2.customerid);
1101-
// if (!user.HasRoleName(customer.name + " admins")) throw new Error("Access denied to customer with " + customer.name);
1101+
if (customer == null) throw new Error("Access denied to customer with id " + user2.customerid + " when updating " + user2._id);
11021102
} else if (user.HasRoleName("customer admins") && !NoderedUtil.IsNullEmpty(user.customerid)) {
11031103
// user2.customerid = user.customerid;
11041104
if (NoderedUtil.IsNullEmpty(user2.selectedcustomerid)) {
@@ -1547,7 +1547,7 @@ export class DatabaseConnection extends events.EventEmitter {
15471547
// User can update, just not created ?
15481548
// if (!user.HasRoleName("customer admins") && !user.HasRoleName("admins")) throw new Error("Access denied (not admin) to customer with id " + user2.customerid);
15491549
customer = await this.getbyid<Customer>(user2.customerid, "users", q.jwt, span)
1550-
if (customer == null) throw new Error("Access denied to customer with id " + user2.customerid);
1550+
if (customer == null) throw new Error("Access denied to customer with id " + user2.customerid + " when updating " + user2._id);
15511551
} else if (user.HasRoleName("customer admins") && !NoderedUtil.IsNullEmpty(user.customerid)) {
15521552
customer = null;
15531553
if (!NoderedUtil.IsNullEmpty(user.selectedcustomerid)) {
@@ -1560,7 +1560,7 @@ export class DatabaseConnection extends events.EventEmitter {
15601560
customer = await this.getbyid<Customer>(user2.customerid, "users", q.jwt, span);
15611561
if (customer != null) user2.customerid = user.customerid;
15621562
if (customer == null) {
1563-
throw new Error("Access denied to customer with id " + user2.customerid);
1563+
throw new Error("Access denied to customer with id " + user2.customerid + " when updating " + user2._id);
15641564
}
15651565
}
15661566
}
@@ -1692,18 +1692,31 @@ export class DatabaseConnection extends events.EventEmitter {
16921692
}
16931693
}
16941694
var _oldversion = 0;
1695+
var _skiphistory = false;
16951696
if (original != null) _oldversion = original._version;
16961697
if (q.item.hasOwnProperty("_skiphistory")) {
16971698
delete (q.item as any)._skiphistory;
16981699
if (!Config.allow_skiphistory) {
16991700
q.item._version = await this.SaveDiff(q.collectionname, original, q.item, span);
1701+
} else {
1702+
_skiphistory = true;
17001703
}
17011704
} else {
17021705
q.item._version = await this.SaveDiff(q.collectionname, original, q.item, span);
17031706
}
1704-
if (_oldversion == q.item._version) {
1705-
q.opresult = { modifiedCount: 1, result: { ok: 1 } };
1706-
return q;
1707+
if (_oldversion == q.item._version && _skiphistory == false) {
1708+
if (q.item._type === 'instance' && q.collectionname === 'workflows') {
1709+
} else {
1710+
const _skip_array: string[] = Config.skip_history_collections.split(",");
1711+
const skip_array: string[] = [];
1712+
_skip_array.forEach(x => skip_array.push(x.trim()));
1713+
if (skip_array.indexOf(q.collectionname) > -1) {
1714+
} else {
1715+
q.result = q.item;
1716+
q.opresult = { modifiedCount: 1, result: { ok: 1 } };
1717+
return q;
1718+
}
1719+
}
17071720
}
17081721
} else {
17091722
let json: string = q.item as any;
@@ -1809,19 +1822,25 @@ export class DatabaseConnection extends events.EventEmitter {
18091822
await this.db.collection(q.collectionname).deleteOne({ _id: safeid });
18101823
}
18111824
}
1812-
if (q.opresult.matchedCount == 0 && (q.w != 0)) {
1825+
if (q.opresult && q.opresult.matchedCount == 0 && (q.w != 0)) {
18131826
throw new Error("ReplaceOne failed, matched 0 documents with query {_id: '" + q.item._id + "'}");
18141827
}
1828+
if (q.opresult == null) {
1829+
Logger.instanse.error("[" + user.username + "][" + q.collectionname + "] opresult is null !!");
1830+
}
18151831
} else {
18161832
const fsc = Config.db.db.collection(q.collectionname);
18171833
const ot_end = Logger.otel.startTimer();
18181834
const mongodbspan: Span = Logger.otel.startSubSpan("mongodb.replaceOne", span);
18191835
q.opresult = await fsc.updateOne(_query, { $set: { metadata: (q.item as any).metadata } });
18201836
Logger.otel.endSpan(mongodbspan);
18211837
Logger.otel.endTimer(ot_end, DatabaseConnection.mongodb_update, { collection: q.collectionname });
1822-
if (q.opresult.matchedCount == 0 && (q.w != 0)) {
1838+
if ((q.opresult && q.opresult.matchedCount == 0) && (q.w != 0)) {
18231839
throw new Error("ReplaceOne failed, matched 0 documents with query {_id: '" + q.item._id + "'}");
18241840
}
1841+
if (q.opresult == null) {
1842+
Logger.instanse.error("[" + user.username + "][" + q.collectionname + "] opresult is null !!");
1843+
}
18251844
}
18261845
} else {
18271846
if ((q.item["$set"]) === undefined) { (q.item["$set"]) = {} };
@@ -2783,7 +2802,6 @@ export class DatabaseConnection extends events.EventEmitter {
27832802
return (Math.floor(num / precision) * precision);
27842803
};
27852804
if (item._type === 'instance' && collectionname === 'workflows') return 0;
2786-
if (item._type === 'instance' && collectionname === 'workflows') return 0;
27872805

27882806
if (!original && item._id) {
27892807
const rootjwt = Crypt.rootToken()

OpenFlow/src/Logger.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export class Logger {
1010
if (info instanceof Error || info.stack) {
1111
return `${info.timestamp} [${info.level}] ${info.message} \n ${info.stack}`;
1212
}
13-
return `${info.timestamp} [${Logger.getLabel()}][${info.level}] ${info.message}`;
13+
if (Config.NODE_ENV == "development") {
14+
return `${info.timestamp} [${Logger.getLabel()}][${info.level}] ${info.message}`;
15+
}
16+
return `${info.timestamp} [${info.level}] ${info.message}`;
1417
});
1518
static getLabel = function () {
1619
let e = new Error();
@@ -130,6 +133,7 @@ export class Logger {
130133
createValueRecorder: () => undefined,
131134
createCounter: () => undefined,
132135
createUpDownSumObserver: () => undefined,
136+
createValueObserver: () => undefined,
133137
}
134138
} as any;
135139
}

OpenFlow/src/LoginProvider.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,11 @@ export class LoginProvider {
7474
public static redirect(res: any, originalUrl: string) {
7575
res.write('<!DOCTYPE html>');
7676
res.write('<body>');
77-
// res.write('<script>top.location = "' + encodeURI(originalUrl) + '";</script>');
7877
res.write('<script>top.location = "' + LoginProvider.escape(originalUrl) + '";</script>');
79-
// res.write('<a href="' + originalUrl + '">click here</a>');
8078
res.write('</body>');
8179
res.write('</html>');
8280
res.end();
83-
// res.redirect(originalUrl);
8481
}
85-
86-
8782
static async validateToken(rawAssertion: string, parent: Span): Promise<User> {
8883
const span: Span = Logger.otel.startSubSpan("LoginProvider.validateToken", parent);
8984
return new Promise<User>((resolve, reject) => {
@@ -285,20 +280,6 @@ export class LoginProvider {
285280
}
286281
});
287282
app.get("/Signout", (req: any, res: any, next: any): void => {
288-
// const providerid: string = req.cookies.provider;
289-
// const provider: passport.Strategy;
290-
// if (providerid != null && providerid != undefined && providerid != "") {
291-
// provider = LoginProvider._providers[providerid];
292-
// }
293-
// if (provider != null && provider != undefined) {
294-
// (provider as any).logout(req, function (err, requestUrl) {
295-
// // LOCAL logout
296-
// req.logout();
297-
// // redirect to the IdP with the encrypted SAML logout request
298-
// res.redirect(requestUrl);
299-
// });
300-
// return;
301-
// }
302283
req.logout();
303284
const originalUrl: any = req.cookies.originalUrl;
304285
if (!NoderedUtil.IsNullEmpty(originalUrl)) {

OpenFlow/src/Messages/Message.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,10 +2129,11 @@ export class Message {
21292129
}
21302130
}
21312131
}
2132+
var port = 3000;
21322133
let livenessProbe: any = {
21332134
httpGet: {
21342135
path: "/livenessprobe",
2135-
port: Config.port,
2136+
port: 3000,
21362137
scheme: "HTTP"
21372138
},
21382139
initialDelaySeconds: Config.nodered_initial_liveness_delay,
@@ -2156,7 +2157,7 @@ export class Message {
21562157
if (!api_ws_url.endsWith("/")) api_ws_url += "/";
21572158

21582159

2159-
2160+
var port = 3000;
21602161
let saml_baseurl = Config.protocol + "://" + hostname + "/";
21612162

21622163
let _samlparsed = url.parse(Config.saml_federation_metadata);
@@ -2193,7 +2194,7 @@ export class Message {
21932194
if (saml_federation_metadata == "https://pc.openiap.io/issue/FederationMetadata/2007-06/FederationMetadata.xml") {
21942195
saml_federation_metadata = "https://demo.openiap.io/issue/FederationMetadata/2007-06/FederationMetadata.xml"
21952196
}
2196-
var port = 3000;
2197+
21972198

21982199
const _deployment = {
21992200
metadata: { name: name, namespace: namespace, labels: { billed: hasbilling.toString(), userid: _id, app: name } },
@@ -2303,7 +2304,8 @@ export class Message {
23032304
}
23042305

23052306
let servicename = name;
2306-
if (!/[a-z]([-a-z0-9]*[a-z0-9])?/.test(servicename)) {
2307+
var test = /[a-z]([-a-z0-9]*[a-z0-9])?/.exec(servicename);
2308+
if (test == null || test.index != 0) {
23072309
servicename = "nr" + name + "svc";
23082310
}
23092311

@@ -2379,11 +2381,15 @@ export class Message {
23792381
throw new Error("failed locating useringress");
23802382
}
23812383
} catch (error) {
2384+
if (error.response && error.response.body && error.response.body.message) {
2385+
Logger.instanse.error(new Error(error.response.body.message));
2386+
error.message = error.response.body.message;
2387+
}
23822388
span?.recordException(error);
2383-
Logger.otel.endSpan(span);
23842389
throw error;
2390+
} finally {
2391+
Logger.otel.endSpan(span);
23852392
}
2386-
Logger.otel.endSpan(span);
23872393
}
23882394
private async _DeleteNoderedInstance(_id: string, jwt: string, parent: Span): Promise<void> {
23892395
const span: Span = Logger.otel.startSubSpan("message._DeleteNoderedInstance", parent);
@@ -2418,7 +2424,8 @@ export class Message {
24182424
Logger.instanse.warn("_DeleteNoderedInstance: Did not find deployment for " + name + " in namespace " + namespace);
24192425
}
24202426
let servicename = name;
2421-
if (!/[a-z]([-a-z0-9]*[a-z0-9])?/.test(servicename)) {
2427+
var test = /[a-z]([-a-z0-9]*[a-z0-9])?/.exec(servicename);
2428+
if (test == null || test.index != 0) {
24222429
servicename = "nr" + name + "svc";
24232430
}
24242431
const service = await KubeUtil.instance().GetService(namespace, servicename);
@@ -2453,10 +2460,10 @@ export class Message {
24532460
}
24542461
} catch (error) {
24552462
span?.recordException(error);
2456-
Logger.otel.endSpan(span);
24572463
throw error;
2464+
} finally {
2465+
Logger.otel.endSpan(span);
24582466
}
2459-
Logger.otel.endSpan(span);
24602467
}
24612468
private async DeleteNoderedInstance(parent: Span): Promise<void> {
24622469
await this.DetectDocker();
@@ -2492,10 +2499,10 @@ export class Message {
24922499
}
24932500
} catch (error) {
24942501
span?.recordException(error);
2495-
Logger.otel.endSpan(span);
24962502
throw error;
2503+
} finally {
2504+
Logger.otel.endSpan(span);
24972505
}
2498-
Logger.otel.endSpan(span);
24992506
}
25002507
private async DeleteNoderedPod(parent: Span): Promise<void> {
25012508
await this.DetectDocker();

OpenFlow/src/MongoAdapter.ts

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Config } from "./Config";
22

33
export class MongoAdapter {
4-
54
public name: string = "";
65
/**
76
*
@@ -37,7 +36,7 @@ export class MongoAdapter {
3736
}
3837

3938
await this.coll().updateOne(
40-
{ _id: id },
39+
{ id },
4140
{ $set: { payload, ...(expiresAt ? { expiresAt } : undefined) } },
4241
{ upsert: true },
4342
);
@@ -164,9 +163,12 @@ export class MongoAdapter {
164163
* @param {string} id Identifier of oidc-provider model
165164
*
166165
*/
167-
async find(id) {
168-
const result = await this.coll().find(
169-
{ _id: id }).limit(1).next();
166+
async find(id: string) {
167+
return MongoAdapter.find(id);
168+
}
169+
static async find(id: string) {
170+
console.log("find: " + id);
171+
const result = await MongoAdapter.coll().find({ id }).limit(1).next();
170172
if (!result) return undefined;
171173
return result.payload;
172174
}
@@ -183,13 +185,14 @@ export class MongoAdapter {
183185
*
184186
*/
185187
async findByUserCode(userCode) {
186-
const result = await this.coll().find(
187-
{ 'payload.userCode': userCode }).limit(1).next();
188-
188+
return MongoAdapter.findByUserCode(userCode);
189+
}
190+
static async findByUserCode(userCode) {
191+
console.log("findByUserCode: " + userCode);
192+
const result = await this.coll().find({ 'payload.userCode': userCode }).limit(1).next();
189193
if (!result) return undefined;
190194
return result.payload;
191195
}
192-
193196
/**
194197
*
195198
* Return previously stored instance of Session by its uid reference property.
@@ -201,30 +204,15 @@ export class MongoAdapter {
201204
*
202205
*/
203206
async findByUid(uid) {
204-
const result = await this.coll().find(
205-
{ 'payload.uid': uid }).limit(1).next();
207+
return MongoAdapter.findByUid(uid);
208+
}
209+
static async findByUid(uid) {
210+
console.log("findByUid: " + uid);
211+
const result = await this.coll().find({ 'payload.uid': uid }).limit(1).next();
206212
if (!result) return undefined;
207213
return result.payload;
208214
}
209215

210-
/**
211-
*
212-
* Mark a stored oidc-provider model as consumed (not yet expired though!). Future finds for this
213-
* id should be fulfilled with an object containing additional property named "consumed" with a
214-
* truthy value (timestamp, date, boolean, etc).
215-
*
216-
* @return {Promise} Promise fulfilled when the operation succeeded. Rejected with error when
217-
* encountered.
218-
* @param {string} id Identifier of oidc-provider model
219-
*
220-
*/
221-
async consume(id) {
222-
await this.coll().findOneAndUpdate(
223-
{ _id: id },
224-
{ $set: { 'payload.consumed': Math.floor(Date.now() / 1000) } },
225-
);
226-
}
227-
228216
/**
229217
*
230218
* Destroy/Drop/Remove a stored oidc-provider model. Future finds for this id should be fulfilled
@@ -236,7 +224,11 @@ export class MongoAdapter {
236224
*
237225
*/
238226
async destroy(id) {
239-
await this.coll().deleteOne({ _id: id });
227+
MongoAdapter.destroy(id);
228+
}
229+
static async destroy(id) {
230+
console.log("destroy: " + id);
231+
await this.coll().deleteOne({ id });
240232
}
241233

242234
/**
@@ -250,10 +242,35 @@ export class MongoAdapter {
250242
*
251243
*/
252244
async revokeByGrantId(grantId) {
245+
MongoAdapter.revokeByGrantId(grantId);
246+
}
247+
static async revokeByGrantId(grantId) {
248+
console.log("revokeByGrantId: " + grantId);
253249
await this.coll().deleteMany({ 'payload.grantId': grantId });
254250
}
255251

252+
/**
253+
*
254+
* Mark a stored oidc-provider model as consumed (not yet expired though!). Future finds for this
255+
* id should be fulfilled with an object containing additional property named "consumed" with a
256+
* truthy value (timestamp, date, boolean, etc).
257+
*
258+
* @return {Promise} Promise fulfilled when the operation succeeded. Rejected with error when
259+
* encountered.
260+
* @param {string} id Identifier of oidc-provider model
261+
*
262+
*/
263+
async consume(id) {
264+
MongoAdapter.consume(id);
265+
}
266+
static async consume(id) {
267+
console.log("consume: " + id);
268+
await this.coll().findOneAndUpdate({ id }, { $set: { 'payload.consumed': Math.floor(Date.now() / 1000) } });
269+
}
256270
coll() {
271+
return MongoAdapter.coll();
272+
}
273+
static coll() {
257274
return Config.db.db.collection("oauthtokens");
258275
}
259276
}

0 commit comments

Comments
 (0)