Skip to content

Commit

Permalink
Worked some more
Browse files Browse the repository at this point in the history
  • Loading branch information
Chelsea486MHz committed Dec 27, 2023
1 parent 9d84363 commit 7c257e2
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ See [configuration.md](./docs/configuration.md)

See [user-manual.md](./docs/user-manual.md)

**API reference**

See [user-manual.md](./docs/api-reference.md)

**Contributions**

See [CONTRIBUTING.md](./contributing.md)
Expand All @@ -38,4 +42,4 @@ See [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md)

**Active contributors**

Chelsea Murgia ([email](mailto:mail@chelsea486mhz.fr))
Chelsea Murgia <[mail@chelsea486mhz.fr](mailto:mail@chelsea486mhz.fr)>
38 changes: 38 additions & 0 deletions compute/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Python deps image
#
FROM python:3.11 AS image-build

RUN apt update
RUN apt install -y --no-install-recommends \
build-essential \
gcc
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Download the Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

#
# Production image
#
FROM python:3.11

# App settings
ENV DEBUG False
WORKDIR /app
EXPOSE 5000
ENV COQUI_TOS_AGREED=1
ENV PYTHONUNBUFFERED=1

# Install dependencies from the build image venv
COPY --from=image-build /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Install the app
COPY app.py .
COPY gunicorn_config.py .

# Run the app
CMD ["gunicorn", "-c", "gunicorn_config.py", "app:app"]
47 changes: 47 additions & 0 deletions compute/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from flask import Flask, request, send_file
from flask_sqlalchemy import SQLAlchemy
import hashlib
import os

# Initialize Flask
app = Flask(__name__)

# Database config
DATABASE_HOST = os.environ.get('DB_HOST')
DATABASE_NAME = os.environ.get('DB_NAME')
DATABASE_USER = os.environ.get('DB_USER')
DATABASE_PASS = os.environ.get('DB_PASS')
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://' + DATABASE_USER + ':' + DATABASE_PASS + '@' + DATABASE_HOST + '/' + DATABASE_NAME

db = SQLAlchemy(app)


# Represents the entries in the database
class Token(db.Model):
__tablename__ = 'token'
id = db.Column(db.Integer, primary_key=True)
token = db.Column(db.String(48), unique=True, nullable=False)


@app.route('/api/version')
def generate_step():
print('Request received')

# Check if the authorization token is included in the request headers
token = request.headers.get('Authorization')
if not token:
return 'Unauthorized', 401
print('Request has authentication token')

# Validate the hashed authorization token against the database
hashed_token = hashlib.sha256(token.encode()).hexdigest()
if not Token.query.filter_by(token=hashed_token).first():
print('Authentication failed')
return 'Unauthorized', 401
print('Request authenticated')

return version


if __name__ == '__main__':
app.run(debug=os.environ.get('DEBUG', False))
22 changes: 22 additions & 0 deletions deployments/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3'

services:
compute-1:
build: ../compute

compute-2:
build: ../compute

compute-3:
build: ../compute

auth:
build: ../auth

manager:
build: ../manager
depends_on:
- compute-1
- compute-2
- compute-3
- auth
90 changes: 90 additions & 0 deletions docs/api-reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
![](./logo.png)

# Stargazer Framework API reference

All Stargazer nodes operate using the Stargazer Framework API. This document is the definite authority on the API.

## Authentication

All Stargazer nodes require an authentication token to be passed as a HTTP header in its API calls:

`$ curl -X POST -H 'Authorization: $TOKEN'`

## Common API calls

The following API calls can be answered by all Stargazer nodes types :

**version**

Returns the current node version.
```
$ curl \
-H 'Authorization: $TOKEN \
-H 'Content-Type: application/json' \
$ENDPOINT/api/version
```

**type**

Returns the node type.
```
$ curl \
-H 'Authorization: $TOKEN \
-H 'Content-Type: application/json' \
$ENDPOINT/api/type
```

## Authentication gateway specific API calls

The following API calls can only be answered by Stargazer authentication gateway nodes :

**token/create**

Registers a new authentication token. Can only be used by authenticating with an admin token. Requires an expiration date to be specified, as well as a boolean specifying if the token is an admin token or not.
```
$ curl \
-H 'Authorization: $TOKEN \
-H 'Content-Type: application/json' \
-d {"admin": "true", "expiration": "90d"}
$ENDPOINT/api/token/create
```

**token/revoke**

Revokes an existing authentication token. Can only be used by authenticating with an admin token. Effective immediately.
```
$ curl \
-H 'Authorization: $TOKEN \
-H 'Content-Type: application/json' \
-d {"token": "$TOKEN_TO_REVOKE"}
$ENDPOINT/api/token/revoke
```

**token/authenticate**

Authenticates a token against the database.
```
$ curl \
-H 'Authorization: $TOKEN \
-H 'Content-Type: application/json' \
-d {"token": "$TOKEN_TO_REVOKE"}
$ENDPOINT/api/token/authenticate
```

## Compute node specific API calls

The following API calls can only be answered by Stargazer compute nodes :

**aaaa**

Returns aaaaaaa.
```
$ curl \
-H 'Authorization: $TOKEN \
-H 'Content-Type: application/json' \
$ENDPOINT/api/aaaaa
```

## Manager node specific API calls

The following API calls can only be answered by Stargazer manager nodes :
8 changes: 8 additions & 0 deletions docs/user-manual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
![](./docs/logo.png)

# Stargazer Framework user manual

---



Empty file added sql/init.sql
Empty file.

0 comments on commit 7c257e2

Please sign in to comment.