capsul-flask/capsulflask/spoke_api.py

26 lines
811 B
Python
Raw Normal View History

2021-01-03 01:07:43 +00:00
import aiohttp
from flask import Blueprint
from flask import current_app
from flask import request
from werkzeug.exceptions import abort
from capsulflask.db import get_model, my_exec_info_message
2021-01-03 01:07:43 +00:00
bp = Blueprint("spoke", __name__, url_prefix="/spoke")
2021-01-03 01:07:43 +00:00
def authorized_as_hub(id):
auth_header_value = request.headers.get('Authorization').replace("Bearer ", "")
2021-01-03 01:07:43 +00:00
return auth_header_value == current_app.config["HUB_TOKEN"]
2021-01-03 01:07:43 +00:00
@bp.route("/heartbeat", methods=("POST"))
def heartbeat():
if authorized_as_hub(id):
# make request to hub-domain.com/hub/heartbeat/{current_app.config["SPOKE_HOST_ID"]}
# succeed or fail based on whether the request succeeds or fails.
pass
else:
2021-01-03 01:07:43 +00:00
current_app.logger.info(f"/hosts/heartbeat returned 401: invalid token")
return abort(401, "invalid hub token")