feat: Add /api endpoint (#1206)

This commit is contained in:
jbe-dw 2022-06-17 16:48:23 +02:00 committed by GitHub
parent 2a75013de4
commit 1112105683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 6 deletions

View File

@ -1,11 +1,14 @@
from .base import login_manager, handle_bad_request, handle_unauthorized_access, handle_access_forbidden, handle_page_not_found, handle_internal_server_error from .base import (
login_manager, handle_bad_request, handle_unauthorized_access,
handle_access_forbidden, handle_page_not_found, handle_internal_server_error
)
from .index import index_bp from .index import index_bp
from .user import user_bp from .user import user_bp
from .dashboard import dashboard_bp from .dashboard import dashboard_bp
from .domain import domain_bp from .domain import domain_bp
from .admin import admin_bp from .admin import admin_bp
from .api import api_bp from .api import api_bp, apilist_bp
def init_app(app): def init_app(app):
@ -17,6 +20,7 @@ def init_app(app):
app.register_blueprint(domain_bp) app.register_blueprint(domain_bp)
app.register_blueprint(admin_bp) app.register_blueprint(admin_bp)
app.register_blueprint(api_bp) app.register_blueprint(api_bp)
app.register_blueprint(apilist_bp)
app.register_error_handler(400, handle_bad_request) app.register_error_handler(400, handle_bad_request)
app.register_error_handler(401, handle_unauthorized_access) app.register_error_handler(401, handle_unauthorized_access)

View File

@ -36,6 +36,7 @@ import secrets
import string import string
api_bp = Blueprint('api', __name__, url_prefix='/api/v1') api_bp = Blueprint('api', __name__, url_prefix='/api/v1')
apilist_bp = Blueprint('apilist', __name__, url_prefix='/')
apikey_schema = ApiKeySchema(many=True) apikey_schema = ApiKeySchema(many=True)
apikey_single_schema = ApiKeySchema() apikey_single_schema = ApiKeySchema()
@ -47,6 +48,7 @@ user_detailed_schema = UserDetailedSchema()
account_schema = AccountSchema(many=True) account_schema = AccountSchema(many=True)
account_single_schema = AccountSchema() account_single_schema = AccountSchema()
def get_user_domains(): def get_user_domains():
domains = db.session.query(Domain) \ domains = db.session.query(Domain) \
.outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \ .outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \
@ -177,6 +179,11 @@ def before_request():
})) }))
@apilist_bp.route('/api', methods=['GET'])
def index():
return '[{"url": "/api/v1", "version": 1}]', 200
@api_bp.route('/pdnsadmin/zones', methods=['POST']) @api_bp.route('/pdnsadmin/zones', methods=['POST'])
@api_basic_auth @api_basic_auth
@api_can_create_domain @api_can_create_domain
@ -294,7 +301,6 @@ def api_login_delete_zone(domain_name):
domain_id=domain_id) domain_id=domain_id)
history.add() history.add()
except Exception as e: except Exception as e:
current_app.logger.error('Error: {0}'.format(e)) current_app.logger.error('Error: {0}'.format(e))
abort(500) abort(500)
@ -1087,7 +1093,7 @@ def api_zone_forward(server_id, zone_id):
if 200 <= status < 300: if 200 <= status < 300:
current_app.logger.debug("Request to powerdns API successful") current_app.logger.debug("Request to powerdns API successful")
if Setting().get('enable_api_rr_history'): if Setting().get('enable_api_rr_history'):
if request.method in ['POST', 'PATCH'] : if request.method in ['POST', 'PATCH']:
data = request.get_json(force=True) data = request.get_json(force=True)
for rrset_data in data['rrsets']: for rrset_data in data['rrsets']:
history = History(msg='{0} zone {1} record of {2}'.format( history = History(msg='{0} zone {1} record of {2}'.format(
@ -1160,8 +1166,10 @@ def api_get_zones(server_id):
return jsonify(domain_schema.dump(domain_obj_list)), 200 return jsonify(domain_schema.dump(domain_obj_list)), 200
else: else:
resp = helper.forward_request() resp = helper.forward_request()
if (g.apikey.role.name not in ['Administrator', 'Operator'] if (
and resp.status_code == 200): g.apikey.role.name not in ['Administrator', 'Operator']
and resp.status_code == 200
):
domain_list = [d['name'] domain_list = [d['name']
for d in domain_schema.dump(g.apikey.domains)] for d in domain_schema.dump(g.apikey.domains)]
@ -1182,6 +1190,7 @@ def api_server_forward():
resp = helper.forward_request() resp = helper.forward_request()
return resp.content, resp.status_code, resp.headers.items() return resp.content, resp.status_code, resp.headers.items()
@api_bp.route('/servers/<string:server_id>', methods=['GET']) @api_bp.route('/servers/<string:server_id>', methods=['GET'])
@apikey_auth @apikey_auth
def api_server_config_forward(server_id): def api_server_config_forward(server_id):