test_perms.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """Test perms"""
  2. import os
  3. import warnings
  4. import pytest
  5. @pytest.mark.parametrize('autoperms', ['notest', 'unset', 'true', 'false'])
  6. @pytest.mark.usefixtures('ds1_copy')
  7. def test_perms(runner, yadm_y, paths, ds1, autoperms):
  8. """Test perms"""
  9. # set the value of auto-perms
  10. if autoperms != 'notest':
  11. if autoperms != 'unset':
  12. os.system(' '.join(yadm_y('config', 'yadm.auto-perms', autoperms)))
  13. # privatepaths will hold all paths that should become secured
  14. privatepaths = [paths.work.join('.ssh'), paths.work.join('.gnupg')]
  15. privatepaths += [paths.work.join(private.path) for private in ds1.private]
  16. # create an archive file
  17. os.system(f'touch "{str(paths.archive)}"')
  18. privatepaths.append(paths.archive)
  19. # create encrypted file test data
  20. efile1 = paths.work.join('efile1')
  21. efile1.write('efile1')
  22. efile2 = paths.work.join('efile2')
  23. efile2.write('efile2')
  24. paths.encrypt.write('efile1\nefile2\n!efile1\n')
  25. insecurepaths = [efile1]
  26. privatepaths.append(efile2)
  27. # assert these paths begin unsecured
  28. for private in privatepaths + insecurepaths:
  29. assert not oct(private.stat().mode).endswith('00'), (
  30. 'Path started secured')
  31. cmd = 'perms'
  32. if autoperms != 'notest':
  33. cmd = 'status'
  34. run = runner(yadm_y(cmd))
  35. assert run.success
  36. assert run.err == ''
  37. if cmd == 'perms':
  38. assert run.out == ''
  39. # these paths should be secured if processing perms
  40. for private in privatepaths:
  41. if '.p2' in private.basename or '.p4' in private.basename:
  42. # Dot files within .ssh/.gnupg are not protected.
  43. # This is a but which must be fixed
  44. warnings.warn('Unhandled bug: private dot files', Warning)
  45. continue
  46. if autoperms == 'false':
  47. assert not oct(private.stat().mode).endswith('00'), (
  48. 'Path should not be secured')
  49. else:
  50. assert oct(private.stat().mode).endswith('00'), (
  51. 'Path has not been secured')
  52. # these paths should never be secured
  53. for private in insecurepaths:
  54. assert not oct(private.stat().mode).endswith('00'), (
  55. 'Path should not be secured')
  56. @pytest.mark.parametrize('sshperms', [None, 'true', 'false'])
  57. @pytest.mark.parametrize('gpgperms', [None, 'true', 'false'])
  58. @pytest.mark.usefixtures('ds1_copy')
  59. def test_perms_control(runner, yadm_y, paths, ds1, sshperms, gpgperms):
  60. """Test fine control of perms"""
  61. # set the value of ssh-perms
  62. if sshperms:
  63. os.system(' '.join(yadm_y('config', 'yadm.ssh-perms', sshperms)))
  64. # set the value of gpg-perms
  65. if gpgperms:
  66. os.system(' '.join(yadm_y('config', 'yadm.gpg-perms', gpgperms)))
  67. # privatepaths will hold all paths that should become secured
  68. privatepaths = [paths.work.join('.ssh'), paths.work.join('.gnupg')]
  69. privatepaths += [paths.work.join(private.path) for private in ds1.private]
  70. # assert these paths begin unsecured
  71. for private in privatepaths:
  72. assert not oct(private.stat().mode).endswith('00'), (
  73. 'Path started secured')
  74. run = runner(yadm_y('perms'))
  75. assert run.success
  76. assert run.err == ''
  77. assert run.out == ''
  78. # these paths should be secured if processing perms
  79. for private in privatepaths:
  80. if '.p2' in private.basename or '.p4' in private.basename:
  81. # Dot files within .ssh/.gnupg are not protected.
  82. # This is a but which must be fixed
  83. warnings.warn('Unhandled bug: private dot files', Warning)
  84. continue
  85. if (
  86. (sshperms == 'false' and 'ssh' in str(private))
  87. or
  88. (gpgperms == 'false' and 'gnupg' in str(private))
  89. ):
  90. assert not oct(private.stat().mode).endswith('00'), (
  91. 'Path should not be secured')
  92. else:
  93. assert oct(private.stat().mode).endswith('00'), (
  94. 'Path has not been secured')