-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserviceAccount.ts
70 lines (64 loc) · 1.8 KB
/
serviceAccount.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
import * as gcp from '@pulumi/gcp';
interface VaultServiceAccountArgs {
serviceAccountIamRoles: string[];
serviceAccountCustomIamRoles: string[];
}
/**
* Create Vault service account, a service account key, add the service account
* to the project and user-specified roles.
*/
export default function createServiceAccount(
args: VaultServiceAccountArgs
): {
serviceAccount: gcp.serviceAccount.Account;
appliedServiceAccountIamRoles: gcp.projects.IAMMember[];
appliedServiceAccountCustomIamRoles: gcp.projects.IAMMember[];
} {
const serviceAccount = new gcp.serviceAccount.Account('vault-server', {
accountId: 'vault-server',
displayName: 'Vault Server',
});
((): gcp.serviceAccount.Key =>
new gcp.serviceAccount.Key(
'vault',
{
serviceAccountId: serviceAccount.accountId,
},
{
additionalSecretOutputs: ['privateKey'],
}
))();
const appliedServiceAccountIamRoles = args.serviceAccountIamRoles.reduce(
(acc, cur) => {
acc.push(
new gcp.projects.IAMMember(`vault-${cur.replace(/[/.]/g, '-')}`, {
member: serviceAccount.email.apply(
email => `serviceAccount:${email}`
),
role: cur,
})
);
return acc;
},
[] as gcp.projects.IAMMember[]
);
const appliedServiceAccountCustomIamRoles = args.serviceAccountCustomIamRoles.reduce(
(acc, cur) => {
acc.push(
new gcp.projects.IAMMember(`vault-${cur.replace(/[/.]/g, '-')}`, {
member: serviceAccount.email.apply(
email => `serviceAccount:${email}`
),
role: cur,
})
);
return acc;
},
[] as gcp.projects.IAMMember[]
);
return {
serviceAccount,
appliedServiceAccountIamRoles,
appliedServiceAccountCustomIamRoles,
};
}