-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
188 lines (161 loc) · 6.34 KB
/
app.py
File metadata and controls
188 lines (161 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import logging
import os
import subprocess
import sys
from flask import Flask, current_app, flash, redirect, render_template, request, url_for
from flask_minify import minify
from flask_restx import Api
from werkzeug.middleware.proxy_fix import ProxyFix
import zoneforge.modal_data
from zoneforge.api.authentication import LoginResource, SignupResource
from zoneforge.api.authentication import api as ns_auth
from zoneforge.api.rbac import api as ns_rbac
from zoneforge.api.records import DnsRecord
from zoneforge.api.records import api as ns_record
from zoneforge.api.status import api as ns_status
from zoneforge.api.types import RecordTypeResource
from zoneforge.api.types import api as ns_types
from zoneforge.api.zones import DnsZone
from zoneforge.api.zones import api as ns_zone
from zoneforge.api.zones import get_zones
from zoneforge.db import db
def get_logging_conf() -> dict:
log_config = {}
log_config["level"] = os.environ.get("LOG_LEVEL", "WARNING").upper()
log_config["format"] = (
"%(levelname)s [%(filename)-s%(funcName)s():%(lineno)s]: %(message)s"
)
if not os.environ.get("CONTAINER", False):
log_config["format"] = f"[%(asctime)s] {log_config['format']}"
log_config["handlers"] = [logging.StreamHandler(sys.stdout)]
return log_config
# pylint: disable=too-many-statements
def create_app():
# Flask App setup
app = Flask(__name__, static_folder="static", static_url_path="")
# by default, only specific file extensions (html, xml, etc) are escaped. We'll escape them all since we're using "j2"
app.jinja_options = {
"autoescape": True,
}
# Configuration with environment variables and defaults
log_config = get_logging_conf()
logging.basicConfig(**log_config)
try:
git_tag = (
subprocess.run(
["git", "describe", "--tags"], capture_output=True, check=False
)
.stdout.decode("utf-8")
.rstrip("\n")
)
except FileNotFoundError:
git_tag = None
app.config["VERSION"] = os.environ.get("VERSION", git_tag)
app.config["ZONE_FILE_FOLDER"] = os.environ.get(
"ZONE_FILE_FOLDER", "./lib/examples"
)
app.config["DEFAULT_ZONE_TTL"] = os.environ.get("DEFAULT_ZONE_TTL", 86400)
app.config["AUTH_ENABLED"] = (
os.environ.get("AUTH_ENABLED", "false").lower() == "true"
)
app.config["SECRET_KEY"] = os.environ.get("AUTH_SECRET_KEY", "secret_key")
app.config["TOKEN_SECRET"] = os.environ.get("AUTH_TOKEN_SECRET", "token_secret")
app.config["REFRESH_TOKEN_SECRET"] = os.environ.get(
"AUTH_REFRESH_TOKEN_SECRET", "refresh_token_secret"
)
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"AUTH_DB_URI", "sqlite:///zoneinfo.db"
)
# Controls whether Flask-RESTx suggests similar endpoints when a 404 Not Found error occurs
app.config["ERROR_404_HELP"] = False
if app.config["AUTH_ENABLED"]:
logging.info("authentication enabled, setting up database")
db.init_app(app)
with app.app_context():
db.create_all()
minify(app=app, html=True, js=True, cssless=True, static=True)
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
# API Setup
api = Api(app, prefix="/api", doc="/api", validate=True)
@app.route("/", methods=["GET"])
def home():
zf_zone = DnsZone()
try:
zones = zf_zone.get()
# generic except to actually let the homepage render, even if internal error
except: # pylint: disable=bare-except
zones = []
zone_create_defaults = (
zoneforge.modal_data.ZONE_DEFAULTS
| zoneforge.modal_data.ZONE_PRIMARY_NS_DEFAULTS
)
return render_template(
"home.html.j2",
zones=zones,
modals=[
zoneforge.modal_data.ZONE_CREATION,
zoneforge.modal_data.ZONE_CREATION_XFR,
],
modal_default_values=zone_create_defaults,
)
@app.route("/zone/<string:zone_name>", methods=["GET"])
def zone(zone_name):
zone = get_zones(
zonefile_folder=current_app.config["ZONE_FILE_FOLDER"], zone_name=zone_name
)[0].to_response()
zf_record = DnsRecord()
records = zf_record.get(zone_name=zone_name)
current_zone_data = {
"name": zone_name,
"soa_ttl": zone["soa"]["ttl"],
"admin_email": zone["soa"]["data"]["rname"],
"refresh": zone["soa"]["data"]["refresh"],
"retry": zone["soa"]["data"]["retry"],
"expire": zone["soa"]["data"]["expire"],
"minimum": zone["soa"]["data"]["minimum"],
"primary_ns": zone["soa"]["data"]["mname"],
}
record_types_list = RecordTypeResource().get()
user_sort = request.args.get("sort", "name")
user_sort_order = request.args.get("sort_order", "desc")
return render_template(
"zone.html.j2",
zone=zone,
modal=zoneforge.modal_data.ZONE_EDIT,
modal_default_values=current_zone_data,
records=records,
record_types=record_types_list,
record_sort=user_sort,
record_sort_order=user_sort_order,
)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
login_response = LoginResource().post()
if login_response[1] != 200:
flash(login_response[0])
return render_template("login.html.j2")
return redirect(url_for("home"))
return render_template("login.html.j2")
@app.route("/signup", methods=["GET", "POST"])
def signup():
if request.method == "POST":
signup_response = SignupResource().post()
flash(signup_response[0])
if signup_response[1] != 200:
return render_template("signup.html.j2")
return redirect(url_for("login"))
return render_template("signup.html.j2")
api.add_namespace(ns_status)
api.add_namespace(ns_zone)
api.add_namespace(ns_record)
api.add_namespace(ns_types)
api.add_namespace(ns_auth)
api.add_namespace(ns_rbac)
return app
# pylint: enable=too-many-statements
if __name__ == "__main__":
dev = create_app()
dev.run()
else:
production = create_app()