feat: single source of truth of models + encrypted vault keys

This commit is contained in:
dextmorgn
2025-09-05 10:11:54 +02:00
parent 980bb85af4
commit 780fabb3e6
22 changed files with 269 additions and 413 deletions
+2 -2
View File
@@ -9,11 +9,11 @@ from dotenv import load_dotenv
# Charger les variables d'environnement
load_dotenv()
# Permet les imports comme from app.models.base import Base
# Permet les imports comme from flowsint_core.core.models.base import Base
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
# Importer Base et toutes les tables pour qu'Alembic les détecte
from app.models.models import * # noqa
from flowsint_core.core.models import * # noqa
# Configuration Alembic
config = context.config
@@ -0,0 +1,55 @@
"""change keys structure with iv, salt, version and cypher
Revision ID: 1098b7a5eabc
Revises: 661ff8ef4425
Create Date: 2025-09-04 19:48:43.467378
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '1098b7a5eabc'
down_revision: Union[str, None] = '661ff8ef4425'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
op.execute("TRUNCATE TABLE keys")
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('keys', sa.Column('ciphertext', sa.LargeBinary(), nullable=False))
op.add_column('keys', sa.Column('iv', sa.LargeBinary(), nullable=False))
op.add_column('keys', sa.Column('salt', sa.LargeBinary(), nullable=False))
op.add_column('keys', sa.Column('key_version', sa.String(), nullable=False))
op.alter_column('keys', 'owner_id',
existing_type=sa.UUID(),
nullable=False)
op.alter_column('keys', 'created_at',
existing_type=postgresql.TIMESTAMP(timezone=True),
nullable=False,
existing_server_default=sa.text('now()'))
op.drop_column('keys', 'encrypted_key')
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('keys', sa.Column('encrypted_key', sa.VARCHAR(), autoincrement=False, nullable=False))
op.alter_column('keys', 'created_at',
existing_type=postgresql.TIMESTAMP(timezone=True),
nullable=True,
existing_server_default=sa.text('now()'))
op.alter_column('keys', 'owner_id',
existing_type=sa.UUID(),
nullable=True)
op.drop_column('keys', 'key_version')
op.drop_column('keys', 'salt')
op.drop_column('keys', 'iv')
op.drop_column('keys', 'ciphertext')
# ### end Alembic commands ###
+1 -1
View File
@@ -4,7 +4,7 @@ from jose import JWTError, jwt
from sqlalchemy.orm import Session
from flowsint_core.core.auth import ALGORITHM
from flowsint_core.core.postgre_db import get_db
from app.models.models import Profile
from flowsint_core.core.models import Profile
import os
from dotenv import load_dotenv
+1 -1
View File
@@ -4,7 +4,7 @@ from typing import List
from datetime import datetime
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
from app.models.models import Analysis, Profile
from flowsint_core.core.models import Analysis, Profile
from app.api.deps import get_current_user
from app.api.schemas.analysis import AnalysisRead, AnalysisCreate, AnalysisUpdate
+1 -1
View File
@@ -5,7 +5,7 @@ from flowsint_core.core.auth import verify_password
from flowsint_core.core.auth import create_access_token, get_password_hash
from sqlalchemy.orm import Session
from app.api.schemas.profile import ProfileCreate
from app.models.models import Profile
from flowsint_core.core.models import Profile
from flowsint_core.core.postgre_db import get_db
router = APIRouter()
+1 -1
View File
@@ -11,7 +11,7 @@ from typing import Dict, List, Optional
from datetime import datetime
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
from app.models.models import Chat, ChatMessage, Profile
from flowsint_core.core.models import Chat, ChatMessage, Profile
from app.api.deps import get_current_user
from app.api.schemas.chat import ChatCreate, ChatRead
+2 -2
View File
@@ -1,12 +1,12 @@
from fastapi import APIRouter, Depends, HTTPException, Request
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
from app.models.models import Log, Sketch
from flowsint_core.core.models import Log, Sketch
from flowsint_core.core.events import event_emitter
from sse_starlette.sse import EventSourceResponse
from flowsint_core.core.types import Event
from app.api.deps import get_current_user
from app.models.models import Profile, Sketch
from flowsint_core.core.models import Profile, Sketch
import json
import asyncio
from datetime import datetime, timedelta
+1 -1
View File
@@ -10,7 +10,7 @@ from flowsint_types import Domain, Phrase, Ip, SocialProfile, Organization, Emai
from flowsint_core.core.types import Node, Edge, FlowStep, FlowBranch
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
from app.models.models import Flow, Profile
from flowsint_core.core.models import Flow, Profile
from app.api.deps import get_current_user
from app.api.schemas.flow import FlowRead, FlowCreate, FlowUpdate
from flowsint_types import (
@@ -4,7 +4,7 @@ from typing import List
from datetime import datetime
from sqlalchemy.orm import Session, selectinload
from flowsint_core.core.postgre_db import get_db
from app.models.models import Analysis, Investigation, Profile, Sketch
from flowsint_core.core.models import Analysis, Investigation, Profile, Sketch
from app.api.deps import get_current_user
from app.api.schemas.investigation import (
InvestigationRead,
+17 -24
View File
@@ -1,24 +1,17 @@
from uuid import UUID, uuid4
from fastapi import APIRouter, HTTPException, Depends, status
from typing import List
from flowsint_core.core.vault import Vault
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
from app.models.models import Profile, Key
from flowsint_core.core.models import Profile, Key
from app.api.deps import get_current_user
from app.api.schemas.key import KeyRead, KeyCreate
from datetime import datetime
router = APIRouter()
def obfuscate_key(key: str) -> str:
"""Obfuscate a key by showing only the last 4 characters, replacing others with asterisks."""
if len(key) <= 4:
return key
return "*" * (len(key) - 4) + key[-4:]
# Get the list of all keys for a user
# Get the list of all keys for a user, just the public method for viewing
@router.get("", response_model=List[KeyRead])
def get_keys(
db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)
@@ -28,7 +21,6 @@ def get_keys(
KeyRead(
id=key.id,
owner_id=key.owner_id,
encrypted_key=obfuscate_key(key.encrypted_key),
name=key.name,
created_at=key.created_at,
)
@@ -37,7 +29,7 @@ def get_keys(
return response_data
# Get a key by ID
# Get a key by ID, just the public method for viewing
@router.get("/{id}", response_model=KeyRead)
def get_key_by_id(
id: UUID,
@@ -52,7 +44,6 @@ def get_key_by_id(
response_data = KeyRead(
id=key.id,
owner_id=key.owner_id,
encrypted_key=obfuscate_key(key.encrypted_key),
name=key.name,
created_at=key.created_at,
)
@@ -66,17 +57,19 @@ def create_key(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
new_key = Key(
id=uuid4(),
name=payload.name,
owner_id=current_user.id,
encrypted_key=payload.key,
created_at=datetime.utcnow(),
)
db.add(new_key)
db.commit()
db.refresh(new_key)
return new_key
try:
vault = Vault(db=db, owner_id=current_user.id)
key = vault.set_secret(vault_ref=payload.name, plain_key=payload.key)
if not key:
raise HTTPException(
status_code=500, detail="An error occured creating the key."
)
return key
except Exception as e:
print(e)
raise HTTPException(
status_code=500, detail="An error occured creating the key."
)
# Delete a key by ID
+1 -1
View File
@@ -3,7 +3,7 @@ from fastapi import APIRouter, HTTPException, Depends, status
from typing import List
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
from app.models.models import Scan, Profile
from flowsint_core.core.models import Scan, Profile
from app.api.deps import get_current_user
from app.api.schemas.scan import ScanRead
+1 -1
View File
@@ -7,7 +7,7 @@ from flowsint_core.utils import flatten
from typing import Dict, Any, List
from sqlalchemy.orm import Session
from app.api.schemas.sketch import SketchCreate, SketchRead, SketchUpdate
from app.models.models import Sketch, Profile
from flowsint_core.core.models import Sketch, Profile
from sqlalchemy.orm import Session
from uuid import UUID
from flowsint_core.core.graph_db import neo4j_connection
+1 -1
View File
@@ -4,7 +4,7 @@ from pydantic import BaseModel
from flowsint_core.core.registry import TransformRegistry
from flowsint_core.core.celery import celery
from flowsint_core.core.types import Node, Edge, FlowBranch
from app.models.models import Profile
from flowsint_core.core.models import Profile
from app.api.deps import get_current_user
-1
View File
@@ -11,6 +11,5 @@ class KeyCreate(BaseModel):
class KeyRead(ORMBase):
id: UUID4
owner_id: UUID4
encrypted_key: str
name: str
created_at: datetime
-1
View File
@@ -1 +0,0 @@
# Models module exports
-306
View File
@@ -1,306 +0,0 @@
import uuid
from datetime import datetime
from sqlalchemy import (
String,
Text,
DateTime,
ForeignKey,
Index,
func,
JSON,
Column,
Enum as SQLEnum,
)
from sqlalchemy.dialects.postgresql import UUID as PGUUID, ARRAY, JSONB
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from flowsint_core.core.enums import EventLevel
class Base(DeclarativeBase):
pass
class Feedback(Base):
__tablename__ = "feedbacks"
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
content = mapped_column(Text, nullable=True)
owner_id = mapped_column(
PGUUID(as_uuid=True), ForeignKey("profiles.id"), nullable=True
)
class Investigation(Base):
__tablename__ = "investigations"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
name = mapped_column(Text)
description = mapped_column(Text)
owner_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("profiles.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=True,
)
last_updated_at = mapped_column(DateTime(timezone=True), server_default=func.now())
status = mapped_column(String, server_default="active")
sketches = relationship("Sketch", back_populates="investigation")
analyses = relationship("Analysis", back_populates="investigation")
chats = relationship("Chat", back_populates="investigation")
owner = relationship("Profile", foreign_keys=[owner_id])
__table_args__ = (
Index("idx_investigations_id", "id"),
Index("idx_investigations_owner_id", "owner_id"),
)
class InvestigationsProfiles(Base):
__tablename__ = "investigations_profiles"
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
investigation_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("investigations.id", onupdate="CASCADE", ondelete="CASCADE"),
)
profile_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("profiles.id", onupdate="CASCADE", ondelete="CASCADE"),
)
role = mapped_column(String, server_default="member")
__table_args__ = (
Index("idx_investigations_profiles_investigation_id", "investigation_id"),
Index("idx_investigations_profiles_profile_id", "profile_id"),
# Uniqueness constraint
Index(
"projects_profiles_unique_profile_project",
"profile_id",
"investigation_id",
unique=True,
),
)
class Log(Base):
__tablename__ = "logs"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
content = mapped_column(JSONB, nullable=True)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
sketch_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("sketches.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=True,
)
type = Column(SQLEnum(EventLevel), default=EventLevel.INFO)
class Profile(Base):
__tablename__ = "profiles"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
first_name = mapped_column(Text, nullable=True)
last_name = mapped_column(Text, nullable=True)
avatar_url = mapped_column(Text, nullable=True)
email: Mapped[str] = mapped_column(String, unique=True, nullable=False)
hashed_password: Mapped[str] = mapped_column(String, nullable=False)
is_active: Mapped[bool] = mapped_column(default=True)
class Scan(Base):
__tablename__ = "scans"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
sketch_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("sketches.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=True,
)
status = Column(SQLEnum(EventLevel), default=EventLevel.PENDING)
started_at = Column(DateTime, default=datetime.utcnow)
completed_at = Column(DateTime, nullable=True)
error = Column(Text, nullable=True)
details = Column(JSON, nullable=True)
# Relationships
sketch = relationship("Sketch", back_populates="scans")
def __repr__(self):
return f"<Scan(id={self.id}, status={self.status})>"
class Sketch(Base):
__tablename__ = "sketches"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
title = mapped_column(Text)
description = mapped_column(Text)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
owner_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("profiles.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=True,
)
status = mapped_column(String, server_default="active")
investigation_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("investigations.id", onupdate="CASCADE", ondelete="CASCADE"),
)
investigation = relationship("Investigation", back_populates="sketches")
last_updated_at = mapped_column(DateTime(timezone=True), server_default=func.now())
scans = relationship("Scan", back_populates="sketch")
__table_args__ = (
Index("idx_sketches_investigation_id", "investigation_id"),
Index("idx_sketches_owner_id", "owner_id"),
)
class SketchesProfiles(Base):
__tablename__ = "sketches_profiles"
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
profile_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("profiles.id", onupdate="CASCADE", ondelete="CASCADE"),
)
sketch_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("sketches.id", onupdate="CASCADE", ondelete="CASCADE"),
)
role = mapped_column(String, server_default="editor")
__table_args__ = (
Index("idx_sketches_profiles_sketch_id", "sketch_id"),
Index("idx_sketches_profiles_profile_id", "profile_id"),
Index(
"investigations_profiles_unique_profile_investigation",
"profile_id",
"sketch_id",
unique=True,
),
)
class Flow(Base):
__tablename__ = "flows"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
name = mapped_column(Text, nullable=False)
description = mapped_column(Text, nullable=True)
category = mapped_column(ARRAY(Text), nullable=True)
flow_schema = mapped_column(JSON, nullable=True)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
last_updated_at = mapped_column(DateTime(timezone=True), server_default=func.now())
class Analysis(Base):
__tablename__ = "analyses"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
title = mapped_column(Text, nullable=False)
description = mapped_column(Text, nullable=True)
content = mapped_column(JSON, nullable=True)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
last_updated_at = mapped_column(DateTime(timezone=True), server_default=func.now())
owner_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("profiles.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=True,
)
investigation_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("investigations.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=True,
)
investigation = relationship("Investigation", back_populates="analyses")
__table_args__ = (
Index("idx_analyses_owner_id", "owner_id"),
Index("idx_analyses_investigation_id", "investigation_id"),
)
class Chat(Base):
__tablename__ = "chats"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
title = mapped_column(Text, nullable=False)
description = mapped_column(Text, nullable=True)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
last_updated_at = mapped_column(DateTime(timezone=True), server_default=func.now())
owner_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("profiles.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=True,
)
investigation_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("investigations.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=True,
)
investigation = relationship("Investigation", back_populates="chats")
messages = relationship("ChatMessage", back_populates="chat", cascade="all, delete")
__table_args__ = (
Index("idx_chats_owner_id", "owner_id"),
Index("idx_chats_investigation_id", "investigation_id"),
)
class ChatMessage(Base):
__tablename__ = "messages"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
content = mapped_column(JSON, nullable=True)
context = mapped_column(JSON, nullable=True)
is_bot: Mapped[bool] = mapped_column(default=False)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
chat_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("chats.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
)
chat = relationship("Chat", back_populates="messages")
__table_args__ = (Index("idx_messages_chat_id", "chat_id"),)
class Key(Base):
__tablename__ = "keys"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
name = mapped_column(String, nullable=False)
owner_id = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("profiles.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=True,
)
encrypted_key = mapped_column(String, nullable=False)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
__table_args__ = (
Index("idx_keys_owner_id", "owner_id"),
Index("idx_keys_service", "name"),
)
@@ -179,11 +179,11 @@ function VaultPage() {
<Zap className="w-4 h-4 mr-2" />
Fast Setup
</Badge>
<Badge variant="secondary" className="bg-emerald-50 text-emerald-700 border-emerald-200 px-4 py-2">
<Badge variant="secondary" className="bg-emerald-500/10 text-emerald-700 border-emerald-200/40 px-4 py-2">
<Lock className="w-4 h-4 mr-2" />
Encrypted
</Badge>
<Badge variant="secondary" className="bg-violet-50 text-violet-700 border-violet-200 px-4 py-2">
<Badge variant="secondary" className="bg-violet-500/10 text-violet-700 border-violet-200/40 px-4 py-2">
<Sparkles className="w-4 h-4 mr-2" />
Secure
</Badge>
+4 -5
View File
@@ -1,6 +1,5 @@
export interface Key {
id: string,
name: string,
encrypted_key: string,
created_at: string,
}
id: string,
name: string,
created_at: string,
}
+40 -40
View File
@@ -1038,49 +1038,49 @@ srsly = ">=2.4.0,<3.0.0"
[[package]]
name = "cryptography"
version = "45.0.6"
version = "45.0.7"
description = "cryptography is a package which provides cryptographic recipes and primitives to Python developers."
optional = false
python-versions = "!=3.9.0,!=3.9.1,>=3.7"
groups = ["main"]
files = [
{file = "cryptography-45.0.6-cp311-abi3-macosx_10_9_universal2.whl", hash = "sha256:048e7ad9e08cf4c0ab07ff7f36cc3115924e22e2266e034450a890d9e312dd74"},
{file = "cryptography-45.0.6-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:44647c5d796f5fc042bbc6d61307d04bf29bccb74d188f18051b635f20a9c75f"},
{file = "cryptography-45.0.6-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:e40b80ecf35ec265c452eea0ba94c9587ca763e739b8e559c128d23bff7ebbbf"},
{file = "cryptography-45.0.6-cp311-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:00e8724bdad672d75e6f069b27970883179bd472cd24a63f6e620ca7e41cc0c5"},
{file = "cryptography-45.0.6-cp311-abi3-manylinux_2_28_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:7a3085d1b319d35296176af31c90338eeb2ddac8104661df79f80e1d9787b8b2"},
{file = "cryptography-45.0.6-cp311-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:1b7fa6a1c1188c7ee32e47590d16a5a0646270921f8020efc9a511648e1b2e08"},
{file = "cryptography-45.0.6-cp311-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:275ba5cc0d9e320cd70f8e7b96d9e59903c815ca579ab96c1e37278d231fc402"},
{file = "cryptography-45.0.6-cp311-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:f4028f29a9f38a2025abedb2e409973709c660d44319c61762202206ed577c42"},
{file = "cryptography-45.0.6-cp311-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:ee411a1b977f40bd075392c80c10b58025ee5c6b47a822a33c1198598a7a5f05"},
{file = "cryptography-45.0.6-cp311-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:e2a21a8eda2d86bb604934b6b37691585bd095c1f788530c1fcefc53a82b3453"},
{file = "cryptography-45.0.6-cp311-abi3-win32.whl", hash = "sha256:d063341378d7ee9c91f9d23b431a3502fc8bfacd54ef0a27baa72a0843b29159"},
{file = "cryptography-45.0.6-cp311-abi3-win_amd64.whl", hash = "sha256:833dc32dfc1e39b7376a87b9a6a4288a10aae234631268486558920029b086ec"},
{file = "cryptography-45.0.6-cp37-abi3-macosx_10_9_universal2.whl", hash = "sha256:3436128a60a5e5490603ab2adbabc8763613f638513ffa7d311c900a8349a2a0"},
{file = "cryptography-45.0.6-cp37-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:0d9ef57b6768d9fa58e92f4947cea96ade1233c0e236db22ba44748ffedca394"},
{file = "cryptography-45.0.6-cp37-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:ea3c42f2016a5bbf71825537c2ad753f2870191134933196bee408aac397b3d9"},
{file = "cryptography-45.0.6-cp37-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:20ae4906a13716139d6d762ceb3e0e7e110f7955f3bc3876e3a07f5daadec5f3"},
{file = "cryptography-45.0.6-cp37-abi3-manylinux_2_28_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:2dac5ec199038b8e131365e2324c03d20e97fe214af051d20c49db129844e8b3"},
{file = "cryptography-45.0.6-cp37-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:18f878a34b90d688982e43f4b700408b478102dd58b3e39de21b5ebf6509c301"},
{file = "cryptography-45.0.6-cp37-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:5bd6020c80c5b2b2242d6c48487d7b85700f5e0038e67b29d706f98440d66eb5"},
{file = "cryptography-45.0.6-cp37-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:eccddbd986e43014263eda489abbddfbc287af5cddfd690477993dbb31e31016"},
{file = "cryptography-45.0.6-cp37-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:550ae02148206beb722cfe4ef0933f9352bab26b087af00e48fdfb9ade35c5b3"},
{file = "cryptography-45.0.6-cp37-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:5b64e668fc3528e77efa51ca70fadcd6610e8ab231e3e06ae2bab3b31c2b8ed9"},
{file = "cryptography-45.0.6-cp37-abi3-win32.whl", hash = "sha256:780c40fb751c7d2b0c6786ceee6b6f871e86e8718a8ff4bc35073ac353c7cd02"},
{file = "cryptography-45.0.6-cp37-abi3-win_amd64.whl", hash = "sha256:20d15aed3ee522faac1a39fbfdfee25d17b1284bafd808e1640a74846d7c4d1b"},
{file = "cryptography-45.0.6-pp310-pypy310_pp73-macosx_10_9_x86_64.whl", hash = "sha256:705bb7c7ecc3d79a50f236adda12ca331c8e7ecfbea51edd931ce5a7a7c4f012"},
{file = "cryptography-45.0.6-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:826b46dae41a1155a0c0e66fafba43d0ede1dc16570b95e40c4d83bfcf0a451d"},
{file = "cryptography-45.0.6-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:cc4d66f5dc4dc37b89cfef1bd5044387f7a1f6f0abb490815628501909332d5d"},
{file = "cryptography-45.0.6-pp310-pypy310_pp73-manylinux_2_34_aarch64.whl", hash = "sha256:f68f833a9d445cc49f01097d95c83a850795921b3f7cc6488731e69bde3288da"},
{file = "cryptography-45.0.6-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl", hash = "sha256:3b5bf5267e98661b9b888a9250d05b063220dfa917a8203744454573c7eb79db"},
{file = "cryptography-45.0.6-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:2384f2ab18d9be88a6e4f8972923405e2dbb8d3e16c6b43f15ca491d7831bd18"},
{file = "cryptography-45.0.6-pp311-pypy311_pp73-macosx_10_9_x86_64.whl", hash = "sha256:fc022c1fa5acff6def2fc6d7819bbbd31ccddfe67d075331a65d9cfb28a20983"},
{file = "cryptography-45.0.6-pp311-pypy311_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:3de77e4df42ac8d4e4d6cdb342d989803ad37707cf8f3fbf7b088c9cbdd46427"},
{file = "cryptography-45.0.6-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:599c8d7df950aa68baa7e98f7b73f4f414c9f02d0e8104a30c0182a07732638b"},
{file = "cryptography-45.0.6-pp311-pypy311_pp73-manylinux_2_34_aarch64.whl", hash = "sha256:31a2b9a10530a1cb04ffd6aa1cd4d3be9ed49f7d77a4dafe198f3b382f41545c"},
{file = "cryptography-45.0.6-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl", hash = "sha256:e5b3dda1b00fb41da3af4c5ef3f922a200e33ee5ba0f0bc9ecf0b0c173958385"},
{file = "cryptography-45.0.6-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:629127cfdcdc6806dfe234734d7cb8ac54edaf572148274fa377a7d3405b0043"},
{file = "cryptography-45.0.6.tar.gz", hash = "sha256:5c966c732cf6e4a276ce83b6e4c729edda2df6929083a952cc7da973c539c719"},
{file = "cryptography-45.0.7-cp311-abi3-macosx_10_9_universal2.whl", hash = "sha256:3be4f21c6245930688bd9e162829480de027f8bf962ede33d4f8ba7d67a00cee"},
{file = "cryptography-45.0.7-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:67285f8a611b0ebc0857ced2081e30302909f571a46bfa7a3cc0ad303fe015c6"},
{file = "cryptography-45.0.7-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:577470e39e60a6cd7780793202e63536026d9b8641de011ed9d8174da9ca5339"},
{file = "cryptography-45.0.7-cp311-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:4bd3e5c4b9682bc112d634f2c6ccc6736ed3635fc3319ac2bb11d768cc5a00d8"},
{file = "cryptography-45.0.7-cp311-abi3-manylinux_2_28_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:465ccac9d70115cd4de7186e60cfe989de73f7bb23e8a7aa45af18f7412e75bf"},
{file = "cryptography-45.0.7-cp311-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:16ede8a4f7929b4b7ff3642eba2bf79aa1d71f24ab6ee443935c0d269b6bc513"},
{file = "cryptography-45.0.7-cp311-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:8978132287a9d3ad6b54fcd1e08548033cc09dc6aacacb6c004c73c3eb5d3ac3"},
{file = "cryptography-45.0.7-cp311-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:b6a0e535baec27b528cb07a119f321ac024592388c5681a5ced167ae98e9fff3"},
{file = "cryptography-45.0.7-cp311-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:a24ee598d10befaec178efdff6054bc4d7e883f615bfbcd08126a0f4931c83a6"},
{file = "cryptography-45.0.7-cp311-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:fa26fa54c0a9384c27fcdc905a2fb7d60ac6e47d14bc2692145f2b3b1e2cfdbd"},
{file = "cryptography-45.0.7-cp311-abi3-win32.whl", hash = "sha256:bef32a5e327bd8e5af915d3416ffefdbe65ed975b646b3805be81b23580b57b8"},
{file = "cryptography-45.0.7-cp311-abi3-win_amd64.whl", hash = "sha256:3808e6b2e5f0b46d981c24d79648e5c25c35e59902ea4391a0dcb3e667bf7443"},
{file = "cryptography-45.0.7-cp37-abi3-macosx_10_9_universal2.whl", hash = "sha256:bfb4c801f65dd61cedfc61a83732327fafbac55a47282e6f26f073ca7a41c3b2"},
{file = "cryptography-45.0.7-cp37-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:81823935e2f8d476707e85a78a405953a03ef7b7b4f55f93f7c2d9680e5e0691"},
{file = "cryptography-45.0.7-cp37-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:3994c809c17fc570c2af12c9b840d7cea85a9fd3e5c0e0491f4fa3c029216d59"},
{file = "cryptography-45.0.7-cp37-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:dad43797959a74103cb59c5dac71409f9c27d34c8a05921341fb64ea8ccb1dd4"},
{file = "cryptography-45.0.7-cp37-abi3-manylinux_2_28_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:ce7a453385e4c4693985b4a4a3533e041558851eae061a58a5405363b098fcd3"},
{file = "cryptography-45.0.7-cp37-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:b04f85ac3a90c227b6e5890acb0edbaf3140938dbecf07bff618bf3638578cf1"},
{file = "cryptography-45.0.7-cp37-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:48c41a44ef8b8c2e80ca4527ee81daa4c527df3ecbc9423c41a420a9559d0e27"},
{file = "cryptography-45.0.7-cp37-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:f3df7b3d0f91b88b2106031fd995802a2e9ae13e02c36c1fc075b43f420f3a17"},
{file = "cryptography-45.0.7-cp37-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:dd342f085542f6eb894ca00ef70236ea46070c8a13824c6bde0dfdcd36065b9b"},
{file = "cryptography-45.0.7-cp37-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:1993a1bb7e4eccfb922b6cd414f072e08ff5816702a0bdb8941c247a6b1b287c"},
{file = "cryptography-45.0.7-cp37-abi3-win32.whl", hash = "sha256:18fcf70f243fe07252dcb1b268a687f2358025ce32f9f88028ca5c364b123ef5"},
{file = "cryptography-45.0.7-cp37-abi3-win_amd64.whl", hash = "sha256:7285a89df4900ed3bfaad5679b1e668cb4b38a8de1ccbfc84b05f34512da0a90"},
{file = "cryptography-45.0.7-pp310-pypy310_pp73-macosx_10_9_x86_64.whl", hash = "sha256:de58755d723e86175756f463f2f0bddd45cc36fbd62601228a3f8761c9f58252"},
{file = "cryptography-45.0.7-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:a20e442e917889d1a6b3c570c9e3fa2fdc398c20868abcea268ea33c024c4083"},
{file = "cryptography-45.0.7-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:258e0dff86d1d891169b5af222d362468a9570e2532923088658aa866eb11130"},
{file = "cryptography-45.0.7-pp310-pypy310_pp73-manylinux_2_34_aarch64.whl", hash = "sha256:d97cf502abe2ab9eff8bd5e4aca274da8d06dd3ef08b759a8d6143f4ad65d4b4"},
{file = "cryptography-45.0.7-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl", hash = "sha256:c987dad82e8c65ebc985f5dae5e74a3beda9d0a2a4daf8a1115f3772b59e5141"},
{file = "cryptography-45.0.7-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:c13b1e3afd29a5b3b2656257f14669ca8fa8d7956d509926f0b130b600b50ab7"},
{file = "cryptography-45.0.7-pp311-pypy311_pp73-macosx_10_9_x86_64.whl", hash = "sha256:4a862753b36620af6fc54209264f92c716367f2f0ff4624952276a6bbd18cbde"},
{file = "cryptography-45.0.7-pp311-pypy311_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:06ce84dc14df0bf6ea84666f958e6080cdb6fe1231be2a51f3fc1267d9f3fb34"},
{file = "cryptography-45.0.7-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:d0c5c6bac22b177bf8da7435d9d27a6834ee130309749d162b26c3105c0795a9"},
{file = "cryptography-45.0.7-pp311-pypy311_pp73-manylinux_2_34_aarch64.whl", hash = "sha256:2f641b64acc00811da98df63df7d59fd4706c0df449da71cb7ac39a0732b40ae"},
{file = "cryptography-45.0.7-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl", hash = "sha256:f5414a788ecc6ee6bc58560e85ca624258a55ca434884445440a810796ea0e0b"},
{file = "cryptography-45.0.7-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:1f3d56f73595376f4244646dd5c5870c14c196949807be39e79e7bd9bac3da63"},
{file = "cryptography-45.0.7.tar.gz", hash = "sha256:4b1654dfc64ea479c242508eb8c724044f1e964a47d1d1cacc5132292d851971"},
]
[package.dependencies]
@@ -1093,7 +1093,7 @@ nox = ["nox (>=2024.4.15)", "nox[uv] (>=2024.3.2) ; python_full_version >= \"3.8
pep8test = ["check-sdist ; python_full_version >= \"3.8.0\"", "click (>=8.0.1)", "mypy (>=1.4)", "ruff (>=0.3.6)"]
sdist = ["build (>=1.0.0)"]
ssh = ["bcrypt (>=3.1.5)"]
test = ["certifi (>=2024)", "cryptography-vectors (==45.0.6)", "pretend (>=0.7)", "pytest (>=7.4.0)", "pytest-benchmark (>=4.0)", "pytest-cov (>=2.10.1)", "pytest-xdist (>=3.5.0)"]
test = ["certifi (>=2024)", "cryptography-vectors (==45.0.7)", "pretend (>=0.7)", "pytest (>=7.4.0)", "pytest-benchmark (>=4.0)", "pytest-cov (>=2.10.1)", "pytest-xdist (>=3.5.0)"]
test-randomorder = ["pytest-randomly"]
[[package]]
@@ -5691,4 +5691,4 @@ propcache = ">=0.2.1"
[metadata]
lock-version = "2.1"
python-versions = ">=3.11,<3.13"
content-hash = "ad8083b0650095db07fe8df95331cbf55aa672c6d72657f6bfcf51d084fcffda"
content-hash = "2bd3f070f3b5f8f7b95106882c851939a2fdf0152bcf4f527728ae585e589e38"
+1
View File
@@ -26,6 +26,7 @@ phonenumbers = "^9.0.8"
python-multipart = "^0.0.20"
docker = "^7.1.0"
pytest = "^8.4.2"
cryptography = "^45.0.7"
[tool.poetry.group.dev.dependencies]
pytest-asyncio = "^0.21"
+18 -8
View File
@@ -10,10 +10,11 @@ from sqlalchemy import (
JSON,
Column,
Enum as SQLEnum,
LargeBinary,
)
from sqlalchemy.dialects.postgresql import UUID as PGUUID, ARRAY, JSONB
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from .enums import EventLevel
from flowsint_core.core.enums import EventLevel
class Base(DeclarativeBase):
@@ -195,8 +196,8 @@ class SketchesProfiles(Base):
)
class Transform(Base):
__tablename__ = "transforms"
class Flow(Base):
__tablename__ = "flows"
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
@@ -291,14 +292,23 @@ class Key(Base):
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
name = mapped_column(String, nullable=False)
owner_id = mapped_column(
name: Mapped[str] = mapped_column(String, nullable=False) # ex: "shodan", "whocy"
owner_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("profiles.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=True,
nullable=False,
)
ciphertext: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
iv: Mapped[bytes] = mapped_column(LargeBinary, nullable=False) # 12 bytes
salt: Mapped[bytes] = mapped_column(LargeBinary, nullable=False) # 16/32 bytes
key_version: Mapped[str] = mapped_column(String, nullable=False) # ex: "V1"
created_at: Mapped[str] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
encrypted_key = mapped_column(String, nullable=False)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
__table_args__ = (
Index("idx_keys_owner_id", "owner_id"),
+119 -13
View File
@@ -1,12 +1,25 @@
from ast import Str
import os
from typing import Protocol, Optional
import uuid
from sqlalchemy.orm import Session
from sqlalchemy import select
from .models import Key
from datetime import datetime
import base64
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from dotenv import load_dotenv
# Charger les variables d'environnement
load_dotenv()
class VaultProtocol(Protocol):
def get_secret(self, vault_ref: str) -> Optional[str]: ...
def set_secret(self, vault_ref: str, plain_key: str) -> Key: ...
class Vault(VaultProtocol):
@@ -14,20 +27,112 @@ class Vault(VaultProtocol):
if not owner_id:
raise ValueError("owner_id is required to use the vault.")
self.db = db
self.owner_id = owner_id
def set_secret(self, vault_ref, plain_key) -> None:
try:
ref_uuid = uuid.UUID(vault_ref)
stmt = select(Key).where(Key.id == ref_uuid)
except ValueError:
stmt = select(Key).where(Key.name == vault_ref)
stmt = stmt.where(Key.owner_id == self.owner_id)
result = self.db.execute(stmt)
row = result.scalars().first()
return row.encrypted_key if row else None
self.owner_id = str(owner_id)
self.version = "V1"
def _get_master_key(self) -> bytes:
"""
Retrieve the master key
"""
raw = os.getenv(f"MASTER_VAULT_KEY_{self.version}")
if raw is None:
raise ValueError(f"Missing master key {self.version}")
if raw.startswith("base64:"):
raw = raw[7:]
key = base64.b64decode(raw)
if len(key) != 32:
raise ValueError("Master key must be 32 bytes (256 bits)")
return key
def _derive_user_data_key(self, master_key: bytes, salt: bytes) -> bytes:
"""
Derives an AES-256 from master key, a salt and a context (user_id).
"""
hkdf = HKDF(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
info=self.owner_id.encode("utf-8"),
)
master_key = self._get_master_key()
return hkdf.derive(master_key)
def _encrypt_key(self, plaintext: str):
"""
Encrypts a secret with AES-256-GCM + HKDF.
Returns iv, salt, ciphertext+tag, key version.
"""
master = self._get_master_key()
salt = os.urandom(16)
iv = os.urandom(12)
data_key = self._derive_user_data_key(master, salt)
aesgcm = AESGCM(data_key)
ciphertext = aesgcm.encrypt(
iv,
plaintext.encode("utf-8"),
self.owner_id.encode("utf-8"), # AAD = links secret to user_id
)
return {
"ciphertext": ciphertext,
"iv": iv,
"salt": salt,
}
def _decrypt_key(self, row: dict) -> str:
"""
Decrypts a secret stored in db.
"""
master = self._get_master_key()
data_key = self._derive_user_data_key(master, row.get("salt"))
aesgcm = AESGCM(data_key)
plaintext = aesgcm.decrypt(
row.get("iv"),
row.get("ciphertext"),
self.owner_id.encode("utf-8"),
)
return plaintext.decode("utf-8")
def set_secret(self, vault_ref: str, plain_key: str) -> Key:
"""_summary_
Args:
vault_ref (str): key name (ex: shodan, whoxy)
plain_key (str): actual key
Returns:
Key: pydantic key_object
"""
encrypted_key_dict = self._encrypt_key(plain_key)
new_key = Key(
id=uuid.uuid4(),
name=vault_ref,
iv=encrypted_key_dict.get("iv"),
salt=encrypted_key_dict.get("salt"),
key_version=self.version,
owner_id=self.owner_id,
ciphertext=encrypted_key_dict.get("ciphertext"),
created_at=datetime.utcnow(),
)
self.db.add(new_key)
self.db.commit()
self.db.refresh(new_key)
return new_key
def get_secret(self, vault_ref: str) -> Optional[str]:
"""_summary_
Args:
vault_ref (str): _description_
Returns:
Optional[str]: _description_
"""
try:
ref_uuid = uuid.UUID(vault_ref)
stmt = select(Key).where(Key.id == ref_uuid)
@@ -36,4 +141,5 @@ class Vault(VaultProtocol):
stmt = stmt.where(Key.owner_id == self.owner_id)
result = self.db.execute(stmt)
row = result.scalars().first()
return row.encrypted_key if row else None
decrypted_key = self._decrypt_key(row.dict())
return decrypted_key if row else None