MEschenbacher's Wireguard Saltstack Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wireguard.py 4.2KB

6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
6 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import yaml
  2. import os
  3. from tempfile import mkstemp
  4. __virtualname__ = 'wg'
  5. def __virtual__():
  6. # do checks for startup
  7. return __virtualname__
  8. def create(name):
  9. """
  10. create a wireguard interface. This will fail if it already exists.
  11. """
  12. __salt__['cmd.run']('ip link add %s type wireguard' % (name,))
  13. return show(name)
  14. def delete(name):
  15. """
  16. delete a wireguard interface. This will fail if it does not exist.
  17. """
  18. return __salt__['cmd.run']('ip link del %s type wireguard' % (name,))
  19. def show(name=None, peer=None):
  20. if peer and not name:
  21. return 'If peer is given, name must also be given'
  22. if not name:
  23. return _wg_ifaces()
  24. elif peer:
  25. return _wg_ifaces().get(name).get('peers').get(peer)
  26. else:
  27. return _wg_ifaces().get(name)
  28. def showconf(name, hide_keys=True):
  29. return __salt__['cmd.run']('wg showconf %s' % (name,),
  30. env={'WG_HIDE_KEYS': 'always' if hide_keys else 'never'})
  31. def set(name, listen_port=None, fwmark=None, private_key=None, peer=None,
  32. preshared_key=None, endpoint=None, persistent_keepalive=None,
  33. allowed_ips=None, remove=False):
  34. s = 'wg set %s' % (name,)
  35. if remove:
  36. if not peer:
  37. return 'If remove is given, peer must also be given'
  38. return __salt__['cmd.run'](
  39. '%s peer %s remove' % (s, peer)
  40. )
  41. if listen_port:
  42. s = '%s listen-port %s' % (s, listen_port)
  43. if fwmark:
  44. s = '%s fwmark %s' % (s, fwmark)
  45. if private_key:
  46. fd, filename = mkstemp(text=True)
  47. with open(filename, 'w') as f:
  48. f.write(private_key)
  49. os.close(fd)
  50. s = '%s private-key %s' % (s, filename)
  51. if peer:
  52. s = '%s peer %s' % (s, peer)
  53. if preshared_key:
  54. if not peer:
  55. return 'If preshared_key is given, peer must also be given'
  56. fd2, filename2 = mkstemp(text=True)
  57. with open(filename2, 'w') as f:
  58. f.write(preshared_key)
  59. os.close(fd2)
  60. s = '%s preshared-key %s' % (s, filename2)
  61. if endpoint:
  62. if not peer:
  63. return 'If endpoint is given, peer must also be given'
  64. s = '%s endpoint %s' % (s, endpoint)
  65. if persistent_keepalive:
  66. if not peer:
  67. return 'If persistent_keepalive is given, peer must also be given'
  68. s = '%s persistent-keepalive %s' % (s, persistent_keepalive)
  69. if allowed_ips:
  70. if not peer:
  71. return 'If allowed_ips is given, peer must also be given'
  72. s = '%s allowed-ips %s' % (s, allowed_ips)
  73. r = __salt__['cmd.run'](s)
  74. if private_key:
  75. os.unlink(filename)
  76. if preshared_key:
  77. os.unlink(filename2)
  78. return r
  79. def remove_peer(name, peer):
  80. return __salt__['cmd.run'](
  81. 'wg set %s peer %s remove' % (name, peer)
  82. )
  83. # def add_peer(name, public_key, allowed_ips=None):
  84. # base = 'wg set %s peer %s' % (name, peer)
  85. #
  86. # return __salt__['cmd.run'](
  87. # )
  88. def genkey():
  89. return __salt__['cmd.run']('wg genkey')
  90. def genpsk():
  91. return __salt__['cmd.run']('wg genpsk')
  92. def setconf(name, path):
  93. return __salt__['cmd.run']('wg setconf %s %s' % (name, path))
  94. def addconf(name, path):
  95. return __salt__['cmd.run']('wg addconf %s %s' % (name, path))
  96. def _wg_ifaces():
  97. """
  98. Parse output from 'wg show'
  99. """
  100. # from https://github.com/saltstack/salt/blob/develop/salt/modules/linux_ip.py
  101. tmp = dict()
  102. tmpiface = dict()
  103. ifaces = dict()
  104. out = __salt__['cmd.run']('wg', env={'WG_HIDE_KEYS': 'never'})
  105. for line in out.splitlines():
  106. if line.startswith('interface: '):
  107. k, v = _wg_splitline(line)
  108. ifaces[v] = dict(peers=dict())
  109. tmpiface = ifaces[v]
  110. tmp = tmpiface
  111. elif line.startswith('peer: '):
  112. k, v = _wg_splitline(line)
  113. tmpiface['peers'][v] = dict()
  114. tmp = tmpiface['peers'][v]
  115. elif line == '':
  116. continue
  117. k, v = _wg_splitline(line)
  118. if k == 'allowed ips':
  119. tmp[k] = [ s.strip() for s in v.split(',') ]
  120. else:
  121. tmp[k] = v
  122. return ifaces
  123. def _wg_splitline(line):
  124. parts = line.split(':', 1)
  125. return parts[0].strip(), parts[1].strip()