Skip to content

Commit 91932e7

Browse files
authored
Notification svc external secret (#223)
* notification-svc: default to external secrets * fixup! notification-svc: default to external secrets * fixup! fixup! notification-svc: default to external secrets * fixup! fixup! fixup! notification-svc: default to external secrets * fixup! fixup! fixup! fixup! notification-svc: default to external secrets
1 parent 8ef1f89 commit 91932e7

File tree

9 files changed

+78
-54
lines changed

9 files changed

+78
-54
lines changed

doc-site/docs/components/kubernetes/notification-service.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ By default, Zero uses Helm to bundle the service, you just need to enable it and
2525
[See the Helm chart][notification-available-values] for all the available configuration options. In the `application` section you can set up your API keys and application-related parameters.
2626

2727
### Setting up API keys
28-
Zero will create a Kubernetes secret containing the API keys and mounted the secret to the deployment using values from `zero-project.yml`.
28+
Zero will create a secret in AWS SecretsManager, and external-secret is created to poll the values then mounted as a secret to the deployment using values from `zero-project.yml`.
2929

3030
[See Documentation][notification-service-config] on how to configure service with environment variables
3131

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) --var "sendgrid_api_key=${sendgridApiKey}" --var "slack_api_key=${notificationServiceSlackApiKey}"&& \
31+
terraform apply $(AUTO_APPROVE) --var "sendgrid_api_key=${sendgridApiKey}" --var "slack_api_key=${notificationServiceSlackApiKey}" --var "twilio_account_id=${notificationServiceTwilioAccountId}" --var "twilio_auth_token=${notificationServiceTwilioAuthToken}"&& \
3232
rm ./terraform.tfstate )
3333

3434
apply-shared-env:

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ module "kubernetes" {
109109
## If you need to add another user-auth instance you will have to create another set of these resources
110110
]<% end %>
111111
notification_service_enabled = <%if eq (index .Params `notificationServiceEnabled`) "yes" %>true<% else %>false<% end %>
112-
notification_service_sendgrid_enabled = <%if ne (index .Params `sendgridApiKey`) "" %>true<% else %>false<% end %>
113-
notification_service_slack_enabled = <%if ne (index .Params `notificationServiceSlackApiKey`) "" %>true<% else %>false<% end %>
114112
notification_service_highly_available = true
113+
notification_service_twilio_phone_number = "<% index .Params `notificationServiceTwilioPhoneNumber` %>"
115114

116115
cache_store = "<% index .Params `cacheStore` %>"
117116

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ module "kubernetes" {
107107
## per environment one of each (database/database secret/private key) is created in the pre-k8s step
108108
## If you need to add another user-auth instance you will have to create another set of these resources
109109
]<% end %>
110-
notification_service_enabled = <%if eq (index .Params `notificationServiceEnabled`) "yes" %>true<% else %>false<% end %>
111-
notification_service_sendgrid_enabled = <%if ne (index .Params `sendgridApiKey`) "" %>true<% else %>false<% end %>
112-
notification_service_slack_enabled = <%if ne (index .Params `notificationServiceSlackApiKey`) "" %>true<% else %>false<% end %>
113-
notification_service_highly_available = false
110+
notification_service_enabled = <%if eq (index .Params `notificationServiceEnabled`) "yes" %>true<% else %>false<% end %>
111+
notification_service_highly_available = false
112+
notification_service_twilio_phone_number = "<% index .Params `notificationServiceTwilioPhoneNumber` %>"
114113

115114
cache_store = "<% index .Params `cacheStore` %>"
116115

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
locals {
22
# Created in terraform/bootstrap/secrets
3-
sendgrid_api_key_secret_name = "${var.project}-sendgrid-${var.random_seed}"
4-
slack_api_key_secret_name = "${var.project}-slack-${var.random_seed}"
5-
}
6-
7-
data "aws_secretsmanager_secret" "sendgrid_api_key" {
8-
count = var.notification_service_enabled && var.notification_service_sendgrid_enabled ? 1 : 0
9-
name = local.sendgrid_api_key_secret_name
10-
}
11-
data "aws_secretsmanager_secret_version" "sendgrid_api_key" {
12-
count = var.notification_service_enabled && var.notification_service_sendgrid_enabled ? 1 : 0
13-
secret_id = data.aws_secretsmanager_secret.sendgrid_api_key[0].id
14-
}
15-
16-
data "aws_secretsmanager_secret" "slack_api_key" {
17-
count = var.notification_service_enabled && var.notification_service_slack_enabled ? 1 : 0
18-
name = local.slack_api_key_secret_name
19-
}
20-
data "aws_secretsmanager_secret_version" "slack_api_key" {
21-
count = var.notification_service_enabled && var.notification_service_slack_enabled ? 1 : 0
22-
secret_id = data.aws_secretsmanager_secret.slack_api_key[0].id
3+
notification_service_secret_name = "${var.project}/kubernetes/${var.environment}/notification-service"
234
}
245

256
resource "kubernetes_namespace" "notification_service" {
@@ -35,7 +16,7 @@ resource "helm_release" "notification_service" {
3516
name = "zero-notification-service"
3617
repository = "https://commitdev.github.io/zero-notification-service/"
3718
chart = "zero-notification-service"
38-
version = "0.0.5"
19+
version = "0.1.0"
3920
namespace = kubernetes_namespace.notification_service[0].metadata[0].name
4021

4122
set {
@@ -62,14 +43,13 @@ resource "helm_release" "notification_service" {
6243
value = var.notification_service_highly_available ? "4" : "2"
6344
}
6445

65-
# These will become secrets provided as env vars
66-
set_sensitive {
67-
name = "application.sendgridApiKey"
68-
value = var.notification_service_enabled && var.notification_service_sendgrid_enabled ? data.aws_secretsmanager_secret_version.sendgrid_api_key[0].secret_string : ""
46+
set {
47+
name = "externalSecret.dataFrom[0]"
48+
value = local.notification_service_secret_name
6949
}
7050

71-
set_sensitive {
72-
name = "application.slackApiKey"
73-
value = var.notification_service_enabled && var.notification_service_slack_enabled ? data.aws_secretsmanager_secret_version.slack_api_key[0].secret_string : ""
51+
set {
52+
name = "application.twilioPhoneNumber"
53+
value = var.notification_service_twilio_phone_number
7454
}
7555
}

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,18 @@ variable "notification_service_enabled" {
107107
default = false
108108
}
109109

110-
variable "notification_service_slack_enabled" {
111-
description = "If enabled, will inject slack_api_key env-vars from secret manager to notification service"
112-
type = bool
113-
default = false
114-
}
115-
116-
variable "notification_service_sendgrid_enabled" {
117-
description = "If enabled, will inject sendgrid_api_key env-vars from secret manager to notification service"
118-
type = bool
119-
default = false
120-
}
121-
122110
variable "notification_service_highly_available" {
123111
description = "If enabled, will make sure a minimum of 2 pods are running and use a horizontal pod autoscaler to make scale the number of pods based on CPU. Recommended for Production."
124112
type = bool
125113
default = true
126114
}
127115

116+
variable "notification_service_twilio_phone_number" {
117+
description = "Twilio Phone Number is the Send from number for your SMS messages for the notification service"
118+
type = string
119+
default = ""
120+
}
121+
128122
variable "cache_store" {
129123
description = "Cache store - redis or memcached"
130124
type = string

templates/terraform/bootstrap/secrets/main.tf

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,34 @@ module "sendgrid_api_key" {
4444
tags = { sendgrid: local.project }
4545
}
4646

47-
module "slack_api_key" {
48-
count = <%if eq (index .Params `notificationServiceSlackApiKey`) "" %>0<% else %>1<% end %>
47+
module "notification_service_secret_prod" {
48+
count = <%if eq (index .Params `notificationServiceEnabled`) "yes" %>1<% else %>0<% end %>
4949
source = "commitdev/zero/aws//modules/secret"
5050
version = "0.0.2"
5151

52-
name = "${local.project}-slack-<% index .Params `randomSeed` %>"
53-
type = "string"
54-
value = var.slack_api_key
55-
tags = { slack: local.project }
52+
name = "${local.project}/kubernetes/prod/notification-service"
53+
type = "map"
54+
values = {
55+
SENDGRID_API_KEY = var.sendgrid_api_key
56+
SLACK_API_KEY = var.slack_api_key
57+
TWILIO_ACCOUNT_ID = var.twilio_account_id
58+
TWILIO_AUTH_TOKEN = var.twilio_auth_token
59+
}
60+
tags = { notification_svc : local.project }
61+
}
62+
63+
module "notification_service_secret_stage" {
64+
count = <%if eq (index .Params `notificationServiceEnabled`) "yes" %>1<% else %>0<% end %>
65+
source = "commitdev/zero/aws//modules/secret"
66+
version = "0.0.2"
67+
68+
name = "${local.project}/kubernetes/stage/notification-service"
69+
type = "map"
70+
values = {
71+
SENDGRID_API_KEY = var.sendgrid_api_key
72+
SLACK_API_KEY = var.slack_api_key
73+
TWILIO_ACCOUNT_ID = var.twilio_account_id
74+
TWILIO_AUTH_TOKEN = var.twilio_auth_token
75+
}
76+
tags = { notification_svc : local.project }
5677
}

templates/terraform/bootstrap/secrets/variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ variable "slack_api_key" {
77
description = "The Slack API key to use with the notification service, if necessary"
88
default = ""
99
}
10+
11+
variable "twilio_account_id" {
12+
description = "The Twilio Account ID to use with the notification service, if necessary"
13+
default = ""
14+
}
15+
16+
variable "twilio_auth_token" {
17+
description = "The Twilio Auth Token to use with the notification service, if necessary"
18+
default = ""
19+
}

zero-module.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@ parameters:
165165
- action: KeyMatchCondition
166166
whenValue: "yes"
167167
matchField: notificationServiceEnabled
168+
- field: notificationServiceTwilioAccountId
169+
label: "Twilio Account ID if you want to send SMS with the Zero Notification Service."
170+
info: "Leave blank if you don't intend to use the Twilio SMS functionality.\nSee https://www.twilio.com/"
171+
conditions:
172+
- action: KeyMatchCondition
173+
whenValue: "yes"
174+
matchField: notificationServiceEnabled
175+
- field: notificationServiceTwilioAuthToken
176+
label: "Twilio Auth Token if you want to send SMS with the Zero Notification Service."
177+
info: "Leave blank if you don't intend to use the Twilio SMS functionality.\nSee https://www.twilio.com/"
178+
conditions:
179+
- action: KeyMatchCondition
180+
whenValue: "yes"
181+
matchField: notificationServiceEnabled
182+
- field: notificationServiceTwilioPhoneNumber
183+
label: "Twilio Phone Number if you want to send SMS with the Zero Notification Service."
184+
info: "This is the Send From number of the SMS messages sent, Leave blank if you don't intend to use the Twilio SMS functionality."
185+
conditions:
186+
- action: KeyMatchCondition
187+
whenValue: "yes"
188+
matchField: notificationServiceEnabled
168189
- field: accountId
169190
label: AWS Account ID
170191
execute: aws sts get-caller-identity --query "Account" | tr -d '"'

0 commit comments

Comments
 (0)