-
Notifications
You must be signed in to change notification settings - Fork 6
/
instance-role.ts
67 lines (59 loc) · 2.64 KB
/
instance-role.ts
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
import { ManagedPolicy, ServicePrincipal } from "aws-cdk-lib/aws-iam";
import { GuAppAwareConstruct } from "../../../utils/mixin/app-aware-construct";
import type { AppIdentity, GuStack } from "../../core";
import {
GuDescribeEC2Policy,
GuGetDistributablePolicy,
GuLogShippingPolicy,
GuParameterStoreReadPolicy,
} from "../policies";
import type { GuPolicy } from "../policies";
import { GuRole } from "./roles";
export interface GuInstanceRoleProps {
/**
* By default, instances are given permissions to write to Kinesis. Set to
* 'true' to prevent this. Note, disabling will prevent not just application
* logs being shipped but also anything else - for example, automatic log
* shipping of Cloud Init and other logs by the cdk-base role in your AMI.
*/
withoutLogShipping?: boolean;
additionalPolicies?: GuPolicy[];
}
export type GuInstanceRolePropsWithApp = GuInstanceRoleProps & AppIdentity;
/**
* Creates an IAM role with common policies that are needed by most Guardian applications.
*
* More specifically:
* 1. Allows for `ssh` access to an EC2 instance via [ssm-scala](https://github.com/guardian/ssm-scala) (instead of standard `ssh`).
* 2. Allows EC2 instances to communicate with Wazuh, for security monitoring.
* 3. Allows EC2 instances to download an artifact from AWS S3, for application deployment.
* 4. Allows EC2 instances to download private configuration from AWS Parameter Store. See [[`GuParameterStoreReadPolicy`]]
* for specific details.
* 5. Allows EC2 instances to write logs into our central ELK stack via Kinesis.
*
* If additional IAM permissions are required, create custom policies and pass them in via the `additionalPolicies` prop.
*
* If log shipping is not required, opt out by setting the `withoutLogShipping` prop to `true`.
*/
export class GuInstanceRole extends GuAppAwareConstruct(GuRole) {
constructor(scope: GuStack, props: GuInstanceRolePropsWithApp) {
super(scope, "InstanceRole", {
path: "/",
assumedBy: new ServicePrincipal("ec2.amazonaws.com"),
...props,
});
const sharedPolicies = [
GuDescribeEC2Policy.getInstance(scope),
...(props.withoutLogShipping ? [] : [GuLogShippingPolicy.getInstance(scope)]),
];
const policies = [
...sharedPolicies,
new GuGetDistributablePolicy(scope, props),
new GuParameterStoreReadPolicy(scope, props),
...(props.additionalPolicies ? props.additionalPolicies : []),
];
const managedPolicies = [ManagedPolicy.fromAwsManagedPolicyName("AmazonSSMManagedInstanceCore")];
policies.forEach((p) => p.attachToRole(this));
managedPolicies.forEach((p) => this.addManagedPolicy(p));
}
}