<% ' Provisioning for the memory API (tenants, principals, API keys, namespaces, grants). ' Run through scripts\provision.cmd, which sets PROV_ACTION and PROV_ARG1..PROV_ARG4: ' ' tenant -> creates a tenant, prints its id ' principal ' -> creates a principal and one API key (key shown ONCE) ' key -> adds another API key for a principal (shown ONCE) ' namespace -> creates the namespace and any missing parents ' grant [inherit 1|0] ' -> perms: comma list of read,write,delete,admin ' revoke -> disables an API key ' list -> prints principals, namespaces and grants ' purge -> DELETES the tenant and everything it owns (tests, cleanup) ' ' The framework folder must be the web root: -e SERVER_WEB_ROOT=/opt/axonasp/site %> <% Dim sh : Set sh = CreateObject("WScript.Shell") Function Arg(name) Dim v v = sh.ExpandEnvironmentStrings("%" & name & "%") If v = "%" & name & "%" Then v = "" Arg = Trim(v) End Function Sub Fail(msg) Response.Write "ERROR: " & msg & vbCrLf End Sub Function TenantExists(id) Dim rs TenantExists = False If Not Api_IsId(id) Then Exit Function Set rs = DAL().Query("SELECT 1 FROM tenants WHERE id = ?", id) TenantExists = Not rs.EOF rs.Close End Function ' Creates the namespace at `path` (and missing parents) in the tenant; returns the leaf id. Function EnsureNamespace(tenantId, path) Dim parts, i, cur, parentId, rs, id, name parts = Split(Mid(path, 2), "/") cur = "" parentId = Null For i = 0 To UBound(parts) name = parts(i) cur = cur & "/" & name Set rs = DAL().Query("SELECT id FROM namespaces WHERE tenant_id = ? AND path = ?", Array(tenantId, cur)) If rs.EOF Then rs.Close id = Api_NewId() DAL().Execute "INSERT INTO namespaces (id, tenant_id, parent_id, name, path, created_at) VALUES (?, ?, ?, ?, ?, ?)", _ Array(id, tenantId, parentId, name, cur, Api_Now()) Else id = "" & rs(0) rs.Close End If parentId = id Next EnsureNamespace = id End Function Function ValidPath(p) Dim i, c ValidPath = False If Len(p) < 2 Or Left(p, 1) <> "/" Or Right(p, 1) = "/" Or InStr(p, "//") > 0 Then Exit Function For i = 1 To Len(p) c = Mid(p, i, 1) If Not ((c >= "a" And c <= "z") Or (c >= "0" And c <= "9") Or c = "/" Or c = "-" Or c = "_" Or c = ".") Then Exit Function Next ValidPath = True End Function Function NewKey(principalId) Dim c, plain, id Set c = Server.CreateObject("G3CRYPTO") plain = "amk_" & c.RandomHex(32) id = Api_NewId() DAL().Execute "INSERT INTO api_keys (id, principal_id, key_hash, key_prefix, status, created_at) VALUES (?, ?, ?, ?, 'active', ?)", _ Array(id, principalId, Api_Sha256(plain), Left(plain, 8), Api_Now()) Response.Write "api_key_id: " & id & vbCrLf Response.Write "api_key: " & plain & " (shown once, store it now)" & vbCrLf NewKey = id End Function Dim action : action = LCase(Arg("PROV_ACTION")) Dim a1 : a1 = Arg("PROV_ARG1") Dim a2 : a2 = Arg("PROV_ARG2") Dim a3 : a3 = Arg("PROV_ARG3") Dim a4 : a4 = Arg("PROV_ARG4") Dim id, rs, nsId, found Select Case action Case "tenant" If Len(a1) = 0 Then Fail "usage: tenant " Else id = Api_NewId() DAL().Execute "INSERT INTO tenants (id, name, status, created_at) VALUES (?, ?, 'active', ?)", Array(id, a1, Api_Now()) Response.Write "tenant_id: " & id & vbCrLf End If Case "principal" If Not TenantExists(a1) Or Len(a2) = 0 Then Fail "usage: principal (tenant must exist)" ElseIf a3 <> "human" And a3 <> "agent" And a3 <> "service" Then Fail "type must be human, agent or service" Else id = Api_NewId() DAL().Execute "INSERT INTO principals (id, tenant_id, type, name, status, created_at) VALUES (?, ?, ?, ?, 'active', ?)", _ Array(id, a1, a3, a2, Api_Now()) Response.Write "principal_id: " & id & vbCrLf nsId = NewKey(id) End If Case "key" Set rs = DAL().Query("SELECT 1 FROM principals WHERE id = ? AND status = 'active'", a1) found = Not rs.EOF rs.Close ' close before writing: a write while a result set is open is lost under G3DB/SQLite If found Then nsId = NewKey(a1) Else Fail "usage: key (active principal must exist)" End If Case "namespace" If Not TenantExists(a1) Or Not ValidPath(a2) Then Fail "usage: namespace (a-z 0-9 - _ . only)" Else Response.Write "namespace_id: " & EnsureNamespace(a1, a2) & vbCrLf End If Case "grant" Dim inh : inh = 1 If a4 = "0" Then inh = 0 Set rs = DAL().Query("SELECT tenant_id FROM principals WHERE id = ?", a1) If rs.EOF Or Not ValidPath(a2) Or Len(a3) = 0 Then Fail "usage: grant [inherit 1|0]" rs.Close Else Dim tid : tid = "" & rs(0) rs.Close nsId = EnsureNamespace(tid, a2) id = Api_NewId() DAL().Execute "INSERT INTO access_grants (id, principal_id, namespace_id, permissions, inherit, created_at) VALUES (?, ?, ?, ?, ?, ?)", _ Array(id, a1, nsId, LCase(Replace(a3, " ", "")), inh, Api_Now()) Response.Write "grant_id: " & id & " namespace_id: " & nsId & vbCrLf End If Case "revoke" DAL().Execute "UPDATE api_keys SET status = 'revoked' WHERE id = ?", a1 Response.Write "revoked (if the key existed): " & a1 & vbCrLf Case "list" Set rs = DAL().Query("SELECT id, type, name, status FROM principals WHERE tenant_id = ? ORDER BY created_at", a1) Response.Write "principals:" & vbCrLf Do While Not rs.EOF Response.Write " " & rs(0) & " " & rs(1) & " " & rs(2) & " " & rs(3) & vbCrLf rs.MoveNext Loop rs.Close Set rs = DAL().Query("SELECT id, path FROM namespaces WHERE tenant_id = ? ORDER BY path", a1) Response.Write "namespaces:" & vbCrLf Do While Not rs.EOF Response.Write " " & rs(0) & " " & rs(1) & vbCrLf rs.MoveNext Loop rs.Close Set rs = DAL().Query("SELECT g.principal_id, n.path, g.permissions, g.inherit FROM access_grants g JOIN namespaces n ON n.id = g.namespace_id WHERE n.tenant_id = ?", a1) Response.Write "grants:" & vbCrLf Do While Not rs.EOF Response.Write " " & rs(0) & " " & rs(1) & " " & rs(2) & " inherit=" & rs(3) & vbCrLf rs.MoveNext Loop rs.Close Case "purge" If Not TenantExists(a1) Then Fail "usage: purge (tenant must exist)" Else ' vector points and their bookkeeping first (the store is outside the transaction; best effort) Emb_DeleteTenant a1 DAL().BeginTransaction DAL().Execute "DELETE FROM chunks_fts WHERE tenant_id = ?", a1 DAL().Execute "DELETE FROM memories_fts WHERE tenant_id = ?", a1 DAL().Execute "DELETE FROM verifications WHERE memory_id IN (SELECT id FROM memories WHERE tenant_id = ?)", a1 DAL().Execute "DELETE FROM jobs WHERE tenant_id = ?", a1 DAL().Execute "DELETE FROM chunks WHERE document_id IN (SELECT id FROM documents WHERE tenant_id = ?)", a1 DAL().Execute "DELETE FROM document_revisions WHERE document_id IN (SELECT id FROM documents WHERE tenant_id = ?)", a1 DAL().Execute "DELETE FROM documents WHERE tenant_id = ?", a1 DAL().Execute "DELETE FROM checkpoints WHERE session_id IN (SELECT id FROM sessions WHERE tenant_id = ?)", a1 DAL().Execute "DELETE FROM sessions WHERE tenant_id = ?", a1 DAL().Execute "DELETE FROM memory_revisions WHERE memory_id IN (SELECT id FROM memories WHERE tenant_id = ?)", a1 DAL().Execute "DELETE FROM memories WHERE tenant_id = ?", a1 DAL().Execute "DELETE FROM idempotency_keys WHERE tenant_id = ?", a1 DAL().Execute "DELETE FROM audit_events WHERE tenant_id = ?", a1 DAL().Execute "DELETE FROM access_grants WHERE principal_id IN (SELECT id FROM principals WHERE tenant_id = ?)", a1 DAL().Execute "DELETE FROM api_keys WHERE principal_id IN (SELECT id FROM principals WHERE tenant_id = ?)", a1 DAL().Execute "DELETE FROM namespaces WHERE tenant_id = ?", a1 DAL().Execute "DELETE FROM principals WHERE tenant_id = ?", a1 DAL().Execute "DELETE FROM tenants WHERE id = ?", a1 DAL().CommitTransaction Response.Write "purged tenant " & a1 & vbCrLf End If Case Else Response.Write "Unknown action '" & action & "'. Use tenant, principal, key, namespace, grant, revoke, list or purge." & vbCrLf End Select %>