test_keepassxc.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.hooks.credential import keepassxc as module
  4. @pytest.mark.parametrize('credential_parameters', ((), ('foo',), ('foo', 'bar', 'baz')))
  5. def test_load_credential_with_invalid_credential_parameters_raises(credential_parameters):
  6. flexmock(module.borgmatic.execute).should_receive('execute_command_and_capture_output').never()
  7. with pytest.raises(ValueError):
  8. module.load_credential(
  9. hook_config={}, config={}, credential_parameters=credential_parameters
  10. )
  11. def test_load_credential_with_missing_database_raises():
  12. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  13. 'database.kdbx'
  14. )
  15. flexmock(module.os.path).should_receive('exists').and_return(False)
  16. flexmock(module.borgmatic.execute).should_receive('execute_command_and_capture_output').never()
  17. with pytest.raises(ValueError):
  18. module.load_credential(
  19. hook_config={}, config={}, credential_parameters=('database.kdbx', 'mypassword')
  20. )
  21. def test_load_credential_with_present_database_fetches_password_from_keepassxc():
  22. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  23. 'database.kdbx'
  24. )
  25. flexmock(module.os.path).should_receive('exists').and_return(True)
  26. flexmock(module.borgmatic.execute).should_receive(
  27. 'execute_command_and_capture_output'
  28. ).with_args(
  29. (
  30. 'keepassxc-cli',
  31. 'show',
  32. '--show-protected',
  33. '--attributes',
  34. 'Password',
  35. 'database.kdbx',
  36. 'mypassword',
  37. )
  38. ).and_return(
  39. 'password'
  40. ).once()
  41. assert (
  42. module.load_credential(
  43. hook_config={}, config={}, credential_parameters=('database.kdbx', 'mypassword')
  44. )
  45. == 'password'
  46. )
  47. def test_load_credential_with_custom_keepassxc_cli_command_calls_it():
  48. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  49. 'database.kdbx'
  50. )
  51. config = {'keepassxc': {'keepassxc_cli_command': '/usr/local/bin/keepassxc-cli --some-option'}}
  52. flexmock(module.os.path).should_receive('exists').and_return(True)
  53. flexmock(module.borgmatic.execute).should_receive(
  54. 'execute_command_and_capture_output'
  55. ).with_args(
  56. (
  57. '/usr/local/bin/keepassxc-cli',
  58. '--some-option',
  59. 'show',
  60. '--show-protected',
  61. '--attributes',
  62. 'Password',
  63. 'database.kdbx',
  64. 'mypassword',
  65. )
  66. ).and_return(
  67. 'password'
  68. ).once()
  69. assert (
  70. module.load_credential(
  71. hook_config=config['keepassxc'],
  72. config=config,
  73. credential_parameters=('database.kdbx', 'mypassword'),
  74. )
  75. == 'password'
  76. )
  77. def test_load_credential_with_expanded_directory_with_present_database_fetches_password_from_keepassxc():
  78. flexmock(module.os.path).should_receive('expanduser').with_args('~/database.kdbx').and_return(
  79. '/root/database.kdbx'
  80. )
  81. flexmock(module.os.path).should_receive('exists').and_return(True)
  82. flexmock(module.borgmatic.execute).should_receive(
  83. 'execute_command_and_capture_output'
  84. ).with_args(
  85. (
  86. 'keepassxc-cli',
  87. 'show',
  88. '--show-protected',
  89. '--attributes',
  90. 'Password',
  91. '/root/database.kdbx',
  92. 'mypassword',
  93. )
  94. ).and_return(
  95. 'password'
  96. ).once()
  97. assert (
  98. module.load_credential(
  99. hook_config={}, config={}, credential_parameters=('~/database.kdbx', 'mypassword')
  100. )
  101. == 'password'
  102. )
  103. def test_load_credential_with_key_file():
  104. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  105. 'database.kdbx'
  106. )
  107. flexmock(module.os.path).should_receive('exists').and_return(True)
  108. flexmock(module.borgmatic.execute).should_receive(
  109. 'execute_command_and_capture_output'
  110. ).with_args(
  111. (
  112. 'keepassxc-cli',
  113. 'show',
  114. '--show-protected',
  115. '--attributes',
  116. 'Password',
  117. '--key-file',
  118. '/path/to/keyfile',
  119. 'database.kdbx',
  120. 'mypassword',
  121. )
  122. ).and_return(
  123. 'password'
  124. ).once()
  125. assert (
  126. module.load_credential(
  127. hook_config={'key_file': '/path/to/keyfile'},
  128. config={},
  129. credential_parameters=('database.kdbx', 'mypassword'),
  130. )
  131. == 'password'
  132. )
  133. def test_load_credential_with_yubikey():
  134. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  135. 'database.kdbx'
  136. )
  137. flexmock(module.os.path).should_receive('exists').and_return(True)
  138. flexmock(module.borgmatic.execute).should_receive(
  139. 'execute_command_and_capture_output'
  140. ).with_args(
  141. (
  142. 'keepassxc-cli',
  143. 'show',
  144. '--show-protected',
  145. '--attributes',
  146. 'Password',
  147. '--yubikey',
  148. '1:7370001',
  149. 'database.kdbx',
  150. 'mypassword',
  151. )
  152. ).and_return(
  153. 'password'
  154. ).once()
  155. assert (
  156. module.load_credential(
  157. hook_config={'yubikey': '1:7370001'},
  158. config={},
  159. credential_parameters=('database.kdbx', 'mypassword'),
  160. )
  161. == 'password'
  162. )
  163. def test_load_credential_with_key_file_and_yubikey():
  164. flexmock(module.os.path).should_receive('expanduser').with_args('database.kdbx').and_return(
  165. 'database.kdbx'
  166. )
  167. flexmock(module.os.path).should_receive('exists').and_return(True)
  168. flexmock(module.borgmatic.execute).should_receive(
  169. 'execute_command_and_capture_output'
  170. ).with_args(
  171. (
  172. 'keepassxc-cli',
  173. 'show',
  174. '--show-protected',
  175. '--attributes',
  176. 'Password',
  177. '--key-file',
  178. '/path/to/keyfile',
  179. '--yubikey',
  180. '2',
  181. 'database.kdbx',
  182. 'mypassword',
  183. )
  184. ).and_return(
  185. 'password'
  186. ).once()
  187. assert (
  188. module.load_credential(
  189. hook_config={'key_file': '/path/to/keyfile', 'yubikey': '2'},
  190. config={},
  191. credential_parameters=('database.kdbx', 'mypassword'),
  192. )
  193. == 'password'
  194. )