#!/usr/local/bin/python

# Creates a ghostunnel. Ensures that /_status endpoint works.

from subprocess import Popen
from test_common import create_root_cert, create_signed_cert, LOCALHOST, SocketPair, print_ok, cleanup_certs
import urllib2, socket, ssl, time, os, signal, json

if __name__ == "__main__":
  ghostunnel = None
  try:
    # Step 1: create certs
    create_root_cert('root')
    create_signed_cert('server', 'root')
    create_signed_cert('new_server', 'root')
    create_signed_cert('client1', 'root')

    # Step 2: start ghostunnel
    ghostunnel = Popen(['../ghostunnel', '--listen={0}:13001'.format(LOCALHOST),
      '--target={0}:13100'.format(LOCALHOST), '--keystore=server.p12',
      '--storepass=', '--cacert=root.crt', '--allow-ou=client1',
      '--status=localhost:13100'])

    # Step 3: read status information
    time.sleep(5)
    status = json.loads(urllib2.urlopen("https://localhost:13100/_status", cafile='root.crt').read())
    metrics = json.loads(urllib2.urlopen("https://localhost:13100/_metrics", cafile='root.crt').read())

    if not status['ok']:
        raise Exception("ghostunnel reported non-ok status")

    if type(metrics) != list:
        raise Exception("ghostunnel metrics expected to be JSON list")

    # Step 4: reload
    os.rename('new_server.p12', 'server.p12')
    ghostunnel.send_signal(signal.SIGUSR1)
    time.sleep(10)

    # Step 3: read status information
    # expecting tunnel to use "new_server" cert, setting cafile accordingly
    status = json.loads(urllib2.urlopen("https://localhost:13100/_status", cafile='root.crt').read())
    metrics = json.loads(urllib2.urlopen("https://localhost:13100/_metrics", cafile='root.crt').read())

    if not status['ok']:
        raise Exception("ghostunnel reported non-ok status")

    if type(metrics) != list:
        raise Exception("ghostunnel metrics expected to be JSON list")

    print_ok("OK")
  finally:
    if ghostunnel:
      ghostunnel.kill()
    cleanup_certs(['root', 'server', 'new_server', 'client1'])

@@ -270,16 +270,21 @@
 al, json
+, sys
 %0A%0Aif __n
@@ -799,16 +799,275 @@
 3100'%5D)%0A
+    time.sleep(5)%0A%0A    # urrllib2.urlopen() needs Python %3E= 2.7.9 for cafile support...%0A    if sys.version_info %3E= (2,7,9):%0A        urlopen = lambda path: urllib2.urlopen(path, cafile='root.crt')%0A    else:%0A        urlopen = lambda path: urllib2.urlopen(path)%0A
 %0A    # S
@@ -1096,34 +1096,16 @@
 ormation
-%0A    time.sleep(5)
 %0A    sta
@@ -1117,32 +1117,24 @@
  json.loads(
-urllib2.
 urlopen(%22htt
@@ -1162,35 +1162,16 @@
 _status%22
-, cafile='root.crt'
 ).read()
@@ -1193,32 +1193,24 @@
  json.loads(
-urllib2.
 urlopen(%22htt
@@ -1239,35 +1239,16 @@
 metrics%22
-, cafile='root.crt'
 ).read()
@@ -1703,32 +1703,24 @@
  json.loads(
-urllib2.
 urlopen(%22htt
@@ -1748,35 +1748,16 @@
 _status%22
-, cafile='root.crt'
 ).read()
@@ -1783,24 +1783,16 @@
 n.loads(
-urllib2.
 urlopen(
@@ -1825,35 +1825,16 @@
 metrics%22
-, cafile='root.crt'
 ).read()

Make status port test work with Python < 2.7.9