您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

204 行
8.6KB

  1. <%
  2. ' Provisioning for the memory API (tenants, principals, API keys, namespaces, grants).
  3. ' Run through scripts\provision.cmd, which sets PROV_ACTION and PROV_ARG1..PROV_ARG4:
  4. '
  5. ' tenant <name> -> creates a tenant, prints its id
  6. ' principal <tenant_id> <name> <human|agent|service>
  7. ' -> creates a principal and one API key (key shown ONCE)
  8. ' key <principal_id> -> adds another API key for a principal (shown ONCE)
  9. ' namespace <tenant_id> </path/like/this> -> creates the namespace and any missing parents
  10. ' grant <principal_id> </path> <perms> [inherit 1|0]
  11. ' -> perms: comma list of read,write,delete,admin
  12. ' revoke <api_key_id> -> disables an API key
  13. ' list <tenant_id> -> prints principals, namespaces and grants
  14. ' purge <tenant_id> -> DELETES the tenant and everything it owns (tests, cleanup)
  15. '
  16. ' The framework folder must be the web root: -e SERVER_WEB_ROOT=/opt/axonasp/site
  17. %>
  18. <!--#include file="../core/autoload_core.asp" -->
  19. <%
  20. Dim sh : Set sh = CreateObject("WScript.Shell")
  21. Function Arg(name)
  22. Dim v
  23. v = sh.ExpandEnvironmentStrings("%" & name & "%")
  24. If v = "%" & name & "%" Then v = ""
  25. Arg = Trim(v)
  26. End Function
  27. Sub Fail(msg)
  28. Response.Write "ERROR: " & msg & vbCrLf
  29. End Sub
  30. Function TenantExists(id)
  31. Dim rs
  32. TenantExists = False
  33. If Not Api_IsId(id) Then Exit Function
  34. Set rs = DAL().Query("SELECT 1 FROM tenants WHERE id = ?", id)
  35. TenantExists = Not rs.EOF
  36. rs.Close
  37. End Function
  38. ' Creates the namespace at `path` (and missing parents) in the tenant; returns the leaf id.
  39. Function EnsureNamespace(tenantId, path)
  40. Dim parts, i, cur, parentId, rs, id, name
  41. parts = Split(Mid(path, 2), "/")
  42. cur = ""
  43. parentId = Null
  44. For i = 0 To UBound(parts)
  45. name = parts(i)
  46. cur = cur & "/" & name
  47. Set rs = DAL().Query("SELECT id FROM namespaces WHERE tenant_id = ? AND path = ?", Array(tenantId, cur))
  48. If rs.EOF Then
  49. rs.Close
  50. id = Api_NewId()
  51. DAL().Execute "INSERT INTO namespaces (id, tenant_id, parent_id, name, path, created_at) VALUES (?, ?, ?, ?, ?, ?)", _
  52. Array(id, tenantId, parentId, name, cur, Api_Now())
  53. Else
  54. id = "" & rs(0)
  55. rs.Close
  56. End If
  57. parentId = id
  58. Next
  59. EnsureNamespace = id
  60. End Function
  61. Function ValidPath(p)
  62. Dim i, c
  63. ValidPath = False
  64. If Len(p) < 2 Or Left(p, 1) <> "/" Or Right(p, 1) = "/" Or InStr(p, "//") > 0 Then Exit Function
  65. For i = 1 To Len(p)
  66. c = Mid(p, i, 1)
  67. If Not ((c >= "a" And c <= "z") Or (c >= "0" And c <= "9") Or c = "/" Or c = "-" Or c = "_" Or c = ".") Then Exit Function
  68. Next
  69. ValidPath = True
  70. End Function
  71. Function NewKey(principalId)
  72. Dim c, plain, id
  73. Set c = Server.CreateObject("G3CRYPTO")
  74. plain = "amk_" & c.RandomHex(32)
  75. id = Api_NewId()
  76. DAL().Execute "INSERT INTO api_keys (id, principal_id, key_hash, key_prefix, status, created_at) VALUES (?, ?, ?, ?, 'active', ?)", _
  77. Array(id, principalId, Api_Sha256(plain), Left(plain, 8), Api_Now())
  78. Response.Write "api_key_id: " & id & vbCrLf
  79. Response.Write "api_key: " & plain & " (shown once, store it now)" & vbCrLf
  80. NewKey = id
  81. End Function
  82. Dim action : action = LCase(Arg("PROV_ACTION"))
  83. Dim a1 : a1 = Arg("PROV_ARG1")
  84. Dim a2 : a2 = Arg("PROV_ARG2")
  85. Dim a3 : a3 = Arg("PROV_ARG3")
  86. Dim a4 : a4 = Arg("PROV_ARG4")
  87. Dim id, rs, nsId, found
  88. Select Case action
  89. Case "tenant"
  90. If Len(a1) = 0 Then
  91. Fail "usage: tenant <name>"
  92. Else
  93. id = Api_NewId()
  94. DAL().Execute "INSERT INTO tenants (id, name, status, created_at) VALUES (?, ?, 'active', ?)", Array(id, a1, Api_Now())
  95. Response.Write "tenant_id: " & id & vbCrLf
  96. End If
  97. Case "principal"
  98. If Not TenantExists(a1) Or Len(a2) = 0 Then
  99. Fail "usage: principal <tenant_id> <name> <human|agent|service> (tenant must exist)"
  100. ElseIf a3 <> "human" And a3 <> "agent" And a3 <> "service" Then
  101. Fail "type must be human, agent or service"
  102. Else
  103. id = Api_NewId()
  104. DAL().Execute "INSERT INTO principals (id, tenant_id, type, name, status, created_at) VALUES (?, ?, ?, ?, 'active', ?)", _
  105. Array(id, a1, a3, a2, Api_Now())
  106. Response.Write "principal_id: " & id & vbCrLf
  107. nsId = NewKey(id)
  108. End If
  109. Case "key"
  110. Set rs = DAL().Query("SELECT 1 FROM principals WHERE id = ? AND status = 'active'", a1)
  111. found = Not rs.EOF
  112. rs.Close ' close before writing: a write while a result set is open is lost under G3DB/SQLite
  113. If found Then
  114. nsId = NewKey(a1)
  115. Else
  116. Fail "usage: key <principal_id> (active principal must exist)"
  117. End If
  118. Case "namespace"
  119. If Not TenantExists(a1) Or Not ValidPath(a2) Then
  120. Fail "usage: namespace <tenant_id> </lower/case/path> (a-z 0-9 - _ . only)"
  121. Else
  122. Response.Write "namespace_id: " & EnsureNamespace(a1, a2) & vbCrLf
  123. End If
  124. Case "grant"
  125. Dim inh : inh = 1
  126. If a4 = "0" Then inh = 0
  127. Set rs = DAL().Query("SELECT tenant_id FROM principals WHERE id = ?", a1)
  128. If rs.EOF Or Not ValidPath(a2) Or Len(a3) = 0 Then
  129. Fail "usage: grant <principal_id> </path> <read,write,delete,admin> [inherit 1|0]"
  130. rs.Close
  131. Else
  132. Dim tid : tid = "" & rs(0)
  133. rs.Close
  134. nsId = EnsureNamespace(tid, a2)
  135. id = Api_NewId()
  136. DAL().Execute "INSERT INTO access_grants (id, principal_id, namespace_id, permissions, inherit, created_at) VALUES (?, ?, ?, ?, ?, ?)", _
  137. Array(id, a1, nsId, LCase(Replace(a3, " ", "")), inh, Api_Now())
  138. Response.Write "grant_id: " & id & " namespace_id: " & nsId & vbCrLf
  139. End If
  140. Case "revoke"
  141. DAL().Execute "UPDATE api_keys SET status = 'revoked' WHERE id = ?", a1
  142. Response.Write "revoked (if the key existed): " & a1 & vbCrLf
  143. Case "list"
  144. Set rs = DAL().Query("SELECT id, type, name, status FROM principals WHERE tenant_id = ? ORDER BY created_at", a1)
  145. Response.Write "principals:" & vbCrLf
  146. Do While Not rs.EOF
  147. Response.Write " " & rs(0) & " " & rs(1) & " " & rs(2) & " " & rs(3) & vbCrLf
  148. rs.MoveNext
  149. Loop
  150. rs.Close
  151. Set rs = DAL().Query("SELECT id, path FROM namespaces WHERE tenant_id = ? ORDER BY path", a1)
  152. Response.Write "namespaces:" & vbCrLf
  153. Do While Not rs.EOF
  154. Response.Write " " & rs(0) & " " & rs(1) & vbCrLf
  155. rs.MoveNext
  156. Loop
  157. rs.Close
  158. 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)
  159. Response.Write "grants:" & vbCrLf
  160. Do While Not rs.EOF
  161. Response.Write " " & rs(0) & " " & rs(1) & " " & rs(2) & " inherit=" & rs(3) & vbCrLf
  162. rs.MoveNext
  163. Loop
  164. rs.Close
  165. Case "purge"
  166. If Not TenantExists(a1) Then
  167. Fail "usage: purge <tenant_id> (tenant must exist)"
  168. Else
  169. DAL().BeginTransaction
  170. DAL().Execute "DELETE FROM checkpoints WHERE session_id IN (SELECT id FROM sessions WHERE tenant_id = ?)", a1
  171. DAL().Execute "DELETE FROM sessions WHERE tenant_id = ?", a1
  172. DAL().Execute "DELETE FROM memory_revisions WHERE memory_id IN (SELECT id FROM memories WHERE tenant_id = ?)", a1
  173. DAL().Execute "DELETE FROM memories WHERE tenant_id = ?", a1
  174. DAL().Execute "DELETE FROM idempotency_keys WHERE tenant_id = ?", a1
  175. DAL().Execute "DELETE FROM audit_events WHERE tenant_id = ?", a1
  176. DAL().Execute "DELETE FROM access_grants WHERE principal_id IN (SELECT id FROM principals WHERE tenant_id = ?)", a1
  177. DAL().Execute "DELETE FROM api_keys WHERE principal_id IN (SELECT id FROM principals WHERE tenant_id = ?)", a1
  178. DAL().Execute "DELETE FROM namespaces WHERE tenant_id = ?", a1
  179. DAL().Execute "DELETE FROM principals WHERE tenant_id = ?", a1
  180. DAL().Execute "DELETE FROM tenants WHERE id = ?", a1
  181. DAL().CommitTransaction
  182. Response.Write "purged tenant " & a1 & vbCrLf
  183. End If
  184. Case Else
  185. Response.Write "Unknown action '" & action & "'. Use tenant, principal, key, namespace, grant, revoke, list or purge." & vbCrLf
  186. End Select
  187. %>

Powered by TurnKey Linux.