feat(etcd): restrict key browsing and writes by auth permissions

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-04 20:24:02 +08:00
1 parent c9c36a8453
commit 259071c09b
32 files changed
+1404 -100

No files matched your search

@@ -29,7 +29,7 @@ describe("etcd v2 feature trimming", () => {
});
it("hides lease binding and the maintenance/lease workspaces for v2 connections", () => {
expect(browserSource).toContain(':supports-lease-binding="!isV2Api"');
expect(browserSource).toContain('<Button v-if="!isV2Api"');
expect(browserSource).toContain(':supports-lease-binding="!isV2Api && canManageEtcd"');
expect(browserSource).toContain('<Button v-if="!isV2Api && canManageEtcd"');
});
});
@@ -65,6 +65,18 @@ describe("treeNodeClick", () => {
expect(treeNodeRowAction("meilisearch-system", false, "double")).toBe("toggle");
});
it("opens etcd workspaces on one click regardless of object activation preference", () => {
for (const type of ["etcd-root", "etcd-access-control", "etcd-dashboard"] as const) {
expect(isDirectNavigationTreeNode(type), type).toBe(true);
expect(shouldActivateTreeNodeOnSingleClick(type, "double"), type).toBe(true);
const action = treeNodeRowAction(type, false, "double");
expect(action, type).toBe("toggle");
expect(isRepeatableNavigationTreeNode(type), type).toBe(true);
expect(shouldRunTreeNodeRowAction(action, 2, isRepeatableNavigationTreeNode(type)), type).toBe(true);
expect(shouldRunTreeNodeRowAction(action, 3, isRepeatableNavigationTreeNode(type)), type).toBe(true);
}
});
it("keeps Nacos namespace and access-control navigation responsive during rapid row switching", () => {
for (const type of ["nacos-namespace", "nacos-access-control"] as const) {
expect(isDirectNavigationTreeNode(type), type).toBe(true);
+1
View File
@@ -3140,6 +3140,7 @@ export interface EtcdAuthUserListResponse {
export interface EtcdAuthUserDetail {
user: string;
roles: string[];
authEnabled?: boolean;
}
export interface EtcdAuthPermission {
access: "read" | "write" | "readwrite";
@@ -0,0 +1,37 @@
import { describe, expect, it } from "vitest";
import type { EtcdAuthPermission, KvValue } from "@/lib/backend/api";
import { etcdPermissionAllowsKey, etcdPermissionsAllowKey } from "@/lib/etcd/keyPermissions";
const utf8 = (data: string): KvValue => ({ encoding: "utf8", data });
const base64 = (bytes: number[]): KvValue => ({ encoding: "base64", data: btoa(String.fromCharCode(...bytes)) });
function permission(access: EtcdAuthPermission["access"], key: KvValue, rangeEnd: KvValue, resource: EtcdAuthPermission["resource"]): EtcdAuthPermission {
return { access, key, rangeEnd, resource };
}
describe("etcd Key write permissions", () => {
it("matches exact Key grants only", () => {
const grant = permission("write", utf8("/app/key"), utf8(""), "key");
expect(etcdPermissionAllowsKey(grant, utf8("/app/key"))).toBe(true);
expect(etcdPermissionAllowsKey(grant, utf8("/app/key/child"))).toBe(false);
});
it("uses etcd's half-open byte ranges", () => {
const grant = permission("readwrite", utf8("/app/"), utf8("/app0"), "prefix");
expect(etcdPermissionAllowsKey(grant, utf8("/app/a"))).toBe(true);
expect(etcdPermissionAllowsKey(grant, utf8("/app0"))).toBe(false);
expect(etcdPermissionAllowsKey(grant, utf8("/other"))).toBe(false);
});
it("supports binary Keys and the unbounded range sentinel", () => {
const grant = permission("write", base64([0x80]), base64([0]), "prefix");
expect(etcdPermissionAllowsKey(grant, base64([0x80, 0x01]))).toBe(true);
expect(etcdPermissionAllowsKey(grant, base64([0x7f]))).toBe(false);
});
it("ignores read-only grants when permissions are combined", () => {
const grants = [permission("read", utf8("/readonly/"), utf8("/readonly0"), "prefix"), permission("readwrite", utf8("/editable/"), utf8("/editable0"), "prefix")];
expect(etcdPermissionsAllowKey(grants, utf8("/readonly/key"))).toBe(false);
expect(etcdPermissionsAllowKey(grants, utf8("/editable/key"))).toBe(true);
});
});
@@ -0,0 +1,45 @@
import type { EtcdAuthPermission, KvValue } from "@/lib/backend/api";
const unboundedRangeEnd = new Uint8Array([0]);
function decodeBase64(value: string): Uint8Array {
const decoded = globalThis.atob(value.replace(/\s+/g, ""));
return Uint8Array.from(decoded, (character) => character.charCodeAt(0));
}
export function etcdKvValueBytes(value: KvValue): Uint8Array {
return value.encoding === "base64" ? decodeBase64(value.data) : new TextEncoder().encode(value.data);
}
function compareBytes(left: Uint8Array, right: Uint8Array): number {
const length = Math.min(left.length, right.length);
for (let index = 0; index < length; index += 1) {
const difference = left[index]! - right[index]!;
if (difference !== 0) return difference;
}
return left.length - right.length;
}
function bytesEqual(left: Uint8Array, right: Uint8Array): boolean {
return compareBytes(left, right) === 0;
}
function isUnboundedRangeEnd(value: Uint8Array): boolean {
return bytesEqual(value, unboundedRangeEnd);
}
/** Tests one Key against etcd's byte-oriented, half-open permission range. */
export function etcdPermissionAllowsKey(permission: EtcdAuthPermission, key: KvValue): boolean {
if (permission.access === "read") return false;
if (permission.resource === "all") return true;
const keyBytes = etcdKvValueBytes(key);
const start = etcdKvValueBytes(permission.key);
const end = etcdKvValueBytes(permission.rangeEnd);
if (permission.resource === "key" || end.length === 0) return bytesEqual(keyBytes, start);
return compareBytes(keyBytes, start) >= 0 && (isUnboundedRangeEnd(end) || compareBytes(keyBytes, end) < 0);
}
export function etcdPermissionsAllowKey(permissions: readonly EtcdAuthPermission[], key: KvValue): boolean {
return permissions.some((permission) => etcdPermissionAllowsKey(permission, key));
}
@@ -35,8 +35,8 @@ const toggleLeafNodeTypes = new Set<TreeNodeType>([
// These are application entry points rather than database objects. They should
// always navigate on a single click, even when the user prefers double-click
// activation for ordinary tree objects.
const directNavigationTreeNodeTypes = new Set<TreeNodeType>(["consul-root", "consul-overview", "nacos-namespace", "nacos-access-control", "meilisearch-system"]);
const repeatableNavigationTreeNodeTypes = new Set<TreeNodeType>(["nacos-namespace", "nacos-access-control"]);
const directNavigationTreeNodeTypes = new Set<TreeNodeType>(["consul-root", "consul-overview", "etcd-root", "etcd-dashboard", "etcd-access-control", "nacos-namespace", "nacos-access-control", "meilisearch-system"]);
const repeatableNavigationTreeNodeTypes = new Set<TreeNodeType>(["etcd-root", "etcd-dashboard", "etcd-access-control", "nacos-namespace", "nacos-access-control"]);
export function isDirectNavigationTreeNode(type: TreeNodeType): boolean {
return directNavigationTreeNodeTypes.has(type);