Guest User

Untitled

a guest
Mar 17th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import uuid
  3. import mysql.connector
  4.  
  5. MYSQL_PASS = 'XXXXXXXXX'
  6.  
  7. cnx = mysql.connector.connect(password=MYSQL_PASS, user='root', database='cloud')
  8.  
  9. cursor = cnx.cursor()
  10.  
  11. cursor.execute("SELECT id FROM security_group")
  12.  
  13. security_groups = []
  14. for row in cursor.fetchall():
  15. security_groups.append(row[0])
  16.  
  17. cursor.close()
  18.  
  19. for security_group_id in security_groups:
  20. add_rule_query = "INSERT INTO security_group_rule (security_group_id, uuid, type, start_port, end_port, protocol, allowed_ip_cidr) VALUES (%s, %s, 'ingress', %s, %s, %s, '::/0')"
  21. protocol_query = "SELECT id FROM security_group_rule WHERE start_port = 0 AND end_port = 65535 AND protocol = %s AND allowed_ip_cidr = '::/0' AND type = 'ingress' AND security_group_id = %s"
  22. for protocol in ['tcp', 'udp']:
  23. cursor = cnx.cursor()
  24. cursor.execute(protocol_query, (protocol, security_group_id))
  25.  
  26. if len(cursor.fetchall()) == 0:
  27. rule_uuid = str(uuid.uuid4())
  28. cursor.execute(add_rule_query, (security_group_id, rule_uuid, 0, 65535, protocol))
  29.  
  30. cursor.close()
  31.  
  32. icmp_query = "SELECT id FROM security_group_rule WHERE start_port = 128 AND end_port = 0 AND protocol = 'icmp' AND allowed_ip_cidr = '::/0' AND type = 'ingress' AND security_group_id = %s"
  33. cursor = cnx.cursor()
  34. cursor.execute(icmp_query, (security_group_id,))
  35. if len(cursor.fetchall()) == 0:
  36. rule_uuid = str(uuid.uuid4())
  37. cursor.execute(add_rule_query, (security_group_id, rule_uuid, 128, 0, 'icmp'))
  38.  
  39. cursor.close()
  40.  
  41. cnx.commit()
  42. cnx.close()
Add Comment
Please, Sign In to add comment