Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧refactor: aws account add filter and extra vairables params #225

Merged
merged 11 commits into from
Dec 31, 2023
Merged
9 changes: 6 additions & 3 deletions sld-api-backend/src/aws/api/container/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ async def create_new_aws_profile(
status_code=409,
detail="The squad or environment field must have a value that is not a string.",
)
db_aws_account = crud_aws.get_squad_aws_profile(
db=db, squad=aws.squad, environment=aws.environment
filters = schemas_aws.AwsAccountFilter()
filters.squad = aws.squad
filters.environment = aws.environment
db_aws_account = crud_aws.get_all_aws_profile(
db=db, filters=filters
)
if db_aws_account:
raise HTTPException(status_code=409, detail="Account already exists")
try:
result = crud_aws.create_aws_profile(db=db, aws=aws)
crud_aws.create_aws_profile(db=db, aws=aws)
crud_activity.create_activity_log(
db=db,
username=current_user.username,
Expand Down
14 changes: 8 additions & 6 deletions sld-api-backend/src/aws/api/container/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
from sqlalchemy.orm import Session

from src.aws.infrastructure import repositories as crud_aws
from src.aws.domain.entities import aws as schemas_aws
from src.shared.security import deps
from src.users.domain.entities import users as schemas_users
from src.users.infrastructure import repositories as crud_users


async def get_all_aws_accounts(
current_user: schemas_users.User = Depends(deps.get_current_active_user),
skip: int = 0,
limit: int = 100,
db: Session = Depends(deps.get_db),
current_user: schemas_users.User = Depends(deps.get_current_active_user),
filters: schemas_aws.AwsAccountFilter = Depends(schemas_aws.AwsAccountFilter),

):
# Check if the user has privileges
if not crud_users.is_master(db, current_user):
return crud_aws.get_squad_aws_profile(
db=db, squad=current_user.squad, environment=None
)
return crud_aws.get_all_aws_profile(db=db)
filters.squad = current_user.squad
return crud_aws.get_all_aws_profile(db=db, filters=filters, skip=skip, limit=limit)
2 changes: 1 addition & 1 deletion sld-api-backend/src/aws/api/v1/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def create_new_aws_profile(

@router.get("/", status_code=200, response_model=list[schemas_aws.AwsAccountResponse])
async def get_all_aws_accounts(
get_aws_profile: schemas_aws.AwsAsumeProfile = Depends(get.get_all_aws_accounts),
get_aws_profile: schemas_aws.AwsAccountResponse = Depends(get.get_all_aws_accounts),
):
return get_aws_profile

Expand Down
43 changes: 30 additions & 13 deletions sld-api-backend/src/aws/domain/entities/aws.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
from typing import Optional
from typing import Optional, Dict, Any

from pydantic import BaseModel, Field, constr
from pydantic import BaseModel, constr


class AwsBase(BaseModel):
squad: constr(strip_whitespace=True)
environment: constr(strip_whitespace=True)
access_key_id: constr(strip_whitespace=True)
secret_access_key: Optional[constr(strip_whitespace=True)] = Field(
None, example="string"
)
secret_access_key: constr(strip_whitespace=True)
default_region: constr(strip_whitespace=True)
extra_variables: Optional[Dict[str, Any]] = None

default_region: constr(strip_whitespace=True)

class AwsAsumeProfile(AwsBase):
profile_name: Optional[constr(strip_whitespace=True)] = None
role_arn: Optional[constr(strip_whitespace=True)] = None
source_profile: Optional[constr(strip_whitespace=True)] = None


class Aws(AwsBase):
Expand All @@ -26,10 +22,31 @@ class Aws(AwsBase):
class Config:
from_attributes = True


class AwsAccountResponse(BaseModel):
id: int
squad: constr(strip_whitespace=True)
environment: constr(strip_whitespace=True)
profile_name: Optional[constr(strip_whitespace=True)] = None
role_arn: Optional[constr(strip_whitespace=True)] = None
source_profile: Optional[constr(strip_whitespace=True)] = None
squad: str
environment: str
default_region: Optional[str]
role_arn: Optional[str]
extra_variables: Optional[Dict[str, Any]]

class Config:
from_attributes = True


class AwsAccountResponseRepo(AwsAccountResponse):
access_key_id: str
secret_access_key: str

class Config:
from_attributes = True


class AwsAccountFilter(BaseModel):
id: Optional[int] = None
squad: Optional[str] = None
access_key_id: Optional[str] = None
environment: Optional[str] = None
default_region: Optional[str] = None
role_arn: Optional[str] = None
5 changes: 3 additions & 2 deletions sld-api-backend/src/aws/infrastructure/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime

from config.database import Base
from sqlalchemy import Column, DateTime, Integer, String, UniqueConstraint
from sqlalchemy import Column, DateTime, Integer, String, UniqueConstraint, JSON


class Aws_provider(Base):
Expand All @@ -12,8 +12,9 @@ class Aws_provider(Base):
access_key_id = Column(String(200), nullable=False)
secret_access_key = Column(String(200), nullable=False)
default_region = Column(String(200))
profile_name = Column(String(200), nullable=False)
profile_name = Column(String(200), nullable=True)
role_arn = Column(String(200), nullable=True)
source_profile = Column(String(200), nullable=True)
extra_variables = Column(JSON, nullable=True)
created_at = Column(DateTime, default=datetime.datetime.now())
__table_args__ = (UniqueConstraint("squad", "environment"),)
150 changes: 72 additions & 78 deletions sld-api-backend/src/aws/infrastructure/repositories.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import datetime
from typing import List

from sqlalchemy.orm import Session
from sqlalchemy import desc, or_


import src.aws.infrastructure.models as models
from src.aws.domain.entities import aws as schemas_aws
from src.shared.security.vault import vault_decrypt, vault_encrypt
from src.shared.security.vault import vault_encrypt


@vault_encrypt
Expand All @@ -15,35 +18,21 @@ def encrypt(secreto):
raise err


@vault_decrypt
def decrypt(secreto):
try:
return secreto
except Exception as err:
raise err


def create_aws_profile(db: Session, aws: schemas_aws.AwsAsumeProfile):
encrypt_access_key_id = encrypt(aws.access_key_id)
encrypt_secret_access_key = encrypt(aws.secret_access_key)
encrypted_extra_variables = {key: encrypt(val) for key, val in aws.extra_variables.items()} if aws.extra_variables else None

db_aws = models.Aws_provider(
access_key_id=encrypt_access_key_id,
secret_access_key=encrypt_secret_access_key,
environment=aws.environment,
default_region=aws.default_region,
profile_name=aws.profile_name,
role_arn=aws.role_arn,
source_profile=aws.source_profile,
extra_variables=encrypted_extra_variables,
created_at=datetime.datetime.now(),
squad=aws.squad,
)
check_None = [None, "string"]
if db_aws.role_arn in check_None:
db_aws.role_arn = ""
if db_aws.profile_name in check_None:
db_aws.profile_name = ""
if db_aws.source_profile in check_None:
db_aws.source_profile = ""
try:
db.add(db_aws)
db.commit()
Expand All @@ -53,84 +42,89 @@ def create_aws_profile(db: Session, aws: schemas_aws.AwsAsumeProfile):
raise err


def get_credentials_aws_profile(db: Session, environment: str, squad: str):
get_access_key = (
db.query(models.Aws_provider.access_key_id)
.filter(models.Aws_provider.environment == environment)
.filter(models.Aws_provider.squad == squad)
.first()
)
get_secret_access_key = (
db.query(models.Aws_provider.secret_access_key)
.filter(models.Aws_provider.environment == environment)
.filter(models.Aws_provider.squad == squad)
.first()
)
default_region = (
db.query(models.Aws_provider.default_region)
.filter(models.Aws_provider.environment == environment)
.filter(models.Aws_provider.squad == squad)
.first()
)
profile_name = (
db.query(models.Aws_provider.profile_name)
def get_credentials_aws_profile(db: Session, environment: str, squad: str) -> schemas_aws.AwsAccountResponseRepo:
aws_provider_data = (
db.query(models.Aws_provider)
.filter(models.Aws_provider.environment == environment)
.filter(models.Aws_provider.squad == squad)
.first()
)
role_arn = (
db.query(models.Aws_provider.role_arn)
.filter(models.Aws_provider.environment == environment)
.filter(models.Aws_provider.squad == squad)
.first()
)
source_profile = (
db.query(models.Aws_provider.source_profile)
.filter(models.Aws_provider.environment == environment)
.filter(models.Aws_provider.squad == squad)
.first()
return schemas_aws.AwsAccountResponseRepo(
id=aws_provider_data.id,
squad=aws_provider_data.squad,
environment=aws_provider_data.environment,
access_key_id=aws_provider_data.access_key_id,
secret_access_key=aws_provider_data.secret_access_key,
role_arn=aws_provider_data.role_arn,
default_region=aws_provider_data.default_region,
extra_variables=aws_provider_data.extra_variables,
)
try:
return {
"access_key": decrypt(get_access_key[0]),
"secret_access_key": decrypt(get_secret_access_key[0]),
"default_region": default_region[0],
"profile_name": profile_name[0],
"role_arn": role_arn[0],
"source_profile": source_profile[0],
}
except Exception as err:
raise err


def get_squad_aws_profile(db: Session, squad: str, environment: str):
def get_squad_aws_profile(
db: Session, squad: str, filters: schemas_aws.AwsAccountFilter, skip: int = 0, limit: int = 100
) -> List[schemas_aws.AwsAccountResponse]:
try:
if environment != None:
return (
db.query(models.Aws_provider)
.filter(models.Aws_provider.squad == squad)
.filter(models.Aws_provider.environment == environment)
.first()
)
result = []
for i in squad:
result.extend(
db.query(models.Aws_provider)
.filter(models.Aws_provider.squad == i)
.all()
query = (
db.query(models.Aws_provider)
.filter(models.Aws_provider.squad == squad)
)

for field, value in filters.model_dump().items():
if value is not None:
query = query.filter(getattr(models.Aws_provider, field) == value)

results = query.order_by(desc(models.Aws_provider.id)).offset(skip).limit(limit).all()

aws_profiles = []
for result in results:
aws_profile = schemas_aws.AwsAccountResponse(
id=result.id,
squad=result.squad,
environment=result.environment,
default_region=result.default_region,
role_arn=result.role_arn,
extra_variables=result.extra_variables,
)
return set(result)
aws_profiles.append(aws_profile)
return aws_profiles
except Exception as err:
raise err


def get_all_aws_profile(db: Session):
def get_all_aws_profile(
db: Session, filters: schemas_aws.AwsAccountFilter, skip: int = 0, limit: int = 100
) -> List[schemas_aws.AwsAccountResponse]:
try:
return db.query(models.Aws_provider).all()
query = db.query(models.Aws_provider)

for field, value in filters.model_dump().items():
if value is not None:
if field == 'squad' and isinstance(value, list):
or_conditions = [getattr(models.Aws_provider, field).like(f"%{v}%") for v in value]
query = query.filter(or_(*or_conditions))
else:
query = query.filter(getattr(models.Aws_provider, field) == value)

results = query.order_by(desc(models.Aws_provider.id)).offset(skip).limit(limit).all()

aws_profiles = []
for result in results:
aws_profile = schemas_aws.AwsAccountResponse(
id=result.id,
squad=result.squad,
environment=result.environment,
default_region=result.default_region,
role_arn=result.role_arn,
extra_variables=result.extra_variables,
)
aws_profiles.append(aws_profile)
return aws_profiles
except Exception as err:
raise err



def delete_aws_profile_by_id(db: Session, aws_profile_id: int):
try:
db.query(models.Aws_provider).filter(
Expand Down
3 changes: 2 additions & 1 deletion sld-api-backend/src/azure/infrastructure/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime

from config.database import Base
from sqlalchemy import Column, DateTime, Integer, String, UniqueConstraint
from sqlalchemy import Column, DateTime, Integer, String, UniqueConstraint, JSON


class Azure_provider(Base):
Expand All @@ -13,5 +13,6 @@ class Azure_provider(Base):
client_secret = Column(String(200), nullable=False)
subscription_id = Column(String(200), nullable=False)
tenant_id = Column(String(200), nullable=False)
extra_variables = Column(JSON, nullable=True)
created_at = Column(DateTime, default=datetime.datetime.now())
__table_args__ = (UniqueConstraint("squad", "environment"),)
1 change: 0 additions & 1 deletion sld-api-backend/src/deploy/api/container/deploy/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async def get_all_deploys(
filters: DeployFilter = Depends(DeployFilter),
) -> List[DeployFilterResponse]:
try:
# Si el usuario no es un maestro, aplicar el filtro de escuadrón
if not crud_users.is_master(db, current_user):
filters.squad = current_user.squad

Expand Down
4 changes: 1 addition & 3 deletions sld-api-backend/src/deploy/infrastructure/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,15 @@ def get_deploys(db: Session, filters: DeployFilter, skip: int = 0, limit: int =

results = query.order_by(desc(models.Deploy.id)).offset(skip).limit(limit).all()

# Crear una lista de DeployFilterResponse a partir de los resultados
deploy_responses = []
for deploy, icon_path in results:
deploy_dict = deploy.__dict__
deploy_dict['icon_path'] = icon_path # Agregar el icon_path al diccionario
deploy_dict['icon_path'] = icon_path
deploy_responses.append(DeployFilterResponse(**deploy_dict))

return deploy_responses



class MetricsFetcher:
def __init__(self, db: Session):
self.db = db
Expand Down
Loading
Loading