You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ab54179
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: lambda/user-bot/README.md
+35-9Lines changed: 35 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# user-bot
2
2
3
-
A Lambda that gives each new IAM user a temporary AWS console password and sends it to them as a Slack DM, with sign-in instructions. Tracked in [#209](https://github.com/hackforla/devops-security/issues/209).
4
-
5
-
> **Not sending DMs yet.** The deployed function uses `StubMessageSender`, which sends nothing and logs who would have been messaged. `SlackMessageSender` is written and tested, but switching to it needs a Slack app, its bot token, a secret the function can read, and permission to read it. None of these exist yet. Until then, the bot resets the password and the new user does not receive it, which is no worse than before: the password Terraform generates was never sent to anyone either.
3
+
A Lambda that gives each new IAM user a temporary AWS console password and sends it to them as a Slack DM, with sign-in instructions. Built in [#209](https://github.com/hackforla/devops-security/issues/209); connected to Slack in [#212](https://github.com/hackforla/devops-security/issues/212).
6
4
7
5
## When it runs
8
6
@@ -23,29 +21,57 @@ It reads the user's tags and **does nothing at all** (no password change, no mes
23
21
24
22
When both hold, it:
25
23
26
-
1. generates a 20-character password containing all four character classes;
27
-
2. sets it with `UpdateLoginProfile` and `PasswordResetRequired: true`, so the user must replace it at first sign-in;
28
-
3. sends the user a message with the sign-in page, their IAM user name and the temporary password.
24
+
1. reads the Slack bot token (see [The Slack token](#the-slack-token)). **If that fails, it stops here and the user's password is not touched;**
25
+
2. generates a 20-character password containing all four character classes;
26
+
3. sets it with `UpdateLoginProfile` and `PasswordResetRequired: true`, so the user must replace it at first sign-in;
27
+
4. DMs the user, as the Slack app, with the sign-in page, their IAM user name and the temporary password.
28
+
29
+
Step 1 comes before the reset on purpose. Reading the token at send time would mean a bad token leaves the user with a new password that nobody received.
29
30
30
31
Every skip is logged with its reason and the user name. **The password is never logged**, on any path; the tests assert this.
31
32
32
33
The `managed-by` check is also enforced by IAM. The execution role may call `UpdateLoginProfile` only on users carrying that tag, so a bug or a hand-crafted invocation still cannot reset anyone else's password, such as an admin or a user tagged `exempt`. If you change the check in `src/handler.ts`, change the policy in `terraform/user-bot.tf` with it.
33
34
34
35
If sending fails after the password was changed, the function throws. Lambda's asynchronous retry then runs it again from the start, which sets a fresh password and sends again.
35
36
37
+
## The Slack token
38
+
39
+
The bot posts as a Slack app with the `chat:write` bot scope, and the app's **Messages tab** is turned on so that users can see the DM thread. Its bot token (`xoxb-…`) is stored in the SSM Parameter Store `SecureString`**`/user-bot/slack-bot-token`**, in us-east-1. The function finds it through its `SLACK_TOKEN_PARAMETER` environment variable.
40
+
41
+
Terraform (`terraform/user-bot.tf`) creates the parameter with a placeholder through the write-only `value_wo`. The real token is set by hand, so it never appears in git **or in Terraform state**. Read the comment above that resource before changing it: bumping `value_wo_version` writes the placeholder back over the real token.
42
+
43
+
To set or rotate the token, run this from your own terminal. It reads the token from a prompt, so it stays out of your shell history:
`MSYS_NO_PATHCONV=1` only matters in Git Bash on Windows, which would otherwise rewrite `/user-bot/...` into a Windows path.
51
+
52
+
The function keeps the token for the life of an execution environment, so a rotated token is picked up as environments are recycled. To pick it up at once, redeploy: run **Deploy user-bot Lambda** from the Actions tab.
53
+
54
+
If the token is missing, unreadable, or still the placeholder (anything not starting `xoxb-`), every user the bot would act on is **refused**:
55
+
- the password is left unchanged;
56
+
- an error naming the parameter is logged (never its value);
57
+
- the invocation fails, so it shows up in the function's error metrics.
58
+
59
+
A failed read is not cached, so the next new user is tried again.
60
+
36
61
## Layout
37
62
38
63
```
39
64
src/
40
-
index.ts Lambda entry point; wires the handler to the stub sender
65
+
index.ts Lambda entry point; wires the handler to SlackMessageSender
41
66
handler.ts the logic above
67
+
slack-token.ts reads and caches the token from SSM
42
68
password.ts password generation
43
69
message.ts the message text and sign-in URL
44
70
logger.ts JSON-line logger
45
71
senders/
46
72
types.ts MessageSender interface
47
73
slack.ts SlackMessageSender (chat.postMessage)
48
-
stub.ts StubMessageSender (logs only)
74
+
stub.ts StubMessageSender (logs only; not deployed, kept for local runs and tests)
49
75
test/ Vitest unit tests; AWS is mocked with aws-sdk-client-mock, Slack by mocking fetch
50
76
```
51
77
@@ -69,7 +95,7 @@ Two halves, each with its own workflow:
69
95
70
96
|| Managed by | Runs when |
71
97
|---|---|---|
72
-
| The function's configuration, execution role, log group, EventBridge rule, and the deploy role |`terraform/user-bot.tf` and `terraform/aws-gha-oidc-providers.tf`, via the existing `terraform-plan.yaml` / `terraform-apply.yaml`| a `.tf` file changes |
98
+
| The function's configuration, execution role, log group, EventBridge rule, the token parameter (not its value), and the deploy role |`terraform/user-bot.tf` and `terraform/aws-gha-oidc-providers.tf`, via the existing `terraform-plan.yaml` / `terraform-apply.yaml`| a `.tf` file changes |
73
99
| The function's **code**|`.github/workflows/user-bot-deploy.yml`| a change under `lambda/user-bot/` merges to `main`|
74
100
75
101
Pull requests touching `lambda/user-bot/` run `.github/workflows/user-bot-test.yml`, which typechecks, tests and builds with no AWS credentials.
0 commit comments