feat: add engine workflow access requests
This commit is contained in:
@@ -547,6 +547,69 @@ app.post("/api/internal/tasker/invite-requests/cancel", asyncRoute(async (req, r
|
||||
res.json({ ok: true, taskerInviteRequest: result.taskerInviteRequest });
|
||||
}));
|
||||
|
||||
app.post("/api/internal/engine/workflow-access-requests", asyncRoute(async (req, res) => {
|
||||
if (!isInternalRequestAuthorized(req)) {
|
||||
res.status(config.internalAccessToken ? 401 : 503).json({
|
||||
ok: false,
|
||||
error: config.internalAccessToken ? "internal_access_unauthorized" : "internal_access_not_configured",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const snapshot = controlPlaneStore.getSnapshot({ name: "NODE.DC Engine workflow access request" });
|
||||
const requesterPayload = typeof req.body?.requester === "object" && req.body.requester !== null ? req.body.requester : {};
|
||||
const requester = findInternalAccessUser(snapshot.data, {
|
||||
subject: requesterPayload.subject,
|
||||
email: requesterPayload.email,
|
||||
userId: requesterPayload.userId,
|
||||
});
|
||||
const targetEmail = typeof req.body?.targetEmail === "string" ? req.body.targetEmail.trim().toLowerCase() : "";
|
||||
const targetUser = findInternalAccessUser(snapshot.data, { email: targetEmail });
|
||||
|
||||
if (!requester) {
|
||||
res.status(404).json({ ok: false, error: "requester_not_found" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!targetUser || targetUser.globalStatus !== "active") {
|
||||
res.status(404).json({ ok: false, error: "user_not_found" });
|
||||
return;
|
||||
}
|
||||
|
||||
const groups = resolveRequiredGroups(snapshot.data, targetUser);
|
||||
const app = getAppsForUser(groups).find((candidate) => candidate.slug === "nodedc");
|
||||
|
||||
if (app?.hasAccess) {
|
||||
res.json({
|
||||
ok: true,
|
||||
alreadyAllowed: true,
|
||||
targetUser: {
|
||||
id: targetUser.id,
|
||||
email: targetUser.email,
|
||||
name: targetUser.name,
|
||||
avatarUrl: targetUser.avatarUrl ?? null,
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await controlPlaneStore.createEngineWorkflowAccessRequest({
|
||||
workflowId: req.body?.workflow?.id ?? req.body?.workflowId,
|
||||
workflowName: req.body?.workflow?.name ?? req.body?.workflowName,
|
||||
targetEmail,
|
||||
role: req.body?.role,
|
||||
requesterUserId: requester.id,
|
||||
requesterEmail: requester.email,
|
||||
requesterName: requester.name,
|
||||
}, requester);
|
||||
|
||||
publishControlPlaneEvent(
|
||||
"engine.workflow-access-request.created",
|
||||
result.affectedUserIds?.length ? result.affectedUserIds : [requester.id, targetUser.id]
|
||||
);
|
||||
res.json({ ok: true, engineWorkflowAccessRequest: result.engineWorkflowAccessRequest });
|
||||
}));
|
||||
|
||||
app.post("/api/internal/tasker/profile-sync", asyncRoute(async (req, res) => {
|
||||
if (!isInternalRequestAuthorized(req)) {
|
||||
res.status(config.internalAccessToken ? 401 : 503).json({
|
||||
@@ -1372,6 +1435,56 @@ app.post("/api/admin/tasker-invite-requests/:taskerInviteRequestId/reject", requ
|
||||
res.json(scopeAdminMutationResult(req, { ...result, tasker: taskerResult }));
|
||||
}));
|
||||
|
||||
app.post("/api/admin/engine-workflow-access-requests/:engineWorkflowAccessRequestId/approve", requireLauncherAdmin, requireRootLauncherAdmin, asyncRoute(async (req, res) => {
|
||||
const snapshot = controlPlaneStore.getSnapshot(req.nodedcSession.user);
|
||||
const request = snapshot.data.engineWorkflowAccessRequests.find(
|
||||
(candidate) => candidate.id === req.params.engineWorkflowAccessRequestId
|
||||
);
|
||||
|
||||
if (!request) {
|
||||
res.status(404).json({ error: "engine_workflow_access_request_not_found" });
|
||||
return;
|
||||
}
|
||||
|
||||
const engineResult = await requestEngineInternalJson(`/api/internal/workflows/${encodeURIComponent(request.workflowId)}/share/users`, {
|
||||
body: {
|
||||
email: request.targetEmail,
|
||||
role: request.role,
|
||||
requestId: request.id,
|
||||
approvedBy: {
|
||||
userId: req.nodedcSession.user?.id,
|
||||
email: req.nodedcSession.user?.email,
|
||||
name: req.nodedcSession.user?.name,
|
||||
},
|
||||
},
|
||||
});
|
||||
const result = await controlPlaneStore.approveEngineWorkflowAccessRequest(
|
||||
req.params.engineWorkflowAccessRequestId,
|
||||
{ engineAppliedAt: engineResult.updatedAt ?? new Date().toISOString(), comment: req.body?.comment },
|
||||
req.nodedcSession.user
|
||||
);
|
||||
const syncResult = await syncUsersToAuthentik(result.data, [result.targetUser.id], req.nodedcSession.user);
|
||||
|
||||
publishControlPlaneEvent("admin.engine-workflow-access-request.approved", [
|
||||
result.engineWorkflowAccessRequest.requesterUserId,
|
||||
result.targetUser.id,
|
||||
]);
|
||||
res.json(scopeAdminMutationResult(req, { ...result, data: syncResult.data, engine: engineResult }));
|
||||
}));
|
||||
|
||||
app.post("/api/admin/engine-workflow-access-requests/:engineWorkflowAccessRequestId/reject", requireLauncherAdmin, requireRootLauncherAdmin, asyncRoute(async (req, res) => {
|
||||
const result = await controlPlaneStore.rejectEngineWorkflowAccessRequest(
|
||||
req.params.engineWorkflowAccessRequestId,
|
||||
req.body,
|
||||
req.nodedcSession.user
|
||||
);
|
||||
|
||||
publishControlPlaneEvent("admin.engine-workflow-access-request.rejected", [
|
||||
result.engineWorkflowAccessRequest.requesterUserId,
|
||||
]);
|
||||
res.json(scopeAdminMutationResult(req, result));
|
||||
}));
|
||||
|
||||
app.post("/api/admin/groups", requireLauncherAdmin, asyncRoute(async (req, res) => {
|
||||
if (!assertAdminCanManageClient(req, res, req.body?.clientId)) {
|
||||
return;
|
||||
@@ -1670,6 +1783,11 @@ function readConfig() {
|
||||
taskInternalLogoutUrl:
|
||||
process.env.TASK_INTERNAL_LOGOUT_URL ??
|
||||
`${(process.env.TASK_BASE_URL ?? `http://${process.env.TASK_DOMAIN ?? "task.local.nodedc"}`).replace(/\/$/, "")}/api/internal/nodedc/logout/`,
|
||||
engineBaseUrl:
|
||||
process.env.NODEDC_ENGINE_INTERNAL_URL ??
|
||||
process.env.NODEDC_ENGINE_BASE_URL ??
|
||||
process.env.ENGINE_BASE_URL ??
|
||||
"https://engine.nodedc.ru",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2232,6 +2350,10 @@ function getTaskBaseUrl() {
|
||||
return taskBaseUrl.replace(/\/$/, "");
|
||||
}
|
||||
|
||||
function getEngineBaseUrl() {
|
||||
return String(config.engineBaseUrl || "https://engine.nodedc.ru").replace(/\/$/, "");
|
||||
}
|
||||
|
||||
async function requestTaskManagerInternalJson(pathname, init = {}) {
|
||||
if (!config.internalAccessToken) {
|
||||
throw new Error("NODE.DC internal access token is not configured");
|
||||
@@ -2260,6 +2382,37 @@ async function requestTaskManagerInternalJson(pathname, init = {}) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
async function requestEngineInternalJson(pathname, init = {}) {
|
||||
if (!config.internalAccessToken) {
|
||||
throw new Error("NODE.DC internal access token is not configured");
|
||||
}
|
||||
|
||||
const targetUrl = new URL(pathname, `${getEngineBaseUrl()}/`);
|
||||
const hasBody = typeof init.body === "object" && init.body !== null;
|
||||
const response = await fetch(targetUrl, {
|
||||
method: init.method ?? (hasBody ? "POST" : "GET"),
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Bearer ${config.internalAccessToken}`,
|
||||
"X-Authentik-Groups": "nodedc_admin nodedc:engine:admin",
|
||||
"X-Authentik-Email": "launcher-internal@nodedc.ru",
|
||||
"X-Authentik-Username": "launcher-internal@nodedc.ru",
|
||||
...(hasBody ? { "Content-Type": "application/json" } : {}),
|
||||
...(init.headers ?? {}),
|
||||
},
|
||||
body: hasBody ? JSON.stringify(init.body) : undefined,
|
||||
});
|
||||
const text = await response.text();
|
||||
const payload = text ? parseJsonResponse(text, targetUrl.toString()) : {};
|
||||
|
||||
if (!response.ok) {
|
||||
const error = typeof payload?.error === "string" ? payload.error : `Engine internal API failed: ${response.status}`;
|
||||
throw new Error(error);
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
async function syncTaskManagerUserProfile(user) {
|
||||
if (!user?.email || !config.internalAccessToken) {
|
||||
return null;
|
||||
@@ -3363,6 +3516,7 @@ function scopeControlPlaneData(data, scope) {
|
||||
invites: data.invites.filter((invite) => clientIds.has(invite.clientId)),
|
||||
accessRequests: [],
|
||||
taskerInviteRequests: [],
|
||||
engineWorkflowAccessRequests: [],
|
||||
grants: data.grants.filter((grant) => {
|
||||
if (grant.targetType === "client") return clientIds.has(grant.targetId);
|
||||
if (grant.targetType === "group") return groupIds.has(grant.targetId);
|
||||
@@ -3398,6 +3552,7 @@ function scopeRuntimeControlPlaneData(data, userId) {
|
||||
accessRequests: [],
|
||||
revokedAccounts: [],
|
||||
taskerInviteRequests: [],
|
||||
engineWorkflowAccessRequests: [],
|
||||
grants: [],
|
||||
exceptions: [],
|
||||
serviceModuleEntitlements: [],
|
||||
@@ -3425,6 +3580,7 @@ function scopeRuntimeControlPlaneData(data, userId) {
|
||||
accessRequests: [],
|
||||
revokedAccounts: [],
|
||||
taskerInviteRequests: [],
|
||||
engineWorkflowAccessRequests: [],
|
||||
grants: data.grants.filter((grant) => {
|
||||
if (grant.targetType === "client") return clientIds.has(grant.targetId);
|
||||
if (grant.targetType === "group") return groupIds.has(grant.targetId);
|
||||
|
||||
Reference in New Issue
Block a user