Skip to content

Commit

Permalink
fix: refactoring example
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Buchleitner <[email protected]>
  • Loading branch information
mabunixda committed Oct 24, 2024
1 parent 88010e8 commit 5fc3be6
Show file tree
Hide file tree
Showing 16 changed files with 312 additions and 44 deletions.
15 changes: 9 additions & 6 deletions db_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 19 additions & 13 deletions db_client_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion nomad/app_dynamic.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
6 changes: 3 additions & 3 deletions nomad/app_hardcoded.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions nomad/app_static.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion nomad/app_transit.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
5 changes: 3 additions & 2 deletions nomad/app_transit_connect.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ job "dynamic-app" {
volumes = [
"local/config.ini:/usr/src/app/config/config.ini"
]

ports = ["web"]
}

Expand All @@ -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 }}
Expand Down
13 changes: 5 additions & 8 deletions nomad/app_transit_connect_traefik.hcl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
job "dynamic-app" {
datacenters = ["dc1"]
type = "service"
namespace = "demo"

group "dynamic-app" {
count = 1
Expand All @@ -15,9 +14,6 @@ job "dynamic-app" {

network {
mode = "bridge"
// port "web" {
// to = 8080
// }
}

vault {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 }}
Expand Down
6 changes: 6 additions & 0 deletions nomad/mysql.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 25 additions & 5 deletions nomad/mysql_connect.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = <<EOF
{{ with secret "dynamic-app/kv/database" }}
MYSQL_DATABASE = "{{ .Data.data.database }}"
MYSQL_USER = "{{ .Data.data.username }}"
MYSQL_PASSWORD = "{{ .Data.data.password }}"
{{ end }}
{{ with secret "dynamic-app/kv/database_root" }}
MYSQL_ROOT_PASSWORD = "{{ .Data.data.password }}"
{{ end }}
EOF
}
resources {
cpu = 500
memory = 500
Expand Down
65 changes: 65 additions & 0 deletions nomad/mysql_static.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
job "mysql-server" {
datacenters = ["dc1"]
type = "service"

group "mysql-server" {
count = 1

vault {
policies = ["nomad-dynamic-app", "nomad-mysql"]
change_mode = "signal"
change_signal = "SIGINT"
}

service {
name = "mysql-server"
port = "db"
}

restart {
attempts = 10
interval = "5m"
delay = "25s"
mode = "delay"
}

task "mysql-server" {
driver = "docker"

config {
image = "mysql:9"
ports = ["db"]
volumes = [
"/srv/mysql/:/var/lib/mysql"
]
}
template {
destination = "secrets/.envs"
change_mode = "noop"
env = true
data = <<EOF
{{ with secret "dynamic-app/kv/database" }}
MYSQL_DATABASE = "{{ .Data.data.database }}"
MYSQL_USER = "{{ .Data.data.username }}"
MYSQL_PASSWORD = "{{ .Data.data.password }}"
{{ end }}
{{ with secret "dynamic-app/kv/database_root" }}
MYSQL_ROOT_PASSWORD = "{{ .Data.data.password }}"
{{ end }}
EOF
}
resources {
cpu = 500
memory = 500
}
}
network {
mode = "bridge"
port "db" {
static = 3306
to = 3306
}
}
}
}
1 change: 0 additions & 1 deletion nomad/whoami.hcl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
job "whoami" {
datacenters = ["dc1"]
type = "service"
namespace = "demo"

group "whoami" {
count = 1
Expand Down
21 changes: 21 additions & 0 deletions setup/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5fc3be6

Please sign in to comment.