Skip to content

Commit

Permalink
Merge pull request #127 from D10S0VSkY-OSS/feature/custom-provider
Browse files Browse the repository at this point in the history
Feature/custom provider
  • Loading branch information
D10S0VSkY-OSS authored Oct 12, 2022
2 parents 3b9d51f + 3208585 commit 0f9cd23
Show file tree
Hide file tree
Showing 34 changed files with 754 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/sld-api-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
- name: Build the Docker image with tag
working-directory: ./sld-api-backend
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-api:2.13.1
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-api:2.14.0

- name: Docker Push with tag
#if: github.event.pull_request.merged == true
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-api:2.13.1
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-api:2.14.0

- name: Build the Docker image
working-directory: ./sld-api-backend
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/sld-dashboard-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
- name: Build the Docker image with tags
working-directory: ./sld-dashboard
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.11.0
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.12.0

- name: Docker Push with tags
#if: github.event.pull_request.merged == true
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.11.0
run: docker push ${{ secrets.DOCKER_USERNAME }}/sld-dashboard:2.12.0

- name: Build the Docker image
working-directory: ./sld-dashboard
Expand Down
2 changes: 1 addition & 1 deletion play-with-sld/kubernetes/k8s/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ kind: Kustomization
commonLabels:
app: stack-lifecycle-deploy
environment: play
version: 2.17.1
version: 2.18.0

resources:
- mysql-service.yml
Expand Down
3 changes: 2 additions & 1 deletion sld-api-backend/config/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Settings(BaseSettings):
AWS_PREFIX: List = ["aws"]
GCLOUD_PREFIX: List = ["gcp"]
AZURE_PREFIX: List = ["azure"]
PROVIDERS_SUPPORT: List = AWS_PREFIX + GCLOUD_PREFIX + AZURE_PREFIX
CUSTOM_PREFIX: List = ["custom"]
PROVIDERS_SUPPORT: List = AWS_PREFIX + GCLOUD_PREFIX + AZURE_PREFIX + CUSTOM_PREFIX
SECRET_KEY: str = os.getenv(
"SLD_SECRET_KEY",
"09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7",
Expand Down
6 changes: 6 additions & 0 deletions sld-api-backend/helpers/get_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from src.aws.infrastructure import repositories as crud_aws
from src.azure.infrastructure import repositories as crud_azure
from src.gcp.infrastructure import repositories as crud_gcp
from src.custom_providers.infrastructure import repositories as crud_custom_provider

r = redis.Redis(
host=settings.BACKEND_SERVER,
Expand Down Expand Up @@ -196,6 +197,11 @@ def check_prefix(db, stack_name: str, environment: str, squad: str):
db=db, environment=environment, squad=squad
)
return secreto
elif any(i in stack_name.lower() for i in settings.CUSTOM_PREFIX):
secreto = crud_custom_provider.get_credentials_custom_provider_profile(
db=db, environment=environment, squad=squad
)
return secreto
else:
raise HTTPException(
status_code=404,
Expand Down
5 changes: 5 additions & 0 deletions sld-api-backend/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import imp
import logging
from pkgutil import ImpImporter
from statistics import mode

from src.shared.api.v1.api import api_router
from config.api import settings
Expand All @@ -12,6 +14,7 @@
from src.gcp.infrastructure import models as model_gcp
from src.stacks.infrastructure import models as model_stacks
from src.tasks.infrastructure import models as model_tasks
from src.custom_providers.infrastructure import models as model_custom_provider
# from db import models
## Need refactor
from src.users.infrastructure import models as model_users
Expand All @@ -28,6 +31,8 @@
model_aws.Base.metadata.create_all(bind=engine)
model_azure.Base.metadata.create_all(bind=engine)
model_gcp.Base.metadata.create_all(bind=engine)
model_custom_provider.Base.metadata.create_all(bind=engine)


app = FastAPI(
title=f"{settings.PROJECT_NAME}",
Expand Down
16 changes: 16 additions & 0 deletions sld-api-backend/security/providers_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

from config.api import settings

class SecretsProviders:
def __init__(self, secret_provider: dict) -> None:
self.secret_provider = secret_provider
def export(self):
for k, v in self.secret_provider.items():
os.environ[k] = v

def createLocalFolder(dir_path: str):
try:
Expand Down Expand Up @@ -116,6 +122,13 @@ def secret(
os.environ["ARM_SUBSCRIPTION_ID"] = secreto.get("subscription_id")
os.environ["ARM_TENANT_ID"] = secreto.get("tenant_id")

elif any(i in stack_name.lower() for i in settings.CUSTOM_PREFIX):
configuration = ast.literal_eval(secreto.get("custom_provider_keyfile_json"))
R = SecretsProviders(configuration)
R.export()

# os.environ["GOOGLE_CLOUD_KEYFILE_JSON"] = gcloud_keyfile


def unsecret(stack_name, environment, squad, name, secreto):
if any(i in stack_name.lower() for i in settings.AWS_PREFIX):
Expand Down Expand Up @@ -157,3 +170,6 @@ def unsecret(stack_name, environment, squad, name, secreto):
os.environ.pop("ARM_CLIENT_SECRET")
os.environ.pop("ARM_SUBSCRIPTION_ID")
os.environ.pop("ARM_TENANT_ID")

elif any(i in stack_name.lower() for i in settings.CUSTOM_PREFIX):
pass
2 changes: 2 additions & 0 deletions sld-api-backend/src/aws/api/v1/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from src.aws.infrastructure import repositories as crud_aws
from src.users.domain.entities import users as schemas_users
from src.users.infrastructure import repositories as crud_users
from src.deploy.infrastructure import repositories as crud_deploy

router = APIRouter()

Expand Down Expand Up @@ -65,6 +66,7 @@ async def delete_aws_account_by_id(

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)
crud_activity.create_activity_log(
db=db,
Expand Down
12 changes: 12 additions & 0 deletions sld-api-backend/src/aws/infrastructure/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,15 @@ def delete_aws_profile_by_id(db: Session, aws_profile_id: int):
return {aws_profile_id: "deleted", "aws_profile_id": aws_profile_id}
except Exception as err:
raise err



def get_cloud_account_by_id(db: Session, provider_id: int):
try:
return (
db.query(models.Aws_provider)
.filter(models.Aws_provider.id == provider_id)
.first()
)
except Exception as err:
raise err
2 changes: 2 additions & 0 deletions sld-api-backend/src/azure/api/v1/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from src.azure.infrastructure import repositories as crud_azure
from src.users.domain.entities import users as schemas_users
from src.users.infrastructure import repositories as crud_users
from src.deploy.infrastructure import repositories as crud_deploy

router = APIRouter()

Expand Down Expand Up @@ -64,6 +65,7 @@ async def delete_azure_account_by_id(

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
)
Expand Down
11 changes: 11 additions & 0 deletions sld-api-backend/src/azure/infrastructure/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,14 @@ def delete_azure_profile_by_id(db: Session, azure_profile_id: int):
return {azure_profile_id: "deleted", "azure_profile_id": azure_profile_id}
except Exception as err:
raise err


def get_cloud_account_by_id(db: Session, provider_id: int):
try:
return (
db.query(models.Azure_provider)
.filter(models.Azure_provider.id == provider_id)
.first()
)
except Exception as err:
raise err
Empty file.
Empty file.
83 changes: 83 additions & 0 deletions sld-api-backend/src/custom_providers/api/v1/custom_providers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import imp
from fastapi import APIRouter, Depends, HTTPException, Response
from security import deps
from sqlalchemy.orm import Session
from src.activityLogs.infrastructure import repositories as crud_activity
from src.custom_providers.domain.entities import custom_providers as schemas_custom_provider
from src.custom_providers.infrastructure import repositories as crud_custom_provider
from src.users.domain.entities import users as schemas_users
from src.users.infrastructure import repositories as crud_users
from src.deploy.infrastructure import repositories as crud_deploy

router = APIRouter()


@router.post("/", status_code=200)
async def create_new_gcloud_profile(
custom_provider: schemas_custom_provider.CustomProviderBase,
response: Response,
current_user: schemas_users.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 [custom_provider.squad, custom_provider.environment]:
raise HTTPException(
status_code=409,
detail="The squad or environment field must have a value that is not a string.",
)
db_custom_provider_account = crud_custom_provider.get_squad_custom_provider_profile(
db=db, squad=custom_provider.squad, environment=custom_provider.environment
)
if db_custom_provider_account:
raise HTTPException(status_code=409, detail="Account already exists")
try:
result = crud_custom_provider.create_custom_provider_profile(
db=db,
squad=custom_provider.squad,
environment=custom_provider.environment,
configuration_keyfile_json=custom_provider.configuration,
)
crud_activity.create_activity_log(
db=db,
username=current_user.username,
squad=current_user.squad,
action=f"Create custom provider account {result.id}",
)
return {"result": f"Create custom provider account {custom_provider.squad} {custom_provider.environment}"}
except Exception as err:
raise HTTPException(status_code=400, detail=err)


@router.get("/")
async def get_all_custom_providers_accounts(
current_user: schemas_users.User = Depends(deps.get_current_active_user),
db: Session = Depends(deps.get_db),
):
if not crud_users.is_master(db, current_user):
return crud_custom_provider.get_squad_custom_provider_profile(
db=db, squad=current_user.squad, environment=None
)
return crud_custom_provider.get_all_custom_profile(db=db)


@router.delete("/{custom_provider_id}")
async def delete_custom_provider_account_by_id(
custom_provider_id,
current_user: schemas_users.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=400, detail="Not enough permissions")

result = crud_custom_provider.delete_custom_profile_by_id(
db=db, custom_profile_id=custom_provider_id
)

crud_activity.create_activity_log(
db=db,
username=current_user.username,
squad=current_user.squad,
action=f"Delete custom provider account {custom_provider_id} squad",
)
return result
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pydantic import BaseModel, constr


class CustomProviderBase(BaseModel):
squad: constr(strip_whitespace=True)
environment: constr(strip_whitespace=True)
configuration: dict


class CustomProvider(CustomProviderBase):
id: int

class Config:
orm_mode = True
Empty file.
14 changes: 14 additions & 0 deletions sld-api-backend/src/custom_providers/infrastructure/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import datetime

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


class Custom_provider(Base):
__tablename__ = "custom_provider"
id = Column(Integer, primary_key=True, index=True)
environment = Column(String(200), nullable=False)
squad = Column(String(200), nullable=False)
configuration = Column(JSON, nullable=False)
created_at = Column(DateTime, default=datetime.datetime.now())
__table_args__ = (UniqueConstraint("squad", "environment"),)
Loading

0 comments on commit 0f9cd23

Please sign in to comment.