From 5fc3be6ef4d2dbfef9b7e873dc08b34bf3a99678 Mon Sep 17 00:00:00 2001 From: Martin Buchleitner Date: Thu, 24 Oct 2024 09:38:19 +0200 Subject: [PATCH] fix: refactoring example Signed-off-by: Martin Buchleitner --- db_client.py | 15 +-- db_client_transform.py | 32 +++--- nomad/app_dynamic.hcl | 4 +- nomad/app_hardcoded.hcl | 6 +- nomad/app_static.hcl | 6 +- nomad/app_transit.hcl | 4 +- nomad/app_transit_connect.hcl | 5 +- nomad/app_transit_connect_traefik.hcl | 13 +-- nomad/mysql.hcl | 6 ++ nomad/mysql_connect.hcl | 30 +++++- nomad/mysql_static.hcl | 65 +++++++++++++ nomad/whoami.hcl | 1 - setup/.terraform.lock.hcl | 21 ++++ setup/main.tf | 134 ++++++++++++++++++++++++++ setup/terraform.tfvars | 4 + setup/vault_kv.sh | 10 +- 16 files changed, 312 insertions(+), 44 deletions(-) create mode 100644 nomad/mysql_static.hcl create mode 100644 setup/.terraform.lock.hcl create mode 100644 setup/main.tf create mode 100644 setup/terraform.tfvars diff --git a/db_client.py b/db_client.py index 5b3a823..7162864 100644 --- a/db_client.py +++ b/db_client.py @@ -64,12 +64,15 @@ def init_vault(self, addr, token, namespace, path, key_name): if not addr or not token: logger.warn('Skipping initialization...') return - else: - logger.warn("Connecting to vault server: {}".format(addr)) - self.vault_client = hvac.Client(url=addr, token=token, namespace=namespace) - self.key_name = key_name - self.mount_point = path - logger.debug("Initialized vault_client: {}".format(self.vault_client)) + logger.warn("Connecting to vault server: {}".format(addr)) + self.vault_client = hvac.Client(url=addr, token=token, namespace=namespace, verify=False) + if not self.vault_client.is_authenticated(): + self.vault_client = None + logger.error("could not authenticate to vault") + return + self.key_name = key_name + self.mount_point = path + logger.debug("Initialized vault_client: {}".format(self.vault_client)) def vault_db_auth(self, path): try: diff --git a/db_client_transform.py b/db_client_transform.py index d9609e0..1434188 100644 --- a/db_client_transform.py +++ b/db_client_transform.py @@ -64,22 +64,26 @@ def init_db(self, uri, prt, uname, pw, db): # Later we will check to see if this is None to see whether to use Vault or not def init_vault(self, addr, token, namespace, path, key_name, transform_path, transform_masking_path, ssn_role, ccn_role): + self.vault_client = None if not addr or not token: logger.warn('Skipping initialization...') return - else: - logger.warn("Connecting to vault server: {}".format(addr)) - self.vault_client = hvac.Client(url=addr, token=token, namespace=namespace) - logging.debug("Vault-token: {}".format(token)) - self.key_name = key_name - self.mount_point = path - self.transform_mount_point = transform_path - self.transform_masking_mount_point = transform_masking_path - self.ssn_role = ssn_role - self.ccn_role = ccn_role - self.namespace = namespace - self.token = token - logger.debug("Initialized vault_client: {}".format(self.vault_client)) + logger.warn("Connecting to vault server: {}".format(addr)) + self.vault_client = hvac.Client(url=addr, token=token, namespace=namespace, verify=False) + if not self.vault_client.is_authenticated(): + self.vault_client = None + logger.error("could not authenticate to vault") + return + logging.debug("Vault-token: {}".format(token)) + self.key_name = key_name + self.mount_point = path + self.transform_mount_point = transform_path + self.transform_masking_mount_point = transform_masking_path + self.ssn_role = ssn_role + self.ccn_role = ccn_role + self.namespace = namespace + self.token = token + logger.debug("Initialized vault_client: {}".format(self.vault_client)) def vault_db_auth(self, path): try: @@ -96,6 +100,8 @@ def encrypt(self, value): response = self.vault_client.secrets.transit.encrypt_data( mount_point = self.mount_point, name = self.key_name, + + plaintext = base64.b64encode(value.encode()).decode('ascii') ) logger.debug('Response: {}'.format(response)) diff --git a/nomad/app_dynamic.hcl b/nomad/app_dynamic.hcl index 91b68ed..0728322 100644 --- a/nomad/app_dynamic.hcl +++ b/nomad/app_dynamic.hcl @@ -54,7 +54,9 @@ job "dynamic-app" { Port = {{ .Port }} {{end}} - Database = my_app + {{ with secret "dynamic-app/kv/database" }} + Database = {{ .Data.data.database }} + {{ end }} {{ with secret "dynamic-app/db/creds/app" }} User = {{ .Data.username }} Password = {{ .Data.password }} diff --git a/nomad/app_hardcoded.hcl b/nomad/app_hardcoded.hcl index bbb2170..db0b4cc 100644 --- a/nomad/app_hardcoded.hcl +++ b/nomad/app_hardcoded.hcl @@ -49,9 +49,9 @@ job "dynamic-app" { Port = {{ .Port }} {{end}} - Database = my_app - User = root - Password = super-duper-password + Database = app + User = app + Password = my-app-super-password EOF } resources { diff --git a/nomad/app_static.hcl b/nomad/app_static.hcl index d738e65..dfccba3 100644 --- a/nomad/app_static.hcl +++ b/nomad/app_static.hcl @@ -55,10 +55,10 @@ job "dynamic-app" { Port = {{ .Port }} {{end}} - Database = my_app {{ with secret "dynamic-app/kv/database" }} - User = {{ .Data.username }} - Password = {{ .Data.password }} + Database = {{ .Data.data.database }} + User = {{ .Data.data.username }} + Password = {{ .Data.data.password }} {{ end }} [VAULT] Enabled = False diff --git a/nomad/app_transit.hcl b/nomad/app_transit.hcl index 8f95828..1612829 100644 --- a/nomad/app_transit.hcl +++ b/nomad/app_transit.hcl @@ -56,7 +56,9 @@ job "dynamic-app" { Port = {{ .Port }} {{end}} - Database = my_app + {{ with secret "dynamic-app/kv/database" }} + Database = {{ .Data.data.database }} + {{ end }} {{ with secret "dynamic-app/db/creds/app" }} User = {{ .Data.username }} Password = {{ .Data.password }} diff --git a/nomad/app_transit_connect.hcl b/nomad/app_transit_connect.hcl index ff8621e..2a705d7 100644 --- a/nomad/app_transit_connect.hcl +++ b/nomad/app_transit_connect.hcl @@ -50,7 +50,6 @@ job "dynamic-app" { volumes = [ "local/config.ini:/usr/src/app/config/config.ini" ] - ports = ["web"] } @@ -64,7 +63,9 @@ job "dynamic-app" { Address = 127.0.0.1 Port = 3306 - Database = my_app + {{ with secret "dynamic-app/kv/database" }} + Database = {{ .Data.data.database }} + {{ end }} {{ with secret "dynamic-app/db/creds/app" }} User = {{ .Data.username }} Password = {{ .Data.password }} diff --git a/nomad/app_transit_connect_traefik.hcl b/nomad/app_transit_connect_traefik.hcl index 5c5abec..2e750ca 100644 --- a/nomad/app_transit_connect_traefik.hcl +++ b/nomad/app_transit_connect_traefik.hcl @@ -1,7 +1,6 @@ job "dynamic-app" { datacenters = ["dc1"] type = "service" - namespace = "demo" group "dynamic-app" { count = 1 @@ -15,9 +14,6 @@ job "dynamic-app" { network { mode = "bridge" - // port "web" { - // to = 8080 - // } } vault { @@ -26,14 +22,13 @@ job "dynamic-app" { change_signal = "SIGINT" } - service { name = "dynamic-app" port = "8080" tags = ["traefik.enable=true", "traefik.http.routers.dynamic-app.rule=Host(`dynamic-app.127.0.0.1.nip.io`)", - "traefik.http.routers.dynamic-app.entrypoints=http", - "traefik.http.routers.dynamic-app.tls=false", + "traefik.http.routers.dynamic-app.entrypoints=https", + "traefik.http.routers.dynamic-app.tls=true", "traefik.connsulcatalog.connect=true" ] connect { @@ -78,7 +73,9 @@ job "dynamic-app" { Address = 127.0.0.1 Port = 3306 - Database = my_app + {{ with secret "dynamic-app/kv/database" }} + Database = {{ .Data.data.database }} + {{ end }} {{ with secret "dynamic-app/db/creds/app" }} User = {{ .Data.username }} Password = {{ .Data.password }} diff --git a/nomad/mysql.hcl b/nomad/mysql.hcl index 14d5e5d..8037e44 100644 --- a/nomad/mysql.hcl +++ b/nomad/mysql.hcl @@ -22,11 +22,17 @@ job "mysql-server" { env = { "MYSQL_ROOT_PASSWORD" = "super-duper-password" + "MYSQL_DATABASE" = "app" + "MYSQL_USER" = "app" + "MYSQL_PASSWORD" = "my-app-super-password" } config { image = "mysql:9" ports = ["db"] + volumes = [ + "/srv/mysql/:/var/lib/mysql" + ] } resources { diff --git a/nomad/mysql_connect.hcl b/nomad/mysql_connect.hcl index 408b1e9..287559c 100644 --- a/nomad/mysql_connect.hcl +++ b/nomad/mysql_connect.hcl @@ -13,6 +13,12 @@ job "mysql-server" { } } + vault { + policies = ["nomad-dynamic-app", "nomad-mysql"] + change_mode = "signal" + change_signal = "SIGINT" + } + restart { attempts = 10 interval = "5m" @@ -23,16 +29,30 @@ job "mysql-server" { task "mysql-server" { driver = "docker" - env = { - "MYSQL_ROOT_PASSWORD" = "super-duper-password" - } - config { image = "mysql:9" - ports = ["db"] + volumes = [ + "/srv/mysql/:/var/lib/mysql" + ] } + template { + destination = "secrets/.envs" + change_mode = "noop" + env = true + data = <