Skip to content

Commit

Permalink
Merge pull request #21 from christianstubbe/feature-mongo
Browse files Browse the repository at this point in the history
Features: Database, Blob Upload, Transformations
  • Loading branch information
ehourdebaigt authored Jul 9, 2023
2 parents ff62a0d + 901b20d commit 27fb0e3
Show file tree
Hide file tree
Showing 38 changed files with 476 additions and 500 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,5 @@ override.tf.json
.terraformrc
terraform.rc
toolbox/terraform/.terraform.lock.hcl

creds.json
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,34 @@ pip install -r requirements.txt
pip install -r requirements.txt
```

## Usage
## Development

Explain how to use your project and include code examples if relevant.
### API Testing

```sh
# To deploy manually instead of automatically using a Github action
terraform apply
```
FastAPI comes with built-in support for automatic generation of API documentation, thanks to its use of Python type hints and the underlying Starlette framework. The built-in docs use OpenAPI and JSON Schema under the hood, so you can make use of any tooling that supports these standards.

You can use these OpenAPI specifications to automatically create Postman requests.

Follow these steps:

1. **Run your FastAPI application**: Run your FastAPI app using `uvicorn`. If your app is in a file called `main.py`, you would do:

```bash
uvicorn main:app --reload
```

2. **Access the OpenAPI docs**: Open your browser and go to `http://localhost:8000/docs` (adjust the URL as necessary for your environment). This will display the Swagger UI, which is a visual representation of your API derived from the OpenAPI spec.

3. **Get the OpenAPI JSON**: You can get the actual OpenAPI JSON used to generate this page by going to `http://localhost:8000/openapi.json`.

4. **Import into Postman**:

> Add screenshots or screen gifs here to make it more appealing.
- Open Postman
- Click on `Import`
- Paste the URL from step 3 (i.e., `http://localhost:8000/openapi.json`)
- Select `Import as API`

Postman will create a new collection with all your API endpoints, including parameters, request bodies, etc. Please note that the application needs to be running in order to access the OpenAPI JSON spec.

## Testing

Expand Down
10 changes: 10 additions & 0 deletions toolbox/benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Preliminary Benchmarks

## Requirements

Linux: `sudo apt install apache2-utils`
macOS: *Comes pre-installed*

## Usage

- Download [the Google API Discovery list](https://discovery.googleapis.com/discovery/v1/apis)
21 changes: 21 additions & 0 deletions toolbox/benchmarks/benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import subprocess
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file('../src/creds.json')


def run_ab(benchmark_url, number_of_requests, concurrency_level):
ab_command = f"ab -n {number_of_requests} -c {concurrency_level} {benchmark_url}"
try:
process = subprocess.run(ab_command, shell=True, check=True, stdout=subprocess.PIPE, universal_newlines=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred while trying to run `ab` command: {e}")
return None
output = process.stdout

return output


output = run_ab("http://localhost:8000", 1000, 10)
if output:
print(output)
22 changes: 0 additions & 22 deletions toolbox/serverless.yml

This file was deleted.

2 changes: 0 additions & 2 deletions toolbox/src/access/__init__.py

This file was deleted.

8 changes: 8 additions & 0 deletions toolbox/src/access/adapters/alchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
This class provides an interface for Casbin to use the custom,
provided model for our data structure.
"""
from casbin_sqlalchemy_adapter import Adapter
from access.db import engine

adapter = Adapter(engine)
72 changes: 72 additions & 0 deletions toolbox/src/access/db/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
""" This package for querying and uploading data to different cloud data storage solutions. """
import os
from sqlalchemy import create_engine, MetaData, Column, Integer, String, ForeignKey, Table
from sqlalchemy.orm import relationship, sessionmaker, Session
from sqlalchemy_utils import UUIDType
from sqlalchemy.ext.declarative import declarative_base
from contextlib import contextmanager
from databases import Database
import uuid

username = os.getenv("DB_USERNAME")
password = os.getenv("DB_PASSWORD")
url = os.getenv("DB_URL")
db_name = os.getenv("DB_NAME")
connection_str = f"postgresql://{username}:{password}@{url}/{db_name}"
engine = create_engine(connection_str, echo=True)
database = Database(connection_str)


@contextmanager
def session_scope():
"""Provide a transactional scope around a series of operations."""
session = Session(engine)
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()


meta = MetaData()

Base = declarative_base()


class Purposes(Base):
__tablename__ = "purposes"
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False, unique=True)
# Column("parent_id", Integer, ForeignKey('tree.id')),
# relationship("parent", 'Tree', remote_side=[id])


class Exceptions(Base):
__tablename__ = "exceptions"
id = Column(Integer, primary_key=True)
name = Column(String(100), unique=True)
purpose_id = Column(Integer, ForeignKey('purposes.id'))
exception_list = Column(String(100))


class Transformations(Base):
__tablename__ = "transformations"
id = Column(Integer, primary_key=True)
name = Column(Integer, ForeignKey('purposes.id'), unique=True)
transformation_list = Column("transformation_list", String(100))


SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base.metadata.create_all(bind=engine)


def populate():
"""
Populate the database with basic information (purposes, exceptions, transformations, ...) provided by the config file
"""

pass
47 changes: 47 additions & 0 deletions toolbox/src/access/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
abac = """
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == r.obj.Owner
"""

rbac = """
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
"""

pbac = """
[request_definition]
r = sub, obj, act, purp
[policy_definition]
p = sub, obj, act, purp
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act && r.purp == p.purp
"""
12 changes: 0 additions & 12 deletions toolbox/src/access/models/abac_model.conf

This file was deleted.

14 changes: 0 additions & 14 deletions toolbox/src/access/models/pbac_model.conf

This file was deleted.

15 changes: 0 additions & 15 deletions toolbox/src/access/models/rbac_model.conf

This file was deleted.

Loading

0 comments on commit 27fb0e3

Please sign in to comment.