export type ResourceType = "knowledgebase" | "memorystore"; export interface MemoryStoreScope { appId?: string; tenantId?: string; agentId?: string; runId?: string; } export interface ParsedArn { region: string; accountId: string; instanceName: string; resourceType: ResourceType; resourceName: string; scope?: MemoryStoreScope; } export function buildPolicyName(credentialId: string): string { return `ts-apikey-${credentialId.slice(-12)}`; } export function buildKbArn( region: string, accountId: string, instanceName: string, kbName: string, ): string { const name = kbName === "*" ? "*" : `${kbName}*`; return `acs:ots:${region}:${accountId}:instance/${instanceName}/knowledgebase/${name}`; } export function buildMemoryStoreArn( region: string, accountId: string, instanceName: string, storeName: string, scope?: MemoryStoreScope, ): string { let path = `acs:ots:${region}:${accountId}:instance/${instanceName}/memorystore/${storeName}`; if (scope) { if (scope.appId) path += `/appid/${scope.appId}`; if (scope.tenantId) path += `/tenantid/${scope.tenantId}`; if (scope.agentId) path += `/agentid/${scope.agentId}`; if (scope.runId) path += `/runid/${scope.runId}`; } if (storeName !== "*") path += "*"; return path; } const ARN_PATTERN = /^acs:ots:([^:]*):([^:]*):instance\/([^/]+)\/(knowledgebase|memorystore)\/(.+)$/; export function parseArn(arn: string): ParsedArn | null { const m = ARN_PATTERN.exec(arn); if (!m) return null; const [, region, accountId, instanceName, resourceType, rest] = m; const parsed: ParsedArn = { region, accountId, instanceName, resourceType: resourceType as ResourceType, resourceName: "", }; if (resourceType === "knowledgebase") { parsed.resourceName = rest.replace(/\*$/, ""); if (!parsed.resourceName) parsed.resourceName = "*"; } else { const parts = rest.split("/"); parsed.resourceName = (parts[0] ?? "").replace(/\*$/, "") || "*"; const scope: MemoryStoreScope = {}; for (let i = 1; i + 1 < parts.length; i += 2) { const key = parts[i]; const val = parts[i + 1].replace(/\*$/, ""); if (key === "appid") scope.appId = val; else if (key === "tenantid") scope.tenantId = val; else if (key === "agentid") scope.agentId = val; else if (key === "runid") scope.runId = val; } if (scope.appId || scope.tenantId || scope.agentId || scope.runId) { parsed.scope = scope; } } return parsed; } // --------------------------------------------------------------------------- // OTS Action definitions grouped by category // --------------------------------------------------------------------------- export const KB_READ_ACTIONS = [ "ots:DescribeKnowledgeBase", "ots:ListKnowledgeBase", "ots:GetDocument", "ots:ListDocuments", "ots:ListChunks", "ots:Retrieve", ] as const; export const KB_WRITE_ACTIONS = [ "ots:AddDocuments", "ots:UpdateDocument", "ots:UpdateChunks", "ots:DeleteDocuments", "ots:UploadDocuments", ] as const; export const KB_MANAGE_ACTIONS = [ "ots:CreateKnowledgeBase", "ots:DeleteKnowledgeBase", "ots:UpdateKnowledgeBase", ] as const; export const MEM_READ_ACTIONS = [ "ots:GetMemoryStore", "ots:ListMemoryStores", "ots:SearchMemories", "ots:ListMemories", "ots:GetMemory", "ots:ListMemoryStoreMessages", "ots:ListMemoryStoreRequests", "ots:ListMemoryStoreScopes", "ots:GetMemoryTask", "ots:ListMemoryTasks", "ots:GetMemoryDreamTask", "ots:ListMemoryDreamTasks", "ots:ListMemoryDreamActions", ] as const; export const MEM_WRITE_ACTIONS = [ "ots:AddMemories", "ots:UpdateMemory", "ots:DeleteMemory", "ots:ApplyMemoryDreamActions", "ots:CreateMemoryDreamTask", "ots:CancelMemoryDreamTask", ] as const; export const MEM_MANAGE_ACTIONS = [ "ots:CreateMemoryStore", "ots:DeleteMemoryStore", "ots:UpdateMemoryStore", ] as const; export type OperationGroup = "read" | "write" | "manage"; export function getActionsForGroup( resourceType: ResourceType, group: OperationGroup, ): readonly string[] { if (resourceType === "knowledgebase") { if (group === "read") return KB_READ_ACTIONS; if (group === "write") return KB_WRITE_ACTIONS; return KB_MANAGE_ACTIONS; } if (group === "read") return MEM_READ_ACTIONS; if (group === "write") return MEM_WRITE_ACTIONS; return MEM_MANAGE_ACTIONS; } export function expandActions( resourceType: ResourceType, groups: OperationGroup[], ): string[] { const actions: string[] = []; for (const group of groups) { actions.push(...getActionsForGroup(resourceType, group)); } return [...new Set(actions)]; } const ALL_KB_ACTIONS: Set = new Set([ ...KB_READ_ACTIONS, ...KB_WRITE_ACTIONS, ...KB_MANAGE_ACTIONS, ]); const ALL_MEM_ACTIONS: Set = new Set([ ...MEM_READ_ACTIONS, ...MEM_WRITE_ACTIONS, ...MEM_MANAGE_ACTIONS, ]); export function detectOperationGroups( resourceType: ResourceType, actions: string[], ): OperationGroup[] { const groups: OperationGroup[] = []; const actionSet = new Set(actions); const defs: { group: OperationGroup; required: readonly string[] }[] = resourceType === "knowledgebase" ? [ { group: "read", required: KB_READ_ACTIONS }, { group: "write", required: KB_WRITE_ACTIONS }, { group: "manage", required: KB_MANAGE_ACTIONS }, ] : [ { group: "read", required: MEM_READ_ACTIONS }, { group: "write", required: MEM_WRITE_ACTIONS }, { group: "manage", required: MEM_MANAGE_ACTIONS }, ]; for (const def of defs) { if (def.required.every((a) => actionSet.has(a))) { groups.push(def.group); } } return groups; } export function isOtsAction(action: string): boolean { return ALL_KB_ACTIONS.has(action) || ALL_MEM_ACTIONS.has(action); } export function arnMatchesResource( arnPattern: string, region: string, accountId: string, instanceName: string, resourceType: ResourceType, resourceName: string, ): boolean { const targetArn = `acs:ots:${region}:${accountId}:instance/${instanceName}/${resourceType}/${resourceName}`; if (arnPattern === "*") return true; const cleanPattern = arnPattern.replace(/\*+$/, ""); if (cleanPattern === arnPattern) { return arnPattern === targetArn; } const patternParts = cleanPattern.split(":"); const targetParts = targetArn.split(":"); if (patternParts.length < 4) return targetArn.startsWith(cleanPattern); for (let i = 0; i < Math.min(4, patternParts.length); i++) { if (patternParts[i] === "") continue; if (patternParts[i] !== targetParts[i]) return false; } if (patternParts.length >= 5) { const patternResource = patternParts.slice(4).join(":"); const targetResource = targetParts.slice(4).join(":"); return targetResource.startsWith(patternResource); } return true; } export function inferResourceTypeFromActions( actions: string[], ): ResourceType | null { const hasKb = actions.some((a) => ALL_KB_ACTIONS.has(a)); const hasMem = actions.some((a) => ALL_MEM_ACTIONS.has(a)); if (hasKb && !hasMem) return "knowledgebase"; if (hasMem && !hasKb) return "memorystore"; return null; }