Quellcode durchsuchen

interface rename && firewall basic add rules

Roberto Berto vor 5 Jahren
Ursprung
Commit
7a7488488a

+ 4 - 1
.gitignore

@@ -132,4 +132,7 @@ dmypy.json
 .DS_Store
 .DS_Store
 
 
 # Database 
 # Database 
-db.sqlite3
+db.sqlite3
+
+# visual studio
+*.code-workspace

+ 1 - 1
vycenter/firewall/templates/firewall/list.html

@@ -27,7 +27,7 @@
 
 
     </table>
     </table>
 {% else %}
 {% else %}
-    <p>No interfaces.</p>
+    <p>No firewalls.</p>
 {% endif %}
 {% endif %}
 
 
 {% endblock %}
 {% endblock %}

+ 45 - 3
vycenter/firewall/templates/firewall/show.html

@@ -5,17 +5,18 @@
 
 
 {% block debug %}
 {% block debug %}
 {{ firewall_all }}
 {{ firewall_all }}
+{{ firewall_name }}
 {% endblock %}
 {% endblock %}
 
 
 {% block content %}
 {% block content %}
 
 
 
 
 
 
-{% if firewall_all %}
+{% if firewall %}
     <table border="1" width="100%">
     <table border="1" width="100%">
     <tr><th>name</th><th>description</th><th>action</th></tr>
     <tr><th>name</th><th>description</th><th>action</th></tr>
 
 
-    {% for key, value in firewall_all.items %}
+    {% for key, value in firewall.items %}
         <tr>
         <tr>
         {% for ifkey, ifvalue in value.items %}
         {% for ifkey, ifvalue in value.items %}
             <td><a href="{% url 'firewall:show' ifkey %}">{{ ifkey }}</a></td>
             <td><a href="{% url 'firewall:show' ifkey %}">{{ ifkey }}</a></td>
@@ -27,9 +28,50 @@
 
 
     </table>
     </table>
 {% else %}
 {% else %}
-    <p>No interfaces.</p>
+    <p>No firewalls.</p>
 {% endif %}
 {% endif %}
 
 
+
+
+<h2>Create new rule</h2>
+
+<form action="{% url 'firewall:addrule' firewall_name %}" method="post">
+    {% csrf_token %}
+    
+    <p>
+        <label for="alias">rule number</label><br>
+        <input type="text" name="rulenumber" id="rulenumber" value="{{ rulenumber }}" size="5">
+    </p>
+    
+    <p>
+        <label for="hostname">action</label><br>
+        <input type="checkbox" name="action" id="action" value="accept"> accept
+        <input type="checkbox" name="action" id="action" value="drop"> drop
+    </p>
+
+    <p>
+        <label for="hostname">protocol</label><br>
+        <input type="checkbox" name="protocol" id="protocol" value="tcp"> tcp
+        <input type="checkbox" name="protocol" id="protocol" value="udp"> udp
+    </p>   
+    
+    <p>
+        <label for="alias">destination port</label><br>
+        <input type="text" name="destinationport" id="destinationport" value="{{ destinationport }}" size="5">
+    </p>
+    
+    <p>
+        <label for="alias">source port</label><br>
+        <input type="text" name="sourceport" id="sourceport" value="{{ sourceport }}" size="5">
+    </p>    
+    
+    
+    <input type="submit" value="Add Rule">
+    </form>
+
+    
+
+
 {% endblock %}
 {% endblock %}
 
 
 
 

+ 2 - 1
vycenter/firewall/urls.py

@@ -7,7 +7,8 @@ app_name = 'firewall'
 
 
 urlpatterns = [
 urlpatterns = [
     path('', views.index, name='firewall-list'),
     path('', views.index, name='firewall-list'),
-    path('show/<str:name>', views.show, name='show'),
+    path('show/<str:firewall_name>', views.show, name='show'),
+    path('addrule/<str:firewall_name>', views.addrule, name='addrule'),
 ]
 ]
 
 
 
 

+ 46 - 3
vycenter/firewall/views.py

@@ -26,20 +26,63 @@ def index(request):
 
 
 
 
 
 
-def show(request, name):
+def show(request, firewall_name):
     #interfaces = vyos.get_interfaces()
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
     hostname_default = vyos.get_hostname_prefered(request)
 
 
-    firewall_all = vyos.get_firewall_all(hostname_default)
+    firewall = vyos.get_firewall(hostname_default, firewall_name)
+    
 
 
     template = loader.get_template('firewall/show.html')
     template = loader.get_template('firewall/show.html')
     context = { 
     context = { 
         #'interfaces': interfaces,
         #'interfaces': interfaces,
         'instances': all_instances,
         'instances': all_instances,
         'hostname_default': hostname_default,
         'hostname_default': hostname_default,
-        'firewall_all':  firewall_all
+        'firewall':  firewall,
+        'firewall_name': firewall_name,
     }   
     }   
     return HttpResponse(template.render(context, request))
     return HttpResponse(template.render(context, request))
 
 
 
 
+
+def addrule(request, firewall_name):
+    #interfaces = vyos.get_interfaces()
+    all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
+
+    firewall = vyos.get_firewall(hostname_default, firewall_name)
+    
+    if 'action' in request.POST:
+        cmd = {"op": "set", "path": ["firewall", "name", firewall_name, "rule", request.POST['rulenumber'], "action", request.POST['action']]}
+        result1 = vyos.set_config(hostname_default, cmd)
+        print(result1)
+
+    if 'protocol' in request.POST:
+        cmd = {"op": "set", "path": ["firewall", "name", firewall_name, "rule", request.POST['rulenumber'], "protocol", request.POST['protocol']]}
+        result2 = vyos.set_config(hostname_default, cmd)
+        print(result2)
+
+    if 'destinationport' in request.POST:
+        cmd = {"op": "set", "path": ["firewall", "name", firewall_name, "rule", request.POST['rulenumber'], "destination", "port", request.POST['destinationport']]}
+        result3 = vyos.set_config(hostname_default, cmd)
+        print(result3)
+
+    if 'sourceport' in request.POST:
+        cmd = {"op": "set", "path": ["firewall", "name", firewall_name, "rule", request.POST['rulenumber'], "source", "port", request.POST['sourceport']]}
+        result3 = vyos.set_config(hostname_default, cmd)
+        print(result3)        
+
+
+
+    template = loader.get_template('firewall/show.html')
+    context = { 
+        #'interfaces': interfaces,
+        'instances': all_instances,
+        'hostname_default': hostname_default,
+        'firewall':  firewall,
+        'firewall_name': firewall_name,
+    }  
+    return HttpResponse(template.render(context, request))
+
+

+ 0 - 0
vycenter/interface/__init__.py


+ 3 - 0
vycenter/interface/admin.py

@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.

+ 5 - 0
vycenter/interface/apps.py

@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class InterfaceConfig(AppConfig):
+    name = 'interface'

+ 0 - 0
vycenter/interface/migrations/__init__.py


+ 3 - 0
vycenter/interface/models.py

@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.

+ 29 - 0
vycenter/interface/templates/interface/index.html

@@ -0,0 +1,29 @@
+{% extends "base.html" %}
+
+{% block header_title %}Instance Dashboard{% endblock %}
+{% block section_title %}Instance Dashboard{% endblock %}
+
+{% block content %}
+
+{% if interfaces %}
+    <table border="1" width="100%">
+    <tr><th>type</th><th>name</th><th>address</th></tr>
+
+    {% for key, value in interfaces.items %}
+        <tr><td>{{ key }}</td>
+        {% for ifkey, ifvalue in value.items %}
+            <td>{% url 'interface:interface-show' key ifkey as url_interface %}
+            <a href="{{ url_interface }}">{{ ifkey }}</a></td><td>{{ ifvalue.address }}</td>
+        {% endfor %}
+        </tr>
+    {% endfor %}
+
+    </table>
+{% else %}
+    <p>No interfaces.</p>
+{% endif %}
+
+{% endblock %}
+
+
+

+ 30 - 0
vycenter/interface/templates/interface/show.html

@@ -0,0 +1,30 @@
+{% extends "base.html" %}
+
+{% block header_title %}Interface{% endblock %}
+{% block section_title %}Interface{% endblock %}
+
+{% block debug %}
+{{ interface }}
+{% endblock %}
+
+
+{% block content %}
+{% if interface %}
+
+    <table border="1" width="100%">
+    <tr>
+    <th>address</th> <td>{{ interface.address }}</td>
+    </tr>
+
+    <tr>
+    <th>mtu</th> <td>{{ interface.mtu }}</td>
+    </tr>
+
+    </table>
+{% else %}
+    <p>Invalid interface.</p>
+{% endif %}
+
+{% endblock %}
+
+

+ 3 - 0
vycenter/interface/tests.py

@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.

+ 10 - 0
vycenter/interface/urls.py

@@ -0,0 +1,10 @@
+from django.urls import path
+
+from . import views
+
+app_name = 'interface'
+
+urlpatterns = [
+    path('', views.index, name='interface-list'),
+    path('interface-show/<slug:interface_type>/<slug:interface_name>', views.interfaceshow, name='interface-show'),
+]

+ 39 - 0
vycenter/interface/views.py

@@ -0,0 +1,39 @@
+from django.shortcuts import render
+from django.http import HttpResponse
+from django.template import loader
+
+import vyos
+
+from config.models import Instance
+
+
+def index(request):
+    hostname_default = vyos.get_hostname_prefered(request)
+
+    all_instances = vyos.instance_getall()
+
+    interfaces = vyos.get_interfaces(hostname_default)
+    
+    template = loader.get_template('interface/index.html')
+    context = {
+        'interfaces': interfaces,
+        'instances': all_instances,
+        'hostname_default': hostname_default,
+    }
+    return HttpResponse(template.render(context, request))
+
+def interfaceshow(request, interface_type, interface_name):
+    all_instances = vyos.instance_getall()
+
+    hostname_default = vyos.get_hostname_prefered(request)
+    
+    interface = vyos.get_interface(interface_type, interface_name, hostname=hostname_default)
+    
+    template = loader.get_template('interface/show.html')
+    context = { 
+        'interface': interface,
+        'instances': all_instances,
+        'hostname_default': hostname_default,
+    }   
+    return HttpResponse(template.render(context, request))
+

+ 1 - 1
vycenter/vycenter/settings.py

@@ -37,7 +37,7 @@ INSTALLED_APPS = [
     'django.contrib.sessions',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'django.contrib.staticfiles',
-    'instance.apps.InstanceConfig',
+    'interface.apps.InterfaceConfig',
     'config.apps.ConfigConfig',
     'config.apps.ConfigConfig',
     'firewall.apps.FirewallConfig',
     'firewall.apps.FirewallConfig',
 ]
 ]

+ 18 - 4
vycenter/vycenter/templates/base.html

@@ -27,7 +27,7 @@
         background-color: #ccc;
         background-color: #ccc;
         padding: 5px;
         padding: 5px;
     }
     }
-    h1 {
+    #menu-logotop h1 {
       font-size: 10px;      
       font-size: 10px;      
     }
     }
     #menu-logotop {
     #menu-logotop {
@@ -38,7 +38,7 @@
       padding-top: 3px;
       padding-top: 3px;
       background-color: #AD343E;
       background-color: #AD343E;
     }
     }
-    h2 {
+    #menu-logotop h2 {
       font-size: 12px;
       font-size: 12px;
     }
     }
     ol {
     ol {
@@ -71,6 +71,20 @@
       padding-top: 0;   
       padding-top: 0;   
       margin: 0;  
       margin: 0;  
     }
     }
+
+    #central h1 {
+      font-size: 18px;
+    }
+
+    #central h2 {
+      margin-top: 20px;
+      font-size: 14px;
+    }
+
+    #central {
+      font-size: 12px;
+    }
+
     </style>
     </style>
   </head>
   </head>
   <body >
   <body >
@@ -106,7 +120,7 @@
       </div>
       </div>
     </div>
     </div>
 
 
-<div class="container">
+<div class="container" id="central">
   <div class="row">
   <div class="row">
     <div class="col-3 menu">
     <div class="col-3 menu">
 
 
@@ -117,7 +131,7 @@
 
 
 
 
     <ol>
     <ol>
-    <li><a href="/instance/">Dashboard</a></li>
+    <li><a href="/interface/">Interfaces</a></li>
     <li><a href="{% url 'firewall:firewall-list' %}">Firewall</a></li>
     <li><a href="{% url 'firewall:firewall-list' %}">Firewall</a></li>
 
 
     <li>Static Routing</li>
     <li>Static Routing</li>

+ 1 - 1
vycenter/vycenter/urls.py

@@ -23,7 +23,7 @@ app_name = 'vycenter'
 
 
 
 
 urlpatterns = [
 urlpatterns = [
-    path('instance/', include('instance.urls')),
+    path('interface/', include('interface.urls')),
     path('config/', include('config.urls')),
     path('config/', include('config.urls')),
     path('dashboard/', include('dashboard.urls')),
     path('dashboard/', include('dashboard.urls')),
     path('firewall/', include('firewall.urls')),
     path('firewall/', include('firewall.urls')),

+ 75 - 20
vycenter/vyos.py

@@ -55,8 +55,6 @@ def get_hostname_prefered(request):
 
 
     return hostname 
     return hostname 
     
     
-
-#data='{"op": "showConfig", "path": ["interfaces", "dummy"]}
 def instance_getall():
 def instance_getall():
     instances = Instance.objects.all()
     instances = Instance.objects.all()
     return instances
     return instances
@@ -69,10 +67,6 @@ def conntry(hostname):
     post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     print(post)
     print(post)
 
 
-
-    # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
-    # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n     address 192.168.168.1/32\n     address 192.168.168.2/32\n     /* That is a description */\n     description \"Test interface\"\n }\n dummy dum1 {\n     address 203.0.113.76/32\n     address 203.0.113.79/32\n }\n", "error": null}
-
     
     
     print(get_url_retrieve(hostname))
     print(get_url_retrieve(hostname))
 
 
@@ -102,10 +96,6 @@ def getall(hostname="179.127.12.142"):
     post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     print(post)
     print(post)
 
 
-
-    # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
-    # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n     address 192.168.168.1/32\n     address 192.168.168.2/32\n     /* That is a description */\n     description \"Test interface\"\n }\n dummy dum1 {\n     address 203.0.113.76/32\n     address 203.0.113.79/32\n }\n", "error": null}
-
     try:
     try:
         resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
         resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
     except requests.exceptions.ConnectionError:
     except requests.exceptions.ConnectionError:
@@ -135,10 +125,6 @@ def get_interfaces(hostname="179.127.12.142"):
     post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     print(post)
     print(post)
 
 
-
-    # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
-    # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n     address 192.168.168.1/32\n     address 192.168.168.2/32\n     /* That is a description */\n     description \"Test interface\"\n }\n dummy dum1 {\n     address 203.0.113.76/32\n     address 203.0.113.79/32\n }\n", "error": null}
-
     try:
     try:
         resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
         resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
     except requests.exceptions.ConnectionError:
     except requests.exceptions.ConnectionError:
@@ -171,10 +157,6 @@ def get_interface(interface_type, interface_name, hostname):
     post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     print(post)
     print(post)
 
 
-
-    # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
-    # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n     address 192.168.168.1/32\n     address 192.168.168.2/32\n     /* That is a description */\n     description \"Test interface\"\n }\n dummy dum1 {\n     address 203.0.113.76/32\n     address 203.0.113.79/32\n }\n", "error": null}
-
     try:
     try:
         resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
         resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
     except requests.exceptions.ConnectionError:
     except requests.exceptions.ConnectionError:
@@ -209,9 +191,40 @@ def get_firewall_all(hostname):
     post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
     print(post)
     print(post)
 
 
+    try:
+        resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
+    except requests.exceptions.ConnectionError:
+        return False
+
+    print(resp.status_code)
+    pprint.pprint(resp)
+
+    pprint.pprint(resp.json())
+
+
+    if resp.status_code != 200:
+        # This means something went wrong.
+        #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
+        return "erro"
+    #for todo_item in resp.json():
+        #print('{} {}'.format(todo_item['id'], todo_item['summary']))
+
+    result1 = resp.json()
+    print(result1['data'])
+    #result2 = json.loads(result1['data'])
+    pprint.pprint(result1)
+
+    return result1['data']
+
+
+
+def get_firewall(hostname, name):
+    cmd = {"op": "showConfig", "path": ["firewall", name]}
+
+    print(json.dumps(cmd))
+    post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
+    print(post)
 
 
-    # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
-    # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n     address 192.168.168.1/32\n     address 192.168.168.2/32\n     /* That is a description */\n     description \"Test interface\"\n }\n dummy dum1 {\n     address 203.0.113.76/32\n     address 203.0.113.79/32\n }\n", "error": null}
 
 
     try:
     try:
         resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
         resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
@@ -237,3 +250,45 @@ def get_firewall_all(hostname):
     pprint.pprint(result1)
     pprint.pprint(result1)
 
 
     return result1['data']
     return result1['data']
+
+
+def set_config(hostname, cmd):
+    print(json.dumps(cmd))
+    post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
+    print(post)
+
+
+    try:
+        resp = requests.post(get_url_configure(hostname), verify=False, data=post, timeout=15)
+    except requests.exceptions.ConnectionError:
+        return False
+
+    print(resp.status_code)
+    pprint.pprint(resp)
+
+    pprint.pprint(resp.json())
+
+
+    if resp.status_code != 200:
+        # This means something went wrong.
+        #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
+        return "erro"
+    #for todo_item in resp.json():
+        #print('{} {}'.format(todo_item['id'], todo_item['summary']))
+
+    result1 = resp.json()
+    print(result1['data'])
+    #result2 = json.loads(result1['data'])
+    pprint.pprint(result1)
+
+    return result1['data']
+
+
+def insert_firewall_rules(hostname, firewall_name):
+    cmd = {"op": "set", "path": ["firewall", firewall_name, "rule", request.POST['rulenumber'], "action", request.POST['action']]}
+    result1 = set_config(hostname, cmd)
+
+
+
+#curl -k -X POST -F data='{"op": "set", "path": ["interfaces", "dummy", "dum1", "address"], "value": "203.0.113.76/32"}' -F key=a6ffb742a8a631a65b07ab2026258629da2632fd https://179.127.12.142:44302/configure
+