Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new endpoints for app v2 #116

Merged
merged 17 commits into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
# Logs
logs

# Tests
tests

# Examples
examples

# Docs
docs

# Certs
certs

.coverage

# Markdown
*.md

Expand Down
10 changes: 7 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ RUN apk add build-base

FROM base

COPY . /bumper

WORKDIR /bumper
COPY requirements.txt /requirements.txt

# install required python packages
RUN pip3 install -r requirements.txt

WORKDIR /bumper

# Copy only required folders instead of all
COPY create_certs/ create_certs/
COPY bumper/ bumper/

ENTRYPOINT ["python3", "-m", "bumper"]
5 changes: 4 additions & 1 deletion bumper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def strtobool(strbool):
bumper_debug = strtobool(os.environ.get("BUMPER_DEBUG")) or False
use_auth = False
token_validity_seconds = 3600 # 1 hour
oauth_validity_days = 15
db = None

mqtt_server = None
Expand Down Expand Up @@ -236,7 +237,8 @@ async def start():
xmpp_server = XMPPServer((bumper_listen, xmpp_listen_port))

# Start MQTT Server
asyncio.create_task(mqtt_server.broker_coro())
# await start otherwise we get an error connecting the helper bot
await asyncio.create_task(mqtt_server.broker_coro())

# Start MQTT Helperbot
asyncio.create_task(mqtt_helperbot.start_helper_bot())
Expand Down Expand Up @@ -264,6 +266,7 @@ async def start():

async def maintenance():
revoke_expired_tokens()
revoke_expired_oauths()


async def shutdown():
Expand Down
131 changes: 55 additions & 76 deletions bumper/confserver.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
#!/usr/bin/env python3

import json
import asyncio
import logging
import ssl
import string
import random
import bumper
import os
from bumper.models import *
from bumper import plugins
from datetime import datetime, timedelta
import asyncio
from aiohttp import web
import ssl

import aiohttp_jinja2
import jinja2
import uuid
import xml.etree.ElementTree as ET
from aiohttp import web

from bumper import plugins
from bumper.models import *


class aiohttp_filter(logging.Filter):
Expand Down Expand Up @@ -62,13 +57,12 @@ def confserver_app(self):

self.app.add_routes(
[

web.get("", self.handle_base, name="base"),
web.get("", self.handle_base, name="base"),
web.get("/bot/remove/{did}", self.handle_RemoveBot, name='remove-bot'),
web.get("/client/remove/{resource}", self.handle_RemoveClient, name='remove-client'),
web.get("/restart_{service}", self.handle_RestartService, name='restart-service'),
web.post("/lookup.do", self.handle_lookup),

web.post("/newauth.do", self.handle_newauth),
]
)

Expand Down Expand Up @@ -217,8 +211,18 @@ async def handle_base(self, request):
async def log_all_requests(self, request, handler):

if request._match_info.route.name not in self.excludelogging:

to_log = {
"request": {
"route_name": f"{request.match_info.route.name}",
"method": f"{request.method}",
"path": f"{request.path}",
"query_string": f"{request.query_string}",
"raw_path": f"{request.raw_path}",
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
}
}
try:
postbody = None
if request.content_length:
if request.content_type == "application/x-www-form-urlencoded":
postbody = await request.post()
Expand All @@ -232,78 +236,33 @@ async def log_all_requests(self, request, handler):

else:
postbody = await request.post()
else:
postbody = None

to_log["request"]["body"] = f"{postbody}"

response = await handler(request)
if response is None:
confserverlog.warning("Response was null!")
confserverlog.warning(json.dumps(to_log))
return response

to_log["response"] = {
"status": f"{response.status}",
}
if not "application/octet-stream" in response.content_type:
logall = {
"request": {
"route_name": f"{request.match_info.route.name}",
"method": f"{request.method}",
"path": f"{request.path}",
"query_string": f"{request.query_string}",
"raw_path": f"{request.raw_path}",
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
"body": f"{postbody}",
},

"response": {
"response_body": f"{json.loads(response.body)}",
"status": f"{response.status}",
}
}
else:
logall = {
"request": {
"route_name": f"{request.match_info.route.name}",
"method": f"{request.method}",
"path": f"{request.path}",
"query_string": f"{request.query_string}",
"raw_path": f"{request.raw_path}",
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
"body": f"{postbody}",
},

"response": {
"status": f"{response.status}",
}
}
to_log["response"]["body"] = f"{json.loads(response.body)}"

confserverlog.debug(json.dumps(logall))
confserverlog.debug(json.dumps(to_log))

return response

except web.HTTPNotFound as notfound:
confserverlog.debug("Request path {} not found".format(request.raw_path))
requestlog = {
"request": {
"route_name": f"{request.match_info.route.name}",
"method": f"{request.method}",
"path": f"{request.path}",
"query_string": f"{request.query_string}",
"raw_path": f"{request.raw_path}",
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
"body": f"{postbody}",
}
}
confserverlog.debug(json.dumps(requestlog))
confserverlog.debug(json.dumps(to_log))
return notfound

except Exception as e:
confserverlog.exception("{}".format(e))
requestlog = {
"request": {
"route_name": f"{request.match_info.route.name}",
"method": f"{request.method}",
"path": f"{request.path}",
"query_string": f"{request.query_string}",
"raw_path": f"{request.raw_path}",
"raw_headers": f'{",".join(map("{}".format, request.raw_headers))}',
"body": f"{postbody}",
}
}
confserverlog.debug(json.dumps(requestlog))
confserverlog.exception("{}".format(e))
confserverlog.error(json.dumps(to_log))
return e

else:
Expand Down Expand Up @@ -507,6 +466,26 @@ async def handle_lookup(self, request):
except Exception as e:
confserverlog.exception("{}".format(e))

async def handle_newauth(self, request):
# Bumper is only returning the submitted token. No reason yet to create another new token
try:
if request.content_type == "application/x-www-form-urlencoded":
postbody = await request.post()
else:
postbody = json.loads(await request.text())

confserverlog.debug(postbody)

body = {
"authCode": postbody["itToken"],
"result": "ok",
"todo": "result"
}

return web.json_response(body)

except Exception as e:
confserverlog.exception("{}".format(e))

async def disconnect(self):
try:
Expand Down
Loading