2021-01-02 23:10:01 +00:00
|
|
|
|
2021-01-03 01:07:43 +00:00
|
|
|
import aiohttp
|
2021-01-02 23:10:01 +00:00
|
|
|
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-02 23:10:01 +00:00
|
|
|
|
2021-01-03 01:07:43 +00:00
|
|
|
def authorized_as_hub(id):
|
2021-01-02 23:10:01 +00:00
|
|
|
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-02 23:10:01 +00:00
|
|
|
|
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
|
2021-01-02 23:10:01 +00:00
|
|
|
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")
|
2021-01-02 23:10:01 +00:00
|
|
|
|