Skip to content

Commit

Permalink
Merge pull request #97 from D10S0VSkY-OSS/feature/create-stack-role-dv
Browse files Browse the repository at this point in the history
Feature/create stack role dv
  • Loading branch information
D10S0VSkY-OSS authored Sep 6, 2022
2 parents 0e4c10b + 3a6041f commit 5ea8e18
Show file tree
Hide file tree
Showing 90 changed files with 4,238 additions and 3,238 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
max-line-length = 88
select = C,E,F,W,B,B9
ignore = E203, E501, W503
exclude = __init__.py
26 changes: 26 additions & 0 deletions script/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh -e
set -x

autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-api-backend/api_v1 --exclude=__init__.py
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-api-backend/config --exclude=__init__.py
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-api-backend/db --exclude=__init__.py
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-api-backend/crud --exclude=__init__.py
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-api-backend/core --exclude=__init__.py
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-api-backend/helpers --exclude=__init__.py
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-api-backend/schemas --exclude=__init__.py
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-api-backend/security --exclude=__init__.py
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-api-backend/tasks --exclude=__init__.py

autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-dashboard/ --exclude=__init__.py
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-remote-state --exclude=__init__.py
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place ../sld-schedule --exclude=__init__.py

black ../sld-api-backend/
black ../sld-dashboard/
black ../sld-remote-state
black ../sld-schedule

isort ../sld-api-backend/ --skip=../sld-api-backend/env/
isort ../sld-dashboard/ --skip=../sld-dashboard/env/
isort ../sld-remote-state --skip=../sld-remote-state/env/
isort ../sld-schedule --skip=../sld-schedule/env/
19 changes: 8 additions & 11 deletions sld-api-backend/api_v1/api.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
from api_v1.endpoints import (activity_logs, auth, aws, azure, deploy, gcp,
healthy, plan, schedule, stacks, tasks, users,
variables)
from fastapi import APIRouter

from api_v1.endpoints import users, auth, stacks
from api_v1.endpoints import aws, azure, gcp
from api_v1.endpoints import deploy, tasks, variables, healthy
from api_v1.endpoints import activity_logs, plan, schedule


api_router = APIRouter()
api_router.include_router(users.router, prefix="/users", tags=["Users"])
api_router.include_router(auth.router,prefix="/authenticate",tags=["AccessToken"])
api_router.include_router(aws.router,prefix="/accounts/aws",tags=["Aws"])
api_router.include_router(gcp.router,prefix="/accounts/gcp",tags=["Gcloud"])
api_router.include_router(azure.router,prefix="/accounts/azure",tags=["Azure"])
api_router.include_router(auth.router, prefix="/authenticate", tags=["AccessToken"])
api_router.include_router(aws.router, prefix="/accounts/aws", tags=["Aws"])
api_router.include_router(gcp.router, prefix="/accounts/gcp", tags=["Gcloud"])
api_router.include_router(azure.router, prefix="/accounts/azure", tags=["Azure"])
api_router.include_router(stacks.router, prefix="/stacks", tags=["Stacks"])
api_router.include_router(plan.router, prefix="/plan", tags=["Plan"])
api_router.include_router(deploy.router, prefix="/deploy", tags=["Deploy"])
api_router.include_router(schedule.router, prefix="/schedule", tags=["Schedule"])
api_router.include_router(tasks.router, prefix="/tasks", tags=["Tasks"])
api_router.include_router(activity_logs.router, prefix="/activity", tags=["Logs"])
api_router.include_router(variables.router,prefix="/variables",tags=["Variables"])
api_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
api_router.include_router(healthy.router, tags=["Healthy"])
42 changes: 22 additions & 20 deletions sld-api-backend/api_v1/endpoints/activity_logs.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from sqlalchemy.orm import Session
from crud import activityLogs as crud_activity
from crud import user as crud_users
from fastapi import APIRouter, Depends, HTTPException

from schemas import schemas
from security import deps
from crud import activityLogs as crud_activity
from crud import user as crud_users
from sqlalchemy.orm import Session

#from fastapi_limiter import FastAPILimiter
#from fastapi_limiter.depends import RateLimiter
#import aioredis
# from fastapi_limiter import FastAPILimiter
# from fastapi_limiter.depends import RateLimiter
# import aioredis

router = APIRouter()

Expand All @@ -20,34 +19,37 @@

@router.get("/id/{username}")
async def get_activity_logs_by_username(
username: str,
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db)):
username: str,
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
):
if not crud_users.is_superuser(db, current_user):
raise HTTPException(status_code=403, detail="Not enough permissions")
if not crud_users.is_master(db, current_user):
squad = current_user.squad
return crud_activity.get_activity_by_username_squad(db=db, username=username, squad=squad)
return crud_activity.get_activity_by_username_squad(
db=db, username=username, squad=squad
)
return crud_activity.get_activity_by_username(db, username=username)


@ router.get("/all")
@router.get("/all")
async def get_all_activity_logs(
current_user: schemas.User = Depends(deps.get_current_active_user),
skip: int = 0,
limit: int = 100,
db: Session = Depends(deps.get_db)):
current_user: schemas.User = Depends(deps.get_current_active_user),
skip: int = 0,
limit: int = 100,
db: Session = Depends(deps.get_db),
):
if not crud_users.is_superuser(db, current_user):
raise HTTPException(status_code=403, detail="Not enough permissions")
try:
if not crud_users.is_master(db, current_user):
squad = current_user.squad
result = crud_activity.get_all_activity_by_squad(
db=db, squad=squad, skip=skip, limit=limit)
db=db, squad=squad, skip=skip, limit=limit
)
return result
result = crud_activity.get_all_activity(db=db, skip=skip, limit=limit)
return result
except Exception as err:
raise HTTPException(
status_code=400,
detail=f"{err}")
raise HTTPException(status_code=400, detail=f"{err}")
13 changes: 6 additions & 7 deletions sld-api-backend/api_v1/endpoints/auth.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from typing import Any
from sqlalchemy.orm import Session

from fastapi import APIRouter, Depends
from fastapi.security import OAuth2PasswordRequestForm

from schemas import schemas
from schemas.schemas import Token
from security import deps
from security.tokens import validate_user

from sqlalchemy.orm import Session

router = APIRouter()


@router.post("/access-token", response_model=Token)
def login_access_token(
user: OAuth2PasswordRequestForm = Depends(),
db: Session = Depends(deps.get_db)) -> Any:
user: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(deps.get_db)
) -> Any:
"""
OAuth2 compatible token login, get an access token for future requests
"""
Expand All @@ -24,8 +23,8 @@ def login_access_token(

@router.post("/access-token-json", response_model=Token)
def login_access_token_json(
user: schemas.UserAuthenticate,
db: Session = Depends(deps.get_db)) -> dict:
user: schemas.UserAuthenticate, db: Session = Depends(deps.get_db)
) -> dict:
"""
OAuth2 compatible token login, get an access token for future requests
"""
Expand Down
54 changes: 28 additions & 26 deletions sld-api-backend/api_v1/endpoints/aws.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
from sqlalchemy.orm import Session
from fastapi import APIRouter, Depends, HTTPException, Response

from schemas import schemas
from crud import activityLogs as crud_activity
from crud import aws as crud_aws
from crud import user as crud_users
from crud import activityLogs as crud_activity
from fastapi import APIRouter, Depends, HTTPException, Response
from schemas import schemas
from security import deps

from sqlalchemy.orm import Session

router = APIRouter()


@router.post("/", status_code=200)
async def create_new_aws_profile(
aws: schemas.AwsAsumeProfile,
response: Response,
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db)):
aws: schemas.AwsAsumeProfile,
response: Response,
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
):
# Check if the user has privileges
if not crud_users.is_master(db, current_user):
raise HTTPException(status_code=403, detail="Not enough permissions")
if "string" in [aws.squad, aws.environment]:
raise HTTPException(
status_code=409,
detail="The squad or environment field must have a value that is not a string.")
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)
db=db, squad=aws.squad, environment=aws.environment
)
if db_aws_account:
raise HTTPException(
status_code=409,
detail="Account already exists")
raise HTTPException(status_code=409, detail="Account already exists")
try:
result = crud_aws.create_aws_profile(db=db, aws=aws)
crud_activity.create_activity_log(
db=db,
username=current_user.username,
squad=current_user.squad,
action=f'Create AWS account {aws.squad} {aws.environment}'
action=f"Create AWS account {aws.squad} {aws.environment}",
)
return {"result": f'Create AWS account {aws.squad} {aws.environment}'}
return {"result": f"Create AWS account {aws.squad} {aws.environment}"}
except Exception as err:
raise HTTPException(status_code=400, detail=str(err))


@router.get("/")
async def get_all_aws_accounts(
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db)):
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
):
# 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_squad_aws_profile(
db=db, squad=current_user.squad, environment=None
)
return crud_aws.get_all_aws_profile(db=db)


@router.delete("/{aws_account_id}")
async def delete_aws_account_by_id(
aws_account_id: int,
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db)):
aws_account_id: int,
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
):

if not crud_users.is_master(db, current_user):
raise HTTPException(status_code=403, detail="Not enough permissions")
result = crud_aws.delete_aws_profile_by_id(
db=db, aws_profile_id=aws_account_id)
result = crud_aws.delete_aws_profile_by_id(db=db, aws_profile_id=aws_account_id)
crud_activity.create_activity_log(
db=db,
username=current_user.username,
squad=current_user.squad,
action=f'Delete AWS account {aws_account_id}'
action=f"Delete AWS account {aws_account_id}",
)
return result
54 changes: 29 additions & 25 deletions sld-api-backend/api_v1/endpoints/azure.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,75 @@
from sqlalchemy.orm import Session
from fastapi import APIRouter, Depends, HTTPException, Response

from schemas import schemas
from crud import activityLogs as crud_activity
from crud import azure as crud_azure
from crud import user as crud_users
from crud import activityLogs as crud_activity
from fastapi import APIRouter, Depends, HTTPException, Response
from schemas import schemas
from security import deps

from sqlalchemy.orm import Session

router = APIRouter()


@router.post("/", status_code=200)
async def create_new_azure_profile(
azure: schemas.AzureBase,
response: Response,
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db)):
azure: schemas.AzureBase,
response: Response,
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
):

if not crud_users.is_master(db, current_user):
raise HTTPException(status_code=403, detail="Not enough permissions")
if "string" in [azure.squad, azure.environment]:
raise HTTPException(
status_code=409,
detail="The squad or environment field must have a value that is not a string.")
detail="The squad or environment field must have a value that is not a string.",
)
db_azure_account = crud_azure.get_squad_azure_profile(
db=db, squad=azure.squad, environment=azure.environment)
db=db, squad=azure.squad, environment=azure.environment
)
if db_azure_account:
raise HTTPException(
status_code=409,
detail="Account already exists")
raise HTTPException(status_code=409, detail="Account already exists")
try:
result = crud_azure.create_azure_profile(db=db, azure=azure)
crud_activity.create_activity_log(
db=db,
username=current_user.username,
squad=current_user.squad,
action=f'Create Azure Account {azure.subscription_id}'
action=f"Create Azure Account {azure.subscription_id}",
)
return {"result": f'Create Azure account {azure.squad} {azure.environment}'}
return {"result": f"Create Azure account {azure.squad} {azure.environment}"}
except Exception as err:
raise HTTPException(status_code=400, detail=str(err))


@router.get("/")
async def get_all_azure_accounts(
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db)):
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
):
if not crud_users.is_master(db, current_user):
return crud_azure.get_squad_azure_profile(db=db, squad=current_user.squad, environment=None)
return crud_azure.get_squad_azure_profile(
db=db, squad=current_user.squad, environment=None
)
return crud_azure.get_all_azure_profile(db=db)


@router.delete("/{azure_account_id}")
async def delete_azure_account_by_id(
azure_account_id: int,
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db)):
azure_account_id: int,
current_user: schemas.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
):

if not crud_users.is_master(db, current_user):
raise HTTPException(status_code=403, detail="Not enough permissions")
result = crud_azure.delete_azure_profile_by_id(
db=db, azure_profile_id=azure_account_id)
db=db, azure_profile_id=azure_account_id
)
crud_activity.create_activity_log(
db=db,
username=current_user.username,
squad=current_user.squad,
action=f'Delete Azure account {azure_account_id}'
action=f"Delete Azure account {azure_account_id}",
)
return result
Loading

0 comments on commit 5ea8e18

Please sign in to comment.