test_systemd.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import io
  2. import sys
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.hooks.credential import systemd as module
  6. @pytest.mark.parametrize('credential_parameters', ((), ('foo', 'bar')))
  7. def test_load_credential_with_invalid_credential_parameters_raises(credential_parameters):
  8. flexmock(module.os.environ).should_receive('get').never()
  9. with pytest.raises(ValueError):
  10. module.load_credential(
  11. hook_config={},
  12. config={},
  13. credential_parameters=credential_parameters,
  14. )
  15. def test_load_credential_without_credentials_directory_falls_back_to_systemd_creds_command():
  16. flexmock(module.os.environ).should_receive('get').with_args('CREDENTIALS_DIRECTORY').and_return(
  17. None,
  18. )
  19. flexmock(module.borgmatic.execute).should_receive(
  20. 'execute_command_and_capture_output'
  21. ).with_args(('systemd-creds', 'decrypt', '/etc/credstore.encrypted/mycredential')).and_return(
  22. 'password'
  23. ).once()
  24. assert (
  25. module.load_credential(hook_config={}, config={}, credential_parameters=('mycredential',))
  26. == 'password'
  27. )
  28. def test_load_credential_without_credentials_directory_calls_custom_systemd_creds_command():
  29. flexmock(module.os.environ).should_receive('get').with_args('CREDENTIALS_DIRECTORY').and_return(
  30. None,
  31. )
  32. flexmock(module.borgmatic.execute).should_receive(
  33. 'execute_command_and_capture_output'
  34. ).with_args(
  35. ('/path/to/systemd-creds', '--flag', 'decrypt', '/etc/credstore.encrypted/mycredential')
  36. ).and_return('password').once()
  37. assert (
  38. module.load_credential(
  39. hook_config={'systemd_creds_command': '/path/to/systemd-creds --flag'},
  40. config={},
  41. credential_parameters=('mycredential',),
  42. )
  43. == 'password'
  44. )
  45. def test_load_credential_without_credentials_directory_uses_custom_encrypted_credentials_directory():
  46. flexmock(module.os.environ).should_receive('get').with_args('CREDENTIALS_DIRECTORY').and_return(
  47. None,
  48. )
  49. flexmock(module.borgmatic.execute).should_receive(
  50. 'execute_command_and_capture_output'
  51. ).with_args(('systemd-creds', 'decrypt', '/my/credstore.encrypted/mycredential')).and_return(
  52. 'password'
  53. ).once()
  54. assert (
  55. module.load_credential(
  56. hook_config={'encrypted_credentials_directory': '/my/credstore.encrypted'},
  57. config={},
  58. credential_parameters=('mycredential',),
  59. )
  60. == 'password'
  61. )
  62. def test_load_credential_with_invalid_credential_name_raises():
  63. flexmock(module.os.environ).should_receive('get').with_args('CREDENTIALS_DIRECTORY').and_return(
  64. '/var',
  65. )
  66. with pytest.raises(ValueError):
  67. module.load_credential(
  68. hook_config={},
  69. config={},
  70. credential_parameters=('../../my!@#$credential',),
  71. )
  72. def test_load_credential_reads_named_credential_from_file():
  73. flexmock(module.os.environ).should_receive('get').with_args('CREDENTIALS_DIRECTORY').and_return(
  74. '/var',
  75. )
  76. credential_stream = io.StringIO('password')
  77. credential_stream.name = '/var/borgmatic.pw'
  78. builtins = flexmock(sys.modules['builtins'])
  79. builtins.should_receive('open').with_args('/var/borgmatic.pw', encoding='utf-8').and_return(
  80. credential_stream
  81. )
  82. assert (
  83. module.load_credential(hook_config={}, config={}, credential_parameters=('borgmatic.pw',))
  84. == 'password'
  85. )
  86. def test_load_credential_with_file_not_found_error_raises():
  87. flexmock(module.os.environ).should_receive('get').with_args('CREDENTIALS_DIRECTORY').and_return(
  88. '/var',
  89. )
  90. builtins = flexmock(sys.modules['builtins'])
  91. builtins.should_receive('open').with_args('/var/mycredential', encoding='utf-8').and_raise(
  92. FileNotFoundError
  93. )
  94. with pytest.raises(ValueError):
  95. module.load_credential(hook_config={}, config={}, credential_parameters=('mycredential',))