This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.ts
110 lines (101 loc) · 3.06 KB
/
vault.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
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
import { getCallerIdentity } from '@pulumi/aws';
import { Policy, User, UserPolicyAttachment } from '@pulumi/aws/iam';
import {
ComponentResource,
CustomResourceOptions,
Input,
ResourceOptions,
interpolate,
} from '@pulumi/pulumi';
interface Args {
// The path to prefix resources with
path?: string;
// The policies that can be assigned by the vault user
policies?: Input<string>[];
}
class Vault extends ComponentResource {
constructor(name: string, args: Args, opts?: CustomResourceOptions) {
super('wafflehacks:infrastructure:Vault', name, { options: opts }, opts);
const defaultResourceOptions: ResourceOptions = { parent: this };
const { path = '/', policies = [] } = args;
const accountId = getCallerIdentity({}).then((c) => c.accountId);
const policy = new Policy(
`${name}-policy`,
{
name,
description:
'Allows a Hashicorp Vault instance to manage AWS credentials',
policy: {
Version: '2012-10-17',
Statement: [
{
// Allow creating access keys and managing vault-created users
Effect: 'Allow',
Action: [
'iam:CreateAccessKey',
'iam:DeleteAccessKey',
'iam:DeleteUser',
'iam:ListAccessKeys',
'iam:ListAttachedUserPolicies',
'iam:ListGroupsForUser',
'iam:ListUserPolicies',
],
Resource: [interpolate`arn:aws:iam::${accountId}:user/vault-*`],
},
{
// Allow assigning policies and creating users with policies
Effect: 'Allow',
Action: [
'iam:AttachUserPolicy',
'iam:CreateUser',
'iam:DeleteUserPolicy',
'iam:DetachUserPolicy',
'iam:PutUserPolicy',
],
Resource: [interpolate`arn:aws:iam::${accountId}:user/vault-*`],
Condition: {
StringEquals: {
'iam:PermissionsBoundary': policies.map(
(policy) =>
interpolate`arn:aws:iam::${accountId}:policy/${policy}`,
),
},
},
},
{
// Allow rotating the current user's access token
Effect: 'Allow',
Action: [
'iam:CreateAccessKey',
'iam:DeleteAccessKey',
'iam:GetUser',
],
Resource: [
interpolate`arn:aws:iam::${accountId}:user${path}\${aws:username}`,
],
},
],
},
},
defaultResourceOptions,
);
const user = new User(
`${name}-user`,
{
name,
path,
},
defaultResourceOptions,
);
new UserPolicyAttachment(
`${name}-policy-attachment`,
{
user: user.name,
policyArn: policy.arn,
},
defaultResourceOptions,
);
this.registerOutputs();
}
}
export default Vault;