/networks/networks_update_vpn.py

https://gitlab.com/jnoennig/burpy · Python · 146 lines · 84 code · 11 blank · 51 comment · 24 complexity · 9c19ca1cb50f5f31e9278b134556044f MD5 · raw file

  1. #!/usr/bin/env python
  2. #encoding: utf-8
  3. """
  4. Author Name: Jason Noennig
  5. Author Email: jnoennig@protonmail.com
  6. Copyright 2016 Jason Noennig
  7. This file is part of burpy.
  8. burpy is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 3 of the License, or
  11. (at your option) any later version.
  12. burpy is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with burpy. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. import json
  20. import re
  21. import requests
  22. from burpy import my_headers
  23. def _val_mode(mode):
  24. """
  25. _val_mode validates the mode is legitimate.
  26. """
  27. if bool(mode) is True:
  28. regex_mode = re.compile('hub|spoke|none')
  29. mode = mode.lower()
  30. match_mode = regex_mode.search(mode)
  31. if match_mode:
  32. return {"mode": mode}
  33. else:
  34. print "Verify you have used 'hub', 'spoke', or 'none' as your vpn mode."
  35. return 1
  36. else:
  37. return ''
  38. def _val_hubs(hubs):
  39. """
  40. _val_hubs validates the hubs.
  41. """
  42. if bool(hubs) is True:
  43. if isinstance(hubs, list):
  44. return {'hubs': hub_lst}
  45. else:
  46. print "hubs needs to be a list."
  47. return 1
  48. else:
  49. return ''
  50. def _val_sub(subnets):
  51. """
  52. _val_sub validates the subnets.
  53. """
  54. if bool(subnets) is True:
  55. if isinstance(subnets, list):
  56. subnet_lst = []
  57. regex_subnet = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}$")
  58. for subnet in subnets:
  59. match_sub = regex_subnet.search(subnet['localSubnet'])
  60. if match_sub is True:
  61. if subnet['useVpn'] is True or subnet['useVpn'] is False:
  62. subnet_lst.append(subnet)
  63. else:
  64. print "'useVpn' needs to be a boolean (True or False)."
  65. return 1
  66. else:
  67. print "Verify the subnet is in the correct format."
  68. return 1
  69. return {"subnets": subnet_lst}
  70. else:
  71. print "subnets needs to be a list."
  72. return 1
  73. else:
  74. return ''
  75. def _data_join(mode, hubs, subnets):
  76. """
  77. _data_join joins mode and subnets into a string.
  78. """
  79. mode = _val_mode(mode)
  80. subnets = _val_sub(subnets)
  81. payload = {}
  82. if bool(mode) is True:
  83. if bool(hubs) is True or bool(subnets) is True:
  84. if bool(mode) is True:
  85. payload.update(mode)
  86. if bool(subnets) is True:
  87. payload.update(subnets)
  88. return payload
  89. else:
  90. print "Verify parameters."
  91. return 1
  92. def UpdateVPN(key='', net_id='', mode='', hubs=None, subnets=None):
  93. """UpdateVPN updates the site-to-site VPN config of the specified network.
  94. ***Only valid for MX networks***
  95. Arguments:
  96. param arg1: key - Admin API key.
  97. type arg1: str or burpy.session.Key object
  98. param arg2: net_id - Network ID.
  99. type arg2: str
  100. param arg3(optional): mode - The site-to-site VPN mode: hub, spoke or none.
  101. type arg3: str
  102. param arg4:(optional): hubs - The list of VPN hubs, in order of preference.
  103. In spoke mode, at least 1 hub is required.
  104. * hubId: The network ID of the hub.
  105. * useDefaultRoute: Only valid in 'spoke' mode. Indicates
  106. whether default route traffic should be sent to this hub.
  107. type arg4: list with nested dict
  108. usage: [{"hubId": <net_id>, "useDefaultRoute": <boolean>}]
  109. example: [{"hubId":"N_1234","useDefaultRoute":True },{"hubId":"N_2345","useDefaultRoute":False}]
  110. param arg5(optional): subnets - Only applicable in split mode. The list of subnets and their VPN presence.
  111. * localSubnet: The CIDR notation subnet used within the VPN
  112. * useVpn: Indicates the presence of the subnet in the VPN
  113. type arg5: list with nested dict
  114. usage: [{"localSubnet": "<subnet>", "useVpn" :<boolean>}]
  115. example: [{"localSubnet": "192.168.1.0/24", "useVpn":True}, {"localSubnet": "192.168.128.0/24", "useVpn":False}]
  116. """
  117. try:
  118. key = str(key)
  119. payload = json.dumps(_data_join(mode, hubs, subnets))
  120. uri = "https://dashboard.meraki.com/api/v0/networks/" + net_id + "/siteToSiteVpn"
  121. r = requests.put(uri, data=payload, headers=my_headers.headers(key))
  122. return (r.status_code, r.json())
  123. except Exception, error:
  124. print error
  125. return 1