test_mongodb.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.hooks.data_source import mongodb as module
  4. def test_use_streaming_true_for_any_non_directory_format_databases():
  5. assert module.use_streaming(
  6. databases=[{'format': 'stuff'}, {'format': 'directory'}, {}],
  7. config=flexmock(),
  8. )
  9. def test_use_streaming_false_for_all_directory_format_databases():
  10. assert not module.use_streaming(
  11. databases=[{'format': 'directory'}, {'format': 'directory'}],
  12. config=flexmock(),
  13. )
  14. def test_use_streaming_false_for_no_databases():
  15. assert not module.use_streaming(databases=[], config=flexmock())
  16. def test_dump_data_sources_runs_mongodump_for_each_database():
  17. databases = [{'name': 'foo'}, {'name': 'bar'}]
  18. processes = [flexmock(), flexmock()]
  19. flexmock(module).should_receive('make_dump_path').and_return('')
  20. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  21. 'databases/localhost/foo'
  22. ).and_return('databases/localhost/bar')
  23. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  24. for name, process in zip(('foo', 'bar'), processes):
  25. flexmock(module).should_receive('execute_command').with_args(
  26. ('mongodump', '--db', name, '--archive', '>', f'databases/localhost/{name}'),
  27. shell=True,
  28. run_to_completion=False,
  29. ).and_return(process).once()
  30. assert (
  31. module.dump_data_sources(
  32. databases,
  33. {},
  34. config_paths=('test.yaml',),
  35. borgmatic_runtime_directory='/run/borgmatic',
  36. patterns=[],
  37. dry_run=False,
  38. )
  39. == processes
  40. )
  41. def test_dump_data_sources_with_dry_run_skips_mongodump():
  42. databases = [{'name': 'foo'}, {'name': 'bar'}]
  43. flexmock(module).should_receive('make_dump_path').and_return('')
  44. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  45. 'databases/localhost/foo'
  46. ).and_return('databases/localhost/bar')
  47. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  48. flexmock(module).should_receive('execute_command').never()
  49. assert (
  50. module.dump_data_sources(
  51. databases,
  52. {},
  53. config_paths=('test.yaml',),
  54. borgmatic_runtime_directory='/run/borgmatic',
  55. patterns=[],
  56. dry_run=True,
  57. )
  58. == []
  59. )
  60. def test_dump_data_sources_runs_mongodump_with_hostname_and_port():
  61. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  62. process = flexmock()
  63. flexmock(module).should_receive('make_dump_path').and_return('')
  64. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  65. 'databases/database.example.org/foo'
  66. )
  67. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  68. flexmock(module).should_receive('execute_command').with_args(
  69. (
  70. 'mongodump',
  71. '--host',
  72. 'database.example.org',
  73. '--port',
  74. '5433',
  75. '--db',
  76. 'foo',
  77. '--archive',
  78. '>',
  79. 'databases/database.example.org/foo',
  80. ),
  81. shell=True,
  82. run_to_completion=False,
  83. ).and_return(process).once()
  84. assert module.dump_data_sources(
  85. databases,
  86. {},
  87. config_paths=('test.yaml',),
  88. borgmatic_runtime_directory='/run/borgmatic',
  89. patterns=[],
  90. dry_run=False,
  91. ) == [process]
  92. def test_dump_data_sources_runs_mongodump_with_username_and_password():
  93. databases = [
  94. {
  95. 'name': 'foo',
  96. 'username': 'mongo',
  97. 'password': 'trustsome1',
  98. 'authentication_database': 'admin',
  99. }
  100. ]
  101. process = flexmock()
  102. flexmock(module).should_receive('make_dump_path').and_return('')
  103. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  104. 'databases/localhost/foo'
  105. )
  106. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  107. 'resolve_credential'
  108. ).replace_with(lambda value, config: value)
  109. flexmock(module).should_receive('make_password_config_file').with_args('trustsome1').and_return(
  110. '/dev/fd/99'
  111. )
  112. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  113. flexmock(module).should_receive('execute_command').with_args(
  114. (
  115. 'mongodump',
  116. '--username',
  117. 'mongo',
  118. '--config',
  119. '/dev/fd/99',
  120. '--authenticationDatabase',
  121. 'admin',
  122. '--db',
  123. 'foo',
  124. '--archive',
  125. '>',
  126. 'databases/localhost/foo',
  127. ),
  128. shell=True,
  129. run_to_completion=False,
  130. ).and_return(process).once()
  131. assert module.dump_data_sources(
  132. databases,
  133. {},
  134. config_paths=('test.yaml',),
  135. borgmatic_runtime_directory='/run/borgmatic',
  136. patterns=[],
  137. dry_run=False,
  138. ) == [process]
  139. def test_dump_data_sources_runs_mongodump_with_directory_format():
  140. databases = [{'name': 'foo', 'format': 'directory'}]
  141. flexmock(module).should_receive('make_dump_path').and_return('')
  142. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  143. 'databases/localhost/foo'
  144. )
  145. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  146. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  147. flexmock(module).should_receive('execute_command').with_args(
  148. ('mongodump', '--out', 'databases/localhost/foo', '--db', 'foo'),
  149. shell=True,
  150. ).and_return(flexmock()).once()
  151. assert (
  152. module.dump_data_sources(
  153. databases,
  154. {},
  155. config_paths=('test.yaml',),
  156. borgmatic_runtime_directory='/run/borgmatic',
  157. patterns=[],
  158. dry_run=False,
  159. )
  160. == []
  161. )
  162. def test_dump_data_sources_runs_mongodump_with_options():
  163. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  164. process = flexmock()
  165. flexmock(module).should_receive('make_dump_path').and_return('')
  166. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  167. 'databases/localhost/foo'
  168. )
  169. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  170. flexmock(module).should_receive('execute_command').with_args(
  171. (
  172. 'mongodump',
  173. '--db',
  174. 'foo',
  175. '--stuff=such',
  176. '--archive',
  177. '>',
  178. 'databases/localhost/foo',
  179. ),
  180. shell=True,
  181. run_to_completion=False,
  182. ).and_return(process).once()
  183. assert module.dump_data_sources(
  184. databases,
  185. {},
  186. config_paths=('test.yaml',),
  187. borgmatic_runtime_directory='/run/borgmatic',
  188. patterns=[],
  189. dry_run=False,
  190. ) == [process]
  191. def test_dump_data_sources_runs_mongodumpall_for_all_databases():
  192. databases = [{'name': 'all'}]
  193. process = flexmock()
  194. flexmock(module).should_receive('make_dump_path').and_return('')
  195. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  196. 'databases/localhost/all'
  197. )
  198. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  199. flexmock(module).should_receive('execute_command').with_args(
  200. ('mongodump', '--archive', '>', 'databases/localhost/all'),
  201. shell=True,
  202. run_to_completion=False,
  203. ).and_return(process).once()
  204. assert module.dump_data_sources(
  205. databases,
  206. {},
  207. config_paths=('test.yaml',),
  208. borgmatic_runtime_directory='/run/borgmatic',
  209. patterns=[],
  210. dry_run=False,
  211. ) == [process]
  212. def test_make_password_config_file_writes_password_to_pipe():
  213. read_file_descriptor = 99
  214. write_file_descriptor = flexmock()
  215. flexmock(module.os).should_receive('pipe').and_return(
  216. (read_file_descriptor, write_file_descriptor)
  217. )
  218. flexmock(module.os).should_receive('write').with_args(
  219. write_file_descriptor, b'password: trustsome1'
  220. ).once()
  221. flexmock(module.os).should_receive('close')
  222. flexmock(module.os).should_receive('set_inheritable')
  223. assert module.make_password_config_file('trustsome1') == '/dev/fd/99'
  224. def test_build_dump_command_with_username_injection_attack_gets_escaped():
  225. database = {'name': 'test', 'username': 'bob; naughty-command'}
  226. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  227. 'resolve_credential'
  228. ).replace_with(lambda value, config: value)
  229. command = module.build_dump_command(database, {}, dump_filename='test', dump_format='archive')
  230. assert "'bob; naughty-command'" in command
  231. def test_restore_data_source_dump_runs_mongorestore():
  232. hook_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
  233. extract_process = flexmock(stdout=flexmock())
  234. flexmock(module).should_receive('make_dump_path')
  235. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  236. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  237. 'resolve_credential'
  238. ).replace_with(lambda value, config: value)
  239. flexmock(module).should_receive('execute_command_with_processes').with_args(
  240. ['mongorestore', '--archive', '--drop'],
  241. processes=[extract_process],
  242. output_log_level=logging.DEBUG,
  243. input_file=extract_process.stdout,
  244. ).once()
  245. module.restore_data_source_dump(
  246. hook_config,
  247. {},
  248. data_source={'name': 'foo'},
  249. dry_run=False,
  250. extract_process=extract_process,
  251. connection_params={
  252. 'hostname': None,
  253. 'port': None,
  254. 'username': None,
  255. 'password': None,
  256. },
  257. borgmatic_runtime_directory='/run/borgmatic',
  258. )
  259. def test_restore_data_source_dump_runs_mongorestore_with_hostname_and_port():
  260. hook_config = [
  261. {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
  262. ]
  263. extract_process = flexmock(stdout=flexmock())
  264. flexmock(module).should_receive('make_dump_path')
  265. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  266. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  267. 'resolve_credential'
  268. ).replace_with(lambda value, config: value)
  269. flexmock(module).should_receive('execute_command_with_processes').with_args(
  270. [
  271. 'mongorestore',
  272. '--archive',
  273. '--drop',
  274. '--host',
  275. 'database.example.org',
  276. '--port',
  277. '5433',
  278. ],
  279. processes=[extract_process],
  280. output_log_level=logging.DEBUG,
  281. input_file=extract_process.stdout,
  282. ).once()
  283. module.restore_data_source_dump(
  284. hook_config,
  285. {},
  286. data_source=hook_config[0],
  287. dry_run=False,
  288. extract_process=extract_process,
  289. connection_params={
  290. 'hostname': None,
  291. 'port': None,
  292. 'username': None,
  293. 'password': None,
  294. },
  295. borgmatic_runtime_directory='/run/borgmatic',
  296. )
  297. def test_restore_data_source_dump_runs_mongorestore_with_username_and_password():
  298. hook_config = [
  299. {
  300. 'name': 'foo',
  301. 'username': 'mongo',
  302. 'password': 'trustsome1',
  303. 'authentication_database': 'admin',
  304. 'schemas': None,
  305. }
  306. ]
  307. extract_process = flexmock(stdout=flexmock())
  308. flexmock(module).should_receive('make_dump_path')
  309. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  310. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  311. 'resolve_credential'
  312. ).replace_with(lambda value, config: value)
  313. flexmock(module).should_receive('make_password_config_file').with_args('trustsome1').and_return(
  314. '/dev/fd/99'
  315. )
  316. flexmock(module).should_receive('execute_command_with_processes').with_args(
  317. [
  318. 'mongorestore',
  319. '--archive',
  320. '--drop',
  321. '--username',
  322. 'mongo',
  323. '--config',
  324. '/dev/fd/99',
  325. '--authenticationDatabase',
  326. 'admin',
  327. ],
  328. processes=[extract_process],
  329. output_log_level=logging.DEBUG,
  330. input_file=extract_process.stdout,
  331. ).once()
  332. module.restore_data_source_dump(
  333. hook_config,
  334. {},
  335. data_source=hook_config[0],
  336. dry_run=False,
  337. extract_process=extract_process,
  338. connection_params={
  339. 'hostname': None,
  340. 'port': None,
  341. 'username': None,
  342. 'password': None,
  343. },
  344. borgmatic_runtime_directory='/run/borgmatic',
  345. )
  346. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  347. hook_config = [
  348. {
  349. 'name': 'foo',
  350. 'username': 'mongo',
  351. 'password': 'trustsome1',
  352. 'authentication_database': 'admin',
  353. 'restore_hostname': 'restorehost',
  354. 'restore_port': 'restoreport',
  355. 'restore_username': 'restoreusername',
  356. 'restore_password': 'restorepassword',
  357. 'schemas': None,
  358. }
  359. ]
  360. extract_process = flexmock(stdout=flexmock())
  361. flexmock(module).should_receive('make_dump_path')
  362. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  363. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  364. 'resolve_credential'
  365. ).replace_with(lambda value, config: value)
  366. flexmock(module).should_receive('make_password_config_file').with_args(
  367. 'clipassword'
  368. ).and_return('/dev/fd/99')
  369. flexmock(module).should_receive('execute_command_with_processes').with_args(
  370. [
  371. 'mongorestore',
  372. '--archive',
  373. '--drop',
  374. '--host',
  375. 'clihost',
  376. '--port',
  377. 'cliport',
  378. '--username',
  379. 'cliusername',
  380. '--config',
  381. '/dev/fd/99',
  382. '--authenticationDatabase',
  383. 'admin',
  384. ],
  385. processes=[extract_process],
  386. output_log_level=logging.DEBUG,
  387. input_file=extract_process.stdout,
  388. ).once()
  389. module.restore_data_source_dump(
  390. hook_config,
  391. {},
  392. data_source=hook_config[0],
  393. dry_run=False,
  394. extract_process=extract_process,
  395. connection_params={
  396. 'hostname': 'clihost',
  397. 'port': 'cliport',
  398. 'username': 'cliusername',
  399. 'password': 'clipassword',
  400. },
  401. borgmatic_runtime_directory='/run/borgmatic',
  402. )
  403. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  404. hook_config = [
  405. {
  406. 'name': 'foo',
  407. 'username': 'mongo',
  408. 'password': 'trustsome1',
  409. 'authentication_database': 'admin',
  410. 'schemas': None,
  411. 'restore_hostname': 'restorehost',
  412. 'restore_port': 'restoreport',
  413. 'restore_username': 'restoreuser',
  414. 'restore_password': 'restorepass',
  415. }
  416. ]
  417. extract_process = flexmock(stdout=flexmock())
  418. flexmock(module).should_receive('make_dump_path')
  419. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  420. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  421. 'resolve_credential'
  422. ).replace_with(lambda value, config: value)
  423. flexmock(module).should_receive('make_password_config_file').with_args(
  424. 'restorepass'
  425. ).and_return('/dev/fd/99')
  426. flexmock(module).should_receive('execute_command_with_processes').with_args(
  427. [
  428. 'mongorestore',
  429. '--archive',
  430. '--drop',
  431. '--host',
  432. 'restorehost',
  433. '--port',
  434. 'restoreport',
  435. '--username',
  436. 'restoreuser',
  437. '--config',
  438. '/dev/fd/99',
  439. '--authenticationDatabase',
  440. 'admin',
  441. ],
  442. processes=[extract_process],
  443. output_log_level=logging.DEBUG,
  444. input_file=extract_process.stdout,
  445. ).once()
  446. module.restore_data_source_dump(
  447. hook_config,
  448. {},
  449. data_source=hook_config[0],
  450. dry_run=False,
  451. extract_process=extract_process,
  452. connection_params={
  453. 'hostname': None,
  454. 'port': None,
  455. 'username': None,
  456. 'password': None,
  457. },
  458. borgmatic_runtime_directory='/run/borgmatic',
  459. )
  460. def test_restore_data_source_dump_runs_mongorestore_with_options():
  461. hook_config = [{'name': 'foo', 'restore_options': '--harder', 'schemas': None}]
  462. extract_process = flexmock(stdout=flexmock())
  463. flexmock(module).should_receive('make_dump_path')
  464. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  465. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  466. 'resolve_credential'
  467. ).replace_with(lambda value, config: value)
  468. flexmock(module).should_receive('execute_command_with_processes').with_args(
  469. ['mongorestore', '--archive', '--drop', '--harder'],
  470. processes=[extract_process],
  471. output_log_level=logging.DEBUG,
  472. input_file=extract_process.stdout,
  473. ).once()
  474. module.restore_data_source_dump(
  475. hook_config,
  476. {},
  477. data_source=hook_config[0],
  478. dry_run=False,
  479. extract_process=extract_process,
  480. connection_params={
  481. 'hostname': None,
  482. 'port': None,
  483. 'username': None,
  484. 'password': None,
  485. },
  486. borgmatic_runtime_directory='/run/borgmatic',
  487. )
  488. def test_restore_databases_dump_runs_mongorestore_with_schemas():
  489. hook_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
  490. extract_process = flexmock(stdout=flexmock())
  491. flexmock(module).should_receive('make_dump_path')
  492. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  493. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  494. 'resolve_credential'
  495. ).replace_with(lambda value, config: value)
  496. flexmock(module).should_receive('execute_command_with_processes').with_args(
  497. [
  498. 'mongorestore',
  499. '--archive',
  500. '--drop',
  501. '--nsInclude',
  502. 'bar',
  503. '--nsInclude',
  504. 'baz',
  505. ],
  506. processes=[extract_process],
  507. output_log_level=logging.DEBUG,
  508. input_file=extract_process.stdout,
  509. ).once()
  510. module.restore_data_source_dump(
  511. hook_config,
  512. {},
  513. data_source=hook_config[0],
  514. dry_run=False,
  515. extract_process=extract_process,
  516. connection_params={
  517. 'hostname': None,
  518. 'port': None,
  519. 'username': None,
  520. 'password': None,
  521. },
  522. borgmatic_runtime_directory='/run/borgmatic',
  523. )
  524. def test_restore_data_source_dump_runs_psql_for_all_database_dump():
  525. hook_config = [{'name': 'all', 'schemas': None}]
  526. extract_process = flexmock(stdout=flexmock())
  527. flexmock(module).should_receive('make_dump_path')
  528. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  529. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  530. 'resolve_credential'
  531. ).replace_with(lambda value, config: value)
  532. flexmock(module).should_receive('execute_command_with_processes').with_args(
  533. ['mongorestore', '--archive'],
  534. processes=[extract_process],
  535. output_log_level=logging.DEBUG,
  536. input_file=extract_process.stdout,
  537. ).once()
  538. module.restore_data_source_dump(
  539. hook_config,
  540. {},
  541. data_source=hook_config[0],
  542. dry_run=False,
  543. extract_process=extract_process,
  544. connection_params={
  545. 'hostname': None,
  546. 'port': None,
  547. 'username': None,
  548. 'password': None,
  549. },
  550. borgmatic_runtime_directory='/run/borgmatic',
  551. )
  552. def test_restore_data_source_dump_with_dry_run_skips_restore():
  553. hook_config = [{'name': 'foo', 'schemas': None}]
  554. flexmock(module).should_receive('make_dump_path')
  555. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  556. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  557. 'resolve_credential'
  558. ).replace_with(lambda value, config: value)
  559. flexmock(module).should_receive('execute_command_with_processes').never()
  560. module.restore_data_source_dump(
  561. hook_config,
  562. {},
  563. data_source={'name': 'foo'},
  564. dry_run=True,
  565. extract_process=flexmock(),
  566. connection_params={
  567. 'hostname': None,
  568. 'port': None,
  569. 'username': None,
  570. 'password': None,
  571. },
  572. borgmatic_runtime_directory='/run/borgmatic',
  573. )
  574. def test_restore_data_source_dump_without_extract_process_restores_from_disk():
  575. hook_config = [{'name': 'foo', 'format': 'directory', 'schemas': None}]
  576. flexmock(module).should_receive('make_dump_path')
  577. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  578. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  579. 'resolve_credential'
  580. ).replace_with(lambda value, config: value)
  581. flexmock(module).should_receive('execute_command_with_processes').with_args(
  582. ['mongorestore', '--dir', '/dump/path', '--drop'],
  583. processes=[],
  584. output_log_level=logging.DEBUG,
  585. input_file=None,
  586. ).once()
  587. module.restore_data_source_dump(
  588. hook_config,
  589. {},
  590. data_source={'name': 'foo'},
  591. dry_run=False,
  592. extract_process=None,
  593. connection_params={
  594. 'hostname': None,
  595. 'port': None,
  596. 'username': None,
  597. 'password': None,
  598. },
  599. borgmatic_runtime_directory='/run/borgmatic',
  600. )
  601. def test_dump_data_sources_uses_custom_mongodump_command():
  602. flexmock(module.borgmatic.hooks.command).should_receive('Before_after_hooks').and_return(
  603. flexmock()
  604. )
  605. databases = [{'name': 'foo', 'mongodump_command': 'custom_mongodump'}]
  606. process = flexmock()
  607. flexmock(module).should_receive('make_dump_path').and_return('')
  608. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  609. 'databases/localhost/foo'
  610. )
  611. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  612. flexmock(module).should_receive('execute_command').with_args(
  613. (
  614. 'custom_mongodump',
  615. '--db',
  616. 'foo',
  617. '--archive',
  618. '>',
  619. 'databases/localhost/foo',
  620. ),
  621. shell=True,
  622. run_to_completion=False,
  623. ).and_return(process).once()
  624. assert module.dump_data_sources(
  625. databases,
  626. {},
  627. config_paths=('test.yaml',),
  628. borgmatic_runtime_directory='/run/borgmatic',
  629. patterns=[],
  630. dry_run=False,
  631. ) == [process]
  632. def test_build_dump_command_prevents_shell_injection():
  633. database = {
  634. 'name': 'testdb; rm -rf /', # Malicious input
  635. 'hostname': 'localhost',
  636. 'port': 27017,
  637. 'username': 'user',
  638. 'password': 'password',
  639. 'mongodump_command': 'mongodump',
  640. 'options': '--gzip',
  641. }
  642. config = {}
  643. dump_filename = '/path/to/dump'
  644. dump_format = 'archive'
  645. command = module.build_dump_command(database, config, dump_filename, dump_format)
  646. # Ensure the malicious input is properly escaped and does not execute
  647. assert 'testdb; rm -rf /' not in command
  648. assert any(
  649. 'testdb' in part for part in command
  650. ) # Check if 'testdb' is in any part of the tuple
  651. def test_restore_data_source_dump_uses_custom_mongorestore_command():
  652. hook_config = [
  653. {
  654. 'name': 'foo',
  655. 'mongorestore_command': 'custom_mongorestore',
  656. 'schemas': None,
  657. 'restore_options': '--gzip',
  658. }
  659. ]
  660. extract_process = flexmock(stdout=flexmock())
  661. flexmock(module).should_receive('make_dump_path')
  662. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  663. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  664. 'resolve_credential'
  665. ).replace_with(lambda value, config: value)
  666. flexmock(module).should_receive('execute_command_with_processes').with_args(
  667. [
  668. 'custom_mongorestore', # Should use custom command instead of default
  669. '--archive',
  670. '--drop',
  671. '--gzip', # Should include restore options
  672. ],
  673. processes=[extract_process],
  674. output_log_level=logging.DEBUG,
  675. input_file=extract_process.stdout,
  676. ).once()
  677. module.restore_data_source_dump(
  678. hook_config,
  679. {},
  680. data_source=hook_config[0],
  681. dry_run=False,
  682. extract_process=extract_process,
  683. connection_params={
  684. 'hostname': None,
  685. 'port': None,
  686. 'username': None,
  687. 'password': None,
  688. },
  689. borgmatic_runtime_directory='/run/borgmatic',
  690. )
  691. def test_build_restore_command_prevents_shell_injection():
  692. database = {
  693. 'name': 'testdb; rm -rf /', # Malicious input
  694. 'restore_hostname': 'localhost',
  695. 'restore_port': 27017,
  696. 'restore_username': 'user',
  697. 'restore_password': 'password',
  698. 'mongorestore_command': 'mongorestore',
  699. 'restore_options': '--gzip',
  700. }
  701. config = {}
  702. dump_filename = '/path/to/dump'
  703. connection_params = {
  704. 'hostname': None,
  705. 'port': None,
  706. 'username': None,
  707. 'password': None,
  708. }
  709. extract_process = None
  710. command = module.build_restore_command(
  711. extract_process, database, config, dump_filename, connection_params
  712. )
  713. # print(command)
  714. # Ensure the malicious input is properly escaped and does not execute
  715. assert 'rm -rf /' not in command
  716. assert ';' not in command