test_mongodb.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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,
  220. b'password: trustsome1',
  221. ).once()
  222. flexmock(module.os).should_receive('close')
  223. flexmock(module.os).should_receive('set_inheritable')
  224. assert module.make_password_config_file('trustsome1') == '/dev/fd/99'
  225. def test_build_dump_command_with_username_injection_attack_gets_escaped():
  226. database = {'name': 'test', 'username': 'bob; naughty-command'}
  227. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  228. 'resolve_credential',
  229. ).replace_with(lambda value, config: value)
  230. command = module.build_dump_command(database, {}, dump_filename='test', dump_format='archive')
  231. assert "'bob; naughty-command'" in command
  232. def test_restore_data_source_dump_runs_mongorestore():
  233. hook_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
  234. extract_process = flexmock(stdout=flexmock())
  235. flexmock(module).should_receive('make_dump_path')
  236. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  237. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  238. 'resolve_credential',
  239. ).replace_with(lambda value, config: value)
  240. flexmock(module).should_receive('execute_command_with_processes').with_args(
  241. ['mongorestore', '--archive', '--drop'],
  242. processes=[extract_process],
  243. output_log_level=logging.DEBUG,
  244. input_file=extract_process.stdout,
  245. ).once()
  246. module.restore_data_source_dump(
  247. hook_config,
  248. {},
  249. data_source={'name': 'foo'},
  250. dry_run=False,
  251. extract_process=extract_process,
  252. connection_params={
  253. 'hostname': None,
  254. 'port': None,
  255. 'username': None,
  256. 'password': None,
  257. },
  258. borgmatic_runtime_directory='/run/borgmatic',
  259. )
  260. def test_restore_data_source_dump_runs_mongorestore_with_hostname_and_port():
  261. hook_config = [
  262. {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None},
  263. ]
  264. extract_process = flexmock(stdout=flexmock())
  265. flexmock(module).should_receive('make_dump_path')
  266. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  267. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  268. 'resolve_credential',
  269. ).replace_with(lambda value, config: value)
  270. flexmock(module).should_receive('execute_command_with_processes').with_args(
  271. [
  272. 'mongorestore',
  273. '--archive',
  274. '--drop',
  275. '--host',
  276. 'database.example.org',
  277. '--port',
  278. '5433',
  279. ],
  280. processes=[extract_process],
  281. output_log_level=logging.DEBUG,
  282. input_file=extract_process.stdout,
  283. ).once()
  284. module.restore_data_source_dump(
  285. hook_config,
  286. {},
  287. data_source=hook_config[0],
  288. dry_run=False,
  289. extract_process=extract_process,
  290. connection_params={
  291. 'hostname': None,
  292. 'port': None,
  293. 'username': None,
  294. 'password': None,
  295. },
  296. borgmatic_runtime_directory='/run/borgmatic',
  297. )
  298. def test_restore_data_source_dump_runs_mongorestore_with_username_and_password():
  299. hook_config = [
  300. {
  301. 'name': 'foo',
  302. 'username': 'mongo',
  303. 'password': 'trustsome1',
  304. 'authentication_database': 'admin',
  305. 'schemas': None,
  306. },
  307. ]
  308. extract_process = flexmock(stdout=flexmock())
  309. flexmock(module).should_receive('make_dump_path')
  310. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  311. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  312. 'resolve_credential',
  313. ).replace_with(lambda value, config: value)
  314. flexmock(module).should_receive('make_password_config_file').with_args('trustsome1').and_return(
  315. '/dev/fd/99',
  316. )
  317. flexmock(module).should_receive('execute_command_with_processes').with_args(
  318. [
  319. 'mongorestore',
  320. '--archive',
  321. '--drop',
  322. '--username',
  323. 'mongo',
  324. '--config',
  325. '/dev/fd/99',
  326. '--authenticationDatabase',
  327. 'admin',
  328. ],
  329. processes=[extract_process],
  330. output_log_level=logging.DEBUG,
  331. input_file=extract_process.stdout,
  332. ).once()
  333. module.restore_data_source_dump(
  334. hook_config,
  335. {},
  336. data_source=hook_config[0],
  337. dry_run=False,
  338. extract_process=extract_process,
  339. connection_params={
  340. 'hostname': None,
  341. 'port': None,
  342. 'username': None,
  343. 'password': None,
  344. },
  345. borgmatic_runtime_directory='/run/borgmatic',
  346. )
  347. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  348. hook_config = [
  349. {
  350. 'name': 'foo',
  351. 'username': 'mongo',
  352. 'password': 'trustsome1',
  353. 'authentication_database': 'admin',
  354. 'restore_hostname': 'restorehost',
  355. 'restore_port': 'restoreport',
  356. 'restore_username': 'restoreusername',
  357. 'restore_password': 'restorepassword',
  358. 'schemas': None,
  359. },
  360. ]
  361. extract_process = flexmock(stdout=flexmock())
  362. flexmock(module).should_receive('make_dump_path')
  363. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  364. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  365. 'resolve_credential',
  366. ).replace_with(lambda value, config: value)
  367. flexmock(module).should_receive('make_password_config_file').with_args(
  368. 'clipassword',
  369. ).and_return('/dev/fd/99')
  370. flexmock(module).should_receive('execute_command_with_processes').with_args(
  371. [
  372. 'mongorestore',
  373. '--archive',
  374. '--drop',
  375. '--host',
  376. 'clihost',
  377. '--port',
  378. 'cliport',
  379. '--username',
  380. 'cliusername',
  381. '--config',
  382. '/dev/fd/99',
  383. '--authenticationDatabase',
  384. 'admin',
  385. ],
  386. processes=[extract_process],
  387. output_log_level=logging.DEBUG,
  388. input_file=extract_process.stdout,
  389. ).once()
  390. module.restore_data_source_dump(
  391. hook_config,
  392. {},
  393. data_source=hook_config[0],
  394. dry_run=False,
  395. extract_process=extract_process,
  396. connection_params={
  397. 'hostname': 'clihost',
  398. 'port': 'cliport',
  399. 'username': 'cliusername',
  400. 'password': 'clipassword',
  401. },
  402. borgmatic_runtime_directory='/run/borgmatic',
  403. )
  404. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  405. hook_config = [
  406. {
  407. 'name': 'foo',
  408. 'username': 'mongo',
  409. 'password': 'trustsome1',
  410. 'authentication_database': 'admin',
  411. 'schemas': None,
  412. 'restore_hostname': 'restorehost',
  413. 'restore_port': 'restoreport',
  414. 'restore_username': 'restoreuser',
  415. 'restore_password': 'restorepass',
  416. },
  417. ]
  418. extract_process = flexmock(stdout=flexmock())
  419. flexmock(module).should_receive('make_dump_path')
  420. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  421. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  422. 'resolve_credential',
  423. ).replace_with(lambda value, config: value)
  424. flexmock(module).should_receive('make_password_config_file').with_args(
  425. 'restorepass',
  426. ).and_return('/dev/fd/99')
  427. flexmock(module).should_receive('execute_command_with_processes').with_args(
  428. [
  429. 'mongorestore',
  430. '--archive',
  431. '--drop',
  432. '--host',
  433. 'restorehost',
  434. '--port',
  435. 'restoreport',
  436. '--username',
  437. 'restoreuser',
  438. '--config',
  439. '/dev/fd/99',
  440. '--authenticationDatabase',
  441. 'admin',
  442. ],
  443. processes=[extract_process],
  444. output_log_level=logging.DEBUG,
  445. input_file=extract_process.stdout,
  446. ).once()
  447. module.restore_data_source_dump(
  448. hook_config,
  449. {},
  450. data_source=hook_config[0],
  451. dry_run=False,
  452. extract_process=extract_process,
  453. connection_params={
  454. 'hostname': None,
  455. 'port': None,
  456. 'username': None,
  457. 'password': None,
  458. },
  459. borgmatic_runtime_directory='/run/borgmatic',
  460. )
  461. def test_restore_data_source_dump_runs_mongorestore_with_options():
  462. hook_config = [{'name': 'foo', 'restore_options': '--harder', 'schemas': None}]
  463. extract_process = flexmock(stdout=flexmock())
  464. flexmock(module).should_receive('make_dump_path')
  465. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  466. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  467. 'resolve_credential',
  468. ).replace_with(lambda value, config: value)
  469. flexmock(module).should_receive('execute_command_with_processes').with_args(
  470. ['mongorestore', '--archive', '--drop', '--harder'],
  471. processes=[extract_process],
  472. output_log_level=logging.DEBUG,
  473. input_file=extract_process.stdout,
  474. ).once()
  475. module.restore_data_source_dump(
  476. hook_config,
  477. {},
  478. data_source=hook_config[0],
  479. dry_run=False,
  480. extract_process=extract_process,
  481. connection_params={
  482. 'hostname': None,
  483. 'port': None,
  484. 'username': None,
  485. 'password': None,
  486. },
  487. borgmatic_runtime_directory='/run/borgmatic',
  488. )
  489. def test_restore_databases_dump_runs_mongorestore_with_schemas():
  490. hook_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
  491. extract_process = flexmock(stdout=flexmock())
  492. flexmock(module).should_receive('make_dump_path')
  493. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  494. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  495. 'resolve_credential',
  496. ).replace_with(lambda value, config: value)
  497. flexmock(module).should_receive('execute_command_with_processes').with_args(
  498. [
  499. 'mongorestore',
  500. '--archive',
  501. '--drop',
  502. '--nsInclude',
  503. 'bar',
  504. '--nsInclude',
  505. 'baz',
  506. ],
  507. processes=[extract_process],
  508. output_log_level=logging.DEBUG,
  509. input_file=extract_process.stdout,
  510. ).once()
  511. module.restore_data_source_dump(
  512. hook_config,
  513. {},
  514. data_source=hook_config[0],
  515. dry_run=False,
  516. extract_process=extract_process,
  517. connection_params={
  518. 'hostname': None,
  519. 'port': None,
  520. 'username': None,
  521. 'password': None,
  522. },
  523. borgmatic_runtime_directory='/run/borgmatic',
  524. )
  525. def test_restore_data_source_dump_runs_psql_for_all_database_dump():
  526. hook_config = [{'name': 'all', 'schemas': None}]
  527. extract_process = flexmock(stdout=flexmock())
  528. flexmock(module).should_receive('make_dump_path')
  529. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  530. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  531. 'resolve_credential',
  532. ).replace_with(lambda value, config: value)
  533. flexmock(module).should_receive('execute_command_with_processes').with_args(
  534. ['mongorestore', '--archive'],
  535. processes=[extract_process],
  536. output_log_level=logging.DEBUG,
  537. input_file=extract_process.stdout,
  538. ).once()
  539. module.restore_data_source_dump(
  540. hook_config,
  541. {},
  542. data_source=hook_config[0],
  543. dry_run=False,
  544. extract_process=extract_process,
  545. connection_params={
  546. 'hostname': None,
  547. 'port': None,
  548. 'username': None,
  549. 'password': None,
  550. },
  551. borgmatic_runtime_directory='/run/borgmatic',
  552. )
  553. def test_restore_data_source_dump_with_dry_run_skips_restore():
  554. hook_config = [{'name': 'foo', 'schemas': None}]
  555. flexmock(module).should_receive('make_dump_path')
  556. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  557. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  558. 'resolve_credential',
  559. ).replace_with(lambda value, config: value)
  560. flexmock(module).should_receive('execute_command_with_processes').never()
  561. module.restore_data_source_dump(
  562. hook_config,
  563. {},
  564. data_source={'name': 'foo'},
  565. dry_run=True,
  566. extract_process=flexmock(),
  567. connection_params={
  568. 'hostname': None,
  569. 'port': None,
  570. 'username': None,
  571. 'password': None,
  572. },
  573. borgmatic_runtime_directory='/run/borgmatic',
  574. )
  575. def test_restore_data_source_dump_without_extract_process_restores_from_disk():
  576. hook_config = [{'name': 'foo', 'format': 'directory', 'schemas': None}]
  577. flexmock(module).should_receive('make_dump_path')
  578. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  579. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  580. 'resolve_credential',
  581. ).replace_with(lambda value, config: value)
  582. flexmock(module).should_receive('execute_command_with_processes').with_args(
  583. ['mongorestore', '--dir', '/dump/path', '--drop'],
  584. processes=[],
  585. output_log_level=logging.DEBUG,
  586. input_file=None,
  587. ).once()
  588. module.restore_data_source_dump(
  589. hook_config,
  590. {},
  591. data_source={'name': 'foo'},
  592. dry_run=False,
  593. extract_process=None,
  594. connection_params={
  595. 'hostname': None,
  596. 'port': None,
  597. 'username': None,
  598. 'password': None,
  599. },
  600. borgmatic_runtime_directory='/run/borgmatic',
  601. )
  602. def test_dump_data_sources_uses_custom_mongodump_command():
  603. flexmock(module.borgmatic.hooks.command).should_receive('Before_after_hooks').and_return(
  604. flexmock(),
  605. )
  606. databases = [{'name': 'foo', 'mongodump_command': 'custom_mongodump'}]
  607. process = flexmock()
  608. flexmock(module).should_receive('make_dump_path').and_return('')
  609. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  610. 'databases/localhost/foo',
  611. )
  612. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  613. flexmock(module).should_receive('execute_command').with_args(
  614. (
  615. 'custom_mongodump',
  616. '--db',
  617. 'foo',
  618. '--archive',
  619. '>',
  620. 'databases/localhost/foo',
  621. ),
  622. shell=True,
  623. run_to_completion=False,
  624. ).and_return(process).once()
  625. assert module.dump_data_sources(
  626. databases,
  627. {},
  628. config_paths=('test.yaml',),
  629. borgmatic_runtime_directory='/run/borgmatic',
  630. patterns=[],
  631. dry_run=False,
  632. ) == [process]
  633. def test_build_dump_command_prevents_shell_injection():
  634. database = {
  635. 'name': 'testdb; rm -rf /', # Malicious input
  636. 'hostname': 'localhost',
  637. 'port': 27017,
  638. 'username': 'user',
  639. 'password': 'password',
  640. 'mongodump_command': 'mongodump',
  641. 'options': '--gzip',
  642. }
  643. config = {}
  644. dump_filename = '/path/to/dump'
  645. dump_format = 'archive'
  646. command = module.build_dump_command(database, config, dump_filename, dump_format)
  647. # Ensure the malicious input is properly escaped and does not execute
  648. assert 'testdb; rm -rf /' not in command
  649. assert any(
  650. 'testdb' in part for part in command
  651. ) # Check if 'testdb' is in any part of the tuple
  652. def test_restore_data_source_dump_uses_custom_mongorestore_command():
  653. hook_config = [
  654. {
  655. 'name': 'foo',
  656. 'mongorestore_command': 'custom_mongorestore',
  657. 'schemas': None,
  658. 'restore_options': '--gzip',
  659. },
  660. ]
  661. extract_process = flexmock(stdout=flexmock())
  662. flexmock(module).should_receive('make_dump_path')
  663. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  664. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  665. 'resolve_credential',
  666. ).replace_with(lambda value, config: value)
  667. flexmock(module).should_receive('execute_command_with_processes').with_args(
  668. [
  669. 'custom_mongorestore', # Should use custom command instead of default
  670. '--archive',
  671. '--drop',
  672. '--gzip', # Should include restore options
  673. ],
  674. processes=[extract_process],
  675. output_log_level=logging.DEBUG,
  676. input_file=extract_process.stdout,
  677. ).once()
  678. module.restore_data_source_dump(
  679. hook_config,
  680. {},
  681. data_source=hook_config[0],
  682. dry_run=False,
  683. extract_process=extract_process,
  684. connection_params={
  685. 'hostname': None,
  686. 'port': None,
  687. 'username': None,
  688. 'password': None,
  689. },
  690. borgmatic_runtime_directory='/run/borgmatic',
  691. )
  692. def test_build_restore_command_prevents_shell_injection():
  693. database = {
  694. 'name': 'testdb; rm -rf /', # Malicious input
  695. 'restore_hostname': 'localhost',
  696. 'restore_port': 27017,
  697. 'restore_username': 'user',
  698. 'restore_password': 'password',
  699. 'mongorestore_command': 'mongorestore',
  700. 'restore_options': '--gzip',
  701. }
  702. config = {}
  703. dump_filename = '/path/to/dump'
  704. connection_params = {
  705. 'hostname': None,
  706. 'port': None,
  707. 'username': None,
  708. 'password': None,
  709. }
  710. extract_process = None
  711. command = module.build_restore_command(
  712. extract_process,
  713. database,
  714. config,
  715. dump_filename,
  716. connection_params,
  717. )
  718. # Ensure the malicious input is properly escaped and does not execute
  719. assert 'rm -rf /' not in command
  720. assert ';' not in command