Reorganized folder structure

This commit is contained in:
2025-04-29 22:40:27 +02:00
parent 0d3f4a0a30
commit 0eaadbe6a7
52 changed files with 10 additions and 38 deletions
+55
View File
@@ -0,0 +1,55 @@
locals {
persistent_folder = var.persistent_folder
mail_path = "${local.persistent_folder}/mail"
mailboxes_path = "${local.mail_path}/mailboxes"
getmail_path = "${local.mail_path}/getmail"
postfix_path = "${local.mail_path}/postfix"
getmail_jobs = flatten([
for account in var.email_accounts : [
for mailbox in account.mailboxes : {
name = "${account.recipient}-${mailbox}"
recipient = "${account.recipient}+${mailbox}"
domain = account.domain
server = account.receive.server
port = account.receive.port
username = account.username
password = account.password
mailbox = mailbox
delete_after = coalesce(account.delete_after, 30)
}
]
])
postfix_env_vars = {
# Email configuration
SMTP_HOST = var.relay_smtp_host
SMTP_PORT = var.relay_smtp_port
MY_PRIMARY_DOMAIN = var.primary_domain
MY_DESTINATION = join(",", [for account in var.email_accounts : account.domain])
MY_NETWORKS = "127.0.0.0/8"
MAILNAME = "smtp.${var.primary_domain}"
DOVECOT_AUTH_ADDRESS = "dovecot-auth.mail.svc.cluster.local"
DOVECOT_AUTH_PORT = 31000
DOVECOT_LMTP_ADDRESS = "dovecot-lmtp.mail.svc.cluster.local"
DOVECOT_LMTP_PORT = 31024
LOG_TO_STDOUT = 0
}
password_hashes = {
for account in var.email_accounts : account.username => data.external.password_hasher[account.username].result.hex_encoded_bcrypt_hash
}
postfix_secrets = {
SMTP_USER = {
key = "username"
name = kubernetes_secret.relay_mail_credentials.metadata[0].name
}
SMTP_PASSWORD = {
key = "password"
name = kubernetes_secret.relay_mail_credentials.metadata[0].name
}
}
}
@@ -0,0 +1,27 @@
import sys
import json
import bcrypt
import binascii
# Read input JSON from stdin
input_data = json.load(sys.stdin)
password = input_data.get("password")
cost = int(input_data.get("cost", "10")) # Default cost 10
if not password:
print(json.dumps({"error": "Password is required"}), file=sys.stderr)
sys.exit(1)
# Generate bcrypt hash
hashed_bytes = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt(rounds=cost))
hashed_string = hashed_bytes.decode('utf-8')
# Hex encode for Glauth
hex_encoded_hash = binascii.hexlify(hashed_string.encode('utf-8')).decode('utf-8')
# Output JSON to stdout
output_data = {
"bcrypt_hash": hashed_string,
"hex_encoded_bcrypt_hash": hex_encoded_hash
}
print(json.dumps(output_data))
+559
View File
@@ -0,0 +1,559 @@
data "external" "password_hasher" {
for_each = { for account in var.email_accounts : account.username => account }
program = ["python3", "${path.module}//lib/bcrypt_hash.py"]
query = {
password = each.value.password
cost = "10"
}
}
resource "kubernetes_namespace" "mail" {
metadata {
annotations = {
name = "mail"
}
labels = {
name = "mail"
}
name = "mail"
}
}
resource "kubernetes_secret" "dovecot_users" {
metadata {
name = "dovecot-users"
namespace = kubernetes_namespace.mail.metadata[0].name
}
data = {
"users" = join("\n", [for account in var.email_accounts : "${account.username}:{BLF-CRYPT.HEX}${local.password_hashes[account.username]}:1000:1000::/var/mail/${account.username}::"])
}
}
resource "kubernetes_secret" "relay_mail_credentials" {
metadata {
name = "relay-mail-credentials"
namespace = kubernetes_namespace.mail.metadata[0].name
}
data = {
username = var.relay_smtp_username
password = var.relay_smtp_password
}
type = "Opaque"
}
resource "kubernetes_secret" "mail_crypt_certs" {
metadata {
name = "mail-crypt-certs"
namespace = kubernetes_namespace.mail.metadata[0].name
}
data = {
"ecprivkey.pem" = var.mail_crypt_private_key
"ecpubkey.pem" = var.mail_crypt_public_key
}
type = "Opaque"
}
resource "kubernetes_service_account" "mail_app" {
metadata {
name = "mail-app-sa"
namespace = kubernetes_namespace.mail.metadata[0].name
}
automount_service_account_token = false
}
resource "kubernetes_config_map" "postfix_config" {
metadata {
name = "postfix-config"
namespace = kubernetes_namespace.mail.metadata[0].name
}
data = {
"run.config" = file("${path.module}/resources/mail/run.config")
}
}
resource "kubernetes_secret" "bcc_map" {
metadata {
name = "bcc-map"
namespace = kubernetes_namespace.mail.metadata[0].name
}
data = {
"bcc_map" = join("\n", [for account in var.email_accounts : "/([^@]+)@${replace(account.domain, ".", "\\.")}/ $${1}+Sent@${account.domain}"])
}
}
resource "kubernetes_config_map" "getmail_config" {
metadata {
name = "getmail-config"
namespace = kubernetes_namespace.mail.metadata[0].name
}
data = { for job in local.getmail_jobs : "getmailrc_${job.name}" =>
templatefile("${path.module}/resources/mail/getmailrc.tpl", {
recipient = job.recipient
domain = job.domain
server = job.server
port = job.port
username = job.username
password = job.password
mailbox = job.mailbox
delete_after = job.delete_after
dovecot_lmtp_address = "dovecot-lmtp.mail.svc.cluster.local"
dovecot_lmtp_port = 31024
})
}
}
resource "kubernetes_config_map" "dovecot_config" {
metadata {
name = "dovecot-config"
namespace = kubernetes_namespace.mail.metadata[0].name
}
data = {
"dovecot.conf" = file("${path.module}/resources/mail/dovecot.conf")
}
}
resource "kubernetes_config_map" "dovecot_sieve" {
metadata {
name = "dovecot-sieve"
namespace = kubernetes_namespace.mail.metadata[0].name
}
data = {
"sieve" = file("${path.module}/resources/mail/sieve")
}
}
resource "kubernetes_manifest" "mail_certificate" {
manifest = {
apiVersion = "cert-manager.io/v1"
kind = "Certificate"
metadata = {
name = "mail-cert"
namespace = kubernetes_namespace.mail.metadata[0].name
}
spec = {
secretName = "mail-tls"
issuerRef = {
name = "letsencrypt-prod"
kind = "ClusterIssuer"
}
commonName = "imap.${var.primary_domain}"
dnsNames = flatten([for account in var.email_accounts : ["imap.${account.domain}", "smtp.${account.domain}"]])
}
}
}
resource "kubernetes_cron_job_v1" "fetch_emails" {
metadata {
namespace = kubernetes_namespace.mail.metadata[0].name
name = "fetch-emails"
}
spec {
schedule = var.fetch_emails_schedule
job_template {
metadata {
name = "fetch-emails"
}
spec {
backoff_limit = 1
parallelism = 1
completions = 1
active_deadline_seconds = 300
template {
metadata {
labels = {
app = "fetch-emails"
}
}
spec {
container {
name = "getmail"
image = "a13labs/getmail6:${var.getmail_tag}"
args = concat(["--getmaildir", "/var/getmail"], flatten([for job in local.getmail_jobs : ["-r", "/getmail/getmailrc_${job.name}"]]))
volume_mount {
name = "getmail-config"
mount_path = "/getmail"
read_only = true
}
security_context {
run_as_user = 1000
run_as_group = 1000
}
volume_mount {
name = "getmail-cache"
mount_path = "/var/getmail"
}
}
restart_policy = "Never"
volume {
name = "getmail-config"
config_map {
name = "getmail-config"
}
}
volume {
name = "getmail-cache"
host_path {
path = local.getmail_path
}
}
}
}
}
}
}
}
resource "kubernetes_deployment" "dovecot" {
metadata {
namespace = kubernetes_namespace.mail.metadata[0].name
name = "dovecot"
labels = {
app = "dovecot"
}
}
spec {
replicas = 1
selector {
match_labels = {
app = "dovecot"
}
}
strategy {
type = "Recreate"
}
template {
metadata {
labels = {
app = "dovecot"
}
}
spec {
service_account_name = kubernetes_service_account.mail_app.metadata[0].name
container {
name = "dovecot"
image = "dovecot/dovecot:${var.dovecot_tag}"
port {
container_port = 31993
host_port = 993
}
port {
container_port = 31024
}
port {
container_port = 31000
}
volume_mount {
name = "email-storage"
mount_path = "/var/mail"
}
volume_mount {
name = "users"
mount_path = "/etc/dovecot/users"
sub_path = "users"
read_only = true
}
volume_mount {
name = "dovecot-config"
mount_path = "/etc/dovecot/dovecot.conf"
sub_path = "dovecot.conf"
read_only = true
}
volume_mount {
name = "dovecot-sieve"
mount_path = "/etc/dovecot/sieve/default.sieve"
sub_path = "sieve"
read_only = true
}
volume_mount {
name = "certs"
mount_path = "/etc/letsencrypt"
read_only = true
}
volume_mount {
name = "mail-crypt-certs"
mount_path = "/etc/mail-crypt"
read_only = true
}
}
volume {
name = "email-storage"
host_path {
path = local.mailboxes_path
}
}
volume {
name = "users"
secret {
secret_name = kubernetes_secret.dovecot_users.metadata[0].name
}
}
volume {
name = "dovecot-config"
config_map {
name = kubernetes_config_map.dovecot_config.metadata[0].name
}
}
volume {
name = "certs"
secret {
secret_name = "mail-tls"
}
}
volume {
name = "dovecot-sieve"
config_map {
name = kubernetes_config_map.dovecot_sieve.metadata[0].name
}
}
volume {
name = "mail-crypt-certs"
secret {
secret_name = kubernetes_secret.mail_crypt_certs.metadata[0].name
}
}
}
}
}
depends_on = [kubernetes_manifest.mail_certificate, kubernetes_service_account.mail_app]
lifecycle {
ignore_changes = [spec[0].template[0].metadata[0].annotations]
replace_triggered_by = [
kubernetes_config_map.dovecot_config,
kubernetes_config_map.dovecot_sieve,
]
}
}
resource "kubernetes_deployment" "postfix" {
metadata {
namespace = kubernetes_namespace.mail.metadata[0].name
name = "postfix"
labels = {
app = "postfix"
}
}
spec {
replicas = 1
selector {
match_labels = {
app = "postfix"
}
}
strategy {
type = "Recreate"
}
template {
metadata {
labels = {
app = "postfix"
}
}
spec {
service_account_name = kubernetes_service_account.mail_app.metadata[0].name
container {
name = "postfix"
image = "tozd/postfix:${var.postfix_tag}"
port {
container_port = 465
host_port = 465
}
dynamic "env" {
for_each = local.postfix_env_vars
content {
name = env.key
value = env.value
}
}
dynamic "env" {
for_each = local.postfix_secrets
content {
name = env.key
value_from {
secret_key_ref {
key = env.value.key
name = env.value.name
}
}
}
}
volume_mount {
name = "email-storage"
mount_path = "/var/spool/postfix"
}
volume_mount {
name = "postfix-run-config"
mount_path = "/etc/service/postfix/run.config"
sub_path = "run.config"
read_only = true
}
volume_mount {
name = "bcc-map"
mount_path = "/etc/postfix/bcc_map"
sub_path = "bcc_map"
read_only = true
}
volume_mount {
name = "certs"
mount_path = "/etc/letsencrypt"
read_only = true
}
volume_mount {
name = "postfix-logs"
mount_path = "/var/log/postfix"
read_only = false
}
}
volume {
name = "email-storage"
host_path {
path = local.postfix_path
}
}
volume {
name = "postfix-run-config"
config_map {
name = kubernetes_config_map.postfix_config.metadata[0].name
}
}
volume {
name = "bcc-map"
secret {
secret_name = kubernetes_secret.bcc_map.metadata[0].name
}
}
volume {
name = "certs"
secret {
secret_name = "mail-tls"
}
}
volume {
name = "postfix-logs"
host_path {
path = "/var/log/postfix"
}
}
}
}
}
depends_on = [kubernetes_manifest.mail_certificate, kubernetes_service_account.mail_app]
lifecycle {
ignore_changes = [spec[0].template[0].metadata[0].annotations]
replace_triggered_by = [
kubernetes_config_map.postfix_config,
kubernetes_secret.relay_mail_credentials,
kubernetes_secret.bcc_map,
]
}
}
resource "kubernetes_service" "dovecot" {
metadata {
namespace = kubernetes_namespace.mail.metadata[0].name
name = "dovecot"
}
spec {
selector = {
app = "dovecot"
}
port {
name = "imap"
port = 993
target_port = 31993
}
type = "LoadBalancer"
}
lifecycle {
ignore_changes = [
metadata[0].annotations
]
}
}
resource "kubernetes_service" "dovecot_lmtp" {
metadata {
name = "dovecot-lmtp"
namespace = kubernetes_namespace.mail.metadata[0].name
}
spec {
selector = {
app = "dovecot"
}
port {
port = 31024
}
cluster_ip = "None"
}
}
resource "kubernetes_service" "dovecot_auth" {
metadata {
name = "dovecot-auth"
namespace = kubernetes_namespace.mail.metadata[0].name
}
spec {
selector = {
app = "dovecot"
}
port {
port = 31000
}
cluster_ip = "None"
}
}
resource "kubernetes_service" "postfix" {
metadata {
namespace = kubernetes_namespace.mail.metadata[0].name
name = "postfix"
}
spec {
selector = {
app = "postfix"
}
port {
name = "smtp"
port = 465
target_port = 465
}
type = "LoadBalancer"
}
lifecycle {
ignore_changes = [
metadata[0].annotations
]
}
}
+11
View File
@@ -0,0 +1,11 @@
output "path" {
value = local.mail_path
}
output "namespace" {
value = kubernetes_namespace.mail.metadata[0].name
}
output "app_name" {
value = "mail"
}
+13
View File
@@ -0,0 +1,13 @@
terraform {
required_version = "~>1.8"
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = "~>2.18"
}
scaleway = {
source = "scaleway/scaleway"
version = "~>2.13"
}
}
}
@@ -0,0 +1,253 @@
dovecot_config_version = "2.4.0"
dovecot_storage_version = "2.4.0"
base_dir = /run/dovecot
state_dir = /run/dovecot
protocols = imap lmtp
listen = *
mail_driver=maildir
mailbox_list_layout=index
mailbox_list_utf8=yes
mail_path=~/mail
mail_home=/var/mail/%{user|lower}
mail_utf8_extensions = yes
default_internal_user = vmail
default_login_user = vmail
default_internal_group = vmail
mail_uid = vmail
mail_gid = vmail
namespace inbox {
inbox = yes
mailbox Drafts {
special_use = \Drafts
auto = create
auto = subscribe
}
mailbox Junk {
special_use = \Junk
auto = create
auto = subscribe
}
mailbox Trash {
special_use = \Trash
auto = create
auto = subscribe
}
mailbox Sent {
special_use = \Sent
auto = create
auto = subscribe
}
mailbox Archive {
special_use = \Archive
auto = create
auto = subscribe
}
}
passdb passwd-file {
driver = passwd-file
passwd_file_path = /etc/dovecot/users
}
ssl_server {
cert_file = /etc/letsencrypt/tls.crt
key_file = /etc/letsencrypt/tls.key
}
mail_attribute {
dict file {
path = %{home}/dovecot-attributes
}
}
log_path = /dev/stdout
imap_hibernate_timeout = 5s
mail_plugins {
fts = yes
fts_flatcurve = yes
mail_log = yes
notify = yes
}
mail_log_events = delete undelete expunge save copy mailbox_create mailbox_delete mailbox_rename flag_change
fts_autoindex = yes
fts_autoindex_max_recent_msgs = 999
fts_search_add_missing = yes
language_filters = normalizer-icu snowball stopwords
language_tokenizers = generic email-address
language_tokenizer_generic_algorithm = simple
language en {
default = yes
filters = lowercase snowball english-possessive stopwords
}
fts flatcurve {
substring_search = yes
}
protocol imap {
mail_plugins {
imap_sieve = yes
imap_filter_sieve = yes
}
}
lmtp_save_to_detail_mailbox = yes
recipient_delimiter = +
protocol lmtp {
mail_plugins {
sieve = yes
}
}
service imap-login {
process_min_avail = 1
client_limit = 100
inet_listener imap {
port = 31143
}
inet_listener imaps {
port = 31993
ssl = yes
}
}
service stats {
process_min_avail = 1
inet_listener http {
port = 9090
}
}
auth_mechanisms = plain login
service auth {
inet_listener auth {
port = 31000
}
}
# LMTP service to store sent emails
service lmtp {
inet_listener auth {
port = 31024
}
mail_plugins {
sieve = yes
}
}
event_exporter log {
format = json
time_format = rfc3339
}
metric auth_success {
filter = (event=auth_request_finished AND success=yes)
}
metric auth_failure {
filter = (event=auth_request_finished AND NOT success=yes)
exporter = log
}
metric imap_command {
filter = event=imap_command_finished
group_by cmd_name {
method discrete {
}
}
group_by tagged_reply_state {
method discrete {
}
}
}
metric smtp_command {
filter = event=smtp_server_command_finished and protocol=submission
group_by cmd_name {
method discrete {
}
}
group_by status_code {
method discrete {
}
}
group_by duration {
method exponential {
base = 10
min_magnitude = 1
max_magnitude = 5
}
}
}
metric lmtp_command {
filter = event=smtp_server_command_finished and protocol=lmtp
group_by cmd_name {
method discrete {
}
}
group_by status_code {
method discrete {
}
}
group_by duration {
method exponential {
base = 10
min_magnitude = 1
max_magnitude = 5
}
}
}
metric mail_delivery {
filter = event=mail_delivery_finished
group_by duration {
method exponential {
base = 10
min_magnitude = 1
max_magnitude = 5
}
}
}
sieve_script default {
path = /etc/dovecot/sieve/default.sieve
}
mail_plugins {
mail_crypt = yes
}
crypt_global_private_key main {
crypt_private_key_file = /etc/mail-crypt/ecprivkey.pem
}
crypt_global_public_key_file = /etc/mail-crypt/ecpubkey.pem
log_path = /dev/stdout
info_log_path = /dev/stdout
debug_log_path = /dev/stdout
# Enable verbose logging
# auth_verbose = yes
# auth_debug = yes
# auth_debug_passwords = yes
# mail_debug = yes
@@ -0,0 +1,20 @@
[retriever]
type = SimpleIMAPSSLRetriever
server = ${server}
port = ${port}
username = ${username}
password = ${password}
mailboxes = ('${mailbox}',)
[destination]
type = MDA_lmtp
host = ${dovecot_lmtp_address}
port = ${dovecot_lmtp_port}
override = ${recipient}@${domain}
[options]
read_all = false
delete_after = ${delete_after}
verbose = 1
mark_read = true
@@ -0,0 +1,81 @@
#!/bin/bash
if [ -z "$SMTP_HOST" ]; then
echo "SMTP_HOST is required"
exit 1
fi
if [ -z "$SMTP_PORT" ]; then
echo "SMTP_PORT is required"
exit 1
fi
if [ -z "$SMTP_USER" ]; then
echo "SMTP_USER is required"
exit 1
fi
if [ -z "$SMTP_PASSWORD" ]; then
echo "SMTP_PASSWORD is required"
exit 1
fi
if [ -z "$DOVECOT_AUTH_ADDRESS" ]; then
echo "DOVECOT_AUTH_ADDRESS is required"
exit 1
fi
if [ -z "$DOVECOT_AUTH_PORT" ]; then
echo "DOVECOT_AUTH_PORT is required"
exit 1
fi
postconf -e "myhostname=$MAILNAME"
postconf -e "mydomain=$MY_PRIMARY_DOMAIN"
postconf -e "myorigin=$MY_PRIMARY_DOMAIN"
postconf -e "smtpd_banner=\$myhostname ESMTP \$mail_name"
postconf -e "mydestination=$MY_DESTINATION"
# Relay configuration
postconf -e "relayhost=[$SMTP_HOST]:$SMTP_PORT"
postconf -e "smtp_sasl_auth_enable=yes"
postconf -e "smtp_sasl_password_maps=static:$SMTP_USER:$SMTP_PASSWORD"
postconf -e "smtp_sasl_security_options=noanonymous"
postconf -e "smtp_tls_security_level=encrypt"
postconf -e "smtp_use_tls=yes"
postconf -e "smtp_tls_CAfile=/etc/ssl/certs/ca-certificates.crt"
# SASL configuration
postconf -e "smtpd_sasl_type=dovecot"
postconf -e "smtpd_sasl_path=inet:$DOVECOT_AUTH_ADDRESS:$DOVECOT_AUTH_PORT"
postconf -e "smtpd_sasl_auth_enable=yes"
postconf -e "smtpd_sasl_security_options=noanonymous"
postconf -e "smtpd_recipient_restrictions=permit_sasl_authenticated, reject"
postconf -e "smtpd_relay_restrictions=permit_sasl_authenticated, reject_unauth_destination"
postconf -e "smtpd_sasl_authenticated_header=yes"
postconf -e "smtpd_tls_wrappermode=yes"
# TLS configuration
postconf -e "smtpd_tls_cert_file=/etc/letsencrypt/tls.crt"
postconf -e "smtpd_tls_key_file=/etc/letsencrypt/tls.key"
postconf -e "smtpd_tls_security_level=encrypt"
postconf -e "smtp_tls_wrappermode=yes"
# BCC configuration
postconf -e "virtual_transport=lmtp:[$DOVECOT_LMTP_ADDRESS]:$DOVECOT_LMTP_PORT"
# Other configuration
postconf -e "inet_interfaces=all"
postconf -e "inet_protocols=ipv4"
postconf -e "queue_directory=/var/spool/postfix"
postconf -e "compatibility_level=2"
# Debugging
# postconf -e "debug_peer_list=127.0.0.1"
# postconf -e "debug_peer_level=3"
echo "
smtps inet n - n - - smtpd
-o syslog_name=postfix/smtps
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
" | sudo tee -a /etc/postfix/master.cf
/usr/sbin/postfix -c /etc/postfix start
/usr/sbin/postfix -c /etc/postfix abort
exec "$POSTFIX_PATH" -c /etc/postfix -s
@@ -0,0 +1,5 @@
require ["fileinto", "envelope"];
if envelope :is "from" "${user}" {
fileinto "Sent";
}
+82
View File
@@ -0,0 +1,82 @@
variable "persistent_folder" {
description = "The path to the persistent folder"
type = string
}
variable "primary_domain" {
description = "The primary domain for the websites"
type = string
}
variable "email_accounts" {
description = "List of email accounts to be created"
type = list(object({
recipient = string
domain = string
username = string
password = string
delete_after = optional(number)
mailboxes = list(string)
receive = object({
server = string
port = number
})
}))
}
variable "relay_smtp_host" {
description = "The SMTP relay host"
type = string
}
variable "relay_smtp_port" {
description = "The SMTP relay port"
type = number
}
variable "relay_smtp_username" {
description = "The SMTP relay username"
type = string
sensitive = true
}
variable "relay_smtp_password" {
description = "The SMTP relay password"
type = string
sensitive = true
}
variable "fetch_emails_schedule" {
description = "The cron schedule for fetching emails"
type = string
}
variable "getmail_tag" {
description = "The version of getmail to install"
type = string
default = "v0.0.1-6.19.07"
}
variable "postfix_tag" {
description = "The version of postfix to install"
type = string
default = "alpine-316"
}
variable "dovecot_tag" {
description = "The version of dovecot to install"
type = string
default = "latest"
}
variable "mail_crypt_private_key" {
description = "The private key for mail encryption"
type = string
sensitive = true
}
variable "mail_crypt_public_key" {
description = "The public key for mail encryption"
type = string
sensitive = true
}