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