Browse Source

Http to https redirect.

Change-Id: Iffde3117b7d94e92991c4e7d5fb0cc4ff2a873c8
Fabio Verboso 2 months ago
parent
commit
4a7dc9166d
2 changed files with 78 additions and 1 deletions
  1. 23
    1
      iotronic/conductor/endpoints.py
  2. 55
    0
      iotronic/wamp/proxies/nginx.py

+ 23
- 1
iotronic/conductor/endpoints.py View File

@@ -596,7 +596,13 @@ class ConductorEndpoint(object):
596 596
         except exception:
597 597
             return exception
598 598
 
599
+        cctx = self.wamp_agent_client.prepare(server=board.agent)
600
+        cctx.call(ctx, 'add_redirect', board_dns=en_webservice.dns,
601
+                  zone=en_webservice.zone, dns=newwbs.name)
602
+        cctx.call(ctx, 'reload_proxy')
603
+
599 604
         newwbs.create()
605
+
600 606
         return serializer.serialize_entity(ctx, newwbs)
601 607
 
602 608
     def destroy_webservice(self, ctx, webservice_id):
@@ -632,6 +638,16 @@ class ConductorEndpoint(object):
632 638
         except exception:
633 639
             return exception
634 640
 
641
+        board = objects.Board.get_by_uuid(ctx, wbsrv.board_uuid)
642
+        if board.agent == None:
643
+            raise exception.BoardInvalidStatus(uuid=board.uuid,
644
+                                               status=board.status)
645
+
646
+        cctx = self.wamp_agent_client.prepare(server=board.agent)
647
+        cctx.call(ctx, 'remove_redirect', board_dns=en_webservice.dns,
648
+                  zone=en_webservice.zone, dns=wbsrv.name)
649
+        cctx.call(ctx, 'reload_proxy')
650
+
635 651
         wbsrv.destroy()
636 652
         designate.delete_record(wbsrv.name + "." + en_webservice.dns,
637 653
                                 en_webservice.zone)
@@ -717,7 +733,6 @@ class ConductorEndpoint(object):
717 733
         cctx = self.wamp_agent_client.prepare(server=board.agent)
718 734
         cctx.call(ctx, 'enable_webservice', board=dns,
719 735
                   https_port=https_port, http_port=http_port, zone=zone)
720
-        cctx.call(ctx, 'reload_proxy')
721 736
 
722 737
         LOG.debug('Configure Web Proxy on Board %s with dns %s (email: %s) ',
723 738
                   board.uuid, dns, email)
@@ -731,6 +746,9 @@ class ConductorEndpoint(object):
731 746
         except exception:
732 747
             return exception
733 748
 
749
+        cctx.call(ctx, 'add_redirect', board_dns=dns, zone=zone)
750
+        cctx.call(ctx, 'reload_proxy')
751
+
734 752
         return serializer.serialize_entity(ctx, en_webservice)
735 753
 
736 754
     def disable_webservice(self, ctx, board_uuid):
@@ -784,6 +802,10 @@ class ConductorEndpoint(object):
784 802
 
785 803
         cctx = self.wamp_agent_client.prepare(server=board.agent)
786 804
         cctx.call(ctx, 'disable_webservice', board=webservice.dns)
805
+
806
+        # cctx.call(ctx, 'remove_redirect', board_dns=en_webservice.dns,
807
+        #          zone=en_webservice.zone)
808
+
787 809
         cctx.call(ctx, 'reload_proxy')
788 810
 
789 811
         webservice.destroy()

+ 55
- 0
iotronic/wamp/proxies/nginx.py View File

@@ -71,6 +71,39 @@ def remove(board):
71 71
           ])
72 72
 
73 73
 
74
+def string_redirect(board, zone, dns=None):
75
+    if not dns:
76
+        host = "%s.%s" % (board, zone)
77
+    else:
78
+        host = "%s.%s.%s" % (dns, board, zone)
79
+    string = "if ($host = %s) { return 301 https://$host$request_uri; }\n" % (
80
+        host)
81
+    return string
82
+
83
+
84
+def insert_entry(line, lines):
85
+    try:
86
+        lines.index(line)
87
+    except Exception:
88
+        lines.insert(4, line)
89
+    return lines
90
+
91
+
92
+def remove_entry(line, lines):
93
+    try:
94
+        lines.remove(line)
95
+    except Exception:
96
+        pass
97
+    return lines
98
+
99
+
100
+def save_conf(f_conf, lines):
101
+    f = open(f_conf, "w")
102
+    lines = "".join(lines)
103
+    f.write(lines)
104
+    f.close()
105
+
106
+
74 107
 class ProxyManager(Proxy):
75 108
 
76 109
     def __init__(self):
@@ -91,3 +124,25 @@ class ProxyManager(Proxy):
91 124
         LOG.debug('Disabling WebService on board %s',
92 125
                   board)
93 126
         remove(board)
127
+
128
+    def add_redirect(self, ctx, board_dns, zone, dns=None):
129
+        line = string_redirect(board_dns, zone, dns)
130
+        path = CONF.nginx.nginx_path + "/servers/" + board_dns
131
+        LOG.debug('Adding redirect %s on %s', line, path)
132
+
133
+        f = open(str(CONF.nginx.nginx_path + "/servers/" + board_dns), "r")
134
+        lines = f.readlines()
135
+        f.close()
136
+        lines = insert_entry(line, lines)
137
+        save_conf(path, lines)
138
+
139
+    def remove_redirect(self, ctx, board_dns, zone, dns=None):
140
+        path = CONF.nginx.nginx_path + "/servers/" + board_dns
141
+        line = string_redirect(board_dns, zone, dns)
142
+        LOG.debug('Removing redirect  %s on %s', line, path)
143
+
144
+        f = open(path, "r")
145
+        lines = f.readlines()
146
+        f.close()
147
+        lines = remove_entry(line, lines)
148
+        save_conf(path, lines)

Loading…
Cancel
Save