mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-28 06:05:24 +00:00
* feat: optional auto reconnect for dropped ssh sessions (#1321) * feat: tighter compact host rows with inline tags (#1325) * feat: bring back the alert feed homepage widget * feat: pick target terminals for snippets again * feat: share button on connection tabs again * feat: recent servers list when linking the desktop app * feat: list the fixed app shortcuts in keyboard shortcuts * fix: translate hardcoded toasts and drop em dashes from ui text * fix: translate leftover hardcoded ui text * fix: toolbar files button opens at the shell directory again * fix: show the remote desktop note under connection origin again * chore: openapi docs for host metrics manager routes * chore: openapi docs for undocumented routes and fix two broken doc blocks * fix: declare terminal.listSessions use for snippet targets * revert: drop the alert feed widget, the notification center replaces it * feat: restore the alert feed homepage widget * fix: quick connect fills the host address from plugin auth editors like tailscale * fix: docker container logs use the base background color * chore: fix dependabot alerts and apply safe npm upgrades * fix: proxmox stats tab reads its switch from plugin host settings * fix: count plugin ssh logins toward host status and stop showing reachable as a warning * fix: hide the built-in source pill in the host editor * fix: host status shows online when the port answers and only warns on a failed login * fix: drop the top border on the split layout menu * fix: host defaults tabs sit where the host editor puts them and skip plugins with nothing to default * fix: remote desktop auth method labels and notes for vnc and telnet * fix: add description to the web endpoint enable switch * fix: crowdin sync and pretranslate for plugin locales, run plugin tests on release * feat: move telemetry out of core into a usage statistics plugin * fix: recent activity card no longer cuts off host names early * fix: remove duplicate top border on split screen menu * fix: redesign session log player controls, fix tooltip overlap and drop dot separators * fix: file manager and sftp text showing raw i18n keys * fix: onboarding rerunning for existing users after upgrading * fix: fleet detail buttons overflowing the sidebar * fix: session log player scrolling off screen horizontally * fix: network graph lag when adding hosts and on every render * fix: maintenance index name, shared host read only view and badge polling * fix: meeting tabs, workspace tab merging, rail panels as tabs and general ui lag * chore: update release notes * fix: remote desktop tabs reconnecting on every tab switch * fix: build plugin sdk before linting in release workflow * chore: lint, format, and bump version to 2.9.0 * chore: skip crowdin pre-translate in release for now * chore: switch to calver, bump version to 26.10.0 * fix(sso): send the PKCE verifier for GitHub login (#1534) * fix(remote-desktop): clip cursor overflow without disabling zoom (#1535) * test(remote-desktop): preserve sessions beyond one hour (#1536) * fix(hosts): expose controls for overflowing editor tabs (#1537) * fix(hosts): expose controls for overflowing editor tabs * test(hosts): mock resize observation in admin panel tests * fix(status): skip TCP probes while host sessions are active (#1538) * fix(database): batch session activity persistence (#1539) * docs(api): describe cookie and API key authentication (#1540) * fix(snippets): repair missing note column on SQLite upgrades (#1541) * fix(docker): retain stored SSH credential association (#1543) * fix(file-manager): render transfer toasts without plugin hooks (#1546) * feat(hosts): keep compact row actions inline (#1547) * fix(auth): allow retrying unavailable second-factor interfaces (#1549) * fix(auth): reject unresolved legacy identity provisioning (#1550) * fix(desktop): ship the plugin SDK inside Mac App Store builds (#1551) * fix(tmux): pass full session info to the terminal's session picker (#1552) The tmux.sessions service reduced detected sessions to bare names, but the picker reads session.name, so every entry rendered blank and selecting one sent an empty name, which created a new session instead of attaching. * fix(ssh): read force keyboard-interactive as a boolean when resolving hosts (#1553) The column holds the text "true"/"false", and the resolver passed it through as-is. The string "false" is truthy, so the password provider treated every saved host as forced keyboard-interactive and left the password out. Jump hops are built straight from the resolved host, so a password hop whose server doesn't offer keyboard-interactive failed with "All configured authentication methods failed". * fix(database): batch database saves for bulk host writes (#1554) * fix(database): yield to the event loop between database saves Each SQLite save serializes and encrypts the whole database into new buffers, which V8 only releases once the event loop turns. Boot migrations and bulk host import write plugin settings per host and per key in an awaited loop, so hundreds of full copies piled up and the container ran out of memory. * fix(database): save once per bulk host write instead of once per row On SQLite every repository write force-saves the whole database. Boot plugin data migrations, host defaults materialization and the bulk host routes write one row per host per setting, so they serialized and encrypted the full database hundreds of times in a row. DatabaseSaveTrigger.batched wraps a function so force saves made while it runs collapse into a single save when it finishes. Nested scopes fold into the outer one, and work that outlives its scope saves normally. * feat(hosts): add instance-wide predefined tag suggestions (#1548) * feat(hosts): add shared predefined tag suggestions * style(hosts): format tag catalog routes * test(database): advance fake timers past the save yield (#1555) * fix(i18n): complete Simplified Chinese core and plugin translations (#1542) * fix(i18n): complete Chinese onboarding and navigation labels * fix(i18n): translate remaining Chinese core and plugin interfaces * fix(i18n): translate host editor tab overflow controls * fix(i18n): use consistent Chinese fleet terminology * fix(i18n): localize hardcoded controls and plugin views * fix(i18n): translate built-in homepage widget catalog * test(homepage): follow translated timezone placeholder * fix(i18n): translate plugin-provided homepage widgets * fix(i18n): localize feature settings section titles * fix: 2.8 oidc accounts unable to sign in after upgrading (#1381) * fix: plugins losing the saved ssh login when copying a host (#1391) * fix: fleets, proxmox and automations not getting the host sudo password * fix: host imports running a defaults pass and metrics restart per host (#1384) * fix: high cpu from status probes and full database saves on every sample (#1300) * fix: user data export freezing the server (#1393) * fix: op:// secret references rejected as ssh keys on credentials (#1394) * fix: slow file deletes from the trash lookups on every delete (#1390) * fix: add openapi docs and error handling to host tag routes * test: compare download stream buffers directly so it stops timing out * chore: drop em dash from host row comment * chore: update release notes for 2.9.1 * feat(ai): add remote coding agent sessions for SSH hosts (#1545) * feat(ai): add host coding agent sessions with configured providers * Wait for agent initialization and shutdown before resuming sessions * Keep Pi transcripts outside its configuration migration directory * Cover Pi session directory separation * Update core permission catalog expectations for remote agents * Install remote agent runtimes from verified official sources * Wait for the identity panel credentials request in its test * Add scoped SSH forwarding setup for remote agents * Verify forwarding setup through a fresh SSH connection * Bound the SSH forwarding verification timeout * Return the forwarding verification callback result * fix(ai): support agent send and interrupt keyboard shortcuts * feat(ai): add agent queues attachments worktrees and session recovery * fix(ai): handle deleted workspace paths and duplicate status events * fix(ai): accept file references in root workspaces * fix(ai): refresh paused queues after agent errors * fix(ai): store agent sessions in plugin tables and lock down OpenCode Agent sessions lived in a single ctx.kv value holding up to 2000 events, which passed the 256 KB kv limit within minutes of a normal run. Every save after that failed, so history, nativeId, queue and archive changes were lost and every event wrote a kv audit row. Sessions, events and queued prompts now have their own tables. A running agent appends events in batches at most once a second with a lazy save; session and queue changes save immediately. Prompts, drafts and stored event text are capped in UTF-8 bytes to fit MySQL TEXT. kv:own is no longer requested. The OpenCode server now requires a per-session password, so other local accounts on the host cannot drive the agent through its loopback port. * test(upgrade): include agent session tables in the reviewed upgrade SQL * test: run the posix-only ai agent tests on linux only * fix: restore space to add host tags and redesign predefined tags editor * fix: host key silently accepted when the host is missing from the database (#1397) * fix: clearer host login failed status label and fix its translations (#1396) * chore: add host key and status label fixes to release notes * fix: sso and ldap provider dialogs overflowing the screen and using mismatched toggles * chore: increment ver * fix(audit): store plugin entries with no acting user as a null user_id (#1556) Plugin audit entries written outside a request used the literal "system" as user_id. That column references users.id, so the insert was refused (Postgres logs it as an FK violation) and the entry was silently dropped. Write null instead and keep "system" / plugin:<id> in username. * chore: add sso/ldap dialog and audit log fixes to release notes * fix: tunnels and host settings missing from shared hosts on desktop * fix: remote desktop logins missing from shared hosts on desktop * fix: simplify host status to online/offline and keep it live without a refresh * chore: sync Crowdin translations for 2.9.1 * fix: build docker frontend/backend natively to stop arm64 hang, build sdk before openapi * chore: rework plugin readmes, json changelogs and standalone repo setup * chore: move plugins to their own repos and drop the plugins folder * chore(deps-dev): bump globals (#1560) Bumps the dev-minor-updates group with 1 update: [globals](https://github.com/sindresorhus/globals). Updates `globals` from 17.12.0 to 17.13.0 - [Release notes](https://github.com/sindresorhus/globals/releases) - [Commits](https://github.com/sindresorhus/globals/compare/v17.12.0...v17.13.0) --- updated-dependencies: - dependency-name: globals dependency-version: 17.13.0 dependency-type: direct:development update-type: version-update:semver-minor dependency-group: dev-minor-updates ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat: drop the 2.8 columns, moves and aliases kept for a downgrade * chore: remove the 2.9 finish list * feat: port the demo redesign with Manage and Settings tabs * chore: pin the plugin signing key and use TERMIX_PAT in workflows * chore: rename the plugin sdk to @termix-ssh/plugin-sdk * feat: add a publish workflow for the plugin sdk * chore: cleanup * fix: harden plugin sockets, public routes, prebuilt binaries and login fallback * chore: add a 2.9.1 upgrade check for postgres and mysql * fix: stop full database rewrites on every plugin db call * fix: stop full database rewrites on every plugin db call * fix: restore release video link in 2.9.2 notes * fix: use TERMIX_PAT in workflows * feat: pin the official plugins in the docker image * feat: implement plugin management UI * feat: shared list kit, settings search, rail order and ui consistency fixes * chore: run all workflows on blacksmith runners * chore: split dashboard quick actions into quarters and drop host status row lines * fix: open plain settings from the profile button, navigation only from the rail menu * feat: consent check, developer mode file installs, install counts and issue links for plugins * feat: row detail options for hosts and credentials, open manage from sidebar header * fix: unify connection failure screens and rebuild stale local plugins * feat: dev:all runner for web, electron and docker with plugin hot reload, termix-plugin dev * chore: clean up npm scripts, make npm run dev the full runner and drop concurrently * fix: settle quick connect panel loads in tests so they stop flaking * fix: stop the dev runner rebuilding plugins when windows reports file reads as changes * chore: update the plugin sdk ui export snapshot * feat: full screen onboarding with a plugin picker and per step versions * feat: changelog.md release notes, v tags, plugin release notes and videos * chore: add temp changelog * chore: release the plugin sdk as 1.0.1 * feat: show only the latest plugin release notes and cap long ones * chore: publish docker images to termix-ssh on ghcr and termixssh on docker hub * chore: rename docker hub token secret to DOCKERHUB_BUGATTIGUY527_TOKEN * chore: renew mac app store provisioning profile * chore: drop docs job from release workflow * chore: run sdk publish on github hosted runner * chore: remove dependabot * chore: general file cleanup * chore: rewrite readme and taglines for plugin-based termix * chore: drop optional from ai plugin row in readme * feat: per-repo issue forms and prefilled bug reports instead of the support repo * feat: add report a bug link to the your account settings section * fix: open settings goes to the plugin's page and hides when it has none * feat: show plugin features from the manifest on the plugin page * fix: retry failed or blocked plugins on restart instead of leaving them disabled * fix: refresh plugin settings after enabling a plugin and match select and disabled bar styling * feat: request a feature link for plugins and use the bug form for unverified plugins * feat: beta channels for core and plugins with one stable/beta release workflow * fix(sync): resolve shared hosts for connect-only recipients (#1568) * fix(ssh): accept full passwords in keyboard-interactive prompts (#1569) * fix(ssh): normalize PKCS8 private keys before connecting (#1570) * fix(sidebar): reclaim space from hidden folder actions (#1571) * fix(hosts): display protocol credential usernames for non-SSH hosts (#1572) * fix: ask for a push/TOTP menu choice as text instead of a push confirm * fix: keep client tunnels open when one of their connections fails (Support#1417) * fix: send plugin form uploads to the plugin's route (Support#1406, Support#1420) * fix: add missing sync ids to old snippet tables so snippets sync again (Support#1419) * fix: start the macOS local terminal when the shell or spawn helper is broken (Support#1418) * fix: refresh cached plugin translations when only a translation changes (Support#1411) * fix: import sshpk as a default so the backend starts under ESM * feat: add a plugin sdk changelog and tag sdk releases, release 1.0.2 * fix: drop the ./ from the plugin sdk bin path so npm stops warning * chore: name core and sdk releases Termix vX and Plugin SDK vX * fix: lock the desktop backend to the app's own origin (GHSA-frx8-qqgg-7j7g) * fix: trust devices by a server-issued token, not a client header (GHSA-6gr3-r7jx-wfmh) * fix: keep global settings out of non-admin data exports (GHSA-v3vp-ffww-q7pw) * fix: refuse sessionless tokens and keep the secrets file owner only * feat: add TRUSTED_PROXIES to choose who nginx takes client IPs from (GHSA-7j7m-85p4-2cpm) * chore: standardize repo setup files * chore: remove unused knip config * chore: add LICENSE and use Node 24 from .nvmrc * chore: update translation guidance, only en.json needs editing * feat: plugin docs and env manifest fields, in-app docs links, SDK 1.0.3 * fix: header separators render as borders and center the status divider * fix: lighter confirm prompt overlay to stop lag when it opens * fix: put plugin-only tailwind classes in core's utilities layer, SDK 1.0.4 * fix: dev stages local plugin builds on paths with spaces and builds them with the local SDK * fix: keep plugin tailwind variants in core's layer so overrides keep their order * fix: manage tab refetches before giving up on a host or credential it cannot find * fix: tab buttons like share show as soon as the terminal is ready * fix: folder rows no longer nest action buttons inside a button * fix: repin remote-desktop after its 1.0.0 re-release * chore: repin bundled plugins after the 1.0.0 re-release * fix: recent activity rows use the same separator as host status * feat: upload banner * fix: reject expired sessions on sockets, api key desktop links and 192.0.0.0/24 * fix: plugin sdk 1.0.5 with optional ui peers, safer timeouts and validate checks * fix: bundled plugins path for source runs and backend cleanup * fix: load plugin strings on guest pages, plugin wording and drop unused keys * fix: beta version stamping, homebrew cask bump, dockerignore and readme platforms * docs: 26.10.0 changelog * fix: current version in the version route example * chore: stop pushing the release- docker tag * feat: remove the donation reminder and its column * docs: note the removed donation reminder * fix: link server wizard always opens inline, onboarding card grows for it * feat: plugin text columns are longtext on mysql so they are not capped at 64KB * fix: linked desktops keep a plugin when its update from the server fails * chore: default mobile download links to v tags * fix: sso only accounts cannot sign in with a passkey or other local method * feat: note in the plugins tab that community plugins are coming * fix: plugin validate accepts beta versions * chore: pin bundled plugins rebuilt on sdk 1.0.5 * fix: skip locale fetches for disabled plugins * fix: show translated names for the built-in admin and user roles * fix: show host keys as SHA256 fingerprints like ssh-keygen * fix: host editor scrolls to a newly enabled protocol section * fix: pass the host to dashboard host row plugins * fix: plugin cards show dividers between author, version and category * fix: refuse plugin archives with paths outside the plugin folder * fix: theme picker shows one dropdown arrow * fix: read activity and audit times as UTC so they show in local time * fix: show the real error for a wrong password or code on plugin sign in * fix: explain that external sign in needs Auto-create external accounts * fix: clear leftover plugin unpack folders at startup * fix: shared session links open for guests again * fix: share panels no longer list yourself as a share target * fix: a host update without an auth type keeps the saved password * fix: guest pages get the tab ids a guest plugin registers * fix: refuse a 2.8 database, which already had a plugins table * fix: dashboard cards fit on a phone * fix: desktop setup no longer asks to secure the local account * fix: the desktop app keeps the desktop layout in a narrow window * fix: the desktop app shows the rail and no phone bar in a narrow window * fix: name every sync type in the Sync panel and pluralize the link prompt * chore: update the sdk ui snapshot for the host key dialog * fix: turning a plugin back on restarts the plugins it turned off * fix: quick connect asks to trust the host key * fix: revoking a manager also removes the shares they handed out * fix: database export works for users with TOTP on (#1426) * fix: rdp and vnc logins use the folder credential when none is picked (#1429) * fix: imported hosts get status checks without a restart * fix: re-pin bundled plugins after 1.0.0 re-release * fix: re-pin bundled plugins after search bar fix * chore: update app icon to smaller version * chore: round the readme logo, keep square source for app icons * feat: grid columns and table view in the plugins tab * fix: match the host key copy button to the fingerprint height * fix: use brand color for toggles in sync settings * fix: re-pin bundled plugins after 1.0.0 re-release * fix: web terminals failing auth behind a proxy that rewrites Host (Support#651) * fix: re-pin snippets and termix-identity after 1.0.0 overwrite * fix: re-pin file-manager after 1.0.0 overwrite * fix: re-pin file-manager after 1.0.0 overwrite * chore: remove Crowdin from the release workflow * chore: remove crowdin config --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: ZacharyZcR <zacharyzcr1984@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2160 lines
62 KiB
TypeScript
2160 lines
62 KiB
TypeScript
import { getErrorMessage } from "../../utils/error-message.js";
|
|
import type { AuthenticatedRequest } from "../../../types/index.js";
|
|
import express, { type Response } from "express";
|
|
import { databaseLogger } from "../../utils/logger.js";
|
|
import { AuthManager } from "../../utils/auth-manager.js";
|
|
import {
|
|
getRequestMeta,
|
|
getAuditUsername,
|
|
logAudit,
|
|
} from "../../utils/audit-logger.js";
|
|
import { isAuthOverrideProtocol } from "../../hosts/protocol-auth/registry.js";
|
|
import {
|
|
SharedHostAuthOverrideService,
|
|
SharedHostAuthOverrideServiceError,
|
|
} from "../../utils/shared-host-auth-override-service.js";
|
|
import {
|
|
PermissionManager,
|
|
SHARE_PERMISSION_LEVELS,
|
|
type SharePermissionLevel,
|
|
} from "../../utils/permission-manager.js";
|
|
import {
|
|
getPermissionCatalog,
|
|
isValidPermission,
|
|
} from "../../utils/permission-catalog.js";
|
|
import {
|
|
createCurrentHostFolderRepository,
|
|
createCurrentFolderAccessRepository,
|
|
createCurrentCredentialRepository,
|
|
createCurrentCredentialAccessRepository,
|
|
createCurrentHostResolutionRepository,
|
|
createCurrentRbacAccessRepository,
|
|
createCurrentRoleRepository,
|
|
createCurrentUserRepository,
|
|
} from "../repositories/factory.js";
|
|
|
|
const router = express.Router();
|
|
|
|
const authManager = AuthManager.getInstance();
|
|
const permissionManager = PermissionManager.getInstance();
|
|
|
|
const authenticateJWT = authManager.createAuthMiddleware();
|
|
const requireDataAccess = authManager.createDataAccessMiddleware();
|
|
const sharedHostAuthOverrideService =
|
|
SharedHostAuthOverrideService.getInstance();
|
|
|
|
function isNonEmptyString(value: unknown): value is string {
|
|
return typeof value === "string" && value.trim().length > 0;
|
|
}
|
|
|
|
export function isSharePermissionLevel(
|
|
value: unknown,
|
|
): value is SharePermissionLevel {
|
|
return SHARE_PERMISSION_LEVELS.includes(value as SharePermissionLevel);
|
|
}
|
|
|
|
export function expiryFromDuration(durationHours: unknown): string | null {
|
|
if (durationHours && typeof durationHours === "number" && durationHours > 0) {
|
|
const expiryDate = new Date();
|
|
expiryDate.setTime(expiryDate.getTime() + durationHours * 60 * 60 * 1000);
|
|
return expiryDate.toISOString();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Sharing is controlled by the owner or any recipient holding "manage".
|
|
async function canManageHostSharing(
|
|
userId: string,
|
|
hostId: number,
|
|
): Promise<{ allowed: boolean; isOwner: boolean }> {
|
|
const access = await permissionManager.canAccessHost(
|
|
userId,
|
|
hostId,
|
|
"manage",
|
|
);
|
|
return { allowed: access.hasAccess, isOwner: access.isOwner };
|
|
}
|
|
|
|
/**
|
|
* Drops shares handed out by someone who can no longer manage sharing on the
|
|
* host, then repeats so shares they passed on further go too.
|
|
*/
|
|
export async function pruneHostGrants(hostId: number): Promise<number> {
|
|
const repository = createCurrentRbacAccessRepository();
|
|
const ownerId =
|
|
await createCurrentHostResolutionRepository().findHostOwnerId(hostId);
|
|
let removed = 0;
|
|
for (let changed = true; changed;) {
|
|
changed = false;
|
|
for (const grant of await repository.listHostAccess(hostId)) {
|
|
if (!grant.grantedBy || grant.grantedBy === ownerId) continue;
|
|
if ((await canManageHostSharing(grant.grantedBy, hostId)).allowed) {
|
|
continue;
|
|
}
|
|
await repository.revokeHostAccess(grant.id, hostId);
|
|
removed++;
|
|
changed = true;
|
|
}
|
|
}
|
|
return removed;
|
|
}
|
|
|
|
/** pruneHostGrants for credential shares. */
|
|
export async function pruneCredentialGrants(
|
|
credentialId: number,
|
|
): Promise<number> {
|
|
const repository = createCurrentCredentialAccessRepository();
|
|
const owner =
|
|
await createCurrentCredentialRepository().findById(credentialId);
|
|
if (!owner) return 0;
|
|
let removed = 0;
|
|
for (let changed = true; changed;) {
|
|
changed = false;
|
|
for (const grant of await repository.listForCredential(credentialId)) {
|
|
if (!grant.grantedBy || grant.grantedBy === owner.userId) continue;
|
|
if (
|
|
(await canManageCredentialSharing(grant.grantedBy, credentialId))
|
|
.allowed
|
|
) {
|
|
continue;
|
|
}
|
|
await repository.revoke(grant.id, credentialId);
|
|
removed++;
|
|
changed = true;
|
|
}
|
|
}
|
|
return removed;
|
|
}
|
|
|
|
/** Prunes everything these users shared, after they lost a role. */
|
|
export async function pruneGrantsBy(userIds: string[]): Promise<void> {
|
|
for (const userId of userIds) {
|
|
permissionManager.invalidateUserPermissionCache(userId);
|
|
for (const hostId of await createCurrentRbacAccessRepository().listHostIdsGrantedBy(
|
|
userId,
|
|
)) {
|
|
await pruneHostGrants(hostId);
|
|
}
|
|
for (const credentialId of await createCurrentCredentialAccessRepository().listCredentialIdsGrantedBy(
|
|
userId,
|
|
)) {
|
|
await pruneCredentialGrants(credentialId);
|
|
}
|
|
}
|
|
}
|
|
|
|
export interface ShareTarget {
|
|
type: "user" | "role";
|
|
id: string | number;
|
|
}
|
|
|
|
export function parseShareTargets(
|
|
body: Record<string, unknown>,
|
|
): ShareTarget[] | null {
|
|
const rawTargets = body.targets;
|
|
if (!Array.isArray(rawTargets) || rawTargets.length === 0) return null;
|
|
|
|
const targets: ShareTarget[] = [];
|
|
for (const raw of rawTargets) {
|
|
if (!raw || typeof raw !== "object") return null;
|
|
const { type, id } = raw as { type?: unknown; id?: unknown };
|
|
if (type === "user" && isNonEmptyString(id)) {
|
|
targets.push({ type: "user", id });
|
|
} else if (
|
|
type === "role" &&
|
|
typeof id === "number" &&
|
|
Number.isInteger(id)
|
|
) {
|
|
targets.push({ type: "role", id });
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
return targets;
|
|
}
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/host/{id}/share:
|
|
* post:
|
|
* summary: Share a host
|
|
* description: Shares a host with one or more users and/or roles at a permission level (connect, view, edit, manage). SSH authentication remains private to the owner; recipients may select one of their own saved SSH credentials.
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* required: [targets]
|
|
* properties:
|
|
* targets:
|
|
* type: array
|
|
* items:
|
|
* type: object
|
|
* properties:
|
|
* type:
|
|
* type: string
|
|
* enum: [user, role]
|
|
* id:
|
|
* oneOf:
|
|
* - type: string
|
|
* - type: integer
|
|
* permissionLevel:
|
|
* type: string
|
|
* enum: [connect, view, edit, manage]
|
|
* durationHours:
|
|
* type: number
|
|
* responses:
|
|
* 200:
|
|
* description: Host shared successfully.
|
|
* 400:
|
|
* description: Invalid request body.
|
|
* 403:
|
|
* description: Caller may not share this host.
|
|
* 404:
|
|
* description: Target user or role not found.
|
|
* 500:
|
|
* description: Failed to share host.
|
|
*/
|
|
router.post(
|
|
"/host/:id/share",
|
|
authenticateJWT,
|
|
permissionManager.requirePermission("hosts.share"),
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
const hostId = parseInt(id, 10);
|
|
const userId = req.userId!;
|
|
|
|
if (isNaN(hostId)) {
|
|
return res.status(400).json({ error: "Invalid host ID" });
|
|
}
|
|
|
|
try {
|
|
const targets = parseShareTargets(req.body ?? {});
|
|
if (!targets) {
|
|
return res.status(400).json({
|
|
error:
|
|
"targets must be a non-empty array of { type: 'user'|'role', id } entries",
|
|
});
|
|
}
|
|
|
|
const { durationHours, permissionLevel = "connect" } = req.body;
|
|
|
|
if (!isSharePermissionLevel(permissionLevel)) {
|
|
return res.status(400).json({
|
|
error: "Invalid permission level",
|
|
validLevels: SHARE_PERMISSION_LEVELS,
|
|
});
|
|
}
|
|
|
|
const sharing = await canManageHostSharing(userId, hostId);
|
|
if (!sharing.allowed) {
|
|
databaseLogger.warn("Permission denied", {
|
|
operation: "rbac_permission_denied",
|
|
userId,
|
|
resource: "host",
|
|
resourceId: hostId,
|
|
action: "share",
|
|
});
|
|
return res.status(403).json({ error: "You may not share this host" });
|
|
}
|
|
|
|
const host =
|
|
await createCurrentHostResolutionRepository().findHostUpdateState(
|
|
hostId,
|
|
);
|
|
if (!host) {
|
|
return res.status(404).json({ error: "Host not found" });
|
|
}
|
|
const ownerId = host.userId;
|
|
|
|
const userRepository = createCurrentUserRepository();
|
|
const roleRepository = createCurrentRoleRepository();
|
|
|
|
for (const target of targets) {
|
|
if (target.type === "user") {
|
|
if (target.id === ownerId) {
|
|
return res
|
|
.status(400)
|
|
.json({ error: "Cannot share a host with its owner" });
|
|
}
|
|
const targetUser = await userRepository.findById(target.id as string);
|
|
if (!targetUser) {
|
|
return res.status(404).json({
|
|
error: "Target user not found",
|
|
targetId: target.id,
|
|
});
|
|
}
|
|
} else {
|
|
const targetRole = await roleRepository.findRoleById(
|
|
target.id as number,
|
|
);
|
|
if (!targetRole) {
|
|
return res.status(404).json({
|
|
error: "Target role not found",
|
|
targetId: target.id,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
const expiresAt = expiryFromDuration(durationHours);
|
|
|
|
const rbacAccessRepository = createCurrentRbacAccessRepository();
|
|
const { SharedHostSecretsManager } =
|
|
await import("../../utils/shared-host-secrets-manager.js");
|
|
const secretsManager = SharedHostSecretsManager.getInstance();
|
|
|
|
const results: Array<{
|
|
type: "user" | "role";
|
|
id: string | number;
|
|
accessId: number;
|
|
created: boolean;
|
|
}> = [];
|
|
|
|
for (const target of targets) {
|
|
const accessGrant = await rbacAccessRepository.upsertHostAccess({
|
|
hostId,
|
|
grantedBy: userId,
|
|
permissionLevel,
|
|
expiresAt,
|
|
...(target.type === "user"
|
|
? { targetType: "user" as const, targetUserId: target.id as string }
|
|
: {
|
|
targetType: "role" as const,
|
|
targetRoleId: target.id as number,
|
|
}),
|
|
});
|
|
|
|
try {
|
|
if (target.type === "user") {
|
|
await secretsManager.snapshotForUser(
|
|
accessGrant.id,
|
|
hostId,
|
|
target.id as string,
|
|
ownerId,
|
|
);
|
|
} else {
|
|
await secretsManager.snapshotForRole(
|
|
accessGrant.id,
|
|
hostId,
|
|
target.id as number,
|
|
ownerId,
|
|
);
|
|
}
|
|
} catch (snapshotError) {
|
|
databaseLogger.warn("Share created but secret snapshot failed", {
|
|
operation: "rbac_host_share_snapshot_failed",
|
|
hostId,
|
|
accessId: accessGrant.id,
|
|
error: getErrorMessage(snapshotError),
|
|
});
|
|
}
|
|
|
|
results.push({
|
|
type: target.type,
|
|
id: target.id,
|
|
accessId: accessGrant.id,
|
|
created: accessGrant.created,
|
|
});
|
|
}
|
|
|
|
await pruneHostGrants(hostId);
|
|
databaseLogger.success("Host shared successfully", {
|
|
operation: "rbac_host_share_success",
|
|
userId,
|
|
hostId,
|
|
targets: results.length,
|
|
permissionLevel,
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
message: "Host shared successfully",
|
|
permissionLevel,
|
|
expiresAt,
|
|
results,
|
|
});
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to share host", error, {
|
|
operation: "share_host",
|
|
hostId,
|
|
userId,
|
|
});
|
|
res.status(500).json({ error: "Failed to share host" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/folder/share:
|
|
* post:
|
|
* summary: Share all hosts in a folder
|
|
* description: Shares every host within a folder (and its subfolders) with one or more users and/or roles at a permission level. Only hosts owned by the caller are shared; skips hosts the caller may not share.
|
|
* tags:
|
|
* - RBAC
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* required: [folder, targets]
|
|
* properties:
|
|
* folder:
|
|
* type: string
|
|
* targets:
|
|
* type: array
|
|
* items:
|
|
* type: object
|
|
* properties:
|
|
* type:
|
|
* type: string
|
|
* enum: [user, role]
|
|
* id:
|
|
* oneOf:
|
|
* - type: string
|
|
* - type: integer
|
|
* permissionLevel:
|
|
* type: string
|
|
* enum: [connect, view, edit, manage]
|
|
* durationHours:
|
|
* type: number
|
|
* responses:
|
|
* 200:
|
|
* description: Folder shared successfully.
|
|
* 400:
|
|
* description: Invalid request body.
|
|
* 500:
|
|
* description: Failed to share folder.
|
|
*/
|
|
router.post(
|
|
"/folder/share",
|
|
authenticateJWT,
|
|
permissionManager.requirePermission("hosts.share"),
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const userId = req.userId!;
|
|
const { folder } = req.body ?? {};
|
|
|
|
if (!isNonEmptyString(folder)) {
|
|
return res.status(400).json({ error: "Folder name is required" });
|
|
}
|
|
|
|
try {
|
|
const targets = parseShareTargets(req.body ?? {});
|
|
if (!targets) {
|
|
return res.status(400).json({
|
|
error:
|
|
"targets must be a non-empty array of { type: 'user'|'role', id } entries",
|
|
});
|
|
}
|
|
|
|
const { durationHours, permissionLevel = "connect" } = req.body;
|
|
|
|
if (!isSharePermissionLevel(permissionLevel)) {
|
|
return res.status(400).json({
|
|
error: "Invalid permission level",
|
|
validLevels: SHARE_PERMISSION_LEVELS,
|
|
});
|
|
}
|
|
|
|
const userRepository = createCurrentUserRepository();
|
|
const roleRepository = createCurrentRoleRepository();
|
|
for (const target of targets) {
|
|
if (target.type === "user") {
|
|
const targetUser = await userRepository.findById(target.id as string);
|
|
if (!targetUser) {
|
|
return res.status(404).json({
|
|
error: "Target user not found",
|
|
targetId: target.id,
|
|
});
|
|
}
|
|
} else {
|
|
const targetRole = await roleRepository.findRoleById(
|
|
target.id as number,
|
|
);
|
|
if (!targetRole) {
|
|
return res.status(404).json({
|
|
error: "Target role not found",
|
|
targetId: target.id,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
const hostsInFolder =
|
|
await createCurrentHostFolderRepository().listHostsInFolder(
|
|
userId,
|
|
folder,
|
|
);
|
|
|
|
const expiresAt = expiryFromDuration(durationHours);
|
|
|
|
// Remember the share on the folder itself so hosts added later inherit it.
|
|
const folderAccessRepository = createCurrentFolderAccessRepository();
|
|
for (const target of targets) {
|
|
await folderAccessRepository.upsert({
|
|
ownerUserId: userId,
|
|
folder,
|
|
grantedBy: userId,
|
|
permissionLevel,
|
|
expiresAt,
|
|
target:
|
|
target.type === "user"
|
|
? { targetType: "user", targetUserId: target.id as string }
|
|
: { targetType: "role", targetRoleId: target.id as number },
|
|
});
|
|
}
|
|
const rbacAccessRepository = createCurrentRbacAccessRepository();
|
|
const { SharedHostSecretsManager } =
|
|
await import("../../utils/shared-host-secrets-manager.js");
|
|
const secretsManager = SharedHostSecretsManager.getInstance();
|
|
|
|
const hostResults: Array<{
|
|
hostId: number;
|
|
shared: boolean;
|
|
reason?: string;
|
|
}> = [];
|
|
|
|
for (const host of hostsInFolder) {
|
|
if (targets.some((t) => t.type === "user" && t.id === host.userId)) {
|
|
hostResults.push({
|
|
hostId: host.id,
|
|
shared: false,
|
|
reason: "owner",
|
|
});
|
|
continue;
|
|
}
|
|
|
|
const sharing = await canManageHostSharing(userId, host.id);
|
|
if (!sharing.allowed) {
|
|
hostResults.push({
|
|
hostId: host.id,
|
|
shared: false,
|
|
reason: "forbidden",
|
|
});
|
|
continue;
|
|
}
|
|
|
|
for (const target of targets) {
|
|
const accessGrant = await rbacAccessRepository.upsertHostAccess({
|
|
hostId: host.id,
|
|
grantedBy: userId,
|
|
permissionLevel,
|
|
expiresAt,
|
|
...(target.type === "user"
|
|
? {
|
|
targetType: "user" as const,
|
|
targetUserId: target.id as string,
|
|
}
|
|
: {
|
|
targetType: "role" as const,
|
|
targetRoleId: target.id as number,
|
|
}),
|
|
});
|
|
|
|
try {
|
|
if (target.type === "user") {
|
|
await secretsManager.snapshotForUser(
|
|
accessGrant.id,
|
|
host.id,
|
|
target.id as string,
|
|
host.userId,
|
|
);
|
|
} else {
|
|
await secretsManager.snapshotForRole(
|
|
accessGrant.id,
|
|
host.id,
|
|
target.id as number,
|
|
host.userId,
|
|
);
|
|
}
|
|
} catch (snapshotError) {
|
|
databaseLogger.warn("Share created but secret snapshot failed", {
|
|
operation: "rbac_folder_share_snapshot_failed",
|
|
hostId: host.id,
|
|
accessId: accessGrant.id,
|
|
error: getErrorMessage(snapshotError),
|
|
});
|
|
}
|
|
}
|
|
|
|
hostResults.push({ hostId: host.id, shared: true });
|
|
}
|
|
|
|
const sharedCount = hostResults.filter((r) => r.shared).length;
|
|
|
|
databaseLogger.success("Folder shared successfully", {
|
|
operation: "rbac_folder_share_success",
|
|
userId,
|
|
folder,
|
|
hostsShared: sharedCount,
|
|
targets: targets.length,
|
|
permissionLevel,
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
message: "Folder shared successfully",
|
|
permissionLevel,
|
|
expiresAt,
|
|
hostsShared: sharedCount,
|
|
hostsTotal: hostsInFolder.length,
|
|
hostResults,
|
|
});
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to share folder", error, {
|
|
operation: "share_folder",
|
|
folder,
|
|
userId,
|
|
});
|
|
res.status(500).json({ error: "Failed to share folder" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/host/{id}/access/{accessId}:
|
|
* patch:
|
|
* summary: Update a host access grant
|
|
* description: Changes the permission level and/or expiry of an existing host access grant. Allowed for the host owner or recipients holding the manage level.
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* - in: path
|
|
* name: accessId
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* permissionLevel:
|
|
* type: string
|
|
* enum: [connect, view, edit, manage]
|
|
* durationHours:
|
|
* type: number
|
|
* nullable: true
|
|
* description: Hours from now until the grant expires; null clears the expiry.
|
|
* responses:
|
|
* 200:
|
|
* description: Grant updated successfully.
|
|
* 400:
|
|
* description: Invalid request.
|
|
* 403:
|
|
* description: Caller may not manage sharing on this host.
|
|
* 404:
|
|
* description: Grant not found.
|
|
* 500:
|
|
* description: Failed to update grant.
|
|
*/
|
|
/**
|
|
* @openapi
|
|
* /rbac/folder/access:
|
|
* get:
|
|
* summary: Standing shares on one of your folders (inherited by hosts added later)
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: query
|
|
* name: folder
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
*/
|
|
router.get(
|
|
"/folder/access",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const folder = String(req.query.folder ?? "");
|
|
if (!isNonEmptyString(folder)) {
|
|
return res.status(400).json({ error: "folder is required" });
|
|
}
|
|
try {
|
|
res.json({
|
|
rules: await createCurrentFolderAccessRepository().listForFolder(
|
|
req.userId!,
|
|
folder,
|
|
),
|
|
});
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to list folder access rules", error, {
|
|
operation: "list_folder_access",
|
|
});
|
|
res.status(500).json({ error: "Failed to list folder access" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/folder/access/{id}:
|
|
* delete:
|
|
* summary: Stop a folder share from applying to hosts added later
|
|
* description: Grants already fanned out to hosts stay; revoke those per host.
|
|
* tags:
|
|
* - RBAC
|
|
*/
|
|
router.delete(
|
|
"/folder/access/:id",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const ruleId = parseInt(String(req.params.id), 10);
|
|
if (isNaN(ruleId)) {
|
|
return res.status(400).json({ error: "Invalid rule ID" });
|
|
}
|
|
try {
|
|
const repository = createCurrentFolderAccessRepository();
|
|
if (!(await repository.findById(ruleId, req.userId!))) {
|
|
return res.status(404).json({ error: "Rule not found" });
|
|
}
|
|
await repository.deleteById(ruleId, req.userId!);
|
|
res.json({ success: true });
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to delete folder access rule", error, {
|
|
operation: "delete_folder_access",
|
|
ruleId,
|
|
});
|
|
res.status(500).json({ error: "Failed to delete folder access" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/host/{id}/access/{accessId}:
|
|
* patch:
|
|
* summary: Change a host access rule
|
|
* description: Updates the permission level or the expiry of one share. Requires the right to manage sharing on the host.
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* - in: path
|
|
* name: accessId
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* permissionLevel:
|
|
* type: string
|
|
* enum: [connect, view, edit, manage]
|
|
* durationHours:
|
|
* type: number
|
|
* nullable: true
|
|
* description: Hours from now until the share expires. Null removes the expiry.
|
|
* responses:
|
|
* 200:
|
|
* description: The updated rule.
|
|
* 400:
|
|
* description: Invalid id or nothing to change.
|
|
* 403:
|
|
* description: Not allowed to manage sharing on this host.
|
|
* 404:
|
|
* description: No such rule.
|
|
*/
|
|
router.patch(
|
|
"/host/:id/access/:accessId",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
const accessIdParam = Array.isArray(req.params.accessId)
|
|
? req.params.accessId[0]
|
|
: req.params.accessId;
|
|
const hostId = parseInt(id, 10);
|
|
const accessId = parseInt(accessIdParam, 10);
|
|
const userId = req.userId!;
|
|
|
|
if (isNaN(hostId) || isNaN(accessId)) {
|
|
return res.status(400).json({ error: "Invalid ID" });
|
|
}
|
|
|
|
try {
|
|
const sharing = await canManageHostSharing(userId, hostId);
|
|
if (!sharing.allowed) {
|
|
return res
|
|
.status(403)
|
|
.json({ error: "You may not manage sharing on this host" });
|
|
}
|
|
|
|
const { permissionLevel, durationHours } = req.body ?? {};
|
|
|
|
if (permissionLevel === undefined && durationHours === undefined) {
|
|
return res.status(400).json({
|
|
error: "At least one of permissionLevel or durationHours is required",
|
|
});
|
|
}
|
|
|
|
if (
|
|
permissionLevel !== undefined &&
|
|
!isSharePermissionLevel(permissionLevel)
|
|
) {
|
|
return res.status(400).json({
|
|
error: "Invalid permission level",
|
|
validLevels: SHARE_PERMISSION_LEVELS,
|
|
});
|
|
}
|
|
|
|
const rbacAccessRepository = createCurrentRbacAccessRepository();
|
|
const grant = await rbacAccessRepository.findHostAccessById(
|
|
accessId,
|
|
hostId,
|
|
);
|
|
if (!grant) {
|
|
return res.status(404).json({ error: "Access grant not found" });
|
|
}
|
|
|
|
const update: { permissionLevel?: string; expiresAt?: string | null } =
|
|
{};
|
|
if (permissionLevel !== undefined) {
|
|
update.permissionLevel = permissionLevel;
|
|
}
|
|
if (durationHours !== undefined) {
|
|
update.expiresAt =
|
|
durationHours === null ? null : expiryFromDuration(durationHours);
|
|
}
|
|
|
|
await rbacAccessRepository.updateHostAccessGrant(
|
|
accessId,
|
|
hostId,
|
|
update,
|
|
);
|
|
await pruneHostGrants(hostId);
|
|
|
|
databaseLogger.info("Host access grant updated", {
|
|
operation: "rbac_host_access_update",
|
|
userId,
|
|
hostId,
|
|
accessId,
|
|
permissionLevel,
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
message: "Access updated",
|
|
expiresAt: update.expiresAt ?? grant.expiresAt,
|
|
});
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to update host access", error, {
|
|
operation: "update_host_access",
|
|
hostId,
|
|
accessId,
|
|
userId,
|
|
});
|
|
res.status(500).json({ error: "Failed to update access" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/host/{id}/access/{accessId}:
|
|
* delete:
|
|
* summary: Revoke host access
|
|
* description: Revokes a user's or role's access to a host.
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* - in: path
|
|
* name: accessId
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* responses:
|
|
* 200:
|
|
* description: Access revoked successfully.
|
|
* 400:
|
|
* description: Invalid ID.
|
|
* 403:
|
|
* description: Caller may not manage sharing on this host.
|
|
* 500:
|
|
* description: Failed to revoke access.
|
|
*/
|
|
router.delete(
|
|
"/host/:id/access/:accessId",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
const accessIdParam = Array.isArray(req.params.accessId)
|
|
? req.params.accessId[0]
|
|
: req.params.accessId;
|
|
const hostId = parseInt(id, 10);
|
|
const accessId = parseInt(accessIdParam, 10);
|
|
const userId = req.userId!;
|
|
|
|
if (isNaN(hostId) || isNaN(accessId)) {
|
|
return res.status(400).json({ error: "Invalid ID" });
|
|
}
|
|
|
|
try {
|
|
const sharing = await canManageHostSharing(userId, hostId);
|
|
if (!sharing.allowed) {
|
|
return res
|
|
.status(403)
|
|
.json({ error: "You may not manage sharing on this host" });
|
|
}
|
|
|
|
await createCurrentRbacAccessRepository().revokeHostAccess(
|
|
accessId,
|
|
hostId,
|
|
);
|
|
await pruneHostGrants(hostId);
|
|
databaseLogger.info("Permission revoked", {
|
|
operation: "rbac_permission_revoke",
|
|
adminId: userId,
|
|
hostId,
|
|
accessId,
|
|
});
|
|
|
|
res.json({ success: true, message: "Access revoked" });
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to revoke host access", error, {
|
|
operation: "revoke_host_access",
|
|
hostId,
|
|
accessId,
|
|
userId,
|
|
});
|
|
res.status(500).json({ error: "Failed to revoke access" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/host/{id}/access:
|
|
* get:
|
|
* summary: Get host access list
|
|
* description: Retrieves the list of users and roles that have access to a host.
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* responses:
|
|
* 200:
|
|
* description: The access list for the host, including each grant's permission level.
|
|
* 400:
|
|
* description: Invalid host ID.
|
|
* 403:
|
|
* description: Caller may not manage sharing on this host.
|
|
* 500:
|
|
* description: Failed to get access list.
|
|
*/
|
|
router.get(
|
|
"/host/:id/access",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
const hostId = parseInt(id, 10);
|
|
const userId = req.userId!;
|
|
|
|
if (isNaN(hostId)) {
|
|
return res.status(400).json({ error: "Invalid host ID" });
|
|
}
|
|
|
|
try {
|
|
const sharing = await canManageHostSharing(userId, hostId);
|
|
if (!sharing.allowed) {
|
|
return res
|
|
.status(403)
|
|
.json({ error: "You may not manage sharing on this host" });
|
|
}
|
|
|
|
const accessList =
|
|
await createCurrentRbacAccessRepository().listHostAccess(hostId);
|
|
|
|
res.json({ accessList, isOwner: sharing.isOwner });
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to get host access list", error, {
|
|
operation: "get_host_access_list",
|
|
hostId,
|
|
userId,
|
|
});
|
|
res.status(500).json({ error: "Failed to get access list" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/shared-hosts:
|
|
* get:
|
|
* summary: Get shared hosts
|
|
* description: Retrieves the list of hosts that have been shared with the authenticated user.
|
|
* tags:
|
|
* - RBAC
|
|
* responses:
|
|
* 200:
|
|
* description: A list of shared hosts.
|
|
* 500:
|
|
* description: Failed to get shared hosts.
|
|
*/
|
|
router.get(
|
|
"/shared-hosts",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const userId = req.userId!;
|
|
|
|
try {
|
|
const roleIds =
|
|
await createCurrentRoleRepository().listUserRoleIds(userId);
|
|
const sharedHosts =
|
|
await createCurrentRbacAccessRepository().listSharedHosts(
|
|
userId,
|
|
roleIds,
|
|
);
|
|
|
|
res.json({ sharedHosts });
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to get shared hosts", error, {
|
|
operation: "get_shared_hosts",
|
|
userId,
|
|
});
|
|
res.status(500).json({ error: "Failed to get shared hosts" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/roles:
|
|
* get:
|
|
* summary: Get all roles
|
|
* description: Retrieves a list of all roles.
|
|
* tags:
|
|
* - RBAC
|
|
* responses:
|
|
* 200:
|
|
* description: A list of roles.
|
|
* 500:
|
|
* description: Failed to get roles.
|
|
*/
|
|
router.get(
|
|
"/roles",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
try {
|
|
const rolesList = (await createCurrentRoleRepository().listRoles()).map(
|
|
({
|
|
id,
|
|
name,
|
|
displayName,
|
|
description,
|
|
isSystem,
|
|
permissions,
|
|
createdAt,
|
|
updatedAt,
|
|
}) => {
|
|
let parsedPermissions: string[] = [];
|
|
try {
|
|
parsedPermissions = permissions
|
|
? (JSON.parse(permissions) as string[])
|
|
: [];
|
|
} catch {
|
|
parsedPermissions = [];
|
|
}
|
|
|
|
return {
|
|
id,
|
|
name,
|
|
displayName,
|
|
description,
|
|
isSystem,
|
|
permissions: parsedPermissions,
|
|
createdAt,
|
|
updatedAt,
|
|
};
|
|
},
|
|
);
|
|
|
|
res.json({ roles: rolesList });
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to get roles", error, {
|
|
operation: "get_roles",
|
|
});
|
|
res.status(500).json({ error: "Failed to get roles" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/roles:
|
|
* post:
|
|
* summary: Create a new role
|
|
* description: Creates a new role.
|
|
* tags:
|
|
* - RBAC
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* name:
|
|
* type: string
|
|
* displayName:
|
|
* type: string
|
|
* description:
|
|
* type: string
|
|
* responses:
|
|
* 201:
|
|
* description: Role created successfully.
|
|
* 400:
|
|
* description: Invalid request body.
|
|
* 409:
|
|
* description: A role with this name already exists.
|
|
* 500:
|
|
* description: Failed to create role.
|
|
*/
|
|
router.post(
|
|
"/roles",
|
|
authenticateJWT,
|
|
permissionManager.requirePermission("admin.roles.manage"),
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const { name, displayName, description } = req.body;
|
|
|
|
if (!isNonEmptyString(name) || !isNonEmptyString(displayName)) {
|
|
return res.status(400).json({
|
|
error: "Role name and display name are required",
|
|
});
|
|
}
|
|
|
|
if (!/^[a-z0-9_-]+$/.test(name)) {
|
|
return res.status(400).json({
|
|
error:
|
|
"Role name must contain only lowercase letters, numbers, underscores, and hyphens",
|
|
});
|
|
}
|
|
|
|
try {
|
|
const existing = await createCurrentRoleRepository().findRoleByName(name);
|
|
|
|
if (existing) {
|
|
return res.status(409).json({
|
|
error: "A role with this name already exists",
|
|
});
|
|
}
|
|
|
|
const newRoleId = await createCurrentRoleRepository().createRole({
|
|
name,
|
|
displayName,
|
|
description: description || null,
|
|
isSystem: false,
|
|
permissions: null,
|
|
});
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
roleId: newRoleId,
|
|
message: "Role created successfully",
|
|
});
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to create role", error, {
|
|
operation: "create_role",
|
|
roleName: name,
|
|
});
|
|
res.status(500).json({ error: "Failed to create role" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/roles/{id}:
|
|
* put:
|
|
* summary: Update a role
|
|
* description: Updates a role by its ID.
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* displayName:
|
|
* type: string
|
|
* description:
|
|
* type: string
|
|
* permissions:
|
|
* type: array
|
|
* items:
|
|
* type: string
|
|
* description: Permission strings validated against the permissions catalog (wildcards like hosts.* and * allowed).
|
|
* responses:
|
|
* 200:
|
|
* description: Role updated successfully.
|
|
* 400:
|
|
* description: Invalid request body or role ID.
|
|
* 404:
|
|
* description: Role not found.
|
|
* 500:
|
|
* description: Failed to update role.
|
|
*/
|
|
router.put(
|
|
"/roles/:id",
|
|
authenticateJWT,
|
|
permissionManager.requirePermission("admin.roles.manage"),
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
const roleId = parseInt(id, 10);
|
|
const { displayName, description, permissions } = req.body;
|
|
|
|
if (isNaN(roleId)) {
|
|
return res.status(400).json({ error: "Invalid role ID" });
|
|
}
|
|
|
|
if (
|
|
!displayName &&
|
|
description === undefined &&
|
|
permissions === undefined
|
|
) {
|
|
return res.status(400).json({
|
|
error:
|
|
"At least one field (displayName, description or permissions) is required",
|
|
});
|
|
}
|
|
|
|
if (permissions !== undefined) {
|
|
if (
|
|
!Array.isArray(permissions) ||
|
|
permissions.some((perm) => typeof perm !== "string")
|
|
) {
|
|
return res
|
|
.status(400)
|
|
.json({ error: "permissions must be an array of strings" });
|
|
}
|
|
|
|
const invalid = (permissions as string[]).filter(
|
|
(perm) => !isValidPermission(perm),
|
|
);
|
|
if (invalid.length > 0) {
|
|
return res.status(400).json({
|
|
error: "Unknown permissions",
|
|
invalid,
|
|
});
|
|
}
|
|
}
|
|
|
|
try {
|
|
const roleRepository = createCurrentRoleRepository();
|
|
const existingRole = await roleRepository.findRoleById(roleId);
|
|
|
|
if (!existingRole) {
|
|
return res.status(404).json({ error: "Role not found" });
|
|
}
|
|
|
|
const updates: {
|
|
displayName?: string;
|
|
description?: string | null;
|
|
permissions?: string | null;
|
|
updatedAt: string;
|
|
} = {
|
|
updatedAt: new Date().toISOString(),
|
|
};
|
|
|
|
if (displayName) {
|
|
updates.displayName = displayName;
|
|
}
|
|
|
|
if (description !== undefined) {
|
|
updates.description = description || null;
|
|
}
|
|
|
|
if (permissions !== undefined) {
|
|
updates.permissions = JSON.stringify(permissions);
|
|
}
|
|
|
|
await roleRepository.updateRole(roleId, updates);
|
|
|
|
if (permissions !== undefined) {
|
|
const memberIds = await roleRepository.listRoleUserIds(roleId);
|
|
for (const memberId of memberIds) {
|
|
permissionManager.invalidateUserPermissionCache(memberId);
|
|
}
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
message: "Role updated successfully",
|
|
});
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to update role", error, {
|
|
operation: "update_role",
|
|
roleId,
|
|
});
|
|
res.status(500).json({ error: "Failed to update role" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/permissions/catalog:
|
|
* get:
|
|
* summary: Get the role permissions catalog
|
|
* description: Returns the grouped catalog of role permission strings used by the role permissions editor.
|
|
* tags:
|
|
* - RBAC
|
|
* responses:
|
|
* 200:
|
|
* description: The permissions catalog.
|
|
*/
|
|
router.get(
|
|
"/permissions/catalog",
|
|
authenticateJWT,
|
|
async (_req: AuthenticatedRequest, res: Response) => {
|
|
res.json({ catalog: getPermissionCatalog() });
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/permissions/me:
|
|
* get:
|
|
* summary: Get the current user's effective role permissions
|
|
* description: >
|
|
* Returns the permission strings granted to the authenticated user
|
|
* through their roles, plus whether they are an admin. The frontend uses
|
|
* this to hide UI a user could not use. It is not a security boundary:
|
|
* every protected route checks again server side.
|
|
* tags:
|
|
* - RBAC
|
|
* responses:
|
|
* 200:
|
|
* description: The current user's permissions.
|
|
* 401:
|
|
* description: Not authenticated.
|
|
*/
|
|
router.get(
|
|
"/permissions/me",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const userId = req.userId!;
|
|
try {
|
|
// isAdmin travels with the list because hasPermission() falls back to it
|
|
// on the deny path. Without it the UI would hide things from an admin
|
|
// that the server would allow.
|
|
const [permissions, isAdmin] = await Promise.all([
|
|
permissionManager.getUserPermissions(userId),
|
|
permissionManager.isAdmin(userId),
|
|
]);
|
|
res.json({ permissions, isAdmin });
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to get current user permissions", error, {
|
|
operation: "get_my_permissions",
|
|
userId,
|
|
});
|
|
res.status(500).json({ error: "Failed to get permissions" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/roles/{id}:
|
|
* delete:
|
|
* summary: Delete a role
|
|
* description: Deletes a role by its ID.
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* responses:
|
|
* 200:
|
|
* description: Role deleted successfully.
|
|
* 400:
|
|
* description: Invalid role ID.
|
|
* 403:
|
|
* description: Cannot delete system roles.
|
|
* 404:
|
|
* description: Role not found.
|
|
* 500:
|
|
* description: Failed to delete role.
|
|
*/
|
|
router.delete(
|
|
"/roles/:id",
|
|
authenticateJWT,
|
|
permissionManager.requirePermission("admin.roles.manage"),
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
const roleId = parseInt(id, 10);
|
|
|
|
if (isNaN(roleId)) {
|
|
return res.status(400).json({ error: "Invalid role ID" });
|
|
}
|
|
|
|
try {
|
|
const role = await createCurrentRoleRepository().findRoleById(roleId);
|
|
|
|
if (!role) {
|
|
return res.status(404).json({ error: "Role not found" });
|
|
}
|
|
|
|
if (role.isSystem) {
|
|
return res.status(403).json({
|
|
error: "Cannot delete system roles",
|
|
});
|
|
}
|
|
|
|
const { deletedUserIds } =
|
|
await createCurrentRoleRepository().deleteRole(roleId);
|
|
|
|
for (const userId of deletedUserIds) {
|
|
permissionManager.invalidateUserPermissionCache(userId);
|
|
}
|
|
await pruneGrantsBy(deletedUserIds);
|
|
|
|
res.json({
|
|
success: true,
|
|
message: "Role deleted successfully",
|
|
});
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to delete role", error, {
|
|
operation: "delete_role",
|
|
roleId,
|
|
});
|
|
res.status(500).json({ error: "Failed to delete role" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/roles/{id}/members:
|
|
* get:
|
|
* summary: List the users holding a role
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* responses:
|
|
* 200:
|
|
* description: Role members.
|
|
* 404:
|
|
* description: Role not found.
|
|
*/
|
|
router.get(
|
|
"/roles/:id/members",
|
|
authenticateJWT,
|
|
permissionManager.requirePermission("admin.roles.manage"),
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
const roleId = parseInt(id, 10);
|
|
|
|
if (isNaN(roleId)) {
|
|
return res.status(400).json({ error: "Invalid role ID" });
|
|
}
|
|
|
|
try {
|
|
const roleRepository = createCurrentRoleRepository();
|
|
if (!(await roleRepository.findRoleById(roleId))) {
|
|
return res.status(404).json({ error: "Role not found" });
|
|
}
|
|
res.json({ members: await roleRepository.listRoleMembers(roleId) });
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to list role members", error, {
|
|
operation: "list_role_members",
|
|
roleId,
|
|
});
|
|
res.status(500).json({ error: "Failed to list role members" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/users/{userId}/roles:
|
|
* post:
|
|
* summary: Assign a role to a user
|
|
* description: Assigns a role to a user.
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: userId
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* roleId:
|
|
* type: integer
|
|
* responses:
|
|
* 200:
|
|
* description: Role assigned successfully.
|
|
* 400:
|
|
* description: Role ID is required.
|
|
* 403:
|
|
* description: System roles cannot be manually assigned.
|
|
* 404:
|
|
* description: User or role not found.
|
|
* 409:
|
|
* description: Role already assigned.
|
|
* 500:
|
|
* description: Failed to assign role.
|
|
*/
|
|
router.post(
|
|
"/users/:userId/roles",
|
|
authenticateJWT,
|
|
permissionManager.requirePermission("admin.roles.manage"),
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const targetUserId = Array.isArray(req.params.userId)
|
|
? req.params.userId[0]
|
|
: req.params.userId;
|
|
const currentUserId = req.userId!;
|
|
|
|
try {
|
|
const { roleId } = req.body;
|
|
|
|
if (typeof roleId !== "number") {
|
|
return res.status(400).json({ error: "Role ID is required" });
|
|
}
|
|
|
|
const targetUser =
|
|
await createCurrentUserRepository().findById(targetUserId);
|
|
|
|
if (!targetUser) {
|
|
return res.status(404).json({ error: "User not found" });
|
|
}
|
|
|
|
const role = await createCurrentRoleRepository().findRoleById(roleId);
|
|
|
|
if (!role) {
|
|
return res.status(404).json({ error: "Role not found" });
|
|
}
|
|
|
|
if (role.isSystem) {
|
|
return res.status(403).json({
|
|
error:
|
|
"System roles (admin, user) are automatically assigned and cannot be manually assigned",
|
|
});
|
|
}
|
|
|
|
const existing = await createCurrentRoleRepository().findUserRole(
|
|
targetUserId,
|
|
roleId,
|
|
);
|
|
|
|
if (existing) {
|
|
return res.status(409).json({ error: "Role already assigned" });
|
|
}
|
|
|
|
await createCurrentRoleRepository().assignRoleToUser({
|
|
userId: targetUserId,
|
|
roleId,
|
|
grantedBy: currentUserId,
|
|
});
|
|
|
|
try {
|
|
const { SharedHostSecretsManager } =
|
|
await import("../../utils/shared-host-secrets-manager.js");
|
|
await SharedHostSecretsManager.getInstance().snapshotForRoleMember(
|
|
roleId,
|
|
targetUserId,
|
|
);
|
|
const { SharedCredentialSecretsManager } =
|
|
await import("../../utils/shared-credential-secrets-manager.js");
|
|
await SharedCredentialSecretsManager.getInstance().snapshotForRoleMember(
|
|
roleId,
|
|
targetUserId,
|
|
);
|
|
} catch (error) {
|
|
databaseLogger.error(
|
|
"Failed to snapshot shared host secrets for new role member",
|
|
error,
|
|
{
|
|
operation: "assign_role_snapshot_secrets",
|
|
targetUserId,
|
|
roleId,
|
|
},
|
|
);
|
|
}
|
|
|
|
permissionManager.invalidateUserPermissionCache(targetUserId);
|
|
databaseLogger.info("Role assigned to user", {
|
|
operation: "rbac_role_assign",
|
|
adminId: currentUserId,
|
|
targetUserId,
|
|
roleId,
|
|
roleName: role.name,
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
message: "Role assigned successfully",
|
|
});
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to assign role", error, {
|
|
operation: "assign_role",
|
|
targetUserId,
|
|
});
|
|
res.status(500).json({ error: "Failed to assign role" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/users/{userId}/roles/{roleId}:
|
|
* delete:
|
|
* summary: Remove a role from a user
|
|
* description: Removes a role from a user.
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: userId
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* - in: path
|
|
* name: roleId
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* responses:
|
|
* 200:
|
|
* description: Role removed successfully.
|
|
* 400:
|
|
* description: Invalid role ID.
|
|
* 403:
|
|
* description: System roles cannot be removed.
|
|
* 404:
|
|
* description: Role not found.
|
|
* 500:
|
|
* description: Failed to remove role.
|
|
*/
|
|
router.delete(
|
|
"/users/:userId/roles/:roleId",
|
|
authenticateJWT,
|
|
permissionManager.requirePermission("admin.roles.manage"),
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const targetUserId = Array.isArray(req.params.userId)
|
|
? req.params.userId[0]
|
|
: req.params.userId;
|
|
const roleIdParam = Array.isArray(req.params.roleId)
|
|
? req.params.roleId[0]
|
|
: req.params.roleId;
|
|
const roleId = parseInt(roleIdParam, 10);
|
|
|
|
if (isNaN(roleId)) {
|
|
return res.status(400).json({ error: "Invalid role ID" });
|
|
}
|
|
|
|
try {
|
|
const role = await createCurrentRoleRepository().findRoleById(roleId);
|
|
|
|
if (!role) {
|
|
return res.status(404).json({ error: "Role not found" });
|
|
}
|
|
|
|
if (role.isSystem) {
|
|
return res.status(403).json({
|
|
error:
|
|
"System roles (admin, user) are automatically assigned and cannot be removed",
|
|
});
|
|
}
|
|
|
|
await createCurrentRoleRepository().removeRoleFromUser(
|
|
targetUserId,
|
|
roleId,
|
|
);
|
|
|
|
try {
|
|
const { createCurrentSharedHostSecretsRepository } =
|
|
await import("../repositories/factory.js");
|
|
await createCurrentSharedHostSecretsRepository().deleteForRoleMember(
|
|
roleId,
|
|
targetUserId,
|
|
);
|
|
const { createCurrentSharedCredentialSecretsRepository } =
|
|
await import("../repositories/factory.js");
|
|
await createCurrentSharedCredentialSecretsRepository().deleteForRoleMember(
|
|
roleId,
|
|
targetUserId,
|
|
);
|
|
} catch (cleanupError) {
|
|
databaseLogger.warn(
|
|
"Failed to clean shared host secrets after role removal",
|
|
{
|
|
operation: "remove_role_secret_cleanup",
|
|
targetUserId,
|
|
roleId,
|
|
error: getErrorMessage(cleanupError),
|
|
},
|
|
);
|
|
}
|
|
|
|
permissionManager.invalidateUserPermissionCache(targetUserId);
|
|
await pruneGrantsBy([targetUserId]);
|
|
databaseLogger.info("Role removed from user", {
|
|
operation: "rbac_role_remove",
|
|
adminId: req.userId!,
|
|
targetUserId,
|
|
roleId,
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
message: "Role removed successfully",
|
|
});
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to remove role", error, {
|
|
operation: "remove_role",
|
|
targetUserId,
|
|
roleId,
|
|
});
|
|
res.status(500).json({ error: "Failed to remove role" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/users/{userId}/roles:
|
|
* get:
|
|
* summary: Get user's roles
|
|
* description: Retrieves a list of roles for a specific user.
|
|
* tags:
|
|
* - RBAC
|
|
* parameters:
|
|
* - in: path
|
|
* name: userId
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: A list of roles.
|
|
* 403:
|
|
* description: Access denied.
|
|
* 500:
|
|
* description: Failed to get user roles.
|
|
*/
|
|
router.get(
|
|
"/users/:userId/roles",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const targetUserId = Array.isArray(req.params.userId)
|
|
? req.params.userId[0]
|
|
: req.params.userId;
|
|
const currentUserId = req.userId!;
|
|
|
|
if (
|
|
targetUserId !== currentUserId &&
|
|
!(await permissionManager.isAdmin(currentUserId))
|
|
) {
|
|
return res.status(403).json({ error: "Access denied" });
|
|
}
|
|
|
|
try {
|
|
const userRolesList =
|
|
await createCurrentRoleRepository().listUserRoles(targetUserId);
|
|
|
|
res.json({ roles: userRolesList });
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to get user roles", error, {
|
|
operation: "get_user_roles",
|
|
targetUserId,
|
|
});
|
|
res.status(500).json({ error: "Failed to get user roles" });
|
|
}
|
|
},
|
|
);
|
|
|
|
// CREDENTIAL SHARING
|
|
|
|
const CREDENTIAL_LEVELS = ["use", "manage"] as const;
|
|
type CredentialLevel = (typeof CREDENTIAL_LEVELS)[number];
|
|
|
|
/** Owner, or a recipient holding "manage". */
|
|
async function canManageCredentialSharing(
|
|
userId: string,
|
|
credentialId: number,
|
|
): Promise<{ allowed: boolean; ownerId: string | null }> {
|
|
const row = await createCurrentCredentialRepository().findById(credentialId);
|
|
if (!row) return { allowed: false, ownerId: null };
|
|
if (row.userId === userId) return { allowed: true, ownerId: row.userId };
|
|
const roleIds = await createCurrentRoleRepository().listUserRoleIds(userId);
|
|
const grant = await createCurrentCredentialAccessRepository().findActiveGrant(
|
|
credentialId,
|
|
userId,
|
|
roleIds,
|
|
);
|
|
return { allowed: grant?.permissionLevel === "manage", ownerId: row.userId };
|
|
}
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/credential/{id}/share:
|
|
* post:
|
|
* summary: Share a credential with users or roles
|
|
* description: Recipients get a copy of the secrets re-encrypted under their own key. "use" lets them attach it to hosts and connect; "manage" also lets them edit and re-share. Owner or a "manage" recipient only.
|
|
* tags:
|
|
* - RBAC
|
|
*/
|
|
router.post(
|
|
"/credential/:id/share",
|
|
authenticateJWT,
|
|
permissionManager.requirePermission("credentials.share"),
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
const credentialId = parseInt(id, 10);
|
|
const userId = req.userId!;
|
|
if (isNaN(credentialId)) {
|
|
return res.status(400).json({ error: "Invalid credential ID" });
|
|
}
|
|
const targets = parseShareTargets(req.body ?? {});
|
|
if (!targets) {
|
|
return res.status(400).json({
|
|
error:
|
|
"targets must be a non-empty array of { type: 'user'|'role', id } entries",
|
|
});
|
|
}
|
|
const { durationHours, permissionLevel = "use" } = req.body ?? {};
|
|
if (!CREDENTIAL_LEVELS.includes(permissionLevel)) {
|
|
return res.status(400).json({
|
|
error: "Invalid permission level",
|
|
validLevels: CREDENTIAL_LEVELS,
|
|
});
|
|
}
|
|
|
|
try {
|
|
const sharing = await canManageCredentialSharing(userId, credentialId);
|
|
if (!sharing.allowed || !sharing.ownerId) {
|
|
return res
|
|
.status(403)
|
|
.json({ error: "Not allowed to share this credential" });
|
|
}
|
|
const ownerId = sharing.ownerId;
|
|
|
|
const userRepository = createCurrentUserRepository();
|
|
const roleRepository = createCurrentRoleRepository();
|
|
for (const target of targets) {
|
|
const found =
|
|
target.type === "user"
|
|
? await userRepository.findById(target.id as string)
|
|
: await roleRepository.findRoleById(target.id as number);
|
|
if (!found) {
|
|
return res.status(404).json({
|
|
error: `Target ${target.type} not found`,
|
|
targetId: target.id,
|
|
});
|
|
}
|
|
}
|
|
|
|
const expiresAt = expiryFromDuration(durationHours);
|
|
const accessRepository = createCurrentCredentialAccessRepository();
|
|
const { SharedCredentialSecretsManager } =
|
|
await import("../../utils/shared-credential-secrets-manager.js");
|
|
const manager = SharedCredentialSecretsManager.getInstance();
|
|
|
|
for (const target of targets) {
|
|
if (target.type === "user" && target.id === ownerId) continue;
|
|
const grant = await accessRepository.upsert({
|
|
credentialId,
|
|
grantedBy: userId,
|
|
permissionLevel: permissionLevel as CredentialLevel,
|
|
expiresAt,
|
|
target:
|
|
target.type === "user"
|
|
? { targetType: "user", targetUserId: target.id as string }
|
|
: { targetType: "role", targetRoleId: target.id as number },
|
|
});
|
|
try {
|
|
if (target.type === "user") {
|
|
await manager.snapshotForUser(
|
|
grant.id,
|
|
credentialId,
|
|
target.id as string,
|
|
ownerId,
|
|
);
|
|
} else {
|
|
await manager.snapshotForRole(
|
|
grant.id,
|
|
credentialId,
|
|
target.id as number,
|
|
ownerId,
|
|
);
|
|
}
|
|
} catch (snapshotError) {
|
|
databaseLogger.warn("Credential shared but secret snapshot failed", {
|
|
operation: "rbac_credential_share_snapshot_failed",
|
|
credentialId,
|
|
accessId: grant.id,
|
|
error: getErrorMessage(snapshotError),
|
|
});
|
|
}
|
|
}
|
|
|
|
const { ipAddress, userAgent } = getRequestMeta(req);
|
|
await logAudit({
|
|
userId,
|
|
username: await getAuditUsername(userId),
|
|
action: "credential_share",
|
|
resourceType: "credential",
|
|
resourceId: String(credentialId),
|
|
details: JSON.stringify({ targets, permissionLevel, expiresAt }),
|
|
ipAddress,
|
|
userAgent,
|
|
success: true,
|
|
});
|
|
|
|
await pruneCredentialGrants(credentialId);
|
|
res.json({ success: true, permissionLevel, expiresAt });
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to share credential", error, {
|
|
operation: "share_credential",
|
|
credentialId,
|
|
userId,
|
|
});
|
|
res.status(500).json({ error: "Failed to share credential" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/credential/{id}/access:
|
|
* get:
|
|
* summary: List who a credential is shared with
|
|
* tags:
|
|
* - RBAC
|
|
*/
|
|
router.get(
|
|
"/credential/:id/access",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
const credentialId = parseInt(id, 10);
|
|
if (isNaN(credentialId)) {
|
|
return res.status(400).json({ error: "Invalid credential ID" });
|
|
}
|
|
try {
|
|
const sharing = await canManageCredentialSharing(
|
|
req.userId!,
|
|
credentialId,
|
|
);
|
|
if (!sharing.allowed) {
|
|
return res.status(403).json({ error: "Not allowed" });
|
|
}
|
|
res.json({
|
|
access:
|
|
await createCurrentCredentialAccessRepository().listForCredential(
|
|
credentialId,
|
|
),
|
|
});
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to list credential access", error, {
|
|
operation: "list_credential_access",
|
|
credentialId,
|
|
});
|
|
res.status(500).json({ error: "Failed to list credential access" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/credential/{id}/access/{accessId}:
|
|
* delete:
|
|
* summary: Revoke a credential share (recipients' copies are removed with it)
|
|
* tags:
|
|
* - RBAC
|
|
*/
|
|
router.delete(
|
|
"/credential/:id/access/:accessId",
|
|
authenticateJWT,
|
|
async (req: AuthenticatedRequest, res: Response) => {
|
|
const credentialId = parseInt(String(req.params.id), 10);
|
|
const accessId = parseInt(String(req.params.accessId), 10);
|
|
if (isNaN(credentialId) || isNaN(accessId)) {
|
|
return res.status(400).json({ error: "Invalid ID" });
|
|
}
|
|
try {
|
|
const sharing = await canManageCredentialSharing(
|
|
req.userId!,
|
|
credentialId,
|
|
);
|
|
if (!sharing.allowed) {
|
|
return res.status(403).json({ error: "Not allowed" });
|
|
}
|
|
const repository = createCurrentCredentialAccessRepository();
|
|
if (!(await repository.findById(accessId, credentialId))) {
|
|
return res.status(404).json({ error: "Access grant not found" });
|
|
}
|
|
await repository.revoke(accessId, credentialId);
|
|
await pruneCredentialGrants(credentialId);
|
|
res.json({ success: true });
|
|
} catch (error) {
|
|
databaseLogger.error("Failed to revoke credential access", error, {
|
|
operation: "revoke_credential_access",
|
|
credentialId,
|
|
accessId,
|
|
});
|
|
res.status(500).json({ error: "Failed to revoke credential access" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/host-access/{hostId}/auth/{protocol}:
|
|
* put:
|
|
* summary: Set personal authentication for a shared host protocol
|
|
* description: Selects one of the authenticated recipient's own credentials, or clears the selection with null. The protocol is "ssh" or one a plugin declares.
|
|
* tags: [RBAC]
|
|
*/
|
|
router.put(
|
|
"/host-access/:hostId/auth/:protocol",
|
|
authenticateJWT,
|
|
requireDataAccess,
|
|
async (req: express.Request, res: express.Response) => {
|
|
try {
|
|
const userId = (req as AuthenticatedRequest).userId!;
|
|
const hostId = Number.parseInt(String(req.params.hostId), 10);
|
|
const protocol = req.params.protocol;
|
|
const { credentialId } = req.body;
|
|
|
|
if (!Number.isInteger(hostId) || hostId <= 0) {
|
|
return res.status(400).json({ error: "Invalid host ID" });
|
|
}
|
|
if (!isAuthOverrideProtocol(protocol)) {
|
|
return res
|
|
.status(400)
|
|
.json({ error: "Invalid authentication protocol" });
|
|
}
|
|
|
|
if (
|
|
credentialId !== null &&
|
|
(!Number.isInteger(credentialId) || credentialId <= 0)
|
|
) {
|
|
return res.status(400).json({
|
|
error: "credentialId must be a positive integer or null",
|
|
});
|
|
}
|
|
|
|
const { ipAddress, userAgent } = getRequestMeta(req);
|
|
await sharedHostAuthOverrideService.setCredentialId(
|
|
hostId,
|
|
userId,
|
|
protocol,
|
|
credentialId,
|
|
{ ipAddress, userAgent },
|
|
);
|
|
res.json({ success: true, protocol, credentialId });
|
|
} catch (error) {
|
|
if (error instanceof SharedHostAuthOverrideServiceError) {
|
|
return res.status(error.statusCode).json({ error: error.message });
|
|
}
|
|
databaseLogger.error("Failed to set override credential", error);
|
|
res.status(500).json({ error: "Failed to update credential" });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* @openapi
|
|
* /rbac/host-access/{hostId}/auth/{protocol}:
|
|
* get:
|
|
* summary: Get the current recipient's shared-host protocol authentication override
|
|
* tags: [RBAC]
|
|
*/
|
|
router.get(
|
|
"/host-access/:hostId/auth/:protocol",
|
|
authenticateJWT,
|
|
requireDataAccess,
|
|
async (req: express.Request, res: express.Response) => {
|
|
try {
|
|
const userId = (req as AuthenticatedRequest).userId!;
|
|
const hostId = Number.parseInt(String(req.params.hostId), 10);
|
|
const protocol = req.params.protocol;
|
|
|
|
if (!Number.isInteger(hostId) || hostId <= 0) {
|
|
return res.status(400).json({ error: "Invalid host ID" });
|
|
}
|
|
if (!isAuthOverrideProtocol(protocol)) {
|
|
return res
|
|
.status(400)
|
|
.json({ error: "Invalid authentication protocol" });
|
|
}
|
|
|
|
const credentialId = await sharedHostAuthOverrideService.getCredentialId(
|
|
hostId,
|
|
userId,
|
|
protocol,
|
|
);
|
|
res.json({ protocol, credentialId });
|
|
} catch (error) {
|
|
if (error instanceof SharedHostAuthOverrideServiceError) {
|
|
return res.status(error.statusCode).json({ error: error.message });
|
|
}
|
|
databaseLogger.error("Failed to get override credential", error);
|
|
res.status(500).json({ error: "Failed to fetch credential" });
|
|
}
|
|
},
|
|
);
|
|
|
|
export default router;
|