Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

9.9 KB
Raw Blame
import { Code, ConnectError, createContextValues } from "@connectrpc/connect";
import { skipErrorNotificationContextKey } from "@/api/context-key";

// The per-resource failures the all-or-nothing BatchGet contract introduces.
// Only these are worth a per-name retry; anything else is a real error.
export const isMissingOrForbidden = (error: unknown) =>
  error instanceof ConnectError &&
  (error.code === Code.NotFound || error.code === Code.PermissionDenied);

import type { DatabaseFilter } from "@/lib/databaseFilter";
import {
  getProjectName,
  getUserName,
  isValidProjectName,
  projectNamePrefix,
  userNamePrefix,
} from "@/lib/resourceName";
import { Engine } from "@/types/proto-es/v1/common_pb";
import type { Binding } from "@/types/proto-es/v1/iam_policy_pb";
import type { Project } from "@/types/proto-es/v1/project_service_pb";
import type { User } from "@/types/proto-es/v1/user_service_pb";
import {
  isValidEnvironmentName,
  unknownEnvironment,
} from "@/types/v1/environment";
import { isValidInstanceName } from "@/types/v1/instance";
import { workspaceCacheScope } from "@/utils/storage-keys";
import { celMapField, celString, celStringList } from "@/utils/v1/celLiteral";
import { bindingScopesResources } from "@/utils/v1/iam";
import type { AppStoreState, ErrorReport } from "./types";

export const MAX_RECENT_PROJECT = 5;
export const MAX_RECENT_VISIT = 10;

const ALL_USERS_NAME = `${userNamePrefix}allUsers`;

export function getCurrentUserEmail(get: () => AppStoreState): string {
  return get().currentUser?.email ?? "";
}

export function getWorkspaceResourceScope(
  get: () => AppStoreState,
  workspaceName?: string
): string {
  return (
    workspaceName ||
    get().currentUser?.workspace ||
    get().serverInfo?.workspace ||
    ""
  );
}

// Workspace segment for localStorage cache keys — "" for self-host (keys stay
// shared/unchanged), the workspace name for SaaS (keys are workspace-isolated).
// Fall back to serverInfo while the current user is still hydrating.
export function getWorkspaceCacheScope(
  get: () => AppStoreState,
  workspaceName?: string
): string {
  const scope = getWorkspaceResourceScope(get, workspaceName);
  return workspaceCacheScope(get().isSaaSMode(), scope);
}

export function readJson<T>(key: string, fallback: T): T {
  try {
    const raw = localStorage.getItem(key);
    if (!raw) return fallback;
    return JSON.parse(raw) as T;
  } catch {
    return fallback;
  }
}

export function writeJson<T>(key: string, value: T) {
  localStorage.setItem(key, JSON.stringify(value));
}

export function buildProjectFilter(query: string | undefined) {
  const filters = ["exclude_default == true"];
  const search = query?.trim().toLowerCase();
  if (search) {
    const value = celString(search);
    filters.push(`(name.contains(${value}) || resource_id.contains(${value}))`);
  }
  return filters.join(" && ");
}

// Converts label selectors like "{key}:{v1},{v2}" into API filter clauses
// (`labels["{key}"] == "v"` or `labels["{key}"] in [...]`). Index syntax, not
// `labels.{key}` — label keys allow dashes, which CEL parses as subtraction.
export function getLabelFilter(labels: string[]): string[] {
  const labelMap = new Map<string, string[]>();
  for (const label of labels) {
    const sections = label.split(":");
    if (sections.length !== 2) {
      continue;
    }
    const [key, rawValue] = sections;
    const values = rawValue.split(",");
    if (!labelMap.has(key)) {
      labelMap.set(key, []);
    }
    labelMap.get(key)?.push(...values);
  }
  return [...labelMap.entries()].reduce((result, [key, values]) => {
    const field = celMapField("labels", key);
    switch (values.length) {
      case 0:
        return result;
      case 1:
        result.push(`${field} == ${celString(values[0])}`);
        return result;
      default:
        result.push(`${field} in ${celStringList(values)}`);
        return result;
    }
  }, [] as string[]);
}

// Builds the CEL filter string for `listDatabases` from a structured
// `DatabaseFilter`.
export function buildDatabaseFilter(filter: DatabaseFilter): string {
  const params: string[] = [];
  if (isValidProjectName(filter.project)) {
    params.push(`project == ${celString(filter.project)}`);
  }
  if (isValidInstanceName(filter.instance)) {
    params.push(`instance == ${celString(filter.instance)}`);
  }
  if (filter.environment === unknownEnvironment().name) {
    params.push(`environment == ""`);
  } else if (isValidEnvironmentName(filter.environment)) {
    params.push(`environment == ${celString(filter.environment)}`);
  }
  if (filter.excludeUnassigned) {
    params.push(`exclude_unassigned == true`);
  }
  if (filter.engines && filter.engines.length > 0) {
    params.push(
      `engine in ${celStringList(filter.engines.map((e) => Engine[e]))}`
    );
  } else if (filter.excludeEngines && filter.excludeEngines.length > 0) {
    params.push(
      `!(engine in ${celStringList(filter.excludeEngines.map((e) => Engine[e]))})`
    );
  }
  const keyword = filter.query?.trim()?.toLowerCase();
  if (keyword) {
    params.push(`name.contains(${celString(keyword)})`);
  }
  if (filter.labels) {
    params.push(...getLabelFilter(filter.labels));
  }
  if (filter.table) {
    params.push(`table.contains(${celString(filter.table)})`);
  }
  return params.join(" && ");
}

function bindingMemberToNames(member: string): string[] {
  if (member === "allUsers" || member === "user:allUsers") {
    return [ALL_USERS_NAME];
  }
  if (member.startsWith("user:")) {
    return [getUserName(member.slice("user:".length))];
  }
  if (member.startsWith("group:")) {
    return [`groups/${member.slice("group:".length)}`];
  }
  if (member.startsWith(userNamePrefix) || member.startsWith("groups/")) {
    return [member];
  }
  return [getUserName(member)];
}

export function getBindingExpirationDate(binding: Binding): Date | undefined {
  const expression = binding.condition?.expression;
  const match = expression?.match(/request\.time\s*<\s*timestamp\("([^"]+)"\)/);
  if (match) {
    return new Date(match[1]);
  }
  return undefined;
}

export function isBindingExpired(binding: Binding): boolean {
  const expiration = getBindingExpirationDate(binding);
  return Boolean(expiration && expiration < new Date());
}

export function bindingMatchesUser(
  policy: AppStoreState["workspacePolicy"],
  user: User
) {
  if (!policy || !user.name) {
    return [];
  }
  const userGroups = new Set(user.groups);
  return policy.bindings.filter((binding) => {
    if (isBindingExpired(binding)) {
      return false;
    }
    return binding.members.some((member) => {
      const names = bindingMemberToNames(member);
      return names.some(
        (name) =>
          name === user.name ||
          name === ALL_USERS_NAME ||
          (name.startsWith("groups/") && userGroups.has(name))
      );
    });
  });
}

// The canonical expiry condition is the only non-empty form the client
// evaluates, and the whole expression must be it — matching a substring would
// let a lower-bounded window ("not before") read as already granting.
const CANONICAL_EXPIRY_CONDITION =
  /^\s*request\.time\s*<\s*timestamp\(\s*"([^"]+)"\s*\)\s*$/;

// Whether the client can positively evaluate the binding's condition as
// granting right now. The server evaluates the full CEL expression; the
// client recognizes the empty condition and the canonical expiry form, and
// treats everything else as not granting — the affordance hides rather than
// dangling on an RPC the server will deny. An unparsable timestamp compares
// as NaN, which also reads as not granting.
function conditionGrantsNow(binding: Binding): boolean {
  const expression = binding.condition?.expression ?? "";
  if (expression.trim() === "") {
    return true;
  }
  const match = expression.match(CANONICAL_EXPIRY_CONDITION);
  if (!match) {
    return false;
  }
  return new Date(match[1]) > new Date();
}

// Mirrors the server's project-wide binding filter (iam.Manager's check with
// projectWideOnly): the member must match explicitly — allUsers is skipped
// for the workspace policy in SaaS mode, where workspace members must be
// explicit, while a project-level allUsers means "all workspace members" —
// a condition that scopes resources confers nothing, and a condition the
// client cannot positively evaluate hides the grant instead of showing an
// affordance the server denies.
export function projectWideBindings(
  policy: AppStoreState["workspacePolicy"],
  user: User,
  { skipAllUsers }: { skipAllUsers: boolean }
): Binding[] {
  if (!policy || !user.name) {
    return [];
  }
  const userGroups = new Set(user.groups);
  return policy.bindings.filter((binding) => {
    if (bindingScopesResources(binding)) {
      return false;
    }
    if (!conditionGrantsNow(binding)) {
      return false;
    }
    return binding.members.some((member) => {
      const names = bindingMemberToNames(member);
      return names.some(
        (name) =>
          name === user.name ||
          (name === ALL_USERS_NAME && !skipAllUsers) ||
          (name.startsWith("groups/") && userGroups.has(name))
      );
    });
  });
}

export function defaultProjectName(get: () => AppStoreState) {
  return get().serverInfo?.defaultProject ?? "";
}

// Call options for a request the caller reports the failure of itself.
export function callOptions(report: ErrorReport | undefined) {
  return report?.byCaller
    ? {
        contextValues: createContextValues().set(
          skipErrorNotificationContextKey,
          true
        ),
      }
    : undefined;
}

export function isConnectAlreadyExists(error: unknown): error is ConnectError {
  return error instanceof ConnectError && error.code === Code.AlreadyExists;
}

export function toError(error: unknown): Error {
  return error instanceof Error ? error : new Error(String(error));
}

export function projectResourceNameFromId(projectId: string | undefined) {
  return projectId ? `${projectNamePrefix}${projectId}` : "";
}

export function getProjectResourceId(project: Project) {
  return getProjectName(project.name);
}