Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Add API dependencies and default token authorization. Migrate to FastAPI lifespan events. Closes #623. Closes #624. Closes #625.

davidmezzetti8bc6940092fe
diff --git a/Makefile b/Makefile
index c5e64af8..0235039c 100644
--- a/Makefile
+++ b/Makefile
@@ -33,8 +33,7 @@ test:
# Run tests while calculating code coverage
coverage:
coverage run -m unittest discover -v -k testann -s ${TEST_DIR}
- coverage run -m unittest discover -v -k testapi.testcluster -s ${TEST_DIR}
- coverage run -m unittest discover -v -k testapi.testextension -s ${TEST_DIR}
+ coverage run -m unittest discover -v -k testapi.testauthorization -k testapi.testcluster -k testapi.testextension -s ${TEST_DIR}
coverage run -m unittest discover -v -k testapp -s ${TEST_DIR}
coverage run -m unittest discover -v -k testarchive -s ${TEST_DIR}
coverage run -m unittest discover -v -k testcloud -s ${TEST_DIR}
diff --git a/setup.py b/setup.py
index b609f61a..124b4167 100644
--- a/setup.py
+++ b/setup.py
@@ -29,7 +29,7 @@ extras["dev"] = [
extras["api"] = [
"aiohttp>=3.8.1",
- "fastapi>=0.61.1",
+ "fastapi>=0.94.0",
"uvicorn>=0.12.1",
]
diff --git a/src/python/txtai/api/__init__.py b/src/python/txtai/api/__init__.py
index 8ee8e447..3c56da41 100644
--- a/src/python/txtai/api/__init__.py
+++ b/src/python/txtai/api/__init__.py
@@ -4,6 +4,7 @@ API imports
# Conditional import
try:
+ from .authorization import Authorization
from .application import app, start
from .base import API
from .cluster import Cluster
diff --git a/src/python/txtai/api/application.py b/src/python/txtai/api/application.py
index 24af2888..eed4233c 100644
--- a/src/python/txtai/api/application.py
+++ b/src/python/txtai/api/application.py
@@ -6,19 +6,14 @@ import inspect
import os
import sys
-from fastapi import APIRouter, FastAPI
+from fastapi import APIRouter, Depends, FastAPI
+from .authorization import Authorization
from .base import API
from .factory import Factory
from ..app import Application
-# API instance
-app = FastAPI()
-
-# Global API instance
-INSTANCE = None
-
def get():
"""
@@ -31,6 +26,31 @@ def get():
return INSTANCE
+def create():
+ """
+ Creates a FastAPI instance.
+ """
+
+ # Application dependencies
+ dependencies = []
+
+ # Default implementation of token authorization
+ token = os.environ.get("TOKEN")
+ if token:
+ dependencies.append(Depends(Authorization(token)))
+
+ # Add custom dependencies
+ deps = os.environ.get("DEPENDENCIES")
+ if deps:
+ for dep in deps.split(","):
+ # Create and add dependency
+ dep = Factory.get(dep.strip())()
+ dependencies.append(Depends(dep))
+
+ # Create FastAPI application
+ return FastAPI(lifespan=lifespan, dependencies=dependencies if dependencies else None)
+
+
def apirouters():
"""
Lists available APIRouters.
@@ -50,20 +70,22 @@ def apirouters():
return available
[email protected]_event("startup")
-def start():
+def lifespan(application):
"""
- FastAPI startup event. Loads API instance.
+ FastAPI lifespan event handler.
+
+ Args:
+ application: FastAPI application to initialize
"""
# pylint: disable=W0603
global INSTANCE
# Load YAML settings
- config = Application.read(os.getenv("CONFIG"))
+ config = Application.read(os.environ.get("CONFIG"))
# Instantiate API instance
- api = os.getenv("API_CLASS")
+ api = os.environ.get("API_CLASS")
INSTANCE = Factory.create(config, api) if api else API(config)
# Get all known routers
@@ -72,20 +94,34 @@ def start():
# Conditionally add routes based on configuration
for name, router in routers.items():
if name in config:
- app.include_router(router)
+ application.include_router(router)
# Special case for embeddings clusters
if "cluster" in config and "embeddings" not in config:
- app.include_router(routers["embeddings"])
+ application.include_router(routers["embeddings"])
# Special case to add similarity instance for embeddings
if "embeddings" in config and "similarity" not in config:
- app.include_router(routers["similarity"])
+ application.include_router(routers["similarity"])
# Execute extensions if present
- extensions = os.getenv("EXTENSIONS")
+ extensions = os.environ.get("EXTENSIONS")
if extensions:
for extension in extensions.split(","):
# Create instance and execute extension
extension = Factory.get(extension.strip())()
- extension(app)
+ extension(application)
+
+ yield
+
+
+def start():
+ """
+ Runs application lifespan handler.
+ """
+
+ list(lifespan(app))
+
+
+# FastAPI instance txtai API instances
+app, INSTANCE = create(), None
diff --git a/src/python/txtai/api/authorization.py b/src/python/txtai/api/authorization.py
new file mode 100644
index 00000000..21eda15c
--- /dev/null
+++ b/src/python/txtai/api/authorization.py
@@ -0,0 +1,53 @@
+"""
+Authorization module
+"""
+
+import hashlib
+import os
+
+from fastapi import Header, HTTPException
+
+
+class Authorization:
+ """
+ Basic token authorization.
+ """
+
+ def __init__(self, token=None):
+ """
+ Creates a new Authorization instance.
+
+ Args:
+ token: SHA-256 hash of token to check
+ """
+
+ self.token = token if token else os.environ.get("TOKEN")
+
+ def __call__(self, authorization: str = Header(default=None)):
+ """
+ Validates authorization header is present and equal to current token.
+
+ Args:
+ authorization: authorization header
+ """
+
+ if not authorization or self.token != self.digest(authorization):
+ raise HTTPException(status_code=401, detail="Invalid Authorization Token")
+
+ def digest(self, authorization):
+ """
+ Computes a SHA-256 hash for input authorization token.
+
+ Args:
+ authorization: authorization header
+
+ Returns:
+ SHA-256 hash of authorization token
+ """
+
+ # Replace Bearer prefix
+ prefix = "Bearer "
+ token = authorization[len(prefix) :] if authorization.startswith(prefix) else authorization
+
+ # Compute SHA-256 hash
+ return hashlib.sha256(token.encode("utf-8")).hexdigest()
diff --git a/src/python/txtai/pipeline/data/textractor.py b/src/python/txtai/pipeline/data/textractor.py
index 1d1f9eaf..3a428841 100644
--- a/src/python/txtai/pipeline/data/textractor.py
+++ b/src/python/txtai/pipeline/data/textractor.py
@@ -69,7 +69,7 @@ class Textractor(Segmentation):
# Get path to java executable if path not set
if not path:
- path = os.getenv("TIKA_JAVA", "java")
+ path = os.environ.get("TIKA_JAVA", "java")
# pylint: disable=R1732,W0702,W1514
# Check if java binary is available on path
diff --git a/test/python/testapi/testauthorization.py b/test/python/testapi/testauthorization.py
new file mode 100644
index 00000000..fc7645d6
--- /dev/null
+++ b/test/python/testapi/testauthorization.py
@@ -0,0 +1,73 @@
+"""
+Authorization module tests
+"""
+
+import hashlib
+import os
+import tempfile
+import unittest
+
+from unittest.mock import patch
+
+from fastapi.testclient import TestClient
+
+from txtai.api import application
+
+
+class TestAuthorization(unittest.TestCase):
+ """
+ API tests for token authorization.
+ """
+
+ @staticmethod
+ @patch.dict(
+ os.environ,
+ {
+ "CONFIG": os.path.join(tempfile.gettempdir(), "testapi.yml"),
+ "DEPENDENCIES": "txtai.api.Authorization",
+ "TOKEN": hashlib.sha256("token".encode("utf-8")).hexdigest(),
+ },
+ )
+ def start():
+ """
+ Starts a mock FastAPI client.
+ """
+
+ config = os.path.join(tempfile.gettempdir(), "testapi.yml")
+
+ with open(config, "w", encoding="utf-8") as output:
+ output.write("embeddings:\n")
+
+ # Create new application and set on client
+ application.app = application.create()
+ client = TestClient(application.app)
+ application.start()
+
+ return client
+
+ @classmethod
+ def setUpClass(cls):
+ """
+ Create API client on creation of class.
+ """
+
+ cls.client = TestAuthorization.start()
+
+ def testInvalid(self):
+ """
+ Test invalid authorization
+ """
+
+ response = self.client.get("count")
+ self.assertEqual(response.status_code, 401)
+
+ response = self.client.get("search?query=test", headers={"Authorization": "Bearer invalid"})
+ self.assertEqual(response.status_code, 401)
+
+ def testValid(self):
+ """
+ Test valid authorization
+ """
+
+ results = self.client.get("search?query=test", headers={"Authorization": "Bearer token"}).json()
+ self.assertEqual(results, [])
diff --git a/test/python/testapi/testcluster.py b/test/python/testapi/testcluster.py
index c89ad3f4..77ff9878 100644
--- a/test/python/testapi/testcluster.py
+++ b/test/python/testapi/testcluster.py
@@ -14,7 +14,7 @@ from unittest.mock import patch
from fastapi.testclient import TestClient
-from txtai.api import app, start
+from txtai.api import application
# Configuration for an embeddings cluster
CLUSTER = """
@@ -109,8 +109,10 @@ class TestCluster(unittest.TestCase):
with open(config, "w", encoding="utf-8") as output:
output.write(CLUSTER)
- client = TestClient(app)
- start()
+ # Create new application and set on client
+ application.app = application.create()
+ client = TestClient(application.app)
+ application.start()
return client
diff --git a/test/python/testapi/testembeddings.py b/test/python/testapi/testembeddings.py
index e07df500..8d506f33 100644
--- a/test/python/testapi/testembeddings.py
+++ b/test/python/testapi/testembeddings.py
@@ -11,7 +11,7 @@ from unittest.mock import patch
from fastapi.testclient import TestClient
-from txtai.api import API, app, start
+from txtai.api import API, application
# Configuration for a read/write embeddings index
INDEX = """
@@ -41,6 +41,9 @@ path: %s
# Allow indexing of documents
writable: False
+
+# Embeddings settings
+embeddings:
"""
# Configuration for an index with custom functions
@@ -85,8 +88,10 @@ class TestEmbeddings(unittest.TestCase):
with open(config, "w", encoding="utf-8") as output:
output.write(yaml % index)
- client = TestClient(app)
- start()
+ # Create new application and set on client
+ application.app = application.create()
+ client = TestClient(application.app)
+ application.start()
return client
diff --git a/test/python/testapi/testextension.py b/test/python/testapi/testextension.py
index 4e21dddb..3fed08b2 100644
--- a/test/python/testapi/testextension.py
+++ b/test/python/testapi/testextension.py
@@ -85,6 +85,8 @@ class TestExtension(unittest.TestCase):
with open(config, "w", encoding="utf-8") as output:
output.write(PIPELINES)
+ # Create new application and set on client
+ application.app = application.create()
client = TestClient(application.app)
application.start()
diff --git a/test/python/testapi/testpipelines.py b/test/python/testapi/testpipelines.py
index fb28a6a3..74a8bf42 100644
--- a/test/python/testapi/testpipelines.py
+++ b/test/python/testapi/testpipelines.py
@@ -11,7 +11,7 @@ from unittest.mock import patch
from fastapi.testclient import TestClient
-from txtai.api import API, app, start
+from txtai.api import API, application
# pylint: disable = C0411
from utils import Utils
@@ -76,8 +76,10 @@ class TestPipelines(unittest.TestCase):
with open(config, "w", encoding="utf-8") as output:
output.write(PIPELINES)
- client = TestClient(app)
- start()
+ # Create new application and set on client
+ application.app = application.create()
+ client = TestClient(application.app)
+ application.start()
return client
diff --git a/test/python/testapi/testworkflow.py b/test/python/testapi/testworkflow.py
index 83ed456d..422b3241 100644
--- a/test/python/testapi/testworkflow.py
+++ b/test/python/testapi/testworkflow.py
@@ -14,7 +14,7 @@ from unittest.mock import patch
from fastapi.testclient import TestClient
-from txtai.api import app, start, API
+from txtai.api import API, application
# Configuration for workflows
WORKFLOWS = """
@@ -149,8 +149,10 @@ class TestWorkflow(unittest.TestCase):
with open(config, "w", encoding="utf-8") as output:
output.write(WORKFLOWS)
- client = TestClient(app)
- start()
+ # Create new application and set on client
+ application.app = application.create()
+ client = TestClient(application.app)
+ application.start()
return client