MEschenbacher's Wireguard Saltstack Formula
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

134 lines
4.6KB

  1. __virtualname__ = 'wg'
  2. def __virtual__():
  3. if 'wg.show' in __salt__:
  4. return __virtualname__
  5. return False
  6. def present(name, listen_port=None, fwmark=None, private_key=None):
  7. """
  8. Make sure a wireguard interface exists.
  9. """
  10. ret = dict(name=name, changes=dict(), result=False, comment=None)
  11. show = __salt__['wg.show'](name, hide_keys=False)
  12. if not show:
  13. __salt__['wg.create'](name)
  14. ret['changes'][name] = 'Interface created.'
  15. show = __salt__['wg.show'](name, hide_keys=False)
  16. if listen_port and int(show.get('listening port', 0)) != int(listen_port):
  17. __salt__['wg.set'](name, listen_port=listen_port)
  18. ret['changes']['listening port'] = dict(
  19. old=show.get('listening port', 0),
  20. new=listen_port,
  21. )
  22. if show.get('fwmark', None) != fwmark:
  23. __salt__['wg.set'](name, fwmark=fwmark)
  24. ret['changes']['fwmark'] = dict(
  25. old=show.get('fwmark', None),
  26. new=fwmark,
  27. )
  28. if show.get('private key') != private_key:
  29. __salt__['wg.set'](name, private_key=private_key)
  30. ret['changes']['private key'] = 'private key changed.'
  31. ret['result'] = True
  32. return ret
  33. def absent(name):
  34. """
  35. Make sure a wireguard interface is absent.
  36. """
  37. ret = dict(name=name, changes=dict(), result=False, comment=None)
  38. interface = __salt__['wg.show'](name)
  39. if not interface:
  40. ret['comment'] = 'Interface %s already absent.' % (name,)
  41. ret['result'] = True
  42. return ret
  43. __salt__['wg.delete'](name)
  44. ret['changes'][name] = dict(old=name, new=None)
  45. ret['result'] = True
  46. return ret
  47. def peer_present(name, interface, endpoint=None, persistent_keepalive=None,
  48. allowed_ips=None, preshared_key=None):
  49. ret = dict(name=name, changes=dict(), result=False, comment=None)
  50. show = __salt__['wg.show'](interface, hide_keys=False)
  51. if not show:
  52. ret['comment'] = 'Interface %s does not exist.' % (interface)
  53. return ret
  54. show = __salt__['wg.show'](name=interface, peer=name, hide_keys=False)
  55. if not show:
  56. __salt__['wg.set'](interface, peer=name, endpoint=endpoint,
  57. persistent_keepalive=persistent_keepalive,
  58. allowed_ips=','.join(allowed_ips), preshared_key=preshared_key)
  59. ret['changes'][name] = 'Peer created.'
  60. ret['result'] = True
  61. return ret
  62. if endpoint and show.get('endpoint', '') != endpoint:
  63. __salt__['wg.set'](interface, peer=name, endpoint=endpoint)
  64. updated_show = __salt__['wg.show'](name=interface, peer=name)
  65. if updated_show.get('endpoint') != show.get('endpoint'):
  66. ret['changes']['endpoint'] = dict(
  67. old=show.get('endpoint'), new=updated_show.get('endpoint'))
  68. if persistent_keepalive and not show.get('persistent keepalive', '').startswith('every %s second' % (persistent_keepalive,)):
  69. __salt__['wg.set'](interface, peer=name,
  70. persistent_keepalive=persistent_keepalive)
  71. ret['changes']['persistent keepalive'] = 'persistent keepalive changed.'
  72. elif not persistent_keepalive and show.get('persistent keepalive'):
  73. __salt__['wg.set'](interface, peer=name, persistent_keepalive=0)
  74. ret['changes']['persistent keepalive'] = 'persistent keepalive removed.'
  75. if sorted(show.get('allowed ips')) != sorted(allowed_ips):
  76. __salt__['wg.set'](interface, peer=name, allowed_ips=','.join(allowed_ips))
  77. ret['changes']['allowed ips'] = dict(new=allowed_ips, old=show.get('allowed ips'))
  78. print(show.get('preshared key'), preshared_key)
  79. if preshared_key and show.get('preshared key') != preshared_key:
  80. __salt__['wg.set'](interface, peer=name, preshared_key=preshared_key)
  81. ret['changes']['preshared key'] = 'preshared key changed.'
  82. elif show.get('preshared key') and not preshared_key:
  83. __salt__['wg.set'](interface, peer=name, preshared_key='')
  84. ret['changes']['preshared key'] = 'preshared key deleted.'
  85. ret['result'] = True
  86. return ret
  87. def peer_absent(name, interface):
  88. ret = dict(name=name, changes=dict(), result=False, comment=None)
  89. show = __salt__['wg.show'](interface)
  90. if not show:
  91. ret['comment'] = 'Interface %s does not exist.' % (interface)
  92. return ret
  93. show = __salt__['wg.show'](name=interface, peer=name)
  94. if not show:
  95. ret['comment'] = 'Peer %s already absent.' % (name)
  96. ret['result'] = True
  97. return ret
  98. __salt__['wg.set'](interface, peer=name, remove=True)
  99. ret['changes'][name] = dict(old=name, new=None)
  100. ret['result'] = True
  101. return ret