insert.sql 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. -- =========================================================
  2. -- TEST DATA FOR AGENT MANAGEMENT SYSTEM
  3. -- PostgreSQL
  4. -- =========================================================
  5. -- =========================================================
  6. -- 100 USERS
  7. -- =========================================================
  8. INSERT INTO users (
  9. username,
  10. password_hash,
  11. role
  12. )
  13. SELECT
  14. 'user_' || gs,
  15. md5(random()::text),
  16. (
  17. ARRAY[
  18. 'admin',
  19. 'operator',
  20. 'viewer'
  21. ]
  22. )[floor(random() * 3 + 1)::int]::user_role
  23. FROM generate_series(1, 100) gs;
  24. -- =========================================================
  25. -- 50 AGENTS
  26. -- =========================================================
  27. INSERT INTO agents (
  28. hostname,
  29. token_hash,
  30. cert_fingerprint,
  31. os_info,
  32. status,
  33. last_seen
  34. )
  35. SELECT
  36. 'agent-' || gs,
  37. md5(random()::text),
  38. md5(random()::text),
  39. jsonb_build_object(
  40. 'os',
  41. (
  42. ARRAY[
  43. 'Windows 11',
  44. 'Ubuntu 22.04',
  45. 'Debian 12',
  46. 'CentOS 9',
  47. 'Windows Server 2022'
  48. ]
  49. )[floor(random() * 5 + 1)::int],
  50. 'ram_gb',
  51. floor(random() * 64 + 4),
  52. 'cpu_cores',
  53. floor(random() * 16 + 1)
  54. ),
  55. (
  56. ARRAY[
  57. 'online',
  58. 'offline',
  59. 'busy',
  60. 'error'
  61. ]
  62. )[floor(random() * 4 + 1)::int]::agent_status,
  63. NOW() - (random() * interval '10 days')
  64. FROM generate_series(1, 50) gs;
  65. -- =========================================================
  66. -- 200 COMMANDS (OPERATIONS HISTORY)
  67. -- =========================================================
  68. INSERT INTO commands (
  69. agent_id,
  70. issued_by,
  71. command_type_id,
  72. command,
  73. status,
  74. output,
  75. exit_code,
  76. created_at,
  77. completed_at
  78. )
  79. SELECT
  80. random_agent_id,
  81. random_user_id,
  82. random_command_type_id,
  83. (
  84. ARRAY[
  85. 'whoami',
  86. 'hostname',
  87. 'ipconfig',
  88. 'ifconfig',
  89. 'ls -la',
  90. 'df -h',
  91. 'uptime',
  92. 'systemctl status nginx',
  93. 'docker ps',
  94. 'cat /etc/os-release'
  95. ]
  96. )[floor(random() * 10 + 1)::int],
  97. random_status,
  98. (
  99. ARRAY[
  100. 'Command executed successfully',
  101. 'Permission denied',
  102. 'Host unreachable',
  103. 'Completed with warnings',
  104. 'Execution timeout',
  105. 'No output'
  106. ]
  107. )[floor(random() * 6 + 1)::int],
  108. floor(random() * 10)::int,
  109. created_time,
  110. CASE
  111. WHEN random_status IN ('completed', 'failed', 'cancelled')
  112. THEN created_time + (random() * interval '5 hours')
  113. ELSE NULL
  114. END
  115. FROM (
  116. SELECT
  117. (
  118. SELECT id
  119. FROM agents
  120. ORDER BY random()
  121. LIMIT 1
  122. ) AS random_agent_id,
  123. (
  124. SELECT id
  125. FROM users
  126. ORDER BY random()
  127. LIMIT 1
  128. ) AS random_user_id,
  129. (
  130. SELECT id
  131. FROM command_types
  132. ORDER BY random()
  133. LIMIT 1
  134. ) AS random_command_type_id,
  135. (
  136. ARRAY[
  137. 'pending',
  138. 'running',
  139. 'completed',
  140. 'failed',
  141. 'cancelled'
  142. ]
  143. )[floor(random() * 5 + 1)::int]::command_status AS random_status,
  144. NOW() - (random() * interval '30 days') AS created_time
  145. FROM generate_series(1, 200)
  146. ) t;
  147. -- =========================================================
  148. -- 80 FILE TRANSFERS
  149. -- =========================================================
  150. INSERT INTO file_transfers (
  151. agent_id,
  152. initiated_by,
  153. command_id,
  154. direction,
  155. filename,
  156. sha256,
  157. size_bytes,
  158. status,
  159. created_at
  160. )
  161. SELECT
  162. (
  163. SELECT id
  164. FROM agents
  165. ORDER BY random()
  166. LIMIT 1
  167. ),
  168. (
  169. SELECT id
  170. FROM users
  171. ORDER BY random()
  172. LIMIT 1
  173. ),
  174. (
  175. SELECT id
  176. FROM commands
  177. ORDER BY random()
  178. LIMIT 1
  179. ),
  180. (
  181. ARRAY[
  182. 'upload',
  183. 'download'
  184. ]
  185. )[floor(random() * 2 + 1)::int]::transfer_direction,
  186. (
  187. ARRAY[
  188. 'backup.zip',
  189. 'logs.tar.gz',
  190. 'report.pdf',
  191. 'config.json',
  192. 'database.dump',
  193. 'image.iso',
  194. 'script.sh'
  195. ]
  196. )[floor(random() * 7 + 1)::int],
  197. md5(random()::text) || md5(random()::text),
  198. floor(random() * 1000000000),
  199. (
  200. ARRAY[
  201. 'pending',
  202. 'running',
  203. 'completed',
  204. 'failed'
  205. ]
  206. )[floor(random() * 4 + 1)::int]::command_status,
  207. NOW() - (random() * interval '30 days')
  208. FROM generate_series(1, 80);
  209. -- =========================================================
  210. -- 150 AUDIT LOGS
  211. -- =========================================================
  212. INSERT INTO audit_logs (
  213. actor_id,
  214. actor_type,
  215. action,
  216. target,
  217. details,
  218. ip_address,
  219. created_at
  220. )
  221. SELECT
  222. (
  223. SELECT id
  224. FROM users
  225. ORDER BY random()
  226. LIMIT 1
  227. ),
  228. 'user'::actor_type_enum,
  229. (
  230. ARRAY[
  231. 'login',
  232. 'logout',
  233. 'command_created',
  234. 'agent_updated',
  235. 'file_uploaded',
  236. 'settings_changed',
  237. 'password_changed'
  238. ]
  239. )[floor(random() * 7 + 1)::int],
  240. (
  241. ARRAY[
  242. 'agent',
  243. 'command',
  244. 'system',
  245. 'user',
  246. 'file_transfer'
  247. ]
  248. )[floor(random() * 5 + 1)::int],
  249. jsonb_build_object(
  250. 'result',
  251. (
  252. ARRAY[
  253. 'success',
  254. 'failed',
  255. 'warning'
  256. ]
  257. )[floor(random() * 3 + 1)::int],
  258. 'session_id',
  259. md5(random()::text)
  260. ),
  261. (
  262. floor(random() * 255)::int || '.' ||
  263. floor(random() * 255)::int || '.' ||
  264. floor(random() * 255)::int || '.' ||
  265. floor(random() * 255)::int
  266. )::inet,
  267. NOW() - (random() * interval '30 days')
  268. FROM generate_series(1, 150);
  269. -- =========================================================
  270. -- 100 HEARTBEATS
  271. -- =========================================================
  272. INSERT INTO agent_heartbeats (
  273. agent_id,
  274. received_at,
  275. metadata
  276. )
  277. SELECT
  278. (
  279. SELECT id
  280. FROM agents
  281. ORDER BY random()
  282. LIMIT 1
  283. ),
  284. NOW() - (random() * interval '30 days'),
  285. jsonb_build_object(
  286. 'cpu_usage',
  287. floor(random() * 100),
  288. 'ram_usage',
  289. floor(random() * 100),
  290. 'disk_usage',
  291. floor(random() * 100)
  292. )
  293. FROM generate_series(1, 100);
  294. -- =========================================================
  295. -- 50 USER SESSIONS
  296. -- =========================================================
  297. INSERT INTO user_sessions (
  298. user_id,
  299. refresh_token_hash,
  300. user_agent,
  301. ip_address,
  302. expires_at,
  303. created_at
  304. )
  305. SELECT
  306. (
  307. SELECT id
  308. FROM users
  309. ORDER BY random()
  310. LIMIT 1
  311. ),
  312. md5(random()::text),
  313. (
  314. ARRAY[
  315. 'Chrome',
  316. 'Firefox',
  317. 'Edge',
  318. 'Safari',
  319. 'Postman'
  320. ]
  321. )[floor(random() * 5 + 1)::int],
  322. (
  323. floor(random() * 255)::int || '.' ||
  324. floor(random() * 255)::int || '.' ||
  325. floor(random() * 255)::int || '.' ||
  326. floor(random() * 255)::int
  327. )::inet,
  328. NOW() + (random() * interval '30 days'),
  329. NOW() - (random() * interval '30 days')
  330. FROM generate_series(1, 50);
  331. -- =========================================================
  332. -- AGENT GROUPS
  333. -- =========================================================
  334. INSERT INTO agent_groups (
  335. name,
  336. description
  337. )
  338. VALUES
  339. ('Production', 'Production infrastructure'),
  340. ('Testing', 'Testing infrastructure'),
  341. ('Development', 'Development infrastructure'),
  342. ('Monitoring', 'Monitoring servers'),
  343. ('Backup', 'Backup infrastructure');
  344. -- =========================================================
  345. -- AGENT GROUP MEMBERS
  346. -- =========================================================
  347. INSERT INTO agent_group_members (
  348. agent_id,
  349. group_id
  350. )
  351. SELECT DISTINCT
  352. (
  353. SELECT id
  354. FROM agents
  355. ORDER BY random()
  356. LIMIT 1
  357. ),
  358. (
  359. SELECT id
  360. FROM agent_groups
  361. ORDER BY random()
  362. LIMIT 1
  363. )
  364. FROM generate_series(1, 100)
  365. ON CONFLICT DO NOTHING;