Skip to content

Commit

Permalink
chore: add first version midaas mock
Browse files Browse the repository at this point in the history
  • Loading branch information
titigmr committed Aug 17, 2024
1 parent ee73c08 commit 6efe817
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
go.work
go.work.sum
tmp/
__pycache__
31 changes: 31 additions & 0 deletions contribute/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
MIDAAS_IMAGE := api-midaas:v1
WEBHOOK_IMAGE := webhook:v1
ENV_MIDAAS_KEYNAME := test
ENV_MIDAAS_KEYVALUE := test
ENV_MIDAAS_ZONES := dev.local
CACHE_CONFIG := /tmp/cluster-name.txt
CLUSTER_NAME := go

build-midaas:
docker build -t ${MIDAAS_IMAGE} midaas-ws/

push-midaas:
kind load docker-image ${MIDAAS_IMAGE} --name ${CLUSTER_NAME}

delete-midaas:
@kubectl delete pod midaas --ignore-not-found
@kubectl delete svc midaas --ignore-not-found

deploy-midaas: delete-midaas
@kubectl run --image ${MIDAAS_IMAGE} --expose=true --port 8080 \
--env "MIDAAS_KEYNAME=${ENV_MIDAAS_KEYNAME}" \
--env "MIDAAS_KEYVALUE=${ENV_MIDAAS_KEYVALUE}" \
--env "MIDAAS_ZONES=${ENV_MIDAAS_ZONES}" midaas
@echo "\n\n-------------------"
@echo "Kubernetes midaas service is listening on port 8080"
@echo "Use 'TSIG_${ENV_MIDAAS_KEYNAME}=${ENV_MIDAAS_KEYVALUE}' on environment variable in webhook"
@echo "For manual actions, OpenAPI schema is available at path /docs"


start-midaas: build-midaas push-midaas deploy-midaas

7 changes: 7 additions & 0 deletions contribute/midaas-ws/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.9
WORKDIR /app
COPY ./requirements.txt .
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
COPY main.py .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080", "--reload"]

114 changes: 114 additions & 0 deletions contribute/midaas-ws/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import os
import pathlib
import json
from fastapi import FastAPI, Response, Request, Body
from pydantic import BaseModel


KEYNAME = os.environ.get("MIDAAS_KEYNAME", "test")
KEYVALUE = os.environ.get("MIDAAS_KEYVALUE", "test")
ZONES = os.environ.get("MIDAAS_ZONE", "dev.local,test.local")
ALL_ZONES = ZONES.split(",")

INFO = f"""
Informations about current midaas instance:
- keyname: {KEYNAME} - (MIDAAS_KEYNAME env var)
- keyvalue: {KEYVALUE} - (MIDAAS_KEYVALUE env var)
Availables zones are comma separated: {ZONES} (MIDAAS_ZONE env var)
"""
app = FastAPI()


class TTLDelete(BaseModel):
keyname: str
keyvalue: str


class TTLCreate(BaseModel):
ttl: int
keyname: str
keyvalue: str


def check_TSIG(keyname, keyvalue):
return keyname == KEYNAME and KEYVALUE == keyvalue


def create_zone(file):
print(f"Creating zone on {file}")
with open(file, "w+") as f:
json.dump({}, f)


def create_zone_if_not_exist(file):
p = pathlib.Path(file)
if not p.exists():
create_zone(file)
else:
# check if zone is not empty
with open(file, "r") as f:
content = f.read()
if not content:
create_zone(file)


def read_zone(file):
with open(file, "r") as f:
data = json.loads(f.read())
return data


@app.get("/ws/{domaine}")
async def list_domain(request: Request, domaine):
records = {}
for zone in ALL_ZONES:
if zone in domaine.strip().lower():
file = f"/tmp/{zone}"
create_zone_if_not_exist(file)
records = read_zone(file)
return records
return records


@app.get("/healthz")
async def health():
return {"status": "OK"}


@app.put("/ws/{domaine}/{type}/{valeur}")
def create(response: Response, request: Request, domaine: str, type: str, valeur: str, TTL: TTLCreate) -> dict:
if not check_TSIG(keyname=TTL.keyname, keyvalue=TTL.keyvalue):
return {"status": "ERROR", "message": "wrong credentials"}

for zone in ALL_ZONES:
if zone in domaine:
file = f"/tmp/{zone}"
create_zone_if_not_exist(file=file)
data = read_zone(file=file)
with open(file, "w+") as f:
updated_data = data | {domaine: {"type": type,
"valeur": valeur,
"ttl": TTL.ttl}}
json.dump(updated_data, f)
return {"status": "OK"}
return {"status": "ERROR", "message": "zone not available"}


@app.delete("/ws/{domaine}/{type}/{valeur}")
def delete(response: Response, request: Request, domaine: str, type: str, valeur: str, TTL: TTLDelete) -> dict:
if not check_TSIG(keyname=TTL.keyname, keyvalue=TTL.keyvalue):
return {"status": "ERROR", "message": "wrong credentials"}

for zone in ALL_ZONES:
if zone in domaine:
file = f"/tmp/{zone}"
create_zone_if_not_exist(file=file)
data = read_zone(file=file)
print(domaine, data)
with open(file, "w") as f:
if domaine in data:
data.pop(domaine)
json.dump(data, f)
return {"status": "OK"}
return {"status": "ERROR", "message": "no domain"}
3 changes: 3 additions & 0 deletions contribute/midaas-ws/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fastapi
requests
uvicorn
6 changes: 3 additions & 3 deletions midaas/midaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ func RequestUrl(method string, url string, body Body) error {
bodyResponse, err := io.ReadAll(response.Body)
defer response.Body.Close()

if err != nil {
return err
}
if err != nil {
return err
}
if response.StatusCode > 200 {
return fmt.Errorf(string(bodyResponse))
}
Expand Down

0 comments on commit 6efe817

Please sign in to comment.