Skip to content

Commit

Permalink
feat: Convert config template to pydantic model
Browse files Browse the repository at this point in the history
  • Loading branch information
romeonicholas committed Feb 27, 2024
1 parent 1dd9f84 commit b4305b9
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 103 deletions.
1 change: 0 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

.idea
config/*
!config/config_template.yaml
.history
**/__pycache__/
.vscode
Expand Down
154 changes: 154 additions & 0 deletions backend/capellacollab/config/config_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import pydantic.dataclasses
import yaml


class DockerConfig(pydantic.BaseModel):
registry: str = "k3d-myregistry.localhost:12345"
externalRegistry: str = "docker.io"


class K8sPodSecurityContext(pydantic.BaseModel):
runAsUser: int = 1004370000
runAsGroup: int = 1004370000
fsGroup: int = 1004370000
runAsNonRoot: bool = True


class K8sClusterConfig(pydantic.BaseModel):
imagePullPolicy: str = "Always"
podSecurityContext: K8sPodSecurityContext = K8sPodSecurityContext()


class K8sPromtailConfig(pydantic.BaseModel):
lokiEnabled: bool = True
lokiUrl: str = "http://localhost:30001/loki/api/v1/push"
lokiUsername: str = "localLokiUser"
lokiPassword: str = "localLokiPassword"
serverPort: int = 3101


# Only required when using operator k8s


class K8sConfig(pydantic.BaseModel):
# Only required, if you'd like to use a local k3d environment
context: str = "k3d-collab-cluster"
namespace: str = "collab-sessions"
storageClassName: str = "local-path"
storageAccessMode: str = "ReadWriteOnce"
cluster: K8sClusterConfig = K8sClusterConfig()
promtail: K8sPromtailConfig = K8sPromtailConfig()
# Only required when no kubectl context is available
# apiURL: str | None = None
# token: str | None = None


class GeneralConfig(pydantic.BaseModel):
host: str = "localhost"
port: int = 8000
scheme: str = "http"
wildcardHost: bool = False


class ExtensionGuacamoleConfig(pydantic.BaseModel):
baseURI: str = "http://localhost:8080/guacamole"
publicURI: str = "http://localhost:8080/guacamole"
username: str = "guacadmin"
password: str = "guacadmin"


class ExtensionJupyterConfig(pydantic.BaseModel):
publicURI: str = "http://localhost:8080/jupyter"


class ExtensionsConfig(pydantic.BaseModel):
guacamole: ExtensionGuacamoleConfig = ExtensionGuacamoleConfig()
jupyter: ExtensionJupyterConfig = ExtensionJupyterConfig()


class AuthOauthClientConfig(pydantic.BaseModel):
id: str = "default"
secret: str | None = None


# Only required when using provider oauth


class AuthOathEndpointsConfig(pydantic.BaseModel):
wellknown: str = (
"http://localhost:8083/default/.well-known/openid-configuration"
)
tokenIssuance: str | None = None
authorization: str | None = None


class AuthOauthConfig(pydantic.BaseModel):
# Only required when using provider oauth
endpoints: AuthOathEndpointsConfig = AuthOathEndpointsConfig()
audience: str = "default"
scopes: list[str] = ["openid"]
client: AuthOauthClientConfig = AuthOauthClientConfig()
redirectURI: str = "http://localhost:4200/oauth2/callback"


class AuthenticationConfig(pydantic.BaseModel):
provider: str = "oauth" # oauth | azure
jwt: dict[str, str] = pydantic.dataclasses.Field(
default_factory=lambda: {"usernameClaim": "sub"}
) # preferred_username
oauth: AuthOauthConfig = AuthOauthConfig()


class PipelineConfig(pydantic.BaseModel):
timeout: int = 60


class DatabaseConfig(pydantic.BaseModel):
url: str = "postgresql://dev:dev@localhost:5432/dev"

Check failure

Code scanning / SonarCloud

PostgreSQL database passwords should not be disclosed High

Make sure this PostgreSQL database password gets changed and removed from the code. See more on SonarCloud


class InitialConfig(pydantic.BaseModel):
admin: str = "admin"


class LoggingConfig(pydantic.BaseModel):
level: str = "DEBUG"
logPath: str = "logs/"


class RequestsConfig(pydantic.BaseModel):
timeout: int = 2


class PrometheusConfig(pydantic.BaseModel):
url: str = "http://localhost:8080/prometheus/"


class AppConfig(pydantic.BaseModel):
docker: DockerConfig = DockerConfig()
k8s: K8sConfig = K8sConfig()
general: GeneralConfig = GeneralConfig()
extensions: ExtensionsConfig = ExtensionsConfig()
authentication: AuthenticationConfig = AuthenticationConfig()
pipelines: PipelineConfig = PipelineConfig()
database: DatabaseConfig = DatabaseConfig()
initial: InitialConfig = InitialConfig()
logging: LoggingConfig = LoggingConfig()
requests: RequestsConfig = RequestsConfig()
prometheus: PrometheusConfig = PrometheusConfig()


def generate_yaml_from_config_template():
app_config = AppConfig()
config_dict = app_config.model_dump()
yaml_str = yaml.dump(config_dict, sort_keys=False)

with open("config.yaml", "w", encoding="utf-8") as yaml_file:
yaml_file.write(yaml_str)


if __name__ == "__main__":
generate_yaml_from_config_template()
102 changes: 0 additions & 102 deletions backend/config/config_template.yaml

This file was deleted.

0 comments on commit b4305b9

Please sign in to comment.