Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/desktop/src/lib/trpc/routers/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from "node:crypto";
import fs from "node:fs/promises";
import { AUTH_PROVIDERS } from "@superset/shared/constants";
import { getDeviceName, getHashedDeviceId } from "@superset/shared/device-info";
import { getHostId, getHostName } from "@superset/shared/host-info";
import { observable } from "@trpc/server/observable";
import { shell } from "electron";
import { env } from "main/env.main";
Expand All @@ -23,8 +23,8 @@ export const createAuthRouter = () => {
getStoredToken: publicProcedure.query(() => loadToken()),

getDeviceInfo: publicProcedure.query(() => ({
deviceId: getHashedDeviceId(),
deviceName: getDeviceName(),
deviceId: getHostId(),
deviceName: getHostName(),
})),

persistToken: publicProcedure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
randomBytes,
scryptSync,
} from "node:crypto";
import { getMachineId } from "@superset/shared/device-info";
import { getMachineId } from "@superset/shared/host-info";

const ALGORITHM = "aes-256-gcm";
const KEY_LENGTH = 32;
Expand Down
13 changes: 8 additions & 5 deletions apps/desktop/src/main/lib/host-service-coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EventEmitter } from "node:events";
import * as fs from "node:fs";
import path from "node:path";
import { settings } from "@superset/local-db";
import { getDeviceName, getHashedDeviceId } from "@superset/shared/device-info";
import { getHostId, getHostName } from "@superset/shared/host-info";
import { app } from "electron";
import { env } from "main/env.main";
import semver from "semver";
Expand Down Expand Up @@ -35,9 +35,12 @@ import { HOOK_PROTOCOL_VERSION } from "./terminal/env";
* which is how we prevent the renderer from talking to a stale host-service
* that's missing newly-added procedures/params.
*
* 0.3.0: host-service registers via cloud `host.ensure` (was
* `device.ensureV2Host`); v2_hosts/v2_users_hosts/v2_workspaces use
* machineId text instead of uuid surrogates.
* 0.2.0: `workspaceCreation.adopt` gained optional `worktreePath`.
*/
const MIN_HOST_SERVICE_VERSION = "0.2.0";
const MIN_HOST_SERVICE_VERSION = "0.3.0";

export type HostServiceStatus = "starting" | "running" | "stopped";

Expand Down Expand Up @@ -75,7 +78,7 @@ export class HostServiceCoordinator extends EventEmitter {
ReturnType<typeof setInterval>
>();
private scriptPath = path.join(__dirname, "host-service.js");
private machineId = getHashedDeviceId();
private machineId = getHostId();
private devReloadWatcher: fs.FSWatcher | null = null;

async start(
Expand Down Expand Up @@ -461,8 +464,8 @@ export class HostServiceCoordinator extends EventEmitter {
...(process.env as Record<string, string>),
ELECTRON_RUN_AS_NODE: "1",
ORGANIZATION_ID: organizationId,
DEVICE_CLIENT_ID: getHashedDeviceId(),
DEVICE_NAME: getDeviceName(),
HOST_CLIENT_ID: getHostId(),
HOST_NAME: getHostName(),
HOST_SERVICE_SECRET: secret,
HOST_SERVICE_PORT: String(port),
HOST_MANIFEST_DIR: organizationDir,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useHostTargetUrl } from "./useHostTargetUrl";
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { buildHostRoutingKey } from "@superset/shared/host-routing";
import { useMemo } from "react";
import { env } from "renderer/env.renderer";
import { authClient } from "renderer/lib/auth-client";
import type { WorkspaceHostTarget } from "renderer/routes/_authenticated/components/DashboardNewWorkspaceModal/components/DashboardNewWorkspaceForm/components/DevicePicker/types";
import { useLocalHostService } from "renderer/routes/_authenticated/providers/LocalHostServiceProvider";

export function useHostTargetUrl(
hostTarget: WorkspaceHostTarget | null | undefined,
): string | null {
const { activeHostUrl } = useLocalHostService();
const { data: session } = authClient.useSession();
const activeOrganizationId = session?.session?.activeOrganizationId ?? null;

return useMemo(() => {
if (!hostTarget) return null;
if (hostTarget.kind === "local") return activeHostUrl;
if (!activeOrganizationId) return null;
const routingKey = buildHostRoutingKey(
activeOrganizationId,
hostTarget.hostId,
);
return `${env.RELAY_URL}/hosts/${routingKey}`;
}, [hostTarget, activeOrganizationId, activeHostUrl]);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { buildHostRoutingKey } from "@superset/shared/host-routing";
import { eq } from "@tanstack/db";
import { useLiveQuery } from "@tanstack/react-db";
import { useMemo } from "react";
Expand All @@ -18,10 +19,11 @@ export function useWorkspaceHostUrl(workspaceId: string | null): string | null {
q
.from({ workspaces: collections.v2Workspaces })
.leftJoin({ hosts: collections.v2Hosts }, ({ workspaces, hosts }) =>
eq(workspaces.hostId, hosts.id),
eq(workspaces.hostId, hosts.machineId),
)
.where(({ workspaces }) => eq(workspaces.id, workspaceId ?? ""))
.select(({ workspaces, hosts }) => ({
organizationId: workspaces.organizationId,
hostId: workspaces.hostId,
hostMachineId: hosts?.machineId ?? null,
})),
Expand All @@ -33,6 +35,7 @@ export function useWorkspaceHostUrl(workspaceId: string | null): string | null {
return useMemo(() => {
if (!match) return null;
if (match.hostMachineId === machineId) return activeHostUrl;
return `${env.RELAY_URL}/hosts/${match.hostId}`;
const routingKey = buildHostRoutingKey(match.organizationId, match.hostId);
return `${env.RELAY_URL}/hosts/${routingKey}`;
}, [match, machineId, activeHostUrl]);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useCallback } from "react";
import type { FileMentionSearchFn } from "renderer/components/MarkdownEditor/components/FileMention";
import { env } from "renderer/env.renderer";
import { useHostTargetUrl } from "renderer/hooks/host-service/useHostTargetUrl";
import { getHostServiceClientByUrl } from "renderer/lib/host-service-client";
import type { WorkspaceHostTarget } from "renderer/routes/_authenticated/components/DashboardNewWorkspaceModal/components/DashboardNewWorkspaceForm/components/DevicePicker/types";
import { useLocalHostService } from "renderer/routes/_authenticated/providers/LocalHostServiceProvider";

const SEARCH_LIMIT = 15;

Expand All @@ -14,12 +13,7 @@ export function useProjectFileSearch({
hostTarget: WorkspaceHostTarget;
projectId: string | null;
}): FileMentionSearchFn | undefined {
const { activeHostUrl } = useLocalHostService();

const hostUrl =
hostTarget.kind === "local"
? activeHostUrl
: `${env.RELAY_URL}/hosts/${hostTarget.hostId}`;
const hostUrl = useHostTargetUrl(hostTarget);

return useCallback<FileMentionSearchFn>(
async (query) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function AutomationsPage() {
(q) =>
q
.from({ h: collections.v2Hosts })
.select(({ h }) => ({ id: h.id, name: h.name })),
.select(({ h }) => ({ machineId: h.machineId, name: h.name })),
[collections.v2Hosts],
);

Expand Down Expand Up @@ -167,7 +167,10 @@ function AutomationsPage() {
const hostsById = useMemo(
() =>
new Map(
(hostRows as Pick<SelectV2Host, "id" | "name">[]).map((h) => [h.id, h]),
(hostRows as Pick<SelectV2Host, "machineId" | "name">[]).map((h) => [
h.machineId,
h,
]),
),
[hostRows],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,42 +154,50 @@ describe("deriveHostPortQueryTargets", () => {
const targets = deriveHostPortQueryTargets({
activeHostUrl: "http://127.0.0.1:4567",
hosts: [
{ id: "remote-host", isOnline: true, machineId: "remote-machine" },
{ id: "local-host", isOnline: true, machineId: "local-machine" },
{
organizationId: "org-1",
machineId: "remote-machine",
isOnline: true,
},
{
organizationId: "org-1",
machineId: "local-machine",
isOnline: true,
},
],
machineId: "local-machine",
relayUrl: "https://relay.example.com",
workspaces: [
{
id: "workspace-b",
name: "Workspace B",
hostId: "local-host",
hostId: "local-machine",
hostMachineId: "local-machine",
},
{
id: "workspace-a",
name: "Workspace A",
hostId: "local-host",
hostId: "local-machine",
hostMachineId: "local-machine",
},
{
id: "workspace-c",
name: "Workspace C",
hostId: "remote-host",
hostId: "remote-machine",
hostMachineId: "remote-machine",
},
],
});

expect(targets).toEqual([
{
id: "remote-host",
machineId: "remote-machine",
hostType: "remote-device",
hostUrl: "https://relay.example.com/hosts/remote-host",
hostUrl: "https://relay.example.com/hosts/org-1:remote-machine",
workspaceIds: ["workspace-c"],
},
{
id: "local-host",
machineId: "local-machine",
hostType: "local-device",
hostUrl: "http://127.0.0.1:4567",
workspaceIds: ["workspace-a", "workspace-b"],
Expand All @@ -201,22 +209,30 @@ describe("deriveHostPortQueryTargets", () => {
const targets = deriveHostPortQueryTargets({
activeHostUrl: null,
hosts: [
{ id: "offline-host", isOnline: false, machineId: "remote-machine" },
{ id: "local-host", isOnline: true, machineId: "local-machine" },
{
organizationId: "org-1",
machineId: "remote-machine",
isOnline: false,
},
{
organizationId: "org-1",
machineId: "local-machine",
isOnline: true,
},
],
machineId: "local-machine",
relayUrl: "https://relay.example.com",
workspaces: [
{
id: "workspace-remote",
name: "Remote",
hostId: "offline-host",
hostId: "remote-machine",
hostMachineId: "remote-machine",
},
{
id: "workspace-local",
name: "Local",
hostId: "local-host",
hostId: "local-machine",
hostMachineId: "local-machine",
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export function useDashboardSidebarPortsData(): {
const { data: hosts = [] } = useLiveQuery(
(q) =>
q.from({ hosts: collections.v2Hosts }).select(({ hosts }) => ({
id: hosts.id,
isOnline: hosts.isOnline,
organizationId: hosts.organizationId,
machineId: hosts.machineId,
isOnline: hosts.isOnline,
})),
[collections],
);
Expand All @@ -53,7 +53,7 @@ export function useDashboardSidebarPortsData(): {
q
.from({ workspaces: collections.v2Workspaces })
.leftJoin({ hosts: collections.v2Hosts }, ({ workspaces, hosts }) =>
eq(workspaces.hostId, hosts.id),
eq(workspaces.hostId, hosts.machineId),
)
.select(({ workspaces, hosts }) => ({
id: workspaces.id,
Expand Down Expand Up @@ -86,7 +86,7 @@ export function useDashboardSidebarPortsData(): {
workspaceIds: host.workspaceIds,
});
return {
hostId: host.id,
hostId: host.machineId,
hostType: host.hostType,
hostUrl: host.hostUrl,
ports,
Expand All @@ -110,7 +110,7 @@ export function useDashboardSidebarPortsData(): {
getHostPortsQueryKey(host),
(result) =>
applyPortEventsToHostPortsResult(result, events, {
hostId: host.id,
hostId: host.machineId,
hostType: host.hostType,
hostUrl: host.hostUrl,
}),
Expand Down Expand Up @@ -175,7 +175,7 @@ export function useDashboardSidebarPortsData(): {
if (!host) return [];
return [
{
hostId: host.id,
hostId: host.machineId,
hostType: host.hostType,
message:
query.error instanceof Error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { buildHostRoutingKey } from "@superset/shared/host-routing";
import type { PortChangedPayload } from "@superset/workspace-client";
import type { DetectedPort } from "shared/types";
import type { DashboardSidebarWorkspaceHostType } from "../../../../types";
Expand Down Expand Up @@ -38,16 +39,16 @@ type HostPortsMetadata = Pick<
>;

export interface HostPortsQueryTarget {
id: string;
machineId: string;
hostType: DashboardSidebarWorkspaceHostType;
hostUrl: string;
workspaceIds: string[];
}

export interface DashboardSidebarHostRow {
id: string;
organizationId: string;
machineId: string;
isOnline: boolean;
machineId: string | null | undefined;
}

export interface DashboardSidebarWorkspaceRow {
Expand All @@ -62,7 +63,7 @@ export function getHostPortsQueryKey(host: HostPortsQueryTarget) {
"host-service",
"ports",
"getAll",
host.id,
host.machineId,
host.hostUrl,
host.workspaceIds,
] as const;
Expand Down Expand Up @@ -139,18 +140,20 @@ export function deriveHostPortQueryTargets({
}

return hosts.flatMap((host) => {
const workspaceIds = workspaceIdsByHostId.get(host.id);
const workspaceIds = workspaceIdsByHostId.get(host.machineId);
if (!workspaceIds || workspaceIds.length === 0) return [];

const isLocal = host.machineId === machineId;
if (!isLocal && !host.isOnline) return [];

const hostUrl = isLocal ? activeHostUrl : `${relayUrl}/hosts/${host.id}`;
const hostUrl = isLocal
? activeHostUrl
: `${relayUrl}/hosts/${buildHostRoutingKey(host.organizationId, host.machineId)}`;
if (!hostUrl) return [];

return [
{
id: host.id,
machineId: host.machineId,
hostType: isLocal
? ("local-device" as const)
: ("remote-device" as const),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export function useDashboardSidebarData() {
eq(sidebarWorkspaces.workspaceId, workspaces.id),
)
.leftJoin({ hosts: collections.v2Hosts }, ({ workspaces, hosts }) =>
eq(workspaces.hostId, hosts.id),
eq(workspaces.hostId, hosts.machineId),
)
.orderBy(
({ sidebarWorkspaces }) => sidebarWorkspaces.sidebarState.tabOrder,
Expand Down Expand Up @@ -272,7 +272,7 @@ export function useDashboardSidebarData() {
q
.from({ workspaces: collections.v2Workspaces })
.leftJoin({ hosts: collections.v2Hosts }, ({ workspaces, hosts }) =>
eq(workspaces.hostId, hosts.id),
eq(workspaces.hostId, hosts.machineId),
)
.where(({ workspaces }) => eq(workspaces.type, "main"))
.select(({ workspaces, hosts }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function V2WorkspaceOpenInButton({
q
.from({ workspaces: collections.v2Workspaces })
.leftJoin({ hosts: collections.v2Hosts }, ({ workspaces, hosts }) =>
eq(workspaces.hostId, hosts.id),
eq(workspaces.hostId, hosts.machineId),
)
.where(({ workspaces }) => eq(workspaces.id, workspaceId))
.select(({ workspaces, hosts }) => ({
Expand Down
Loading
Loading