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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
2 # Description:
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
3 # Farol Web Application
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
4 #
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
5 # Authors:
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
6 # BenoƮt Allard <benoit.allard@greenbone.net>
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
7 #
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
8 # Copyright:
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
9 # Copyright (C) 2014 Greenbone Networks GmbH
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
10 #
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
11 # This program is free software; you can redistribute it and/or
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
12 # modify it under the terms of the GNU General Public License
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
13 # as published by the Free Software Foundation; either version 2
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
14 # of the License, or (at your option) any later version.
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
15 #
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
16 # This program is distributed in the hope that it will be useful,
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
19 # GNU General Public License for more details.
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
20 #
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
21 # You should have received a copy of the GNU General Public License
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
22 # along with this program; if not, write to the Free Software
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
23 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
24
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
31 from farolluz.py2 import urlopen
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
32 from farolluz.renderer import render as render_cvrf
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
33 from farolluz.utils import utcnow
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
36 from werkzeug import secure_filename
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
41 from .vulnerability import vulnerability
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
42 from .producttree import producttree
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
50 app.register_blueprint(vulnerability, url_prefix='/vulnerability')
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
51 app.register_blueprint(producttree, url_prefix='/producttree')
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
57 @app.context_processor
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
58 def cache_content():
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
62
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
63 @app.context_processor
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
64 def doc_properties():
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
65 if not has_current():
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
66 return {'has_current': False}
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
67 cvrf = get_current()
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
68 vulns = [(v.getTitle(), v._ordinal) for v in cvrf._vulnerabilities]
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
69 prods = []
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
70 if cvrf._producttree:
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
71 prods = [(p._name, p._productid) for p in cvrf._producttree._products]
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
72 try:
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
73 cvrf.validate()
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
74 error = None
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
75 except ValidationError as ve:
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
76 error = ve
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
77 return dict(has_current=True, vulnerabilities=vulns, products=prods, error=error)
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
78
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
79 @app.template_filter('secure_filename')
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
80 def makeId(string):
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
81 return secure_filename(string)
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
90 @app.route('/new', methods=['GET', 'POST'])
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
91 def new():
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
92 if request.method != 'POST':
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
109 elif 'local' in request.files:
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
110 upload = request.files['local']
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
111 if not upload.filename.endswith('.xml'):
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
112 flash('Uploaded files should end in .xml', 'danger')
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
113 return redirect(url_for('new'))
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
114 fpath = os.path.join('/tmp', secure_filename(upload.filename))
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
115 upload.save(fpath)
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
116 with open(fpath, 'rt') as f:
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
117 set_current(parse(f))
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
118 os.remove(fpath)
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
119 elif 'text' in request.form:
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
120 set_current(parse(request.form['text'].encode('utf-8')))
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
121 else:
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
124
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
125 @app.route('/render/<format_>')
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
126 @document_required
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
127 def render(format_):
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
128 cvrf = get_current()
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
129 doc = render_cvrf(cvrf, format_ + '.j2')
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
130 return render_template('render.j2', format_=format_, title=cvrf._title, type_=cvrf._type, doc=doc )
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
131
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
132 @app.route('/about')
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
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
4a9f23230eba Initial Release
Benoît Allard <benoit.allard@greenbone.net>
parents:
diff changeset
135

http://farol.wald.intevation.org