Mercurial > farol > farol
annotate farol/main.py @ 8:2ce3676c9b2e
Ease the import of new documents
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Thu, 25 Sep 2014 17:49:43 +0200 |
parents | 8f41bb7f4681 |
children | 5b84a2c4f30c |
rev | line source |
---|---|
0 | 1 # -*- encoding: utf-8 -*- |
2 # Description: | |
3 # Farol Web Application | |
4 # | |
5 # Authors: | |
6 # BenoƮt Allard <benoit.allard@greenbone.net> | |
7 # | |
8 # Copyright: | |
9 # Copyright (C) 2014 Greenbone Networks GmbH | |
10 # | |
11 # This program is free software; you can redistribute it and/or | |
12 # modify it under the terms of the GNU General Public License | |
13 # as published by the Free Software Foundation; either version 2 | |
14 # of the License, or (at your option) any later version. | |
15 # | |
16 # This program is distributed in the hope that it will be useful, | |
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 # GNU General Public License for more details. | |
20 # | |
21 # You should have received a copy of the GNU General Public License | |
22 # along with this program; if not, write to the Free Software | |
23 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
24 | |
25 import os | |
26 | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
27 from farolluz.cvrf import CVRF, ValidationError |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
28 from farolluz.parsers.cvrf import parse |
0 | 29 from farolluz.py2 import urlopen |
30 from farolluz.renderer import render as render_cvrf | |
31 from farolluz.utils import utcnow | |
32 | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
33 from flask import Flask, request, render_template, redirect, url_for, flash |
0 | 34 from werkzeug import secure_filename |
35 | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
36 from .document import document |
0 | 37 from .session import get_current, set_current, has_current, del_current, document_required |
38 from .vulnerability import vulnerability | |
39 from .producttree import producttree | |
3
3478e20885fd
Add a ReverseProxy and fix the cache path everywhere
Benoît Allard <benoit.allard@greenbone.net>
parents:
1
diff
changeset
|
40 from .proxy import ReverseProxied |
0 | 41 |
1
63b00c10ada8
Add Configuration support, warning when in DEBUG mode, and Deployment instruction
Benoît Allard <benoit.allard@greenbone.net>
parents:
0
diff
changeset
|
42 app = Flask(__name__, instance_relative_config=True) |
63b00c10ada8
Add Configuration support, warning when in DEBUG mode, and Deployment instruction
Benoît Allard <benoit.allard@greenbone.net>
parents:
0
diff
changeset
|
43 app.config.from_object('farol.config.Config') |
63b00c10ada8
Add Configuration support, warning when in DEBUG mode, and Deployment instruction
Benoît Allard <benoit.allard@greenbone.net>
parents:
0
diff
changeset
|
44 app.config.from_pyfile('farol.cfg', silent=True) |
0 | 45 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
46 app.register_blueprint(document, url_prefix='/document') |
0 | 47 app.register_blueprint(vulnerability, url_prefix='/vulnerability') |
48 app.register_blueprint(producttree, url_prefix='/producttree') | |
49 | |
3
3478e20885fd
Add a ReverseProxy and fix the cache path everywhere
Benoît Allard <benoit.allard@greenbone.net>
parents:
1
diff
changeset
|
50 app.wsgi_app = ReverseProxied(app.wsgi_app) |
3478e20885fd
Add a ReverseProxy and fix the cache path everywhere
Benoît Allard <benoit.allard@greenbone.net>
parents:
1
diff
changeset
|
51 |
0 | 52 @app.context_processor |
53 def cache_content(): | |
54 """ List the documents in cache """ | |
1
63b00c10ada8
Add Configuration support, warning when in DEBUG mode, and Deployment instruction
Benoît Allard <benoit.allard@greenbone.net>
parents:
0
diff
changeset
|
55 dirname = app.config.get('CACHE_DIRECTORY', |
63b00c10ada8
Add Configuration support, warning when in DEBUG mode, and Deployment instruction
Benoît Allard <benoit.allard@greenbone.net>
parents:
0
diff
changeset
|
56 os.path.join(app.instance_path, '_cache')) |
0 | 57 if not os.path.exists(dirname): |
1
63b00c10ada8
Add Configuration support, warning when in DEBUG mode, and Deployment instruction
Benoît Allard <benoit.allard@greenbone.net>
parents:
0
diff
changeset
|
58 os.makedirs(dirname) |
0 | 59 l = [] |
60 for path in os.listdir(dirname): | |
61 name, ext = os.path.splitext(path) | |
62 if ext == '.xml': | |
63 l.append(name) | |
64 return dict(cache=l) | |
65 | |
66 @app.context_processor | |
67 def doc_properties(): | |
68 if not has_current(): | |
69 return {'has_current': False} | |
70 cvrf = get_current() | |
71 vulns = [(v.getTitle(), v._ordinal) for v in cvrf._vulnerabilities] | |
72 prods = [] | |
73 if cvrf._producttree: | |
74 prods = [(p._name, p._productid) for p in cvrf._producttree._products] | |
75 try: | |
76 cvrf.validate() | |
77 error = None | |
78 except ValidationError as ve: | |
79 error = ve | |
80 return dict(has_current=True, vulnerabilities=vulns, products=prods, error=error) | |
81 | |
82 @app.template_filter('secure_filename') | |
83 def makeId(string): | |
84 return secure_filename(string) | |
85 | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
86 @app.route('/') |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
87 def welcome(): |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
88 return render_template('welcome.j2') |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
89 |
8
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
90 def parse_url(url): |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
91 set_current(parse(urlopen(url).read())) |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
92 |
0 | 93 @app.route('/new', methods=['GET', 'POST']) |
94 def new(): | |
95 if request.method != 'POST': | |
96 return render_template('new.j2', has_document=has_current(), now=utcnow()) | |
8
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
97 url = None |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
98 if 'rhsa' in request.form: |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
99 year, index = request.form['id'].split(':') |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
100 parse_url("https://www.redhat.com/security/data/cvrf/%(year)s/cvrf-rhsa-%(year)s-%(index)s.xml" % {'year': year, 'index': index}) |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
101 elif 'oracle' in request.form: |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
102 parse_url("http://www.oracle.com/ocom/groups/public/@otn/documents/webcontent/%s.xml" % request.form['id']) |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
103 elif 'cisco' in request.form: |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
104 kind, date, name = request.form['id'].split('-', 3) |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
105 kind = {'sa': 'Advisory', 'sr': 'Response'}[kind] |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
106 parse_url("http://tools.cisco.com/security/center/contentxml/CiscoSecurity%(kind)s/cisco-%(id)s/cvrf/cisco-%(id)s_cvrf.xml" % {'kind': kind, 'id': request.form['id']}) |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
107 elif 'nasl' in request.form: |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
108 flash("I'm not able to parse NASL scripts yet", 'danger') |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
109 return redirect(url_for('new')) |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
110 elif 'url' in request.form: |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
111 parse_url(request.form['url']) |
0 | 112 elif 'local' in request.files: |
113 upload = request.files['local'] | |
114 if not upload.filename.endswith('.xml'): | |
115 flash('Uploaded files should end in .xml', 'danger') | |
116 return redirect(url_for('new')) | |
117 fpath = os.path.join('/tmp', secure_filename(upload.filename)) | |
118 upload.save(fpath) | |
119 with open(fpath, 'rt') as f: | |
120 set_current(parse(f)) | |
121 os.remove(fpath) | |
122 elif 'text' in request.form: | |
123 set_current(parse(request.form['text'].encode('utf-8'))) | |
124 else: | |
125 set_current(CVRF(request.form['title'], request.form['type'])) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
126 return redirect(url_for('document.view')) |
0 | 127 |
128 @app.route('/render/<format_>') | |
129 @document_required | |
130 def render(format_): | |
131 cvrf = get_current() | |
132 doc = render_cvrf(cvrf, format_ + '.j2') | |
133 return render_template('render.j2', format_=format_, title=cvrf._title, type_=cvrf._type, doc=doc ) | |
134 | |
135 @app.route('/save', methods=['GET', 'POST']) | |
136 @document_required | |
137 def save(): | |
138 if request.method != 'POST': | |
139 return render_template('save.j2', id_=get_current()._tracking._identification._id) | |
140 # Get some kind of filename, and save the cvrf on cache (disk) | |
141 path = secure_filename(request.form['fname']) | |
142 path, _ = os.path.splitext(path) | |
3
3478e20885fd
Add a ReverseProxy and fix the cache path everywhere
Benoît Allard <benoit.allard@greenbone.net>
parents:
1
diff
changeset
|
143 dirname = app.config.get('CACHE_DIRECTORY', |
3478e20885fd
Add a ReverseProxy and fix the cache path everywhere
Benoît Allard <benoit.allard@greenbone.net>
parents:
1
diff
changeset
|
144 os.path.join(app.instance_path, '_cache')) |
3478e20885fd
Add a ReverseProxy and fix the cache path everywhere
Benoît Allard <benoit.allard@greenbone.net>
parents:
1
diff
changeset
|
145 with open(os.path.join(dirname, path + '.xml'), 'wt') as f: |
0 | 146 f.write(render_cvrf(get_current(), 'cvrf.j2').encode('utf-8')) |
147 flash('File saved as %s' % path) | |
148 del_current() | |
149 return redirect(url_for('new')) | |
150 | |
151 @app.route('/load/<element>', methods=['GET', 'POST']) | |
152 def load(element): | |
153 if request.method != 'POST': | |
154 if has_current(): | |
155 # Suggest to save first | |
156 return render_template('load.j2', element=element) | |
157 | |
3
3478e20885fd
Add a ReverseProxy and fix the cache path everywhere
Benoît Allard <benoit.allard@greenbone.net>
parents:
1
diff
changeset
|
158 dirname = app.config.get('CACHE_DIRECTORY', |
3478e20885fd
Add a ReverseProxy and fix the cache path everywhere
Benoît Allard <benoit.allard@greenbone.net>
parents:
1
diff
changeset
|
159 os.path.join(app.instance_path, '_cache')) |
3478e20885fd
Add a ReverseProxy and fix the cache path everywhere
Benoît Allard <benoit.allard@greenbone.net>
parents:
1
diff
changeset
|
160 fpath = os.path.join(dirname, element+'.xml') |
0 | 161 with open(fpath, 'rt') as f: |
162 set_current(parse(f)) | |
163 os.remove(fpath) | |
164 flash('"%s" has been removed from cache' % element) | |
165 # Get some kind of id, and load the file. | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
166 return redirect(url_for('document.view')) |
0 | 167 |
168 @app.route('/about') | |
169 def about(): | |
1
63b00c10ada8
Add Configuration support, warning when in DEBUG mode, and Deployment instruction
Benoît Allard <benoit.allard@greenbone.net>
parents:
0
diff
changeset
|
170 return render_template('about.j2', instance_dir=app.instance_path) |
0 | 171 |