test_mongodb.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  25. '/path/to/working/dir'
  26. )
  27. for name, process in zip(('foo', 'bar'), processes):
  28. flexmock(module).should_receive('execute_command').with_args(
  29. ('mongodump', '--db', name, '--archive', '>', f'databases/localhost/{name}'),
  30. shell=True,
  31. run_to_completion=False,
  32. working_directory='/path/to/working/dir',
  33. ).and_return(process).once()
  34. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  35. '/run/borgmatic',
  36. 'mongodb_databases',
  37. [
  38. module.borgmatic.actions.restore.Dump('mongodb_databases', 'foo'),
  39. module.borgmatic.actions.restore.Dump('mongodb_databases', 'bar'),
  40. ],
  41. ).once()
  42. flexmock(module.borgmatic.hooks.data_source.config).should_receive('inject_pattern').with_args(
  43. object,
  44. module.borgmatic.borg.pattern.Pattern(
  45. '/run/borgmatic/mongodb_databases',
  46. source=module.borgmatic.borg.pattern.Pattern_source.HOOK,
  47. ),
  48. ).once()
  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=False,
  57. )
  58. == processes
  59. )
  60. def test_dump_data_sources_with_dry_run_skips_mongodump():
  61. databases = [{'name': 'foo'}, {'name': 'bar'}]
  62. flexmock(module).should_receive('make_dump_path').and_return('')
  63. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  64. 'databases/localhost/foo',
  65. ).and_return('databases/localhost/bar')
  66. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  67. flexmock(module).should_receive('execute_command').never()
  68. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').never()
  69. flexmock(module.borgmatic.hooks.data_source.config).should_receive('inject_pattern').never()
  70. assert (
  71. module.dump_data_sources(
  72. databases,
  73. {},
  74. config_paths=('test.yaml',),
  75. borgmatic_runtime_directory='/run/borgmatic',
  76. patterns=[],
  77. dry_run=True,
  78. )
  79. == []
  80. )
  81. def test_dump_data_sources_runs_mongodump_with_hostname_and_port():
  82. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 27018}]
  83. process = flexmock()
  84. flexmock(module).should_receive('make_dump_path').and_return('')
  85. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  86. 'databases/database.example.org/foo',
  87. )
  88. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  89. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  90. flexmock(module).should_receive('execute_command').with_args(
  91. (
  92. 'mongodump',
  93. '--host',
  94. 'database.example.org',
  95. '--port',
  96. '27018',
  97. '--db',
  98. 'foo',
  99. '--archive',
  100. '>',
  101. 'databases/database.example.org/foo',
  102. ),
  103. shell=True,
  104. run_to_completion=False,
  105. working_directory=None,
  106. ).and_return(process).once()
  107. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  108. '/run/borgmatic',
  109. 'mongodb_databases',
  110. [
  111. module.borgmatic.actions.restore.Dump(
  112. 'mongodb_databases', 'foo', 'database.example.org', 27018
  113. ),
  114. ],
  115. ).once()
  116. flexmock(module.borgmatic.hooks.data_source.config).should_receive('inject_pattern').with_args(
  117. object,
  118. module.borgmatic.borg.pattern.Pattern(
  119. '/run/borgmatic/mongodb_databases',
  120. source=module.borgmatic.borg.pattern.Pattern_source.HOOK,
  121. ),
  122. ).once()
  123. assert module.dump_data_sources(
  124. databases,
  125. {},
  126. config_paths=('test.yaml',),
  127. borgmatic_runtime_directory='/run/borgmatic',
  128. patterns=[],
  129. dry_run=False,
  130. ) == [process]
  131. def test_dump_data_sources_runs_mongodump_with_username_and_password():
  132. databases = [
  133. {
  134. 'name': 'foo',
  135. 'username': 'mongo',
  136. 'password': 'trustsome1',
  137. 'authentication_database': 'admin',
  138. },
  139. ]
  140. process = flexmock()
  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.borgmatic.hooks.credential.parse).should_receive(
  146. 'resolve_credential',
  147. ).replace_with(lambda value, config: value)
  148. flexmock(module).should_receive('make_password_config_file').with_args('trustsome1').and_return(
  149. '/dev/fd/99',
  150. )
  151. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  152. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  153. flexmock(module).should_receive('execute_command').with_args(
  154. (
  155. 'mongodump',
  156. '--username',
  157. 'mongo',
  158. '--config',
  159. '/dev/fd/99',
  160. '--authenticationDatabase',
  161. 'admin',
  162. '--db',
  163. 'foo',
  164. '--archive',
  165. '>',
  166. 'databases/localhost/foo',
  167. ),
  168. shell=True,
  169. run_to_completion=False,
  170. working_directory=None,
  171. ).and_return(process).once()
  172. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  173. '/run/borgmatic',
  174. 'mongodb_databases',
  175. [
  176. module.borgmatic.actions.restore.Dump('mongodb_databases', 'foo'),
  177. ],
  178. ).once()
  179. flexmock(module.borgmatic.hooks.data_source.config).should_receive('inject_pattern').with_args(
  180. object,
  181. module.borgmatic.borg.pattern.Pattern(
  182. '/run/borgmatic/mongodb_databases',
  183. source=module.borgmatic.borg.pattern.Pattern_source.HOOK,
  184. ),
  185. ).once()
  186. assert module.dump_data_sources(
  187. databases,
  188. {},
  189. config_paths=('test.yaml',),
  190. borgmatic_runtime_directory='/run/borgmatic',
  191. patterns=[],
  192. dry_run=False,
  193. ) == [process]
  194. def test_dump_data_sources_runs_mongodump_with_directory_format():
  195. databases = [{'name': 'foo', 'format': 'directory'}]
  196. flexmock(module).should_receive('make_dump_path').and_return('')
  197. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  198. 'databases/localhost/foo',
  199. )
  200. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  201. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  202. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  203. flexmock(module).should_receive('execute_command').with_args(
  204. ('mongodump', '--out', 'databases/localhost/foo', '--db', 'foo'),
  205. shell=True,
  206. working_directory=None,
  207. ).and_return(flexmock()).once()
  208. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  209. '/run/borgmatic',
  210. 'mongodb_databases',
  211. [
  212. module.borgmatic.actions.restore.Dump('mongodb_databases', 'foo'),
  213. ],
  214. ).once()
  215. flexmock(module.borgmatic.hooks.data_source.config).should_receive('inject_pattern').with_args(
  216. object,
  217. module.borgmatic.borg.pattern.Pattern(
  218. '/run/borgmatic/mongodb_databases',
  219. source=module.borgmatic.borg.pattern.Pattern_source.HOOK,
  220. ),
  221. ).once()
  222. assert (
  223. module.dump_data_sources(
  224. databases,
  225. {},
  226. config_paths=('test.yaml',),
  227. borgmatic_runtime_directory='/run/borgmatic',
  228. patterns=[],
  229. dry_run=False,
  230. )
  231. == []
  232. )
  233. def test_dump_data_sources_runs_mongodump_with_options():
  234. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  235. process = flexmock()
  236. flexmock(module).should_receive('make_dump_path').and_return('')
  237. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  238. 'databases/localhost/foo',
  239. )
  240. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  241. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  242. flexmock(module).should_receive('execute_command').with_args(
  243. (
  244. 'mongodump',
  245. '--db',
  246. 'foo',
  247. '--stuff=such',
  248. '--archive',
  249. '>',
  250. 'databases/localhost/foo',
  251. ),
  252. shell=True,
  253. run_to_completion=False,
  254. working_directory=None,
  255. ).and_return(process).once()
  256. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  257. '/run/borgmatic',
  258. 'mongodb_databases',
  259. [
  260. module.borgmatic.actions.restore.Dump('mongodb_databases', 'foo'),
  261. ],
  262. ).once()
  263. flexmock(module.borgmatic.hooks.data_source.config).should_receive('inject_pattern').with_args(
  264. object,
  265. module.borgmatic.borg.pattern.Pattern(
  266. '/run/borgmatic/mongodb_databases',
  267. source=module.borgmatic.borg.pattern.Pattern_source.HOOK,
  268. ),
  269. ).once()
  270. assert module.dump_data_sources(
  271. databases,
  272. {},
  273. config_paths=('test.yaml',),
  274. borgmatic_runtime_directory='/run/borgmatic',
  275. patterns=[],
  276. dry_run=False,
  277. ) == [process]
  278. def test_dump_data_sources_runs_mongodumpall_for_all_databases():
  279. databases = [{'name': 'all'}]
  280. process = flexmock()
  281. flexmock(module).should_receive('make_dump_path').and_return('')
  282. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  283. 'databases/localhost/all',
  284. )
  285. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  286. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  287. flexmock(module).should_receive('execute_command').with_args(
  288. ('mongodump', '--archive', '>', 'databases/localhost/all'),
  289. shell=True,
  290. run_to_completion=False,
  291. working_directory=None,
  292. ).and_return(process).once()
  293. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  294. '/run/borgmatic',
  295. 'mongodb_databases',
  296. [
  297. module.borgmatic.actions.restore.Dump('mongodb_databases', 'all'),
  298. ],
  299. ).once()
  300. flexmock(module.borgmatic.hooks.data_source.config).should_receive('inject_pattern').with_args(
  301. object,
  302. module.borgmatic.borg.pattern.Pattern(
  303. '/run/borgmatic/mongodb_databases',
  304. source=module.borgmatic.borg.pattern.Pattern_source.HOOK,
  305. ),
  306. ).once()
  307. assert module.dump_data_sources(
  308. databases,
  309. {},
  310. config_paths=('test.yaml',),
  311. borgmatic_runtime_directory='/run/borgmatic',
  312. patterns=[],
  313. dry_run=False,
  314. ) == [process]
  315. def test_make_password_config_file_writes_password_to_pipe():
  316. read_file_descriptor = 99
  317. write_file_descriptor = flexmock()
  318. flexmock(module.os).should_receive('pipe').and_return(
  319. (read_file_descriptor, write_file_descriptor),
  320. )
  321. flexmock(module.os).should_receive('write').with_args(
  322. write_file_descriptor,
  323. b'password: trustsome1',
  324. ).once()
  325. flexmock(module.os).should_receive('close')
  326. flexmock(module.os).should_receive('set_inheritable')
  327. assert module.make_password_config_file('trustsome1') == '/dev/fd/99'
  328. def test_build_dump_command_with_username_injection_attack_gets_escaped():
  329. database = {'name': 'test', 'username': 'bob; naughty-command'}
  330. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  331. 'resolve_credential',
  332. ).replace_with(lambda value, config: value)
  333. command = module.build_dump_command(database, {}, dump_filename='test', dump_format='archive')
  334. assert "'bob; naughty-command'" in command
  335. def test_restore_data_source_dump_runs_mongorestore():
  336. hook_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
  337. extract_process = flexmock(stdout=flexmock())
  338. flexmock(module).should_receive('make_dump_path')
  339. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  340. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  341. 'resolve_credential',
  342. ).replace_with(lambda value, config: value)
  343. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  344. flexmock(module).should_receive('execute_command_with_processes').with_args(
  345. ['mongorestore', '--archive', '--drop'],
  346. processes=[extract_process],
  347. output_log_level=logging.DEBUG,
  348. input_file=extract_process.stdout,
  349. working_directory=None,
  350. ).once()
  351. module.restore_data_source_dump(
  352. hook_config,
  353. {},
  354. data_source={'name': 'foo'},
  355. dry_run=False,
  356. extract_process=extract_process,
  357. connection_params={
  358. 'hostname': None,
  359. 'port': None,
  360. 'username': None,
  361. 'password': None,
  362. },
  363. borgmatic_runtime_directory='/run/borgmatic',
  364. )
  365. def test_restore_data_source_dump_runs_mongorestore_with_hostname_and_port():
  366. hook_config = [
  367. {'name': 'foo', 'hostname': 'database.example.org', 'port': 27018, 'schemas': None},
  368. ]
  369. extract_process = flexmock(stdout=flexmock())
  370. flexmock(module).should_receive('make_dump_path')
  371. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  372. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  373. 'resolve_credential',
  374. ).replace_with(lambda value, config: value)
  375. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  376. flexmock(module).should_receive('execute_command_with_processes').with_args(
  377. [
  378. 'mongorestore',
  379. '--archive',
  380. '--drop',
  381. '--host',
  382. 'database.example.org',
  383. '--port',
  384. '27018',
  385. ],
  386. processes=[extract_process],
  387. output_log_level=logging.DEBUG,
  388. input_file=extract_process.stdout,
  389. working_directory=None,
  390. ).once()
  391. module.restore_data_source_dump(
  392. hook_config,
  393. {},
  394. data_source=hook_config[0],
  395. dry_run=False,
  396. extract_process=extract_process,
  397. connection_params={
  398. 'hostname': None,
  399. 'port': None,
  400. 'username': None,
  401. 'password': None,
  402. },
  403. borgmatic_runtime_directory='/run/borgmatic',
  404. )
  405. def test_restore_data_source_dump_runs_mongorestore_with_username_and_password():
  406. hook_config = [
  407. {
  408. 'name': 'foo',
  409. 'username': 'mongo',
  410. 'password': 'trustsome1',
  411. 'authentication_database': 'admin',
  412. 'schemas': None,
  413. },
  414. ]
  415. extract_process = flexmock(stdout=flexmock())
  416. flexmock(module).should_receive('make_dump_path')
  417. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  418. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  419. 'resolve_credential',
  420. ).replace_with(lambda value, config: value)
  421. flexmock(module).should_receive('make_password_config_file').with_args('trustsome1').and_return(
  422. '/dev/fd/99',
  423. )
  424. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  425. flexmock(module).should_receive('execute_command_with_processes').with_args(
  426. [
  427. 'mongorestore',
  428. '--archive',
  429. '--drop',
  430. '--username',
  431. 'mongo',
  432. '--config',
  433. '/dev/fd/99',
  434. '--authenticationDatabase',
  435. 'admin',
  436. ],
  437. processes=[extract_process],
  438. output_log_level=logging.DEBUG,
  439. input_file=extract_process.stdout,
  440. working_directory=None,
  441. ).once()
  442. module.restore_data_source_dump(
  443. hook_config,
  444. {},
  445. data_source=hook_config[0],
  446. dry_run=False,
  447. extract_process=extract_process,
  448. connection_params={
  449. 'hostname': None,
  450. 'port': None,
  451. 'username': None,
  452. 'password': None,
  453. },
  454. borgmatic_runtime_directory='/run/borgmatic',
  455. )
  456. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  457. hook_config = [
  458. {
  459. 'name': 'foo',
  460. 'username': 'mongo',
  461. 'password': 'trustsome1',
  462. 'authentication_database': 'admin',
  463. 'restore_hostname': 'restorehost',
  464. 'restore_port': 'restoreport',
  465. 'restore_username': 'restoreusername',
  466. 'restore_password': 'restorepassword',
  467. 'schemas': None,
  468. },
  469. ]
  470. extract_process = flexmock(stdout=flexmock())
  471. flexmock(module).should_receive('make_dump_path')
  472. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  473. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  474. 'resolve_credential',
  475. ).replace_with(lambda value, config: value)
  476. flexmock(module).should_receive('make_password_config_file').with_args(
  477. 'clipassword',
  478. ).and_return('/dev/fd/99')
  479. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  480. flexmock(module).should_receive('execute_command_with_processes').with_args(
  481. [
  482. 'mongorestore',
  483. '--archive',
  484. '--drop',
  485. '--host',
  486. 'clihost',
  487. '--port',
  488. 'cliport',
  489. '--username',
  490. 'cliusername',
  491. '--config',
  492. '/dev/fd/99',
  493. '--authenticationDatabase',
  494. 'admin',
  495. ],
  496. processes=[extract_process],
  497. output_log_level=logging.DEBUG,
  498. input_file=extract_process.stdout,
  499. working_directory=None,
  500. ).once()
  501. module.restore_data_source_dump(
  502. hook_config,
  503. {},
  504. data_source=hook_config[0],
  505. dry_run=False,
  506. extract_process=extract_process,
  507. connection_params={
  508. 'hostname': 'clihost',
  509. 'port': 'cliport',
  510. 'username': 'cliusername',
  511. 'password': 'clipassword',
  512. },
  513. borgmatic_runtime_directory='/run/borgmatic',
  514. )
  515. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  516. hook_config = [
  517. {
  518. 'name': 'foo',
  519. 'username': 'mongo',
  520. 'password': 'trustsome1',
  521. 'authentication_database': 'admin',
  522. 'schemas': None,
  523. 'restore_hostname': 'restorehost',
  524. 'restore_port': 'restoreport',
  525. 'restore_username': 'restoreuser',
  526. 'restore_password': 'restorepass',
  527. },
  528. ]
  529. extract_process = flexmock(stdout=flexmock())
  530. flexmock(module).should_receive('make_dump_path')
  531. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  532. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  533. 'resolve_credential',
  534. ).replace_with(lambda value, config: value)
  535. flexmock(module).should_receive('make_password_config_file').with_args(
  536. 'restorepass',
  537. ).and_return('/dev/fd/99')
  538. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  539. flexmock(module).should_receive('execute_command_with_processes').with_args(
  540. [
  541. 'mongorestore',
  542. '--archive',
  543. '--drop',
  544. '--host',
  545. 'restorehost',
  546. '--port',
  547. 'restoreport',
  548. '--username',
  549. 'restoreuser',
  550. '--config',
  551. '/dev/fd/99',
  552. '--authenticationDatabase',
  553. 'admin',
  554. ],
  555. processes=[extract_process],
  556. output_log_level=logging.DEBUG,
  557. input_file=extract_process.stdout,
  558. working_directory=None,
  559. ).once()
  560. module.restore_data_source_dump(
  561. hook_config,
  562. {},
  563. data_source=hook_config[0],
  564. dry_run=False,
  565. extract_process=extract_process,
  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_runs_mongorestore_with_options():
  575. hook_config = [{'name': 'foo', 'restore_options': '--harder', 'schemas': None}]
  576. extract_process = flexmock(stdout=flexmock())
  577. flexmock(module).should_receive('make_dump_path')
  578. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  579. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  580. 'resolve_credential',
  581. ).replace_with(lambda value, config: value)
  582. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  583. flexmock(module).should_receive('execute_command_with_processes').with_args(
  584. ['mongorestore', '--archive', '--drop', '--harder'],
  585. processes=[extract_process],
  586. output_log_level=logging.DEBUG,
  587. input_file=extract_process.stdout,
  588. working_directory=None,
  589. ).once()
  590. module.restore_data_source_dump(
  591. hook_config,
  592. {},
  593. data_source=hook_config[0],
  594. dry_run=False,
  595. extract_process=extract_process,
  596. connection_params={
  597. 'hostname': None,
  598. 'port': None,
  599. 'username': None,
  600. 'password': None,
  601. },
  602. borgmatic_runtime_directory='/run/borgmatic',
  603. )
  604. def test_restore_databases_dump_runs_mongorestore_with_schemas():
  605. hook_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
  606. extract_process = flexmock(stdout=flexmock())
  607. flexmock(module).should_receive('make_dump_path')
  608. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  609. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  610. 'resolve_credential',
  611. ).replace_with(lambda value, config: value)
  612. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  613. flexmock(module).should_receive('execute_command_with_processes').with_args(
  614. [
  615. 'mongorestore',
  616. '--archive',
  617. '--drop',
  618. '--nsInclude',
  619. 'bar',
  620. '--nsInclude',
  621. 'baz',
  622. ],
  623. processes=[extract_process],
  624. output_log_level=logging.DEBUG,
  625. input_file=extract_process.stdout,
  626. working_directory=None,
  627. ).once()
  628. module.restore_data_source_dump(
  629. hook_config,
  630. {},
  631. data_source=hook_config[0],
  632. dry_run=False,
  633. extract_process=extract_process,
  634. connection_params={
  635. 'hostname': None,
  636. 'port': None,
  637. 'username': None,
  638. 'password': None,
  639. },
  640. borgmatic_runtime_directory='/run/borgmatic',
  641. )
  642. def test_restore_data_source_dump_runs_psql_for_all_database_dump():
  643. hook_config = [{'name': 'all', 'schemas': None}]
  644. extract_process = flexmock(stdout=flexmock())
  645. flexmock(module).should_receive('make_dump_path')
  646. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  647. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  648. 'resolve_credential',
  649. ).replace_with(lambda value, config: value)
  650. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  651. flexmock(module).should_receive('execute_command_with_processes').with_args(
  652. ['mongorestore', '--archive'],
  653. processes=[extract_process],
  654. output_log_level=logging.DEBUG,
  655. input_file=extract_process.stdout,
  656. working_directory=None,
  657. ).once()
  658. module.restore_data_source_dump(
  659. hook_config,
  660. {},
  661. data_source=hook_config[0],
  662. dry_run=False,
  663. extract_process=extract_process,
  664. connection_params={
  665. 'hostname': None,
  666. 'port': None,
  667. 'username': None,
  668. 'password': None,
  669. },
  670. borgmatic_runtime_directory='/run/borgmatic',
  671. )
  672. def test_restore_data_source_dump_with_dry_run_skips_restore():
  673. hook_config = [{'name': 'foo', 'schemas': None}]
  674. flexmock(module).should_receive('make_dump_path')
  675. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  676. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  677. 'resolve_credential',
  678. ).replace_with(lambda value, config: value)
  679. flexmock(module).should_receive('execute_command_with_processes').never()
  680. module.restore_data_source_dump(
  681. hook_config,
  682. {},
  683. data_source={'name': 'foo'},
  684. dry_run=True,
  685. extract_process=flexmock(),
  686. connection_params={
  687. 'hostname': None,
  688. 'port': None,
  689. 'username': None,
  690. 'password': None,
  691. },
  692. borgmatic_runtime_directory='/run/borgmatic',
  693. )
  694. def test_restore_data_source_dump_without_extract_process_restores_from_disk():
  695. hook_config = [{'name': 'foo', 'format': 'directory', 'schemas': None}]
  696. flexmock(module).should_receive('make_dump_path')
  697. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  698. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  699. 'resolve_credential',
  700. ).replace_with(lambda value, config: value)
  701. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  702. flexmock(module).should_receive('execute_command_with_processes').with_args(
  703. ['mongorestore', '--dir', '/dump/path', '--drop'],
  704. processes=[],
  705. output_log_level=logging.DEBUG,
  706. input_file=None,
  707. working_directory=None,
  708. ).once()
  709. module.restore_data_source_dump(
  710. hook_config,
  711. {},
  712. data_source={'name': 'foo'},
  713. dry_run=False,
  714. extract_process=None,
  715. connection_params={
  716. 'hostname': None,
  717. 'port': None,
  718. 'username': None,
  719. 'password': None,
  720. },
  721. borgmatic_runtime_directory='/run/borgmatic',
  722. )
  723. def test_dump_data_sources_uses_custom_mongodump_command():
  724. flexmock(module.borgmatic.hooks.command).should_receive('Before_after_hooks').and_return(
  725. flexmock(),
  726. )
  727. databases = [{'name': 'foo', 'mongodump_command': 'custom_mongodump'}]
  728. process = flexmock()
  729. flexmock(module).should_receive('make_dump_path').and_return('')
  730. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  731. 'databases/localhost/foo',
  732. )
  733. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  734. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  735. flexmock(module).should_receive('execute_command').with_args(
  736. (
  737. 'custom_mongodump',
  738. '--db',
  739. 'foo',
  740. '--archive',
  741. '>',
  742. 'databases/localhost/foo',
  743. ),
  744. shell=True,
  745. run_to_completion=False,
  746. working_directory=None,
  747. ).and_return(process).once()
  748. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  749. '/run/borgmatic',
  750. 'mongodb_databases',
  751. [
  752. module.borgmatic.actions.restore.Dump('mongodb_databases', 'foo'),
  753. ],
  754. ).once()
  755. assert module.dump_data_sources(
  756. databases,
  757. {},
  758. config_paths=('test.yaml',),
  759. borgmatic_runtime_directory='/run/borgmatic',
  760. patterns=[],
  761. dry_run=False,
  762. ) == [process]
  763. def test_build_dump_command_prevents_shell_injection():
  764. database = {
  765. 'name': 'testdb; rm -rf /', # Malicious input
  766. 'hostname': 'localhost',
  767. 'port': 27017,
  768. 'username': 'user',
  769. 'password': 'password',
  770. 'mongodump_command': 'mongodump',
  771. 'options': '--gzip',
  772. }
  773. config = {}
  774. dump_filename = '/path/to/dump'
  775. dump_format = 'archive'
  776. command = module.build_dump_command(database, config, dump_filename, dump_format)
  777. # Ensure the malicious input is properly escaped and does not execute
  778. assert 'testdb; rm -rf /' not in command
  779. assert any(
  780. 'testdb' in part for part in command
  781. ) # Check if 'testdb' is in any part of the tuple
  782. def test_restore_data_source_dump_uses_custom_mongorestore_command():
  783. hook_config = [
  784. {
  785. 'name': 'foo',
  786. 'mongorestore_command': 'custom_mongorestore',
  787. 'schemas': None,
  788. 'restore_options': '--gzip',
  789. },
  790. ]
  791. extract_process = flexmock(stdout=flexmock())
  792. flexmock(module).should_receive('make_dump_path')
  793. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  794. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  795. 'resolve_credential',
  796. ).replace_with(lambda value, config: value)
  797. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  798. flexmock(module).should_receive('execute_command_with_processes').with_args(
  799. [
  800. 'custom_mongorestore', # Should use custom command instead of default
  801. '--archive',
  802. '--drop',
  803. '--gzip', # Should include restore options
  804. ],
  805. processes=[extract_process],
  806. output_log_level=logging.DEBUG,
  807. input_file=extract_process.stdout,
  808. working_directory=None,
  809. ).once()
  810. module.restore_data_source_dump(
  811. hook_config,
  812. {},
  813. data_source=hook_config[0],
  814. dry_run=False,
  815. extract_process=extract_process,
  816. connection_params={
  817. 'hostname': None,
  818. 'port': None,
  819. 'username': None,
  820. 'password': None,
  821. },
  822. borgmatic_runtime_directory='/run/borgmatic',
  823. )
  824. def test_build_restore_command_prevents_shell_injection():
  825. database = {
  826. 'name': 'testdb; rm -rf /', # Malicious input
  827. 'restore_hostname': 'localhost',
  828. 'restore_port': 27017,
  829. 'restore_username': 'user',
  830. 'restore_password': 'password',
  831. 'mongorestore_command': 'mongorestore',
  832. 'restore_options': '--gzip',
  833. }
  834. config = {}
  835. dump_filename = '/path/to/dump'
  836. connection_params = {
  837. 'hostname': None,
  838. 'port': None,
  839. 'username': None,
  840. 'password': None,
  841. }
  842. extract_process = None
  843. command = module.build_restore_command(
  844. extract_process,
  845. database,
  846. config,
  847. dump_filename,
  848. connection_params,
  849. )
  850. # Ensure the malicious input is properly escaped and does not execute
  851. assert 'rm -rf /' not in command
  852. assert ';' not in command