From e782f17e9f6b26751b86312974d4d54551467570 Mon Sep 17 00:00:00 2001 From: akvlad Date: Tue, 31 Oct 2023 14:21:07 +0200 Subject: [PATCH] #fix: 369 --- lib/handlers/errors.js | 7 ++ package-lock.json | 246 +++++++++++++++++------------------------ package.json | 46 ++++---- qryn_bun.mjs | 35 +++++- qryn_node.js | 2 +- 5 files changed, 164 insertions(+), 172 deletions(-) diff --git a/lib/handlers/errors.js b/lib/handlers/errors.js index 4ee3e542..e9226ac9 100644 --- a/lib/handlers/errors.js +++ b/lib/handlers/errors.js @@ -28,6 +28,13 @@ const handler = (err, req, res) => { message: err.message }) } + if (err.name && err.name === 'UnauthorizedError') { + return res.status(401).send({ + statusCode: 401, + error: 'Unauthorized', + message: err.message + }) + } asyncLogError(err, req.log) return res.status(500).send({ statusCode: 500, diff --git a/package-lock.json b/package-lock.json index 38bbf90b..b6b30234 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "qryn", - "version": "2.4.4", + "version": "3.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "qryn", - "version": "2.4.4", + "version": "3.0.0", "hasInstallScript": true, "license": "AGPL-3.0", "dependencies": { @@ -19,9 +19,10 @@ "@fastify/websocket": "^7.1.1", "@qxip/influx-line-protocol-parser": "^0.2.1", "@qxip/plugnplay": "^3.3.1", - "@qxip/promql2logql": "^1.0.30", - "@stricjs/router": "^4.2.9", + "@stricjs/router": "^5.0.6", + "@stricjs/utils": "^1.6.1", "axios": "^0.21.4", + "basic-auth": "^2.0.1", "bnf": "^1.0.1", "csv-writer": "^1.6.0", "date-fns": "^2.27.0", @@ -34,6 +35,7 @@ "glob": "^7.1.2", "handlebars": "^4.7.7", "handlebars-helpers": "^0.9.8", + "http-errors": "^2.0.0", "json-stable-stringify": "^1.0.1", "jsonic": "^0.3.1", "logfmt": "^1.3.2", @@ -919,14 +921,6 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" }, - "node_modules/@fastify/static/node_modules/depd": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", - "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", - "engines": { - "node": ">= 0.8" - } - }, "node_modules/@fastify/static/node_modules/destroy": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", @@ -954,21 +948,6 @@ "url": "https://github.com/sponsors/isaacs" } }, - "node_modules/@fastify/static/node_modules/http-errors": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", - "integrity": "sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ==", - "dependencies": { - "depd": "2.0.0", - "inherits": "2.0.4", - "setprototypeof": "1.2.0", - "statuses": "2.0.1", - "toidentifier": "1.0.1" - }, - "engines": { - "node": ">= 0.8" - } - }, "node_modules/@fastify/static/node_modules/minimatch": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.0.tgz", @@ -1030,14 +1009,6 @@ "node": ">= 0.8.0" } }, - "node_modules/@fastify/static/node_modules/statuses": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", - "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==", - "engines": { - "node": ">= 0.8" - } - }, "node_modules/@fastify/websocket": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/@fastify/websocket/-/websocket-7.1.1.tgz", @@ -2322,26 +2293,6 @@ "node": ">=6.0.0" } }, - "node_modules/@qxip/promql-parser-js": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/@qxip/promql-parser-js/-/promql-parser-js-0.1.1.tgz", - "integrity": "sha512-/jEZBk7I3oH3ETTSWKaYbHSNkMVkOFouQmYEOiesz4wBPTPRKedLYO808hh2wAUzv8lUqluMY53dRhXyk0ZAZg==" - }, - "node_modules/@qxip/promql2logql": { - "version": "1.0.30", - "resolved": "https://registry.npmjs.org/@qxip/promql2logql/-/promql2logql-1.0.30.tgz", - "integrity": "sha512-5mVTmhiZRzOz3WUvdxKFsZ1JntR5/PfpzbIO0xN0dSyvFfvcT7xEAf4Rc2OfKHvPLZdhzY1TVm32QmiciT3cGA==", - "dependencies": { - "@qxip/promql-parser-js": "^0.1.1", - "jsonic": "^1.0.1", - "squirrelly": "^8.0.8" - } - }, - "node_modules/@qxip/promql2logql/node_modules/jsonic": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/jsonic/-/jsonic-1.0.1.tgz", - "integrity": "sha512-6GitEN4plTuB/I1o9kDZl7Pgc+DvFG1BG88IqaUz4eQglCA1uAgxWdXhLNA6ffaYsmzPjOysDpp6CYTwRiuXLw==" - }, "node_modules/@qxip/to-file": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/@qxip/to-file/-/to-file-0.2.1.tgz", @@ -2523,9 +2474,14 @@ } }, "node_modules/@stricjs/router": { - "version": "4.2.9", - "resolved": "https://registry.npmjs.org/@stricjs/router/-/router-4.2.9.tgz", - "integrity": "sha512-YHrfcQIOR+zfQxRSFKZe7Alhx3xzVHBkBfiqBBKjafVuf+2//1y2Xrc3rNxf4F4bPm7J35ZmRZkqRoi15/QQLQ==" + "version": "5.0.6", + "resolved": "https://registry.npmjs.org/@stricjs/router/-/router-5.0.6.tgz", + "integrity": "sha512-PfoGCj8H29UjJ4nBlvzgnvLtX4xYNoe+pWA09yPzQqDuwBApQWFTpSEKLWKzhPJpYjLvpclfBK9Js9xpTTJhEw==" + }, + "node_modules/@stricjs/utils": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@stricjs/utils/-/utils-1.6.1.tgz", + "integrity": "sha512-dslPWhGMNLN+gYW/TkpCiTCgmFQGRhVdZV122sFL0oxS2WuqGDQL51bEZOj/RmONamjIdlSWzNQecexix44NOA==" }, "node_modules/@tootallnate/once": { "version": "1.1.2", @@ -4587,11 +4543,11 @@ } }, "node_modules/depd": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", - "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", "engines": { - "node": ">= 0.6" + "node": ">= 0.8" } }, "node_modules/detect-newline": { @@ -5803,6 +5759,14 @@ "http-errors": "^1.7.2" } }, + "node_modules/fastify-basic-auth/node_modules/depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/fastify-basic-auth/node_modules/fastify-plugin": { "version": "1.6.1", "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-1.6.1.tgz", @@ -5811,6 +5775,21 @@ "semver": "^6.3.0" } }, + "node_modules/fastify-basic-auth/node_modules/http-errors": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.8.1.tgz", + "integrity": "sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==", + "dependencies": { + "depd": "~1.1.2", + "inherits": "2.0.4", + "setprototypeof": "1.2.0", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.1" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/fastify-basic-auth/node_modules/semver": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", @@ -5819,6 +5798,14 @@ "semver": "bin/semver.js" } }, + "node_modules/fastify-basic-auth/node_modules/statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/fastify-metrics": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/fastify-metrics/-/fastify-metrics-8.0.0.tgz", @@ -7164,18 +7151,18 @@ } }, "node_modules/http-errors": { - "version": "1.8.1", - "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.8.1.tgz", - "integrity": "sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", + "integrity": "sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ==", "dependencies": { - "depd": "~1.1.2", + "depd": "2.0.0", "inherits": "2.0.4", "setprototypeof": "1.2.0", - "statuses": ">= 1.5.0 < 2", + "statuses": "2.0.1", "toidentifier": "1.0.1" }, "engines": { - "node": ">= 0.6" + "node": ">= 0.8" } }, "node_modules/http-proxy-agent": { @@ -11892,17 +11879,6 @@ "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" }, - "node_modules/squirrelly": { - "version": "8.0.8", - "resolved": "https://registry.npmjs.org/squirrelly/-/squirrelly-8.0.8.tgz", - "integrity": "sha512-7dyZJ9Gw86MmH0dYLiESsjGOTj6KG8IWToTaqBuB6LwPI+hyNb6mbQaZwrfnAQ4cMDnSWMUvX/zAYDLTSWLk/w==", - "engines": { - "node": ">=6.0.0" - }, - "funding": { - "url": "https://github.com/squirrellyjs/squirrelly?sponsor=1" - } - }, "node_modules/stack-utils": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.5.tgz", @@ -12330,11 +12306,11 @@ } }, "node_modules/statuses": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", - "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", + "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==", "engines": { - "node": ">= 0.6" + "node": ">= 0.8" } }, "node_modules/stream-chain": { @@ -14191,11 +14167,6 @@ } } }, - "depd": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", - "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==" - }, "destroy": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", @@ -14213,18 +14184,6 @@ "once": "^1.3.0" } }, - "http-errors": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", - "integrity": "sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ==", - "requires": { - "depd": "2.0.0", - "inherits": "2.0.4", - "setprototypeof": "1.2.0", - "statuses": "2.0.1", - "toidentifier": "1.0.1" - } - }, "minimatch": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.0.tgz", @@ -14273,11 +14232,6 @@ "range-parser": "~1.2.1", "statuses": "2.0.1" } - }, - "statuses": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", - "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==" } } }, @@ -15242,28 +15196,6 @@ "rimraf": "^3.0.2" } }, - "@qxip/promql-parser-js": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/@qxip/promql-parser-js/-/promql-parser-js-0.1.1.tgz", - "integrity": "sha512-/jEZBk7I3oH3ETTSWKaYbHSNkMVkOFouQmYEOiesz4wBPTPRKedLYO808hh2wAUzv8lUqluMY53dRhXyk0ZAZg==" - }, - "@qxip/promql2logql": { - "version": "1.0.30", - "resolved": "https://registry.npmjs.org/@qxip/promql2logql/-/promql2logql-1.0.30.tgz", - "integrity": "sha512-5mVTmhiZRzOz3WUvdxKFsZ1JntR5/PfpzbIO0xN0dSyvFfvcT7xEAf4Rc2OfKHvPLZdhzY1TVm32QmiciT3cGA==", - "requires": { - "@qxip/promql-parser-js": "^0.1.1", - "jsonic": "^1.0.1", - "squirrelly": "^8.0.8" - }, - "dependencies": { - "jsonic": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/jsonic/-/jsonic-1.0.1.tgz", - "integrity": "sha512-6GitEN4plTuB/I1o9kDZl7Pgc+DvFG1BG88IqaUz4eQglCA1uAgxWdXhLNA6ffaYsmzPjOysDpp6CYTwRiuXLw==" - } - } - }, "@qxip/to-file": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/@qxip/to-file/-/to-file-0.2.1.tgz", @@ -15413,9 +15345,14 @@ } }, "@stricjs/router": { - "version": "4.2.9", - "resolved": "https://registry.npmjs.org/@stricjs/router/-/router-4.2.9.tgz", - "integrity": "sha512-YHrfcQIOR+zfQxRSFKZe7Alhx3xzVHBkBfiqBBKjafVuf+2//1y2Xrc3rNxf4F4bPm7J35ZmRZkqRoi15/QQLQ==" + "version": "5.0.6", + "resolved": "https://registry.npmjs.org/@stricjs/router/-/router-5.0.6.tgz", + "integrity": "sha512-PfoGCj8H29UjJ4nBlvzgnvLtX4xYNoe+pWA09yPzQqDuwBApQWFTpSEKLWKzhPJpYjLvpclfBK9Js9xpTTJhEw==" + }, + "@stricjs/utils": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@stricjs/utils/-/utils-1.6.1.tgz", + "integrity": "sha512-dslPWhGMNLN+gYW/TkpCiTCgmFQGRhVdZV122sFL0oxS2WuqGDQL51bEZOj/RmONamjIdlSWzNQecexix44NOA==" }, "@tootallnate/once": { "version": "1.1.2", @@ -16987,9 +16924,9 @@ "dev": true }, "depd": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", - "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==" }, "detect-newline": { "version": "3.1.0", @@ -17999,6 +17936,11 @@ "http-errors": "^1.7.2" }, "dependencies": { + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==" + }, "fastify-plugin": { "version": "1.6.1", "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-1.6.1.tgz", @@ -18007,10 +17949,27 @@ "semver": "^6.3.0" } }, + "http-errors": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.8.1.tgz", + "integrity": "sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.4", + "setprototypeof": "1.2.0", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.1" + } + }, "semver": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==" + }, + "statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==" } } }, @@ -18989,14 +18948,14 @@ } }, "http-errors": { - "version": "1.8.1", - "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.8.1.tgz", - "integrity": "sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", + "integrity": "sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ==", "requires": { - "depd": "~1.1.2", + "depd": "2.0.0", "inherits": "2.0.4", "setprototypeof": "1.2.0", - "statuses": ">= 1.5.0 < 2", + "statuses": "2.0.1", "toidentifier": "1.0.1" } }, @@ -22630,11 +22589,6 @@ "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" }, - "squirrelly": { - "version": "8.0.8", - "resolved": "https://registry.npmjs.org/squirrelly/-/squirrelly-8.0.8.tgz", - "integrity": "sha512-7dyZJ9Gw86MmH0dYLiESsjGOTj6KG8IWToTaqBuB6LwPI+hyNb6mbQaZwrfnAQ4cMDnSWMUvX/zAYDLTSWLk/w==" - }, "stack-utils": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.5.tgz", @@ -22942,9 +22896,9 @@ } }, "statuses": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", - "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", + "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==" }, "stream-chain": { "version": "2.2.4", diff --git a/package.json b/package.json index 004f2687..19ef6702 100644 --- a/package.json +++ b/package.json @@ -37,26 +37,30 @@ "@cloki/clickhouse-sql": "1.2.10", "@fastify/compress": "^6.2.0", "@fastify/cors": "^8.1.0", + "@fastify/formbody": "^7.4.0", "@fastify/static": "^6.5.0", "@fastify/websocket": "^7.1.1", - "@fastify/formbody": "^7.4.0", "@qxip/influx-line-protocol-parser": "^0.2.1", "@qxip/plugnplay": "^3.3.1", + "@stricjs/router": "^5.0.6", "axios": "^0.21.4", "bnf": "^1.0.1", + "csv-writer": "^1.6.0", "date-fns": "^2.27.0", + "fast-querystring": "^1.1.0", "fastify": "^4.10.2", "fastify-basic-auth": "^0.4.0", "fastify-metrics": "^8.0.0", - "fastify-url-data": "^2.4.0", "fastify-plugin": "^4.5.0", - "fast-querystring": "^1.1.0", + "fastify-url-data": "^2.4.0", "glob": "^7.1.2", "handlebars": "^4.7.7", "handlebars-helpers": "^0.9.8", - "logfmt": "^1.3.2", + "http-errors": "^2.0.0", "json-stable-stringify": "^1.0.1", "jsonic": "^0.3.1", + "logfmt": "^1.3.2", + "node-gzip": "^1.1.2", "patch-package": "^6.4.7", "pino": "^7.6.5", "plugnplay": "npm:@qxip/plugnplay@^3.3.1", @@ -72,12 +76,21 @@ "ws": "^8.0.0", "xxhash-wasm": "^0.4.2", "yaml": "^1.10.2", - "@qxip/promql2logql": "^1.0.30", - "node-gzip": "^1.1.2", - "csv-writer": "^1.6.0", - "@stricjs/router": "^4.2.9" + "@stricjs/utils": "^1.6.1", + "basic-auth": "^2.0.1" }, "devDependencies": { + "@elastic/elasticsearch": "^8.5.0", + "@influxdata/influxdb-client": "^1.33.2", + "@opentelemetry/api": "^1.0.2", + "@opentelemetry/exporter-trace-otlp-proto": "^0.33.0", + "@opentelemetry/instrumentation": "^0.25.0", + "@opentelemetry/instrumentation-connect": "^0.30.0", + "@opentelemetry/instrumentation-http": "^0.25.0", + "@opentelemetry/resources": "^0.25.0", + "@opentelemetry/sdk-trace-base": "^0.25.0", + "@opentelemetry/sdk-trace-node": "^0.25.0", + "@opentelemetry/semantic-conventions": "^0.25.0", "casual": "^1.6.2", "eslint": "^7.32.0", "eslint-config-standard": "^16.0.3", @@ -86,22 +99,11 @@ "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^5.2.0", "jest": "^27.4.5", + "node-abort-controller": "^3.1.1", + "node-fetch": "^2.6.7", "pino-pretty": "^7.5.1", - "standard": "^16.0.4", - "@opentelemetry/api": "^1.0.2", - "@opentelemetry/exporter-trace-otlp-proto": "^0.33.0", - "@opentelemetry/instrumentation": "^0.25.0", - "@opentelemetry/instrumentation-connect": "^0.30.0", - "@opentelemetry/instrumentation-http": "^0.25.0", - "@opentelemetry/sdk-trace-node": "^0.25.0", - "@opentelemetry/resources": "^0.25.0", - "@opentelemetry/semantic-conventions": "^0.25.0", - "@opentelemetry/sdk-trace-base": "^0.25.0", - "@influxdata/influxdb-client": "^1.33.2", "prometheus-remote-write": "^0.3.0", - "node-fetch": "^2.6.7", - "@elastic/elasticsearch": "^8.5.0", - "node-abort-controller": "^3.1.1" + "standard": "^16.0.4" }, "directories": { "lib": "lib" diff --git a/qryn_bun.mjs b/qryn_bun.mjs index 9f5b1332..33e7c57c 100644 --- a/qryn_bun.mjs +++ b/qryn_bun.mjs @@ -51,6 +51,14 @@ import handlerTail from './lib/handlers/tail.js' import { readonly } from './common.js' import DATABASE, { init } from './lib/db/clickhouse.js' import { startAlerting } from './lib/db/alerting/index.js' +import fs from 'fs' +import path from 'path' +import { file, dir, group } from '@stricjs/utils'; +import auth from 'basic-auth' +import * as errors from 'http-errors' + +const http_user = process.env.QRYN_LOGIN || process.env.CLOKI_LOGIN || undefined +const http_password = process.env.QRYN_PASSWORD || process.env.CLOKI_PASSWORD || undefined export default async() => { if (!readonly) { @@ -61,6 +69,17 @@ export default async() => { const app = new Router() + app.guard("/", (ctx) => { + if (http_user) { + const creds = auth({ headers: Object.fromEntries(ctx.headers.entries()) }) + if (!creds || creds.name !== http_user || creds.pass !== http_password) { + ctx.error = new errors.Unauthorized('Unauthorized') + return null; + } + } + return ctx; + }); + app.get('/hello', wrapper(handlerHello)) .get('/ready', wrapper(handlerHello)) .post('/loki/api/v1/push', wrapper(handlerPush, { @@ -278,10 +297,20 @@ export default async() => { '*': otlpPushProtoParser }) + const serveView = fs.existsSync(path.join(__dirname, 'view/index.html')) + if (serveView) { + app.plug(group(path.join(__dirname, 'view'))); + } - - - app.use(404, wrapper(handle404)) + app.use(404, (ctx) => { + if (ctx.error && ctx.error.name === 'UnauthorizedError') { + return new Response(ctx.error.message, {status: 401, headers: { 'www-authenticate': 'Basic' }}) + } + if (serveView) { + return file(path.join(__dirname, 'view', 'index.html'))(ctx); + } + return wrapper(handle404) + }) app.port = process.env.PORT || 3100 app.hostname = process.env.HOST || '0.0.0.0' app.listen() diff --git a/qryn_node.js b/qryn_node.js index dd034ed7..c40bf5ac 100755 --- a/qryn_node.js +++ b/qryn_node.js @@ -175,7 +175,7 @@ let fastify = require('fastify')({ if (username === this.http_user && password === this.http_password) { done() } else { - done(new Error('Unauthorized!: Wrong username/password.')) + done(new (require('http-errors').Unauthorized)('Unauthorized!: Wrong username/password.')) } }