-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ec2d4f
commit 14ff44e
Showing
14 changed files
with
267 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from fastapi import APIRouter, HTTPException | ||
from typing import List | ||
from app import crud, models, schemas | ||
|
||
router = APIRouter() | ||
|
||
@router.post("/", response_model=schemas.Connector) | ||
def create_connector(connector: schemas.ConnectorCreate): | ||
# You'd use the cryptography library to encrypt the config details here before storing them in the database. | ||
encrypted_config = encrypt_config(connector.config) | ||
connector_in_db = crud.connector.create_with_encrypted_config(connector, encrypted_config) | ||
return connector_in_db | ||
|
||
@router.get("/{connector_id}", response_model=schemas.Connector) | ||
def read_connector(connector_id: int): | ||
connector_in_db = crud.connector.get(connector_id) | ||
if connector_in_db is None: | ||
raise HTTPException(status_code=404, detail="Connector not found") | ||
# You'd use the cryptography library to decrypt the config details here before returning them. | ||
decrypted_config = decrypt_config(connector_in_db.config) | ||
connector_in_db.config = decrypted_config | ||
return connector_in_db | ||
|
||
@router.put("/{connector_id}", response_model=schemas.Connector) | ||
def update_connector(connector_id: int, connector: schemas.ConnectorUpdate): | ||
connector_in_db = crud.connector.get(connector_id) | ||
if connector_in_db is None: | ||
raise HTTPException(status_code=404, detail="Connector not found") | ||
# You'd use the cryptography library to encrypt the config details here before storing them in the database. | ||
encrypted_config = encrypt_config(connector.config) | ||
connector_in_db = crud.connector.update_with_encrypted_config(connector_in_db, connector, encrypted_config) | ||
return connector_in_db | ||
|
||
@router.delete("/{connector_id}", response_model=schemas.Connector) | ||
def delete_connector(connector_id: int): | ||
connector_in_db = crud.connector.get(connector_id) | ||
if connector_in_db is None: | ||
raise HTTPException(status_code=404, detail="Connector not found") | ||
crud.connector.remove(connector_id) | ||
return connector_in_db | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from cryptography.fernet import Fernet | ||
from cryptography.hazmat.primitives import hashes | ||
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC | ||
from cryptography.hazmat.backends import default_backend | ||
from base64 import urlsafe_b64encode, urlsafe_b64decode | ||
from app.core.config import get_settings | ||
|
||
class EncryptionManager: | ||
def __init__(self): | ||
settings = get_settings() | ||
key = settings.ENCRYPTION_KEY | ||
salt = settings.ENCRYPTION_SALT | ||
if key is None or salt is None: | ||
raise ValueError('No encryption key or salt provided in config.') | ||
self.key = key.encode() | ||
self.salt = salt.encode() | ||
kdf = PBKDF2HMAC( | ||
algorithm=hashes.SHA256(), | ||
length=32, | ||
salt=self.salt, | ||
iterations=100000, | ||
backend=default_backend() | ||
) | ||
self.fernet_key = urlsafe_b64encode(kdf.derive(self.key)) | ||
self.fernet = Fernet(self.fernet_key) | ||
|
||
def encrypt(self, data): | ||
data = data.encode() | ||
encrypted = self.fernet.encrypt(data) | ||
return encrypted.decode() | ||
|
||
def decrypt(self, encrypted_data): | ||
encrypted_data = encrypted_data.encode() | ||
decrypted = self.fernet.decrypt(encrypted_data) | ||
return decrypted.decode() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from sqlalchemy import Column, Integer, String, DateTime, JSON | ||
from sqlalchemy.sql import func | ||
from app.database import Base | ||
|
||
|
||
class Connector(Base): | ||
__tablename__ = "connectors" | ||
|
||
id = Column(Integer, primary_key=True, index=True) | ||
description = Column(String) | ||
connector_type = Column(String, index=True) | ||
created_at = Column(DateTime(timezone=True), server_default=func.now()) | ||
updated_at = Column(DateTime(timezone=True), onupdate=func.now()) | ||
last_message_sent = Column(DateTime(timezone=True)) | ||
last_message_received = Column(DateTime(timezone=True)) | ||
last_successful_message = Column(DateTime(timezone=True)) | ||
config = Column(JSON) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,4 @@ sendButton.addEventListener('click', () => { | |
}); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.