<repo_name>dgc-nuna/bin<file_sep>/list_public_ips.py

import boto.ec2 
import WH


if __name__ == '__main__':
    wh = WH.WH()
    wh.connect()
    conn = boto.ec2.connect_to_region("us-west-2")
    for a in conn.get_all_addresses():
        print "%s" % (a.public_ip)
<file_sep>/test_WH.py
import WH

conn = WH.WH()
conn.connect()
r = conn.add_allowed_host(40466,"https","nunahealth.com")
print r.text
<file_sep>/report_wh_ec2_diffs.py
import WH
import boto.ec2
import json
import re

wh_site_id = 40466
if __name__ == '__main__':
  wh = WH.WH()
  wh.connect()
  ahs = wh.get_allowed_hosts(wh_site_id)['allowed_hosts']
  eps = wh.get_entry_points(wh_site_id)['entry_points']
  sorted_ahs = [ h for h in sorted(ahs, key=lambda a: a['hostname'],) if re.search('\d+(\.\d+){3}',h['hostname'])]
  conn = boto.ec2.connect_to_region("us-west-2")
  ec2addrs = conn.get_all_addresses()
  new = set([addr.public_ip for addr in ec2addrs if addr.public_ip])
  old = set([addr['hostname'] for addr in sorted_ahs])
  hosts_to_add = new - old
  hosts_to_del = old - new
  for addr in hosts_to_del:
      print "Del %s" % addr
  for addr in hosts_to_add:
      print "Add %s" % addr



<file_sep>/WH.py
import yaml
import requests
import json
import os
import sys
import urllib
import pprint
api_root="https://sentinel.whitehatsec.com/api"
class WH:
    def __init__(self):
        with open(os.path.expanduser("~") + "/.wh","r") as ymlfile:
            cfg = yaml.load(ymlfile)
            self.password = cfg["<PASSWORD>"]
            self.username = cfg["username"]

    def connect(self):
        self.session = requests.session()
        r = self.session.post(
            "%s/user/%s/login" % (api_root,urllib.quote(self.username)), 
            data={"password": self.password})
        #sys.stderr.write(r.text + "\n")
        return r

    def add_allowed_host(self,site_id,scheme,host):
        r = self.session.post(
            "%s/site/%d/allowed_hosts" % (api_root,site_id), 
            data=json.dumps(
                {
                    "scheme":scheme,
                    "hostname":host
                }),
            headers={"Content-Type":"application/json"}
            )
        print r.status_code
        print r.text
        return r

    def add_entry_point(self,site_id,method,url):
        r = self.session.post(
            "%s/site/%d/entry_points" % (api_root,site_id),
            data=json.dumps(
                [{
                    "method": method,
                    "uri"   : url
                }]
                ),
            headers={"Content-Type":"application/json"}
            )
        print r.status_code
        print r.text
        return r

    def get_entry_points(self,site_id):
        r = self.session.get(
            "%s/site/%d/entry_points" % (api_root,site_id),
            headers={"Accept":"application/json"}
            )
        return json.loads(r.text)

    def get_allowed_hosts(self,site_id):
        r = self.session.get(
            "%s/site/%d/allowed_hosts" % (api_root,site_id),
            headers={"Accept":"application/json"}
            )
        return json.loads(r.text)

    def delete(self,href):
        r = self.session.delete(
            "%s/%s" % (api_root,href),
            headers={"Accept":"application/json"}
            )
        return r

<file_sep>/apply_wh_ec2_diffs.py
import WH
import boto.ec2
import json
import re

wh_site_id = 40466
if __name__ == '__main__':
  wh = WH.WH()
  wh.connect()
  ahs = wh.get_allowed_hosts(wh_site_id)['allowed_hosts']
  eps = wh.get_entry_points(wh_site_id)['entry_points']
  sorted_ahs = [ h for h in sorted(ahs, key=lambda a: a['hostname'],) if re.search('\d+(\.\d+){3}',h['hostname'])]
  conn = boto.ec2.connect_to_region("us-west-2")
  ec2addrs = conn.get_all_addresses()
  new = set([addr.public_ip for addr in ec2addrs if addr.public_ip])
  old = set([addr['hostname'] for addr in sorted_ahs])
  hosts_to_add = new - old
  hosts_to_del = old - new
  for addr in hosts_to_del:
      print "Del %s" % addr
  for addr in hosts_to_add:
      print "Add %s" % addr
  eps_to_delete = [ep['href'] for ep in eps if  ep['uri'] in set([ 'https://' + h for h in hosts_to_del]) | set([ 'http://' + h for h in hosts_to_del])]
  ahs_to_delete = [ah['href'] for ah in ahs if ah['hostname'] in hosts_to_del]
  for ep in eps_to_delete:
      tail = ep[5:] # strip "/api/"
      print "deleting %s" % tail
      r = wh.delete(tail)
  for ah in ahs_to_delete:
      tail = ah[5:] # strip "/api/"
      print "deleting %s" % tail
      r = wh.delete(tail)
      print r
  for a in hosts_to_add:
        print wh.add_allowed_host(wh_site_id,"http",a).request
        print wh.add_allowed_host(wh_site_id,"https",a).request
        print wh.add_entry_point(wh_site_id,"GET","http://" + a + "/").request
        print wh.add_entry_point(wh_site_id,"GET","https://" + a + "/").request
<file_sep>/wh_add_public_ips.py

import boto.ec2 
import WH


site_id=40466
if __name__ == '__main__':
    wh = WH.WH()
    wh.connect()
    conn = boto.ec2.connect_to_region("us-west-2")
    for a in conn.get_all_addresses():
        print "%s" % (a.public_ip)
        print wh.add_allowed_host(site_id,"http",a.public_ip).request
        print wh.add_allowed_host(site_id,"https",a.public_ip).request
        print wh.add_entry_point(site_id,"GET","http://" + a.public_ip + "/").request
        print wh.add_entry_point(site_id,"GET","https://" + a.public_ip + "/").request
