Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions remote/export-cert.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module -Name Az.KeyVault -RequiredVersion 6.3.1 -AllowClobber -Scope AllUsers -Force
Install-Module -Name Az.Accounts -RequiredVersion 4.0.2 -AllowClobber -Scope AllUsers -Force
Connect-AzAccount -Identity

$CertBase64 = Get-AzKeyVaultSecret -VaultName "kv-loggle" -Name "kibana" -AsPlainText

if (-not [string]::IsNullOrEmpty($CertBase64)) {
$CertBytes = [Convert]::FromBase64String($CertBase64)
Set-Content -Path "/etc/loggle/certs/kv-export-kibana.pfx" -Value $CertBytes -AsByteStream

openssl pkcs12 `
-in /etc/loggle/certs/kv-export-kibana.pfx `
-clcerts `
-nokeys `
-out /etc/loggle/certs/fullchain.pem `
-passin pass:

openssl pkcs12 `
-in /etc/loggle/certs/kv-export-kibana.pfx `
-nocerts `
-nodes `
-out /etc/loggle/certs/privkey.pem `
-passin pass:

chmod 750 /etc/loggle/certs/fullchain.pem
chmod 750 /etc/loggle/certs/privkey.pem
}
26 changes: 16 additions & 10 deletions remote/import-cert.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
openssl pkcs12 -export `
-in "/etc/letsencrypt/live/kibana.loggle.co/fullchain.pem" `
-inkey "/etc/letsencrypt/live/kibana.loggle.co/privkey.pem" `
-out "/etc/loggle/certs/kv-import-kibana.pfx" `
-passout pass:
$fullchainPath = "/etc/letsencrypt/live/kibana.loggle.co/fullchain.pem"
$privkeyPath = "/etc/letsencrypt/live/kibana.loggle.co/privkey.pem"

Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module -Name Az.KeyVault -RequiredVersion 6.3.1 -AllowClobber -Scope AllUsers -Force
Install-Module -Name Az.Accounts -RequiredVersion 4.0.2 -AllowClobber -Scope AllUsers -Force
Connect-AzAccount -Identity
if ((Test-Path $fullchainPath) -and (Test-Path $privkeyPath)){

openssl pkcs12 -export `
-in $fullchainPath `
-inkey $privkeyPath `
-out "/etc/loggle/certs/kv-import-kibana.pfx" `
-passout pass:

Import-AzKeyVaultCertificate -VaultName 'kv-loggle' -Name 'kibana' -FilePath '/etc/loggle/certs/kv-import-kibana.pfx'
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module -Name Az.KeyVault -RequiredVersion 6.3.1 -AllowClobber -Scope AllUsers -Force
Install-Module -Name Az.Accounts -RequiredVersion 4.0.2 -AllowClobber -Scope AllUsers -Force
Connect-AzAccount -Identity

Import-AzKeyVaultCertificate -VaultName 'kv-loggle' -Name 'kibana' -FilePath '/etc/loggle/certs/kv-import-kibana.pfx'
}
24 changes: 0 additions & 24 deletions remote/reload-cert.ps1

This file was deleted.

52 changes: 36 additions & 16 deletions remote/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export DEBIAN_FRONTEND=noninteractive
export NEEDRESTART_MODE=a

# Move configuration and setup files (copied from the repo via Terraform) from /tmp to /etc/loggle
mv /tmp/docker-compose.yml /tmp/otel-collector-config.yaml /tmp/kibana.yml /tmp/import-cert.ps1 /tmp/reload-cert.ps1 /etc/loggle/
mv /tmp/docker-compose.yml /tmp/otel-collector-config.yaml /tmp/kibana.yml /tmp/import-cert.ps1 /tmp/export-cert.ps1 /etc/loggle/
mv /tmp/loggle.service /etc/systemd/system/
mv /tmp/es-init /etc/loggle/
chmod -R a+rw /etc/loggle/elasticsearch-data
Expand Down Expand Up @@ -42,24 +42,44 @@ python3 -m venv /opt/certbot/
/opt/certbot/bin/pip install --upgrade pip
/opt/certbot/bin/pip install certbot
ln -s /opt/certbot/bin/certbot /usr/bin/certbot
certbot certonly --standalone -d kibana.loggle.co -m [email protected] --agree-tos --no-eff-email --preferred-challenges=http-01 --staging

sudo chmod -R 750 /etc/letsencrypt/live
sudo chmod -R 750 /etc/letsencrypt/archive
# Try exporting cert from Key Vault
kv_export=$(sudo pwsh /etc/loggle/export-cert.ps1)
echo "$kv_export"

# Import cert to key vault
kv_import=$(pwsh /etc/loggle/import-cert.ps1)
echo "$kv_import"
# TODO:
# Since running of certbot is now optional (if the certificate exists in key vault and is not expired),
# we need to add make sure that certbot is run when cert is expiring
if [ ! -f "/etc/loggle/certs/fullchain.pem" ] || [ ! -f "/etc/loggle/certs/privkey.pem" ]; then
echo "One or both certificate files are missing, running certbot..."
certbot certonly --standalone -d kibana.loggle.co -m [email protected] --agree-tos --no-eff-email --preferred-challenges=http-01
else
cert_exit_code=$(sudo openssl x509 -checkend 0 -noout -in /etc/loggle/certs/fullchain.pem >/dev/null 2>&1
echo $?
)

# Certbot renewal hook: Export updated certificate from Key Vault and reload Kibana.
sudo tee /etc/letsencrypt/renewal-hooks/post/reload-cert.sh << 'EOF'
#!/bin/bash
pwsh /etc/loggle/reload-cert.ps1
EOF
if [ "$cert_exit_code" -eq 1 ]; then
echo "Certificate is expired, running certbot..."
certbot certonly --standalone -d kibana.loggle.co -m [email protected] --agree-tos --no-eff-email --preferred-challenges=http-01
fi
fi

if [ -d "/etc/letsencrypt/live" ]; then
sudo chmod -R 750 /etc/letsencrypt/live
fi

if [ -d "/etc/letsencrypt/archive" ]; then
sudo chmod -R 750 /etc/letsencrypt/archive
fi

kv_import=$(sudo pwsh /etc/loggle/import-cert.ps1)
echo "$kv_import"

sudo chmod +x /etc/letsencrypt/renewal-hooks/post/reload-cert.sh
sudo docker compose -f /etc/loggle/docker-compose.yml pull
sudo /etc/letsencrypt/renewal-hooks/post/reload-cert.sh
# TODO:
# Add the import export powershell scripts to the post-renewal hooks
#
# sudo chmod +x /etc/letsencrypt/renewal-hooks/post/export-cert.sh
# sudo /etc/letsencrypt/renewal-hooks/post/export-cert.sh

sudo systemctl daemon-reload
sudo systemctl enable loggle.service
Expand All @@ -84,4 +104,4 @@ else
fi

es_output=$(pwsh /etc/loggle/es-init/batch-indexmanagement.ps1)
echo "$es_output"
echo "$es_output"
2 changes: 1 addition & 1 deletion terraform/azure/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ resource "azurerm_virtual_machine" "vm" {
"sudo /etc/loggle/setup.sh"
]
}
depends_on = [azurerm_user_assigned_identity.auth_id,azurerm_key_vault.kv]
depends_on = [azurerm_user_assigned_identity.auth_id, azurerm_key_vault.kv]
lifecycle {
prevent_destroy = false
}
Expand Down