|
| 1 | +#Notifications |
| 2 | + |
| 3 | +Notifications are sent by Coder in response to specific internal events, such as |
| 4 | +a workspace being deleted or a user being created. |
| 5 | + |
| 6 | +**Notifications are currently an experimental feature.** |
| 7 | + |
| 8 | +##Enable experiment |
| 9 | + |
| 10 | +In order to activate the notifications feature, you'll need to enable the |
| 11 | +`notifications` experiment. |
| 12 | + |
| 13 | +```bash |
| 14 | +# Using the CLI flag |
| 15 | +$ coder server --experiments=notifications |
| 16 | + |
| 17 | +# Alternatively, using the `CODER_EXPERIMENTS` environment variable |
| 18 | +$ CODER_EXPERIMENTS=notifications coder server |
| 19 | +``` |
| 20 | + |
| 21 | +More information on experiments can be found |
| 22 | +[here](https://coder.com/docs/contributing/feature-stages#experimental-features). |
| 23 | + |
| 24 | +##Event Types |
| 25 | + |
| 26 | +Notifications are sent in response to internal events, to alert the affected |
| 27 | +user(s) of this event. Currently we support the following list of events: |
| 28 | + |
| 29 | +###Workspace Events |
| 30 | + |
| 31 | +_These notifications are sent to the workspace owner._ |
| 32 | + |
| 33 | +- Workspace Deleted |
| 34 | +- Workspace Manual Build Failure |
| 35 | +- Workspace Automatic Build Failure |
| 36 | +- Workspace Automatically Updated |
| 37 | +- Workspace Dormant |
| 38 | +- Workspace Marked For Deletion |
| 39 | + |
| 40 | +###User Events |
| 41 | + |
| 42 | +_These notifications are sent to users with**owner** and**user admin** roles._ |
| 43 | + |
| 44 | +- User Account Created |
| 45 | +- User Account Deleted |
| 46 | +- User Account Suspended |
| 47 | +- User Account Activated |
| 48 | +-_(coming soon) User Password Reset_ |
| 49 | +-_(coming soon) User Email Verification_ |
| 50 | + |
| 51 | +_These notifications are sent to the user themselves._ |
| 52 | + |
| 53 | +- User Account Suspended |
| 54 | +- User Account Activated |
| 55 | + |
| 56 | +###Template Events |
| 57 | + |
| 58 | +_These notifications are sent to users with**template admin** roles._ |
| 59 | + |
| 60 | +- Template Deleted |
| 61 | + |
| 62 | +##Configuration |
| 63 | + |
| 64 | +You can modify the notification delivery behavior using the following server |
| 65 | +flags. |
| 66 | + |
| 67 | +| Required| CLI| Env| Type| Description| Default| |
| 68 | +| :------:| -----------------------------------| ---------------------------------------| ----------| ---------------------------------------------------------------------------------------------------------------------| -------| |
| 69 | +| ✔️|`--notifications-dispatch-timeout`|`CODER_NOTIFICATIONS_DISPATCH_TIMEOUT`|`duration`| How long to wait while a notification is being sent before giving up.| 1m| |
| 70 | +| ✔️|`--notifications-method`|`CODER_NOTIFICATIONS_METHOD`|`string`| Which delivery method to use (available options: 'smtp', 'webhook'). See[Delivery Methods](#delivery-methods) below.| smtp| |
| 71 | +| -️|`--notifications-max-send-attempts`|`CODER_NOTIFICATIONS_MAX_SEND_ATTEMPTS`|`int`| The upper limit of attempts to send a notification.| 5| |
| 72 | + |
| 73 | +##Delivery Methods |
| 74 | + |
| 75 | +Notifications can currently be delivered by either SMTP or webhook. Each message |
| 76 | +can only be delivered to one method, and this method is configured globally with |
| 77 | +[`CODER_NOTIFICATIONS_METHOD`](https://coder.com/docs/reference/cli/server#--notifications-method) |
| 78 | +(default:`smtp`). |
| 79 | + |
| 80 | +Enterprise customers can configured which method to use for each of the |
| 81 | +supported[Events](#events); see the[Preferences](#preferences) section below |
| 82 | +for more details. |
| 83 | + |
| 84 | +##SMTP (Email) |
| 85 | + |
| 86 | +Use the`smtp` method to deliver notifications by email to your users. Coder |
| 87 | +does not ship with an SMTP server, so you will need to configure Coder to use an |
| 88 | +existing one. |
| 89 | + |
| 90 | +**Server Settings:** |
| 91 | + |
| 92 | +| Required| CLI| Env| Type| Description| Default| |
| 93 | +| :------:| ---------------------------------| -------------------------------------| -----------| -----------------------------------------| -------------| |
| 94 | +| ✔️|`--notifications-email-from`|`CODER_NOTIFICATIONS_EMAIL_FROM`|`string`| The sender's address to use.|| |
| 95 | +| ✔️|`--notifications-email-smarthost`|`CODER_NOTIFICATIONS_EMAIL_SMARTHOST`|`host:port`| The SMTP relay to send messages through.| localhost:587| |
| 96 | +| -️|`--notifications-email-hello`|`CODER_NOTIFICATIONS_EMAIL_HELLO`|`string`| The hostname identifying the SMTP server.| localhost| |
| 97 | + |
| 98 | +**Authentication Settings:** |
| 99 | + |
| 100 | +| Required| CLI| Env| Type| Description| |
| 101 | +| :------:| ------------------------------------------| ----------------------------------------------| --------| -------------------------------------------------------------------------| |
| 102 | +| -|`--notifications-email-auth-username`|`CODER_NOTIFICATIONS_EMAIL_AUTH_USERNAME`|`string`| Username to use with PLAIN/LOGIN authentication.| |
| 103 | +| -|`--notifications-email-auth-password`|`CODER_NOTIFICATIONS_EMAIL_AUTH_PASSWORD`|`string`| Password to use with PLAIN/LOGIN authentication.| |
| 104 | +| -|`--notifications-email-auth-password-file`|`CODER_NOTIFICATIONS_EMAIL_AUTH_PASSWORD_FILE`|`string`| File from which to load password for use with PLAIN/LOGIN authentication.| |
| 105 | +| -|`--notifications-email-auth-identity`|`CODER_NOTIFICATIONS_EMAIL_AUTH_IDENTITY`|`string`| Identity to use with PLAIN authentication.| |
| 106 | + |
| 107 | +**TLS Settings:** |
| 108 | + |
| 109 | +| Required| CLI| Env| Type| Description| Default| |
| 110 | +| :------:| -----------------------------------------| -------------------------------------------| --------| ----------------------------------------------------------------------------------------------------------------------------------------------------------------| -------| |
| 111 | +| -|`--notifications-email-force-tls`|`CODER_NOTIFICATIONS_EMAIL_FORCE_TLS`|`bool`| Force a TLS connection to the configured SMTP smarthost. If port 465 is used, TLS will be forced. Seehttps://datatracker.ietf.org/doc/html/rfc8314#section-3.3.| false| |
| 112 | +| -|`--notifications-email-tls-starttls`|`CODER_NOTIFICATIONS_EMAIL_TLS_STARTTLS`|`bool`| Enable STARTTLS to upgrade insecure SMTP connections using TLS. Ignored if`CODER_NOTIFICATIONS_EMAIL_FORCE_TLS` is set.| false| |
| 113 | +| -|`--notifications-email-tls-skip-verify`|`CODER_NOTIFICATIONS_EMAIL_TLS_SKIPVERIFY`|`bool`| Skip verification of the target server's certificate (**insecure**).| false| |
| 114 | +| -|`--notifications-email-tls-server-name`|`CODER_NOTIFICATIONS_EMAIL_TLS_SERVERNAME`|`string`| Server name to verify against the target certificate.|| |
| 115 | +| -|`--notifications-email-tls-cert-file`|`CODER_NOTIFICATIONS_EMAIL_TLS_CERTFILE`|`string`| Certificate file to use.|| |
| 116 | +| -|`--notifications-email-tls-cert-key-file`|`CODER_NOTIFICATIONS_EMAIL_TLS_CERTKEYFILE`|`string`| Certificate key file to use.|| |
| 117 | + |
| 118 | +**NOTE:** you_MUST_ use`CODER_NOTIFICATIONS_EMAIL_FORCE_TLS` if your smarthost |
| 119 | +supports TLS on a port other than`465`. |
| 120 | + |
| 121 | +###Send emails using G-Suite |
| 122 | + |
| 123 | +After setting the required fields above: |
| 124 | + |
| 125 | +1. Create an[App Password](https://myaccount.google.com/apppasswords) using the |
| 126 | + account you wish to send from |
| 127 | +2. Set the following configuration options: |
| 128 | +``` |
| 129 | + CODER_NOTIFICATIONS_EMAIL_SMARTHOST=smtp.gmail.com:465 |
| 130 | + CODER_NOTIFICATIONS_EMAIL_AUTH_USERNAME=<user>@<domain> |
| 131 | + CODER_NOTIFICATIONS_EMAIL_AUTH_PASSWORD="<app password created above>" |
| 132 | +``` |
| 133 | + |
| 134 | +See |
| 135 | +[this help article from Google](https://support.google.com/a/answer/176600?hl=en) |
| 136 | +for more options. |
| 137 | + |
| 138 | +###Send emails using Outlook.com |
| 139 | + |
| 140 | +After setting the required fields above: |
| 141 | + |
| 142 | +1. Setup an account on Microsoft 365 or outlook.com |
| 143 | +2. Set the following configuration options: |
| 144 | +``` |
| 145 | + CODER_NOTIFICATIONS_EMAIL_SMARTHOST=smtp-mail.outlook.com:587 |
| 146 | + CODER_NOTIFICATIONS_EMAIL_TLS_STARTTLS=true |
| 147 | + CODER_NOTIFICATIONS_EMAIL_AUTH_USERNAME=<user>@<domain> |
| 148 | + CODER_NOTIFICATIONS_EMAIL_AUTH_PASSWORD="<account password>" |
| 149 | +``` |
| 150 | + |
| 151 | +See |
| 152 | +[this help article from Microsoft](https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040) |
| 153 | +for more options. |
| 154 | + |
| 155 | +##Webhook |
| 156 | + |
| 157 | +The webhook delivery method sends an HTTP POST request to the defined endpoint. |
| 158 | +The purpose of webhook notifications is to enable integrations with other |
| 159 | +systems. |
| 160 | + |
| 161 | +**Settings**: |
| 162 | + |
| 163 | +| Required| CLI| Env| Type| Description| |
| 164 | +| :------:| ----------------------------------| --------------------------------------| -----| ---------------------------------------| |
| 165 | +| ✔️|`--notifications-webhook-endpoint`|`CODER_NOTIFICATIONS_WEBHOOK_ENDPOINT`|`url`| The endpoint to which to send webhooks.| |
| 166 | + |
| 167 | +Here is an example payload for Coder's webhook notification: |
| 168 | + |
| 169 | +```json |
| 170 | +{ |
| 171 | +"_version":"1.0", |
| 172 | +"msg_id":"88750cad-77d4-4663-8bc0-f46855f5019b", |
| 173 | +"payload": { |
| 174 | +"_version":"1.0", |
| 175 | +"notification_name":"Workspace Deleted", |
| 176 | +"user_id":"4ac34fcb-8155-44d5-8301-e3cd46e88b35", |
| 177 | +"user_email":"danny@coder.com", |
| 178 | +"user_name":"danny", |
| 179 | +"user_username":"danny", |
| 180 | +"actions": [ |
| 181 | +{ |
| 182 | +"label":"View workspaces", |
| 183 | +"url":"https://et23ntkhpueak.pit-1.try.coder.app/workspaces" |
| 184 | +}, |
| 185 | +{ |
| 186 | +"label":"View templates", |
| 187 | +"url":"https://et23ntkhpueak.pit-1.try.coder.app/templates" |
| 188 | +} |
| 189 | +], |
| 190 | +"labels": { |
| 191 | +"initiator":"danny", |
| 192 | +"name":"my-workspace", |
| 193 | +"reason":"initiated by user" |
| 194 | +} |
| 195 | +}, |
| 196 | +"title":"Workspace\"my-workspace\" deleted", |
| 197 | +"body":"Hi danny\n\nYour workspace my-workspace was deleted.\nThe specified reason was\"initiated by user (danny)\"." |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +The top-level object has these keys: |
| 202 | + |
| 203 | +-`_version`: describes the version of this schema; follows semantic versioning |
| 204 | +-`msg_id`: the UUID of the notification (matches the ID in the |
| 205 | +`notification_messages` table) |
| 206 | +-`payload`: contains the specific details of the notification; described below |
| 207 | +-`title`: the title of the notification message (equivalent to a subject in |
| 208 | + SMTP delivery) |
| 209 | +-`body`: the body of the notification message (equivalent to the message body |
| 210 | + in SMTP delivery) |
| 211 | + |
| 212 | +The`payload` object has these keys: |
| 213 | + |
| 214 | +-`_version`: describes the version of this inner schema; follows semantic |
| 215 | + versioning |
| 216 | +-`notification_name`: name of the event which triggered the notification |
| 217 | +-`user_id`: Coder internal user identifier of the target user (UUID) |
| 218 | +-`user_email`: email address of the target user |
| 219 | +-`user_name`: name of the target user |
| 220 | +-`user_username`: username of the target user |
| 221 | +-`actions`: a list of CTAs (Call-To-Action); these are mainly relevant for SMTP |
| 222 | + delivery in which they're shown as buttons |
| 223 | +-`labels`: dynamic map of zero or more string key-value pairs; these vary from |
| 224 | + event to event |
| 225 | + |
| 226 | +##User Preferences |
| 227 | + |
| 228 | +All users have the option to opt-out of any notifications. Go to**Account** -> |
| 229 | +**Notifications** to turn notifications on or off. The delivery method for each |
| 230 | +notification is indicated on the right hand side of this table. |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | +##Delivery Preferences (enterprise) |
| 235 | + |
| 236 | +Administrators can configure which delivery methods are used for each different |
| 237 | +[event type](#event-types). |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | +You can find this page under |
| 242 | +`https://$CODER_ACCESS_URL/deployment/notifications?tab=events`. |
| 243 | + |
| 244 | +##Stop sending notifications |
| 245 | + |
| 246 | +Administrators may wish to stop_all_ notifications across the deployment. We |
| 247 | +support a killswitch in the CLI for these cases. |
| 248 | + |
| 249 | +To pause sending notifications, execute |
| 250 | +[`coder notifications pause`](https://coder.com/docs/reference/cli/notifications_pause). |
| 251 | + |
| 252 | +To resume sending notifications, execute |
| 253 | +[`coder notifications resume`](https://coder.com/docs/reference/cli/notifications_resume). |
| 254 | + |
| 255 | +##Internals |
| 256 | + |
| 257 | +The notification system is built to operate concurrently in a single- or |
| 258 | +multi-replica Coder deployment, and has a built-in retry mechanism. It uses the |
| 259 | +configured Postgres database to store notifications in a queue and facilitate |
| 260 | +concurrency. |
| 261 | + |
| 262 | +All messages are stored in the`notification_messages` table. |
| 263 | + |
| 264 | +Messages older than 7 days are deleted. |
| 265 | + |
| 266 | +###Message States |
| 267 | + |
| 268 | + |
| 269 | + |
| 270 | +_A notifier here refers to a Coder replica which is responsible for dispatching |
| 271 | +the notification. All running replicas act as notifiers to process pending |
| 272 | +messages._ |
| 273 | + |
| 274 | +- a message begins in`pending` state |
| 275 | +- transitions to`leased` when a Coder replica acquires new messages from the |
| 276 | + database |
| 277 | +- new messages are checked for every`CODER_NOTIFICATIONS_FETCH_INTERVAL` |
| 278 | + (default: 15s) |
| 279 | +- if a message is delivered successfully, it transitions to`sent` state |
| 280 | +- if a message encounters a non-retryable error (e.g. misconfiguration), it |
| 281 | + transitions to`permanent_failure` |
| 282 | +- if a message encounters a retryable error (e.g. temporary server outage), it |
| 283 | + transitions to`temporary_failure` |
| 284 | +- this message will be retried up to`CODER_NOTIFICATIONS_MAX_SEND_ATTEMPTS` |
| 285 | + (default: 5) |
| 286 | +- this message will transition back to`pending` state after |
| 287 | +`CODER_NOTIFICATIONS_RETRY_INTERVAL` (default: 5m) and be retried |
| 288 | +- after`CODER_NOTIFICATIONS_MAX_SEND_ATTEMPTS` is exceeded, it transitions to |
| 289 | +`permanent_failure` |
| 290 | + |
| 291 | +Diagnostic messages will be saved in the`notification_messages` table and will |
| 292 | +be logged, in the case of failure. |