mirror of
https://github.com/pi-hole/FTL.git
synced 2024-10-26 16:52:18 +02:00
Add POST /api/teleporter to upload and install backed up configuration
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
@@ -58,9 +58,9 @@ if __name__ == "__main__":
|
||||
verifyer = ResponseVerifyer(ftl, openapi)
|
||||
errors = verifyer.verify_endpoint(path)
|
||||
if len(errors) == 0:
|
||||
print(" " + path + ": OK")
|
||||
print(" " + path + " (" + verifyer.auth_method + " auth): OK")
|
||||
else:
|
||||
print(" " + path + ":")
|
||||
print(" " + path + " (" + verifyer.auth_method + " auth):")
|
||||
for error in errors:
|
||||
print(" - " + error)
|
||||
errs[2] += len(errors)
|
||||
@@ -79,6 +79,5 @@ if __name__ == "__main__":
|
||||
exit(1)
|
||||
|
||||
# If there are no errors, exit with success
|
||||
# (this is important for the CI)
|
||||
print("Everything okay!")
|
||||
exit(0)
|
||||
|
||||
+17
-4
@@ -9,6 +9,7 @@
|
||||
# This file is copyright under the latest version of the EUPL.
|
||||
# Please see LICENSE file for your rights under this license.
|
||||
|
||||
from enum import Enum
|
||||
import requests
|
||||
from typing import List
|
||||
import json
|
||||
@@ -25,6 +26,11 @@ valid = session["session"]["valid"] # True / False
|
||||
sid = session["session"]["sid"] # SID string if succesful, null otherwise
|
||||
"""
|
||||
|
||||
class AuthenticationMethods(Enum):
|
||||
HEADER = 1
|
||||
BODY = 2
|
||||
COOKIE = 3
|
||||
|
||||
# Class to query the FTL API
|
||||
class FTLAPI():
|
||||
def __init__(self, api_url: str):
|
||||
@@ -95,7 +101,7 @@ class FTLAPI():
|
||||
self.session = response["session"]
|
||||
|
||||
# Query the FTL API (GET) and return the response
|
||||
def GET(self, uri: str, params: List[str] = [], expected_mimetype: str = "application/json"):
|
||||
def GET(self, uri: str, params: List[str] = [], expected_mimetype: str = "application/json", authenticate: AuthenticationMethods = AuthenticationMethods.BODY):
|
||||
self.errors = []
|
||||
try:
|
||||
# Add parameters to the URI (if any)
|
||||
@@ -104,13 +110,20 @@ class FTLAPI():
|
||||
|
||||
# Add session ID to the request (if any)
|
||||
data = None
|
||||
headers = None
|
||||
cookies = None
|
||||
if self.session is not None and 'sid' in self.session:
|
||||
data = {"sid": self.session['sid'] }
|
||||
if authenticate == AuthenticationMethods.HEADER:
|
||||
headers = {"X-FTL-SID": self.session['sid']}
|
||||
elif authenticate == AuthenticationMethods.BODY:
|
||||
data = {"sid": self.session['sid'] }
|
||||
elif authenticate == AuthenticationMethods.COOKIE:
|
||||
cookies = {"sid": self.session['sid'] }
|
||||
|
||||
# Query the API
|
||||
if self.verbose:
|
||||
print("GET " + self.api_url + uri + " with data: " + json.dumps(data))
|
||||
with requests.get(url = self.api_url + uri, json = data) as response:
|
||||
with requests.get(url = self.api_url + uri, json = data, headers=headers, cookies=cookies) as response:
|
||||
if self.verbose:
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
if expected_mimetype == "application/json":
|
||||
@@ -149,4 +162,4 @@ class FTLAPI():
|
||||
print("Exception when pre-processing endpoints from FTL: " + str(e))
|
||||
exit(1)
|
||||
|
||||
return self.endpoints
|
||||
return self.endpoints
|
||||
|
||||
@@ -10,10 +10,11 @@
|
||||
# Please see LICENSE file for your rights under this license.
|
||||
|
||||
import io
|
||||
import random
|
||||
import zipfile
|
||||
from libs.openAPI import openApi
|
||||
import urllib.request, urllib.parse
|
||||
from libs.FTLAPI import FTLAPI
|
||||
from libs.FTLAPI import FTLAPI, AuthenticationMethods
|
||||
from collections.abc import MutableMapping
|
||||
|
||||
class ResponseVerifyer():
|
||||
@@ -22,6 +23,8 @@ class ResponseVerifyer():
|
||||
YAML_TYPES = { "string": [str], "integer": [int], "number": [int, float], "boolean": [bool], "array": [list] }
|
||||
TELEPORTER_FILES = ["etc/pihole/gravity.db", "etc/pihole/pihole.toml", "etc/pihole/pihole-FTL.db", "etc/hosts"]
|
||||
|
||||
auth_method = "?"
|
||||
|
||||
def __init__(self, ftl: FTLAPI, openapi: openApi):
|
||||
self.ftl = ftl
|
||||
self.openapi = openapi
|
||||
@@ -62,6 +65,10 @@ class ResponseVerifyer():
|
||||
|
||||
# Get YAML response schema and examples (if applicable)
|
||||
expected_mimetype = True
|
||||
# Assign random authentication method so we can test them all
|
||||
authentication_method = random.choice([a for a in AuthenticationMethods])
|
||||
self.auth_method = authentication_method.name
|
||||
# Check if the expected response is defined in the API specs
|
||||
response_rcode = self.openapi.paths[endpoint][method]['responses'][str(rcode)]
|
||||
if 'content' in response_rcode:
|
||||
content = response_rcode['content']
|
||||
@@ -73,6 +80,8 @@ class ResponseVerifyer():
|
||||
elif 'application/zip' in content:
|
||||
expected_mimetype = 'application/zip'
|
||||
jsonData = content[expected_mimetype]
|
||||
# Thie endpoint requires HEADER authentication
|
||||
authentication_method = AuthenticationMethods.HEADER
|
||||
YAMLresponseSchema = None
|
||||
YAMLresponseExamples = None
|
||||
else:
|
||||
@@ -93,7 +102,7 @@ class ResponseVerifyer():
|
||||
FTLparameters.append(param['name'] + "=" + urllib.parse.quote_plus(str(param['example'])))
|
||||
|
||||
# Get FTL response
|
||||
FTLresponse = self.ftl.GET("/api" + endpoint, FTLparameters, expected_mimetype)
|
||||
FTLresponse = self.ftl.GET("/api" + endpoint, FTLparameters, expected_mimetype, authentication_method)
|
||||
if FTLresponse is None:
|
||||
return self.ftl.errors
|
||||
|
||||
|
||||
Reference in New Issue
Block a user