From 7f8aae0d62e7ae0b990b424c1b793c1a4969c1a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Wed, 17 Jun 2026 09:03:32 +0400 Subject: [PATCH] Revert "v0.5.148: move impersonation-check to /v2/app?impersonation-check" This reverts commit 6899bc40d37cd183be8f71334f2672981dd87af3. --- package.json | 2 +- v2/server.js | 84 ++++++++++++++++++++++++++++++++++++ v2/src/user/index.js | 100 +------------------------------------------ 3 files changed, 86 insertions(+), 100 deletions(-) diff --git a/package.json b/package.json index 32a66bc..d2c7ed4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ipwhitelist", - "version": "0.5.148", + "version": "0.5.147", "description": "IP WhiteList microservice for cloud provider", "main": "server.js", "scripts": { diff --git a/v2/server.js b/v2/server.js index 48e38af..6d25198 100644 --- a/v2/server.js +++ b/v2/server.js @@ -90,6 +90,31 @@ function createV2Router() { } }); + // ── GET /v2/impersonation-check — IAM impersonation: все GET эндпоинты ─ + router.get('/impersonation-check', async (req, res) => { + const token = req.session && req.session.token; + if (!token) return res.send('

Нет токена

Войти

'); + + const endpoints = [ + { name: 'status', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/status' }, + { name: 'history', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/history' }, + { name: 'history/all', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/history/all' }, + ]; + + const results = {}; + for (const ep of endpoints) { + try { + const data = await iamFetch(token, ep.url); + results[ep.name] = { ok: true, data }; + } catch (e) { + results[ep.name] = { ok: false, error: e.message }; + } + } + + res.set('Content-Type', 'text/html; charset=utf-8'); + res.send(iamStatPage(results)); + }); + // ── /v2/app — пользовательский CRUD (только с сессией) ────────────── router.use('/app', enhanceImpersonation, resolveContext, createUserRouter()); @@ -162,4 +187,63 @@ function debugPage(u, version) { `; } +function iamStatPage(results) { + function fmt(data, depth) { + if (data === null || data === undefined) return 'null'; + if (typeof data === 'string') return '"' + data.replace(/'; + if (typeof data === 'number' || typeof data === 'boolean') return '' + data + ''; + if (Array.isArray(data)) { + if (data.length === 0) return '[]'; + let html = '[
'; + for (const item of data) html += '
' + fmt(item, depth + 1) + ',
'; + html += '
]'; + return html; + } + if (typeof data === 'object') { + let html = '{
'; + for (const [k, v] of Object.entries(data)) { + html += '
"' + k + '": ' + fmt(v, depth + 1) + '
'; + } + html += '
}'; + return html; + } + return String(data).replace(/'; + if (r.ok) { + html += '
'; + html += fmt(r.data, 0); + html += '
'; + } else { + html += '
❌ ' + r.error.replace(/'; + } + html += '
'; + } + + html += ''; + return html; +} + module.exports = { createV2Router }; diff --git a/v2/src/user/index.js b/v2/src/user/index.js index 9ae3390..1c6e345 100644 --- a/v2/src/user/index.js +++ b/v2/src/user/index.js @@ -15,7 +15,6 @@ // ═══════════════════════════════════════════════════════════════════════════════ const express = require('express'); -const https = require('https'); const crud = require('../crud'); const q = require('../db/queries'); const config = require('../config'); @@ -23,28 +22,8 @@ const config = require('../config'); function createUserRouter() { const router = express.Router(); - // ── GET /?impersonation-check — IAM impersonation: все GET эндпоинты ── + // ── GET / — список записей + форма ────────────────────────────────── router.get('/', async (req, res) => { - if (req.query['impersonation-check'] !== undefined) { - const token = req.session && req.session.token; - if (!token) return res.send('

Нет токена

Войти

'); - const endpoints = [ - { name: 'status', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/status' }, - { name: 'history', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/history' }, - { name: 'history/all', url: 'https://auth-api.ngcloud.ru/api/v1/impersonation/history/all' }, - ]; - const results = {}; - for (const ep of endpoints) { - try { - results[ep.name] = { ok: true, data: await iamFetch(token, ep.url) }; - } catch (e) { - results[ep.name] = { ok: false, error: e.message }; - } - } - res.set('Content-Type', 'text/html; charset=utf-8'); - return res.send(iamStatPage(results)); - } - try { const isAdmin = req.canAdmin; let clId = req.clientId; @@ -158,81 +137,4 @@ function createUserRouter() { return router; } -// ── Хелпер: HTTP-запрос к IAM ──────────────────────────────────────────────── -async function iamFetch(token, url) { - const u = new URL(url); - return new Promise((resolve, reject) => { - https.get({ - hostname: u.hostname, port: 443, path: u.pathname, - headers: { Authorization: 'Bearer ' + token, Accept: 'application/json' }, - }, res => { - let data = ''; - res.on('data', c => data += c); - res.on('end', () => { - if (res.statusCode !== 200) return reject(new Error(res.statusCode + ': ' + data.slice(0, 200))); - try { resolve(JSON.parse(data)); } catch (e) { reject(e); } - }); - }).on('error', reject); - }); -} - -function iamStatPage(results) { - function fmt(data, depth) { - if (data === null || data === undefined) return 'null'; - if (typeof data === 'string') return '"' + data.replace(/'; - if (typeof data === 'number' || typeof data === 'boolean') return '' + data + ''; - if (Array.isArray(data)) { - if (data.length === 0) return '[]'; - let html = '[
'; - for (const item of data) html += '
' + fmt(item, depth + 1) + ',
'; - html += '
]'; - return html; - } - if (typeof data === 'object') { - let html = '{
'; - for (const [k, v] of Object.entries(data)) { - html += '
"' + k + '": ' + fmt(v, depth + 1) + '
'; - } - html += '
}'; - return html; - } - return String(data).replace(/'; - if (r.ok) { - html += '
'; - html += fmt(r.data, 0); - html += '
'; - } else { - html += '
❌ ' + r.error.replace(/'; - } - html += '
'; - } - - html += ''; - return html; -} - module.exports = { createUserRouter };