Skip to content

Commit

Permalink
Clean up and expose DB config
Browse files Browse the repository at this point in the history
  • Loading branch information
skadefro committed Nov 20, 2023
1 parent c5591bd commit 6421752
Show file tree
Hide file tree
Showing 16 changed files with 729 additions and 635 deletions.
44 changes: 44 additions & 0 deletions OpenFlow/src/Audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ export class Audit {
Logger.otel.endSpan(span);
}
}
public static async IssueLicense(username: string,userid: string, customerid: string, remoteip: string, domain: string, months: number, success: boolean, error: string, parent: Span): Promise<void> {
const span: Span = Logger.otel.startSubSpan("Audit.IssueLicense", parent);
try {
const log: LicenseKey = new LicenseKey();
log.type = "issue";
log.remoteip = remoteip;
log.ip = Audit.dot2num(log.remoteip);
log.success = success;
log.months = months;
log.customerid = customerid;
if(success) {
log.name = domain + " " + months + " months";
} else {
if(error != null && error != "") {
log.name = error;
} else {
log.name = domain + " failed";
}
}
log.username = username;
log.userid = userid;
Config.db.InsertOne(log, "audit", 0, false, Crypt.rootToken(), span);
} catch (error) {
Logger.instanse.error(error, span);
}
finally {
Logger.otel.endSpan(span);
}
}
static dot2num(dot: string): number {
if (NoderedUtil.IsNullEmpty(dot)) return 0;
if (dot.indexOf(".") == -1) return 0;
Expand Down Expand Up @@ -223,6 +252,21 @@ export class Nodered extends Base {
this._type = "nodered";
}
}
export class LicenseKey extends Base {
public success: boolean;
public type: string;
public userid: string;
public username: string;
public remoteip: string;
public ip: number;
public domain: string;
public months: number;
public customerid: string;
constructor() {
super();
this._type = "license";
}
}
export class auditWorkitem extends Base {
public success: boolean;
public type: string;
Expand Down
559 changes: 53 additions & 506 deletions OpenFlow/src/Config.ts

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion OpenFlow/src/DatabaseConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ export class DatabaseConnection extends events.EventEmitter {
if (collectionname === "config" && _type === "config") {
discardspan = false;
await dbConfig.Reload(Crypt.rootToken(), span);

}
}
span.updateName("Watch " + collectionname + " " + next.operationType + " " + _type);
Expand Down Expand Up @@ -1090,9 +1091,11 @@ export class DatabaseConnection extends events.EventEmitter {
// }
}
span?.setAttribute("results", arr.length);
let ms = Logger.otel.endTimer(ot_end, DatabaseConnection.mongodb_query, DatabaseConnection.otel_label(collectionname, user, "query"));
var ms = Logger.otel.endTimer(ot_end, DatabaseConnection.mongodb_query, DatabaseConnection.otel_label(collectionname, user, "query"));
if (decrypt) for (let i: number = 0; i < arr.length; i++) { arr[i] = this.decryptentity(arr[i]); }
DatabaseConnection.traversejsondecode(arr);
var log_database_queries = Config.log_database_queries;
var log_database_queries_ms = Config.log_database_queries_ms;
if (Config.log_database_queries && ms >= Config.log_database_queries_ms) {
Logger.instanse.debug(JSON.stringify(query), span, { collection: collectionname, user: user?.username, ms, count: arr.length });
} else {
Expand Down
201 changes: 110 additions & 91 deletions OpenFlow/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export class Logger {
}
public json(obj, span: Span) {
if (Config.unittesting) return;
if(obj.func == "_Housekeeping") {
obj.cls = "Housekeeping";
}

const { cls, func, message, lvl } = obj;
if (!NoderedUtil.IsNullEmpty(func) && span != null && span.isRecording()) {
var stringifyError = function (err, filter, space) {
Expand All @@ -112,10 +116,9 @@ export class Logger {
}
span.addEvent(obj.message, obj)
}
if (obj.ms != null && obj.ms != "") {
if (obj.ms != null && obj.ms != "" && obj.func != "query" && Config.log_database_queries) {
if (obj.ms < Config.log_database_queries_ms) return;
}
if (Logger.enabled[cls]) {
} else if (Logger.enabled[cls]) {
if (Logger.enabled[cls] < lvl) return;
} else {
if (Config.log_silly) {
Expand Down Expand Up @@ -279,7 +282,7 @@ export class Logger {
if (Config.db != null) await Config.db.shutdown();
await Logger.otel.shutdown();
}
public static reload() {
public static async reload() {
Logger.log_with_trace = Config.log_with_trace;
Logger.usecolors = Config.log_with_colors;
// if (Config.NODE_ENV == "development") Logger.log_with_trace = true;
Expand All @@ -302,6 +305,11 @@ export class Logger {
if (Config.otel_err_log) Logger.enabled["WebSocketServerClient"] = level.Error;
if (Config.log_database_queries) Logger.enabled["log_database_queries"] = level.Verbose;

try {
await Logger.License?.validate();
} catch (error) {

}
}
static hasDockerEnv(): boolean {
try {
Expand Down Expand Up @@ -333,114 +341,125 @@ export class Logger {
Logger._isKubernetes = true;
return true;
}

static async relaodotel() {

}
static _otel_require: any = null;
static async configure(skipotel: boolean, skiplic: boolean): Promise<void> {
Logger.DBHelper = new DBHelper();
Logger.reload()
Logger.instanse = new Logger();
let _lic_require: any = null;
try {
if (!skiplic) _lic_require = require("./ee/license-file");
} catch (error) {
}
if (_lic_require != null) {
Logger.License = new _lic_require.LicenseFile();
} else {
Logger.License = {} as any;
Logger.License.ofid = Logger.ofid;
Logger.License.shutdown = () => undefined;
await Logger.reload()
if(Logger.instanse == null) Logger.instanse = new Logger();
if(Logger.License == null) {
let _lic_require: any = null;
try {
if (!skiplic && _lic_require == null) _lic_require = require("./ee/license-file");
} catch (error) {
}
if (_lic_require != null) {
Logger.License = new _lic_require.LicenseFile();
} else {
Logger.License = {} as any;
Logger.License.ofid = Logger.ofid;
Logger.License.shutdown = () => undefined;
}
}

let _otel_require: any = null;
try {
if (!skipotel) _otel_require = require("./ee/otel");
} catch (error) {

}
if (_otel_require != null) {
Logger.otel = await _otel_require.otel.configure();
if(Logger.otel == null) {
try {
if (!skipotel && Logger._otel_require == null) Logger._otel_require = require("./ee/otel");
} catch (error) {

}
if (Logger._otel_require != null) {
Logger.otel = await Logger._otel_require.otel.configure();
} else {
const fakespan = {
context: () => undefined,
setAttribute: () => undefined,
setAttributes: () => undefined,
addEvent: () => undefined,
setStatus: () => undefined,
updateName: () => undefined,
end: () => undefined,
isRecording: () => undefined,
recordException: () => undefined,
};
Logger.otel =
{
startSpan: () => fakespan,
startSubSpan: () => fakespan,
startSpanExpress: () => fakespan,
GetTraceSpanId(span: Span): [string, string] { return ["", ""]; },
endSpan: () => undefined,
startTimer: () => undefined,
endTimer: () => undefined,
setdefaultlabels: () => undefined,
shutdown: () => undefined,
meter: {
createHistogram: () => undefined,
createCounter: () => undefined,
createObservableUpDownCounter: () => undefined,
createUpDownCounter: () => undefined,
createValueObserver: () => undefined,
createObservableGauge: () => undefined,
}
} as any;
}
} else {
const fakespan = {
context: () => undefined,
setAttribute: () => undefined,
setAttributes: () => undefined,
addEvent: () => undefined,
setStatus: () => undefined,
updateName: () => undefined,
end: () => undefined,
isRecording: () => undefined,
recordException: () => undefined,
};
Logger.otel =
{
startSpan: () => fakespan,
startSubSpan: () => fakespan,
startSpanExpress: () => fakespan,
GetTraceSpanId(span: Span): [string, string] { return ["", ""]; },
endSpan: () => undefined,
startTimer: () => undefined,
endTimer: () => undefined,
setdefaultlabels: () => undefined,
shutdown: () => undefined,
meter: {
createHistogram: () => undefined,
createCounter: () => undefined,
createObservableUpDownCounter: () => undefined,
createUpDownCounter: () => undefined,
createValueObserver: () => undefined,
createObservableGauge: () => undefined,
}
} as any;
if (Logger._otel_require != null) {
Logger.otel = await Logger._otel_require.otel.configure();
}
}


this.agentdriver = null; // with npm -omit=optional we need to install npm i openid-client
if(this.agentdriver == null) {
this.agentdriver = null; // with npm -omit=optional we need to install npm i openid-client

if (NoderedUtil.IsNullEmpty(process.env["USE_KUBERNETES"])) {
try {
this.agentdriver = new dockerdriver();
if (!(await this.agentdriver.detect())) {
if (NoderedUtil.IsNullEmpty(process.env["USE_KUBERNETES"])) {
try {
this.agentdriver = new dockerdriver();
if (!(await this.agentdriver.detect())) {
this.agentdriver = null;
}
} catch (error) {
this.agentdriver = null;
}
} catch (error) {
this.agentdriver = null;
}
}
if (this.agentdriver == null && (!NoderedUtil.IsNullEmpty(process.env["KUBERNETES_SERVICE_HOST"]) || !NoderedUtil.IsNullEmpty(process.env["USE_KUBERNETES"]))) {
let _driver: any = null;
try {
_driver = require("./ee/kubedriver");
} catch (error) {
console.log(error)
if (this.agentdriver == null && (!NoderedUtil.IsNullEmpty(process.env["KUBERNETES_SERVICE_HOST"]) || !NoderedUtil.IsNullEmpty(process.env["USE_KUBERNETES"]))) {
let _driver: any = null;
try {
_driver = require("./ee/kubedriver");
} catch (error) {
console.log(error)
}
try {
if (_driver != null) {
this.agentdriver = new _driver.kubedriver();
} else {
this.agentdriver = new dockerdriver();
}
if (_driver != null) {
if (!(await this.agentdriver.detect())) {
this.agentdriver = null;
}
}
} catch (error) {
this.agentdriver = null;
Logger.instanse.error(error, null);
}
}
try {
if (_driver != null) {
this.agentdriver = new _driver.kubedriver();
} else {
if (this.agentdriver == null) {
try {
this.agentdriver = new dockerdriver();
}
if (_driver != null) {
if (!(await this.agentdriver.detect())) {
this.agentdriver = null;
}
}
} catch (error) {
this.agentdriver = null;
Logger.instanse.error(error, null);
}
}
if (this.agentdriver == null) {
try {
this.agentdriver = new dockerdriver();
if (!(await this.agentdriver.detect())) {
} catch (error) {
this.agentdriver = null;
Logger.instanse.error(error, null);
}
} catch (error) {
this.agentdriver = null;
Logger.instanse.error(error, null);
}
}

}
static instanse: Logger = null;
private static _ofid = null;
Expand Down
1 change: 0 additions & 1 deletion OpenFlow/src/LoginProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,6 @@ export class LoginProvider {
validate_emails: Config.validate_emails,
forgot_pass_emails: Config.forgot_pass_emails,
supports_watch: Config.supports_watch,
nodered_images: Config.nodered_images,
agent_images: Config.agent_images,
amqp_enabled_exchange: Config.amqp_enabled_exchange,
multi_tenant: Config.multi_tenant,
Expand Down
Loading

0 comments on commit 6421752

Please sign in to comment.