Skip to content

Commit

Permalink
Initial Implementation (#1)
Browse files Browse the repository at this point in the history
* Initial implementation with proxy endpoints partially implemented
Includes accessor methods for MQ Transactional requests
Includes API request/response schemas for backend services

* Implement basic auth with unit tests

* Implement WolframAlpha API with examples

* Add remaining backend transactional endpoints

* Refactor into modules

* Stub config handling
Add site title and summary

* Implement LLM API route

* Implement configuration aod Docker with default overlay

* Implement JWT token refresh

* Implement rate limiting

* Refactor to `hana` and add documentation

* Update automation for new repo
Fix copy/paste leftovers in setup.py

* Update excluded licenses
Add pythin build tests
Update unit test covered versions

* Update tests and remove default token strings

* Update auth tests

* Update auth tests

* Update documentation and default config
Add test coverage for refresh requests with updated handling

---------

Co-authored-by: Daniel McKnight <[email protected]>
  • Loading branch information
NeonDaniel and NeonDaniel authored Jan 19, 2024
1 parent 885ec6e commit 29a4207
Show file tree
Hide file tree
Showing 33 changed files with 3,837 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/license_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Run License Tests
on:
push:
workflow_dispatch:
pull_request:
branches:
- master
jobs:
license_tests:
uses: neongeckocom/.github/.github/workflows/license_tests.yml@master
with:
packages-exclude: '^(neon-hana|dnspython).*'
28 changes: 28 additions & 0 deletions .github/workflows/propose_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Propose Stable Release
on:
workflow_dispatch:
inputs:
release_type:
type: choice
description: Release Type
options:
- patch
- minor
- major
jobs:
update_version:
uses: neongeckocom/.github/.github/workflows/propose_semver_release.yml@master
with:
branch: dev
release_type: ${{ inputs.release_type }}
update_changelog: True
version_file: "neon_hana/version.py"
pull_changes:
uses: neongeckocom/.github/.github/workflows/pull_master.yml@master
needs: update_version
with:
pr_reviewer: neonreviewers
pr_assignee: ${{ github.actor }}
pr_draft: false
pr_title: ${{ needs.update_version.outputs.version }}
pr_body: ${{ needs.update_version.outputs.changelog }}
24 changes: 24 additions & 0 deletions .github/workflows/publish_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This workflow will generate a release distribution and upload it to PyPI

name: Publish Build and GitHub Release
on:
push:
branches:
- master

jobs:
tag_release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Get Version
run: |
VERSION=$(python setup.py --version)
echo "VERSION=${VERSION}" >> $GITHUB_ENV
- uses: ncipollo/release-action@v1
with:
token: ${{secrets.GITHUB_TOKEN}}
tag: ${{env.VERSION}}
build_and_publish_docker:
uses: neongeckocom/.github/.github/workflows/publish_docker.yml@master
secrets: inherit
23 changes: 23 additions & 0 deletions .github/workflows/publish_test_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This workflow will generate a distribution and upload it to PyPI

name: Publish Alpha Build
on:
push:
branches:
- dev
paths-ignore:
- 'neon_hana/version.py'
- '**/Chart.yaml'

jobs:
publish_alpha_release:
uses: neongeckocom/.github/.github/workflows/publish_alpha_release.yml@master
secrets: inherit
with:
version_file: "version.py"
publish_prerelease: true
publish_pypi: false
build_and_publish_docker:
needs: publish_alpha_release
uses: neongeckocom/.github/.github/workflows/publish_docker.yml@master
secrets: inherit
29 changes: 29 additions & 0 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Run Unit Tests
on:
pull_request:
workflow_dispatch:

jobs:
py_build_tests:
uses: neongeckocom/.github/.github/workflows/python_build_tests.yml@master
with:
python_version: "3.9"
unit_tests:
strategy:
matrix:
python-version: [ 3.9, "3.10", "3.11" ]
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install . -r requirements/test_requirements.txt
- name: Run Tests
run: |
pytest tests
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.9-slim

LABEL vendor=neon.ai \
ai.neon.name="neon-hana"

ENV OVOS_CONFIG_BASE_FOLDER neon
ENV OVOS_CONFIG_FILENAME diana.yaml
ENV XDG_CONFIG_HOME /config

COPY docker_overlay/ /

WORKDIR /app
COPY . /app
RUN pip install /app

CMD ["python3", "/app/neon_hana/app/__main__.py"]
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2021 Neongecko.com Inc.
# BSD-3

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# HANA
HANA (HTTP API for Neon Applications) provides a unified front-end for
accessing services in a [Neon DIANA](https://github.com/NeonGeckoCom/neon-diana-utils) deployment. This API should generally
be hosted as part of a Diana deployment to safely expose services to outside
traffic.

Full API documentation is automatically generated and accessible at `/docs`.

## Configuration
User configuration belongs in `diana.yaml`, mounted in the container path
`/config/neon/`. An example user configuration could be:
```yaml
MQ:
server: mq.mydomain.com
hana:
mq_default_timeout: 10
access_token_ttl: 86400 # 1 day
refresh_token_ttl: 604800 # 1 week
requests_per_minute: 60
access_token_secret: a800445648142061fc238d1f84e96200da87f4f9fa7835cac90db8b4391b117b
refresh_token_secret: 833d369ac73d883123743a44b4a7fe21203cffc956f4c8fec712e71aafa8e1aa
fastapi_title: "My HANA API Host"
fastapi_summary: "Personal HTTP API to access my DIANA backend."
disable_auth: True
```
It is recommended to generate unique values for configured tokens, these are 32
bytes in hexadecimal representation.
## Deployment
You can build a Docker container from this repository, or pull a built container
from the GitHub Container Registry. Start Hana via:
```shell
docker run -p 8080:8080 -v ~/.config/neon:/config/neon ghcr.io/neongeckocom/neon-hana
```
> This assumes you have configuration defined in `~/.config/neon/diana.yaml` and
are using the default port 8080

## Usage
Full API documentation is available at `/docs`. The `/auth/login` endpoint should
be used to generate a `client_id`, `access_token`, and `refresh_token`. The
`access_token` should be included in every request and upon expiration of the
`access_token`, a new token can be obtained from the `auth/refresh` endpoint.
Empty file.
28 changes: 28 additions & 0 deletions docker_overlay/etc/neon/diana.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
log_level: INFO
logs:
level_overrides:
error:
- pika
warning:
- filelock
info:
- openai
debug: []
MQ:
server: neon-rabbitmq
port: 5672
users:
mq_handler:
user: neon_api_utils
password: Klatchat2021
hana:
mq_default_timeout: 10
access_token_ttl: 86400 # 1 day
refresh_token_ttl: 604800 # 1 week
requests_per_minute: 60
access_token_secret: a800445648142061fc238d1f84e96200da87f4f9f784108ac90db8b4391b117b
refresh_token_secret: 833d369ac73d883123743a44b4a7fe21203cffc956f4c8a99be6e71aafa8e1aa
server_host: "0.0.0.0"
server_port: 8080
fastapi_title: "Hana"
fastapi_summary: "HANA (HTTP API for Neon Applications) is the HTTP component of the Device Independent API for Neon Applications (DIANA)"
25 changes: 25 additions & 0 deletions neon_hana/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2021 Neongecko.com Inc.
# BSD-3
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 changes: 47 additions & 0 deletions neon_hana/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2021 Neongecko.com Inc.
# BSD-3
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from fastapi import FastAPI

from neon_hana.app.dependencies import client_manager, jwt_bearer, mq_connector
from neon_hana.app.routers.api_proxy import proxy_route
from neon_hana.app.routers.llm import llm_route
from neon_hana.app.routers.mq_backend import mq_route
from neon_hana.app.routers.auth import auth_route
from neon_hana.version import __version__


def create_app(config: dict):
title = config.get('fastapi_title') or "HANA: HTTP API for Neon Applications"
summary = config.get('fastapi_summary') or ""
version = __version__
app = FastAPI(title=title, summary=summary, version=version)
app.include_router(auth_route)
app.include_router(proxy_route)
app.include_router(mq_route)
app.include_router(llm_route)

return app
42 changes: 42 additions & 0 deletions neon_hana/app/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2021 Neongecko.com Inc.
# BSD-3
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import uvicorn

from ovos_config.config import Configuration

from neon_hana.app import create_app


def main():
config = Configuration().get("hana", {})
app = create_app(config)
uvicorn.run(app, host=config.get('server_host', "0.0.0.0"),
port=config.get('port', 8080))


if __name__ == "__main__":
main()
35 changes: 35 additions & 0 deletions neon_hana/app/dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2021 Neongecko.com Inc.
# BSD-3
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from ovos_config.config import Configuration

from neon_hana.mq_service_api import MQServiceManager
from neon_hana.auth.client_manager import ClientManager, UserTokenAuth

config = Configuration().get("hana") or dict()
mq_connector = MQServiceManager(config)
client_manager = ClientManager(config)
jwt_bearer = UserTokenAuth(client_manager)
Loading

0 comments on commit 29a4207

Please sign in to comment.