Skip to content

Commit

Permalink
clean up how we determin libretranslate api keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebot committed Nov 11, 2024
1 parent 49c7851 commit 23f6928
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "smol_k8s_lab"
version = "5.19.1"
version = "5.19.2"
description = "CLI and TUI to quickly install slimmer Kubernetes distros and then manage apps declaratively using Argo CD"
authors = ["Jesse Hitch <jessebot@linux.com>",
"Max Roby <emax@cloudydev.net>"]
Expand Down
5 changes: 4 additions & 1 deletion smol_k8s_lab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ def main(config: str = "",
# set up self hosted translation
libre_translate_dict = apps.pop('libre_translate', {})
if libre_translate_dict:
configure_libretranslate(argocd, libre_translate_dict, bw)
libretranslate_api_key = configure_libretranslate(argocd,
libre_translate_dict,
bw)

# setup nextcloud, home assistant, mastodon, and matrix
setup_federated_apps(
Expand All @@ -319,6 +321,7 @@ def main(config: str = "",
pvc_storage_class,
zitadel_hostname,
oidc_obj,
libretranslate_api_key,
bw
)

Expand Down
1 change: 1 addition & 0 deletions smol_k8s_lab/config/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ apps:
- MASTODON_S3_BACKUP_ACCESS_ID
- MASTODON_S3_BACKUP_SECRET_KEY
- MASTODON_RESTIC_REPO_PASSWORD
- MASTODON_LIBRETRANSLATE_API_KEY
enabled: false
init:
enabled: true
Expand Down
3 changes: 2 additions & 1 deletion smol_k8s_lab/k8s_apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def setup_federated_apps(argocd: ArgoCD,
pvc_storage_class: str = "local-path",
zitadel_hostname: str = "",
zitadel_obj: Zitadel = None,
libretranslate_api_key: str = "",
bw: BwCLI = None) -> None:
"""
Setup any federated apps with initialization supported
Expand All @@ -243,7 +244,7 @@ def setup_federated_apps(argocd: ArgoCD,
zitadel_obj, bw)

if mastodon_dict.get('enabled', False):
configure_mastodon(argocd, mastodon_dict, pvc_storage_class, bw)
configure_mastodon(argocd, mastodon_dict, pvc_storage_class, libretranslate_api_key, bw)

if matrix_dict.get('enabled', False):
configure_matrix(argocd, matrix_dict, pvc_storage_class, zitadel_obj, bw)
39 changes: 28 additions & 11 deletions smol_k8s_lab/k8s_apps/social/libre_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def configure_libretranslate(argocd: ArgoCD,
cfg: dict,
bitwarden: BwCLI = None) -> None:
bitwarden: BwCLI = None) -> str:
"""
creates a libretranslate app and initializes it with secrets if you'd like :)
Expand All @@ -20,6 +20,8 @@ def configure_libretranslate(argocd: ArgoCD,
optional:
bitwarden - BwCLI() object with session token to create bitwarden items
Returns api key for libretranslate for programatic access
"""
# check immediately if this app is installed
app_installed = argocd.check_if_app_exists('libretranslate')
Expand Down Expand Up @@ -49,16 +51,19 @@ def configure_libretranslate(argocd: ArgoCD,
# we need namespace no matter the install type
libre_translate_namespace = cfg['argo']['namespace']

# api key for programatic access to libretranslate: set it to blank just in case
api_key = ""

# if the user has chosen to use smol-k8s-lab initialization
if not app_installed and init_enabled:
# immediately create namespace
argocd.k8s.create_namespace(libre_translate_namespace)

# if bitwarden is enabled, we create login items for each set of credentials
if bitwarden and not restore_enabled:
setup_bitwarden_items(argocd,
libretranslate_hostname,
bitwarden)
api_key = setup_bitwarden_items(argocd,
libretranslate_hostname,
bitwarden)
# these are standard k8s secrets
else:
# libretranslate admin credentials and smtp credentials
Expand All @@ -77,42 +82,54 @@ def configure_libretranslate(argocd: ArgoCD,
# if bitwarden and init are enabled, make sure we populate appset secret
# plugin secret with bitwarden item IDs
if bitwarden and init_enabled:
refresh_bitwarden(argocd, libretranslate_hostname, bitwarden)
api_key = refresh_bitwarden(argocd, libretranslate_hostname, bitwarden)

return api_key


def setup_bitwarden_items(argocd: ArgoCD,
libretranslate_hostname: str,
bitwarden: BwCLI) -> None:
bitwarden: BwCLI) -> str:
"""
setup initial bitwarden items for home assistant
setup initial bitwarden items for libretranslate
returns the api key used for libretranslate so you can use it in other apps
"""
sub_header("Creating libretranslate items in Bitwarden")
api_key = bitwarden.generate()

# admin credentials for initial owner user
origin = create_custom_field('origin', libretranslate_hostname)
api_id = bitwarden.create_login(
name=f'libretranslate-credentials-{libretranslate_hostname}',
item_url=libretranslate_hostname,
user="n/a",
password=bitwarden.generate(),
password=api_key,
fields=[origin]
)

# update the libretranslate values for the argocd appset
argocd.update_appset_secret({'libretranslate_credentials_bitwarden_id': api_id})

return api_key


def refresh_bitwarden(argocd: ArgoCD,
libretranslate_hostname: str,
bitwarden: BwCLI) -> None:
bitwarden: BwCLI) -> str:
"""
refresh bitwardens item in the appset secret plugin
returns the api key used for libretranslate so you can use it in other apps
"""
log.debug("Making sure libretranslate Bitwarden item IDs are in appset "
"secret plugin secret")

api_id = bitwarden.get_item(
api_item = bitwarden.get_item(
f"libretranslate-credentials-{libretranslate_hostname}"
)[0]['id']
)[0]
api_id = api_item['id']

argocd.update_appset_secret({'libretranslate_credentials_bitwarden_id': api_id})

return api_item['data']['login']['password']
49 changes: 21 additions & 28 deletions smol_k8s_lab/k8s_apps/social/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from smol_k8s_lab.utils.rich_cli.console_logging import sub_header, header
from smol_k8s_lab.utils.run.subproc import subproc
from smol_k8s_lab.utils.value_from import extract_secret, process_backup_vals
from smol_k8s_lab.utils.minio_lib import BetterMinio

# external libraries
import logging as log
Expand All @@ -19,15 +18,16 @@
def configure_mastodon(argocd: ArgoCD,
cfg: dict,
pvc_storage_class: str,
bitwarden: BwCLI = None,
minio_obj: BetterMinio = {}) -> bool:
libretranslate_api_key: str = "",
bitwarden: BwCLI = None) -> bool:
"""
creates a mastodon app and initializes it with secrets if you'd like :)
required:
argocd - ArgoCD() object for Argo CD operations
cfg - dict, with at least argocd key and init key
pvc_storage_class - str, storage class of PVC
argocd - ArgoCD() object for Argo CD operations
cfg - dict, with at least argocd key and init key
pvc_storage_class - str, storage class of PVC
libretranslate_api_key - str, api key to enable automatic translations
optional:
bitwarden - BwCLI() object with session token to create bitwarden items
Expand Down Expand Up @@ -93,13 +93,7 @@ def configure_mastodon(argocd: ArgoCD,
# get the api key for LibreTranslate, so we can translate posts
libre_api_key = extract_secret(init_values.get('libretranslate_api_key'))
if not libre_api_key:
# check if it's already in bitwarden
libre_api_key = bitwarden.get_item(
f"libretranslate-credentials-{mastodon_libretranslate_hostname}"
)[0]['login']['password']
# else, just give it fake data
if not libre_api_key:
libre_api_key = "notapplicable"
libre_api_key = libretranslate_api_key

s3_endpoint = secrets.get('s3_endpoint', "")
log.debug(f"Mastodon s3_endpoint at the start is: {s3_endpoint}")
Expand Down Expand Up @@ -162,6 +156,7 @@ def configure_mastodon(argocd: ArgoCD,
pvc_storage_class,
'mastodon-postgres',
mastodon_libretranslate_hostname,
libre_api_key,
bitwarden)

if not init_enabled:
Expand Down Expand Up @@ -189,7 +184,7 @@ def configure_mastodon(argocd: ArgoCD,
log.info("mastodon already installed 🎉")

if bitwarden and init_enabled:
refresh_bweso(argocd, mastodon_hostname, mastodon_libretranslate_hostname, bitwarden)
refresh_bweso(argocd, mastodon_hostname, mastodon_libretranslate_hostname, libre_api_key, bitwarden)


def create_user(user: str, email: str, pod_namespace: str) -> str:
Expand Down Expand Up @@ -224,6 +219,7 @@ def create_user(user: str, email: str, pod_namespace: str) -> str:
def refresh_bweso(argocd: ArgoCD,
mastodon_hostname: str,
mastodon_libretranslate_hostname: str,
libre_api_key: str,
bitwarden: BwCLI) -> None:
"""
if mastodon already installed, but bitwarden and init are enabled, still
Expand Down Expand Up @@ -272,20 +268,16 @@ def refresh_bweso(argocd: ArgoCD,
f"mastodon-server-secrets-{mastodon_hostname}", False
)[0]['id']

libretranslate_api_key_id = bitwarden.get_item(
f"libretranslate-credentials-{mastodon_hostname}", False
)[0]['id']
# do some checking here since this isn't required and so it may not be available
libretranslate_api_key_item = bitwarden.get_item(
f"mastodon-libretranslate-credentials-{mastodon_hostname}", False
)[0]
libretranslate_api_key_id = libretranslate_api_key_item.get('id', "")
if not libretranslate_api_key_id:
# check if it's already in bitwarden
libre_api_key = bitwarden.get_item(
f"libretranslate-credentials-{mastodon_libretranslate_hostname}"
)[0]['login']['password']
if not libre_api_key:
libre_api_key = "notapplicable"

endpoint = create_custom_field('endpoint', mastodon_libretranslate_hostname)
endpoint = create_custom_field('endpoint',
mastodon_libretranslate_hostname)
libretranslate_api_key_id = bitwarden.create_login(
name=f'libretranslate-credentials-{mastodon_hostname}',
name=f'mastodon-libretranslate-credentials-{mastodon_hostname}',
item_url=mastodon_libretranslate_hostname,
user="n/a",
password=libre_api_key,
Expand Down Expand Up @@ -472,7 +464,7 @@ def setup_bitwarden_items(argocd: ArgoCD,

endpoint = create_custom_field('endpoint', mastodon_libretranslate_hostname)
libretranslate_api_key_id = bitwarden.create_login(
name=f'libretranslate-credentials-{mastodon_hostname}',
name=f'mastodon-libretranslate-credentials-{mastodon_hostname}',
item_url=mastodon_libretranslate_hostname,
user="n/a",
password=libre_api_key,
Expand Down Expand Up @@ -515,6 +507,7 @@ def restore_mastodon(argocd: ArgoCD,
global_pvc_storage_class: str,
pgsql_cluster_name: str,
mastodon_libretranslate_hostname: str,
libre_api_key: str,
bitwarden: BwCLI) -> None:
"""
restore mastodon seaweedfs PVCs, mastodon files and/or config PVC(s),
Expand All @@ -534,7 +527,7 @@ def restore_mastodon(argocd: ArgoCD,

# first we grab existing bitwarden items if they exist
if bitwarden:
refresh_bweso(argocd, mastodon_hostname, mastodon_libretranslate_hostname, bitwarden)
refresh_bweso(argocd, mastodon_hostname, mastodon_libretranslate_hostname, libre_api_key, bitwarden)

# apply the external secrets so we can immediately use them for restores
external_secrets_yaml = (
Expand Down

0 comments on commit 23f6928

Please sign in to comment.