Skip to content

Commit 399362e

Browse files
authored
Add mail support for kratos using sendgrid (#165)
* Add mail support for kratos using sendgrid Fixed ClusterIssuer API version Sendgrid key is no longer templated into secret creation terraform (closes #156) * Pass in user auth mail from address to k8s module
1 parent baf4a90 commit 399362e

10 files changed

Lines changed: 55 additions & 24 deletions

File tree

templates/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ apply-secrets:
2828
aws secretsmanager describe-secret --secret-id "$(PROJECT)-$(ENVIRONMENT)-rds-<% index .Params `randomSeed` %>" > /dev/null 2>&1 || ( \
2929
cd terraform/bootstrap/secrets && \
3030
terraform init && \
31-
terraform apply $(AUTO_APPROVE) && \
31+
terraform apply $(AUTO_APPROVE) --var "sendgrid_api_key=${sendgridApiKey}" && \
3232
rm ./terraform.tfstate )
3333

3434
apply-shared-env:

templates/kubernetes/terraform/environments/prod/main.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ module "kubernetes" {
7474
["Carter L", "10.10.99.202/32", "h2jMuaXNIlx7Z0a3owWFjPsAA8B+ZpQH3FbZK393+08="],
7575
]
7676

77-
auth_enabled = <% if eq (index .Params `userAuth`) "yes" %>true<% else %>false<% end %>
78-
auth_domain = "auth.${local.domain_name}"
79-
backend_service_domain = "<% index .Params `productionBackendSubdomain` %>${local.domain_name}"
80-
frontend_service_domain = "<% index .Params `productionFrontendSubdomain` %>${local.domain_name}"
81-
77+
domain_name = local.domain_name
78+
auth_enabled = <% if eq (index .Params `userAuth`) "yes" %>true<% else %>false<% end %>
79+
auth_domain = "auth.${local.domain_name}"
80+
backend_service_domain = "<% index .Params `productionBackendSubdomain` %>${local.domain_name}"
81+
frontend_service_domain = "<% index .Params `productionFrontendSubdomain` %>${local.domain_name}"
82+
# This domain or address must be verified by the mail provider (Sendgrid, SES, etc.)
83+
user_auth_mail_from_address = "noreply@${local.domain_name}"
8284
}

templates/kubernetes/terraform/environments/stage/main.tf

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ module "kubernetes" {
7373
["Carter L", "10.10.199.202/32", "h2jMuaXNIlx7Z0a3owWFjPsAA8B+ZpQH3FbZK393+08="],
7474
]
7575

76-
auth_enabled = <% if eq (index .Params `userAuth`) "yes" %>true<% else %>false<% end %>
77-
auth_domain = "auth.${local.domain_name}"
78-
backend_service_domain = "<% index .Params `stagingBackendSubdomain` %>${local.domain_name}"
79-
frontend_service_domain = "<% index .Params `stagingFrontendSubdomain` %>${local.domain_name}"
76+
domain_name = local.domain_name
77+
auth_enabled = <% if eq (index .Params `userAuth`) "yes" %>true<% else %>false<% end %>
78+
auth_domain = "auth.${local.domain_name}"
79+
backend_service_domain = "<% index .Params `stagingBackendSubdomain` %>${local.domain_name}"
80+
frontend_service_domain = "<% index .Params `stagingFrontendSubdomain` %>${local.domain_name}"
81+
# This domain or address must be verified by the mail provider (Sendgrid, SES, etc.)
82+
user_auth_mail_from_address = "noreply@${local.domain_name}"
8083
}

templates/kubernetes/terraform/modules/kubernetes/files/cert_manager_issuer.yaml.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
apiVersion: cert-manager.io/v1alpha2
1+
apiVersion: cert-manager.io/v1
22
kind: ClusterIssuer
33
metadata:
44
name: "${name}"
@@ -18,7 +18,7 @@ spec:
1818

1919
---
2020

21-
apiVersion: cert-manager.io/v1alpha2
21+
apiVersion: cert-manager.io/v1
2222
kind: ClusterIssuer
2323
metadata:
2424
name: "${name}-dns"

templates/kubernetes/terraform/modules/kubernetes/user_auth.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ resource "helm_release" "kratos" {
9696
# # value = "${local.db_type}://${kubernetes_service.app_db.metadata[0].name}.${kubernetes_service.app_db.metadata[0].namespace}"
9797
# }
9898

99+
set {
100+
name = "kratos.config.courier.smtp.from_address"
101+
value = var.user_auth_mail_from_address
102+
}
103+
99104
set {
100105
name = "kratos.config.serve.public.base_url"
101106
value = "https://${var.backend_service_domain}/.ory/kratos/public"

templates/kubernetes/terraform/modules/kubernetes/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ variable "cf_signing_enabled" {
8888
description = "Enable CloudFront signed URLs"
8989
}
9090

91+
variable "domain_name" {
92+
description = "Root domain name"
93+
type = string
94+
default = ""
95+
}
96+
9197
variable "internal_domain" {
9298
description = "Internal domain to create records in"
9399
type = string
@@ -117,3 +123,9 @@ variable "frontend_service_domain" {
117123
type = string
118124
default = ""
119125
}
126+
127+
variable "user_auth_mail_from_address" {
128+
description = "Mail from the user management system will come from this address"
129+
type = string
130+
default = ""
131+
}

templates/scripts/db-ops/secret-user-auth.yml.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ stringData:
99
dsn: $DB_TYPE://$DB_APP_USERNAME:$DB_APP_PASSWORD@$DB_ENDPOINT_FOR_DSN/$DB_NAME
1010
secretsCookie: cookie-secret-$PROJECT_NAME-$SEED
1111
secretsDefault: default-secret-$PROJECT_NAME-$SEED
12-
smtpConnectionURI: http://mail-server-todo:1234
12+
smtpConnectionURI: $SMTP_URI

templates/scripts/pre-k8s.sh

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@ fi
3333

3434
kubectl --context ${KUBE_CONTEXT} -n user-auth get secrets ${PROJECT} > /dev/null 2>&1
3535
if [[ $? -ne 0 ]]; then
36-
REGION=${AWS_DEFAULT_REGION} \
37-
SEED=${RANDOM_SEED} \
38-
PROJECT_NAME=${PROJECT} \
39-
ENVIRONMENT=${ENVIRONMENT} \
40-
NAMESPACE=user-auth \
41-
DATABASE_TYPE=<% index .Params `database` %> \
42-
DATABASE_NAME=user_auth \
43-
USER_NAME=kratos \
44-
CREATE_SECRET=secret-user-auth.yml.tpl \
45-
sh ./create-db-user.sh
36+
REGION=${AWS_DEFAULT_REGION} \
37+
SEED=${RANDOM_SEED} \
38+
PROJECT_NAME=${PROJECT} \
39+
ENVIRONMENT=${ENVIRONMENT} \
40+
NAMESPACE=user-auth \
41+
DATABASE_TYPE=<% index .Params `database` %> \
42+
DATABASE_NAME=user_auth \
43+
USER_NAME=kratos \
44+
CREATE_SECRET=secret-user-auth.yml.tpl \
45+
<%- if ne (index .Params `sendgridApiKey`) "" %>
46+
SMTP_URI=smtps://apikey:${sendgridApiKey}@smtp.sendgrid.net:465 \
47+
<%- else %>
48+
SMTP_URI=smtps://no-value-specified:25 \
49+
<%- end %>
50+
sh ./create-db-user.sh
4651
fi
4752

4853
<% end %>

templates/terraform/bootstrap/secrets/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ module "sendgrid_api_key" {
4040

4141
name = "${local.project}-sendgrid-<% index .Params `randomSeed` %>"
4242
type = "string"
43-
value = "<% index .Params `sendgridApiKey` %>"
43+
value = var.sendgrid_api_key
4444
tags = map("sendgrid", local.project)
4545
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "sendgrid_api_key" {
2+
description = "The Sendgrid API key to use for mailing, if necessary"
3+
default = ""
4+
}

0 commit comments

Comments
 (0)