-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathforwarder.ts
More file actions
95 lines (87 loc) · 3.75 KB
/
Copy pathforwarder.ts
File metadata and controls
95 lines (87 loc) · 3.75 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
/*
* Unless explicitly stated otherwise all files in this repository are licensed
* under the Apache License Version 2.0.
*
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2020-2026 Datadog, Inc.
*/
import * as crypto from "crypto";
import { Names } from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import { FilterPattern, ILogGroup } from "aws-cdk-lib/aws-logs";
import { LambdaDestination } from "aws-cdk-lib/aws-logs-destinations";
import * as sfn from "aws-cdk-lib/aws-stepfunctions";
import { Construct } from "constructs";
import log from "loglevel";
import { SUBSCRIPTION_FILTER_PREFIX } from "./index";
import { LambdaFunction } from "./interfaces";
function getForwarder(scope: Construct, forwarderArn: string) {
const forwarderConstructId = generateForwarderConstructId(forwarderArn);
if (scope.node.tryFindChild(forwarderConstructId)) {
return scope.node.tryFindChild(forwarderConstructId) as lambda.IFunction;
} else {
return lambda.Function.fromFunctionArn(scope, forwarderConstructId, forwarderArn);
}
}
export function addForwarder(
scope: Construct,
lam: LambdaFunction,
forwarderArn: string,
createForwarderPermissions: boolean,
): void {
const forwarder = getForwarder(scope, forwarderArn);
const forwarderDestination = new LambdaDestination(forwarder, { addPermissions: createForwarderPermissions });
const subscriptionFilterName = generateSubscriptionFilterName(Names.nodeUniqueId(lam.permissionsNode), forwarderArn);
log.debug(`Adding log subscription ${subscriptionFilterName} for ${lam.functionName}`);
lam.logGroup.addSubscriptionFilter(subscriptionFilterName, {
destination: forwarderDestination,
filterPattern: FilterPattern.allEvents(),
});
}
export function addForwarderForStateMachine(
scope: Construct,
stateMachine: sfn.StateMachine,
forwarderArn: string,
logGroup: ILogGroup,
): void {
const forwarder = getForwarder(scope, forwarderArn);
const forwarderDestination = new LambdaDestination(forwarder);
const subscriptionFilterName = generateSubscriptionFilterName(Names.uniqueId(stateMachine), forwarderArn);
log.debug(`Adding log subscription ${subscriptionFilterName} for ${stateMachine.node.path}`);
logGroup.addSubscriptionFilter(subscriptionFilterName, {
destination: forwarderDestination,
filterPattern: FilterPattern.allEvents(),
});
}
export function addForwarderToLogGroups(
scope: Construct,
logGroups: ILogGroup[],
forwarderArn: string,
createForwarderPermissions: boolean,
): void {
const forwarder = getForwarder(scope, forwarderArn);
const forwarderDestination = new LambdaDestination(forwarder, { addPermissions: createForwarderPermissions });
logGroups.forEach((group) => {
const subscriptionFilterName = generateSubscriptionFilterName(Names.nodeUniqueId(group.node), forwarderArn);
group.addSubscriptionFilter(subscriptionFilterName, {
destination: forwarderDestination,
filterPattern: FilterPattern.allEvents(),
});
});
}
export function generateForwarderConstructId(forwarderArn: string): string {
log.debug("Generating construct Id for Datadog Lambda Forwarder");
return "forwarder" + crypto.createHash("sha256").update(forwarderArn).digest("hex");
}
export function generateSubscriptionFilterName(functionUniqueId: string, forwarderArn: string): string {
const subscriptionFilterValue: string = crypto
.createHash("sha256")
.update(functionUniqueId)
.update(forwarderArn)
.digest("hex");
const subscriptionFilterValueLength = subscriptionFilterValue.length;
const subscriptionFilterName =
SUBSCRIPTION_FILTER_PREFIX +
subscriptionFilterValue.substring(subscriptionFilterValueLength - 8, subscriptionFilterValueLength);
return subscriptionFilterName;
}