Repository navigation
Expand file tree
/
Copy pathhandler.ts
More file actions
180 lines (157 loc) · 6.55 KB
/
Copy pathhandler.ts
File metadata and controls
180 lines (157 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {
type IAMClient,
ListUserTagsCommand,
type Tag,
UpdateLoginProfileCommand,
} from "@aws-sdk/client-iam";
import type { EventBridgeEvent } from "aws-lambda";
import type { Logger } from "./logger";
import { buildWelcomeMessage } from "./message";
import { generatePassword as defaultGeneratePassword } from "./password";
import type { MessageSender } from "./senders/types";
// Only users this repo's Terraform created are acted on. The execution role's IAM
// policy enforces the same tag on UpdateLoginProfile, so this check and that policy
// must change together.
export const MANAGED_BY_TAG = "managed-by";
export const MANAGED_BY_VALUE = "terraform-devops-security";
export const SLACK_ID_TAG = "slack_id";
// Same pattern as the slack_id validation in terraform/modules/aws-users/variables.tf.
// Checked here too because a tag can be set by hand, and a password must not be reset
// when it cannot be delivered.
export const SLACK_ID_PATTERN = /^[UW][A-Z0-9]{8,}$/;
// The fields of a CloudTrail record that this handler reads. EventBridge delivers the
// whole record as the event's detail.
export interface CloudTrailDetail {
eventSource?: string;
eventName?: string;
errorCode?: string;
requestParameters?: { userName?: string } | null;
}
export type CreateLoginProfileEvent = EventBridgeEvent<"AWS API Call via CloudTrail", CloudTrailDetail>;
export type SkipReason =
| "not-create-login-profile"
| "api-call-failed"
| "no-user-name"
| "user-not-found"
| "not-managed-by-devops-security"
| "no-slack-id"
| "invalid-slack-id";
export type Result =
| { outcome: "sent"; userName: string }
| { outcome: "skipped"; reason: SkipReason; userName?: string };
export interface Dependencies {
iam: IAMClient;
// Called after the tag checks and before the password is touched. Anything the
// sender needs that can fail -- reading the Slack token, above all -- belongs in
// here, so that a failure leaves the user's password unchanged instead of resetting
// it to something nobody receives.
getSender: () => Promise<MessageSender>;
logger: Logger;
generatePassword?: () => string;
}
async function listAllUserTags(iam: IAMClient, userName: string): Promise<Map<string, string>> {
const tags = new Map<string, string>();
let marker: string | undefined;
do {
const page = await iam.send(new ListUserTagsCommand({ UserName: userName, Marker: marker }));
for (const tag of page.Tags ?? ([] as Tag[])) {
if (tag.Key !== undefined && tag.Value !== undefined) {
tags.set(tag.Key, tag.Value);
}
}
marker = page.IsTruncated ? page.Marker : undefined;
} while (marker);
return tags;
}
function errorName(error: unknown): string {
return error instanceof Error ? error.name : "UnknownError";
}
export function createHandler(deps: Dependencies) {
const { iam, getSender, logger } = deps;
const generatePassword = deps.generatePassword ?? defaultGeneratePassword;
function skip(reason: SkipReason, userName?: string): Result {
logger.info("Skipping: no password set and no message sent", { reason, userName });
return { outcome: "skipped", reason, userName };
}
return async function handler(event: CreateLoginProfileEvent): Promise<Result> {
const detail = event.detail ?? {};
// The EventBridge rule already filters on both of these. Checked again so that a
// misconfigured rule or a hand-crafted invocation cannot get past them.
if (detail.eventSource !== "iam.amazonaws.com" || detail.eventName !== "CreateLoginProfile") {
return skip("not-create-login-profile");
}
if (detail.errorCode) {
return skip("api-call-failed", detail.requestParameters?.userName);
}
const userName = detail.requestParameters?.userName;
if (!userName) {
return skip("no-user-name");
}
let tags: Map<string, string>;
try {
tags = await listAllUserTags(iam, userName);
} catch (error) {
// Deleted again before this ran: nothing to do, and not worth a retry.
if (errorName(error) === "NoSuchEntityException") {
return skip("user-not-found", userName);
}
logger.error("ListUserTags failed", { userName, error: errorName(error) });
throw error;
}
if (tags.get(MANAGED_BY_TAG) !== MANAGED_BY_VALUE) {
return skip("not-managed-by-devops-security", userName);
}
const slackId = tags.get(SLACK_ID_TAG);
if (!slackId) {
return skip("no-slack-id", userName);
}
if (!SLACK_ID_PATTERN.test(slackId)) {
return skip("invalid-slack-id", userName);
}
// Resolved before the password is reset, not when sending. If the Slack token is
// missing, unreadable or still the placeholder, the user keeps their current
// password. Errors from here are logged with their message, so getSender must never
// put a secret in one (SlackTokenError names the parameter, not its value).
let sender: MessageSender;
try {
sender = await getSender();
} catch (error) {
const detail = error instanceof Error ? error.message : undefined;
logger.error("No message sender available; password left unchanged", {
userName,
error: errorName(error),
detail,
});
throw new Error(`No message sender available for ${userName}: ${errorName(error)}`);
}
const password = generatePassword();
try {
await iam.send(
new UpdateLoginProfileCommand({
UserName: userName,
Password: password,
PasswordResetRequired: true,
}),
);
} catch (error) {
// Nothing is sent: the user's password is unchanged, so there is nothing to deliver.
logger.error("UpdateLoginProfile failed; no message sent", { userName, error: errorName(error) });
throw new Error(`UpdateLoginProfile failed for ${userName}: ${errorName(error)}`);
}
try {
await sender.send({ slackId, userName, text: buildWelcomeMessage(userName, password) });
} catch (error) {
// The password has been changed but not delivered. Throwing lets Lambda's async
// retry run the whole handler again, which sets a fresh password and resends.
logger.error("Password was reset but the message was not sent", {
userName,
slackId,
error: errorName(error),
});
throw new Error(`Sending the message for ${userName} failed: ${errorName(error)}`);
}
// "handed to the sender" rather than "sent": with the stub sender nothing leaves.
logger.info("Temporary password set and message handed to the sender", { userName, slackId });
return { outcome: "sent", userName };
};
}