Skip to content

Commit

Permalink
add new endpoints for app v2 (#116)
Browse files Browse the repository at this point in the history
* add some new endpoints for app v2

* add oauth

* update EcoVacsHomeProducts
- new attribute model

* add some missing endpoints for app v2

* - use only one level subdomain urls on service list
- add status

* add new used subdomains (for europe)

* improve docker images creation as requirements are change less often

* reformat area list

* add revoke expired oauths

* add docker-compose example

* wait for mqtt start fixes #107

* make images even smaller by copying only required folders instead of all (.git,...)

* fix exception during logging

* return fail if we can't get clean logs

* improve docker docs. Thanks for the hit @Bustel

* fix body not json serializable

* add docker-compose example in separate folder
  • Loading branch information
edenhaus authored Jun 5, 2021
1 parent 9b1ae26 commit df36bdd
Show file tree
Hide file tree
Showing 21 changed files with 2,774 additions and 900 deletions.
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

0 comments on commit df36bdd

Please sign in to comment.