Mercurial > farol > farol
annotate farol/main.py @ 13:d5265a0da13a
Always log exceptions to file.
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Tue, 30 Sep 2014 14:25:25 +0200 |
parents | 4219d6fb4c38 |
children | f8d51aaac8bc |
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 | |
13
d5265a0da13a
Always log exceptions to file.
Benoît Allard <benoit.allard@greenbone.net>
parents:
12
diff
changeset
|
26 import logging |
d5265a0da13a
Always log exceptions to file.
Benoît Allard <benoit.allard@greenbone.net>
parents:
12
diff
changeset
|
27 from logging import FileHandler |
0 | 28 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
29 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
|
30 from farolluz.parsers.cvrf import parse |
0 | 31 from farolluz.py2 import urlopen |
32 from farolluz.renderer import render as render_cvrf | |
33 from farolluz.utils import utcnow | |
34 | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
35 from flask import Flask, request, render_template, redirect, url_for, flash |
0 | 36 from werkzeug import secure_filename |
37 | |
12
4219d6fb4c38
Implement three kind of caches
Benoît Allard <benoit.allard@greenbone.net>
parents:
11
diff
changeset
|
38 from . import cache |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
39 from .document import document |
12
4219d6fb4c38
Implement three kind of caches
Benoît Allard <benoit.allard@greenbone.net>
parents:
11
diff
changeset
|
40 from .session import get_current, set_current, has_current, document_required |
0 | 41 from .vulnerability import vulnerability |
42 from .producttree import producttree | |
43 | |
1
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 = 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
|
45 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
|
46 app.config.from_pyfile('farol.cfg', silent=True) |
0 | 47 |
12
4219d6fb4c38
Implement three kind of caches
Benoît Allard <benoit.allard@greenbone.net>
parents:
11
diff
changeset
|
48 app.register_blueprint(cache.mod, url_prefix='/cache') |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
49 app.register_blueprint(document, url_prefix='/document') |
0 | 50 app.register_blueprint(vulnerability, url_prefix='/vulnerability') |
51 app.register_blueprint(producttree, url_prefix='/producttree') | |
52 | |
13
d5265a0da13a
Always log exceptions to file.
Benoît Allard <benoit.allard@greenbone.net>
parents:
12
diff
changeset
|
53 file_handler = FileHandler(os.path.join(app.instance_path, 'farol.log')) |
d5265a0da13a
Always log exceptions to file.
Benoît Allard <benoit.allard@greenbone.net>
parents:
12
diff
changeset
|
54 file_handler.setLevel(logging.WARNING) |
d5265a0da13a
Always log exceptions to file.
Benoît Allard <benoit.allard@greenbone.net>
parents:
12
diff
changeset
|
55 app.logger.addHandler(file_handler) |
3
3478e20885fd
Add a ReverseProxy and fix the cache path everywhere
Benoît Allard <benoit.allard@greenbone.net>
parents:
1
diff
changeset
|
56 |
0 | 57 @app.context_processor |
58 def cache_content(): | |
59 """ List the documents in cache """ | |
12
4219d6fb4c38
Implement three kind of caches
Benoît Allard <benoit.allard@greenbone.net>
parents:
11
diff
changeset
|
60 return dict(caching=cache.caching_type(), |
4219d6fb4c38
Implement three kind of caches
Benoît Allard <benoit.allard@greenbone.net>
parents:
11
diff
changeset
|
61 cache=cache.cache_content()) |
0 | 62 |
63 @app.context_processor | |
64 def doc_properties(): | |
65 if not has_current(): | |
66 return {'has_current': False} | |
67 cvrf = get_current() | |
68 vulns = [(v.getTitle(), v._ordinal) for v in cvrf._vulnerabilities] | |
69 prods = [] | |
70 if cvrf._producttree: | |
71 prods = [(p._name, p._productid) for p in cvrf._producttree._products] | |
72 try: | |
73 cvrf.validate() | |
74 error = None | |
75 except ValidationError as ve: | |
76 error = ve | |
77 return dict(has_current=True, vulnerabilities=vulns, products=prods, error=error) | |
78 | |
79 @app.template_filter('secure_filename') | |
80 def makeId(string): | |
81 return secure_filename(string) | |
82 | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
83 @app.route('/') |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
84 def welcome(): |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
85 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
|
86 |
8
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
87 def parse_url(url): |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
88 set_current(parse(urlopen(url).read())) |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
89 |
0 | 90 @app.route('/new', methods=['GET', 'POST']) |
91 def new(): | |
92 if request.method != 'POST': | |
93 return render_template('new.j2', has_document=has_current(), now=utcnow()) | |
12
4219d6fb4c38
Implement three kind of caches
Benoît Allard <benoit.allard@greenbone.net>
parents:
11
diff
changeset
|
94 |
8
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
95 if 'rhsa' in request.form: |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
96 year, index = request.form['id'].split(':') |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
97 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
|
98 elif 'oracle' in request.form: |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
99 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
|
100 elif 'cisco' in request.form: |
11
a32f9b86edb4
main: Fix parsing of cisco id
Benoît Allard <benoit.allard@greenbone.net>
parents:
9
diff
changeset
|
101 kind, date, name = request.form['id'].split('-', 2) |
8
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
102 kind = {'sa': 'Advisory', 'sr': 'Response'}[kind] |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
103 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
|
104 elif 'nasl' in request.form: |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
105 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
|
106 return redirect(url_for('new')) |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
107 elif 'url' in request.form: |
2ce3676c9b2e
Ease the import of new documents
Benoît Allard <benoit.allard@greenbone.net>
parents:
7
diff
changeset
|
108 parse_url(request.form['url']) |
0 | 109 elif 'local' in request.files: |
110 upload = request.files['local'] | |
111 if not upload.filename.endswith('.xml'): | |
112 flash('Uploaded files should end in .xml', 'danger') | |
113 return redirect(url_for('new')) | |
114 fpath = os.path.join('/tmp', secure_filename(upload.filename)) | |
115 upload.save(fpath) | |
116 with open(fpath, 'rt') as f: | |
117 set_current(parse(f)) | |
118 os.remove(fpath) | |
119 elif 'text' in request.form: | |
120 set_current(parse(request.form['text'].encode('utf-8'))) | |
121 else: | |
122 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
|
123 return redirect(url_for('document.view')) |
0 | 124 |
125 @app.route('/render/<format_>') | |
126 @document_required | |
127 def render(format_): | |
128 cvrf = get_current() | |
129 doc = render_cvrf(cvrf, format_ + '.j2') | |
130 return render_template('render.j2', format_=format_, title=cvrf._title, type_=cvrf._type, doc=doc ) | |
131 | |
132 @app.route('/about') | |
133 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
|
134 return render_template('about.j2', instance_dir=app.instance_path) |
0 | 135 |