Skip to content

Commit 7505aa1

Browse files
fix(core): IAM Policy creation based on Org config (#610)
* Reading Organizational-Units config for policy definitions * Adding WorkLoad Accounts * Creating IAM Assets from workload configuration
1 parent 6b61f66 commit 7505aa1

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

src/deployments/cdk/src/apps/phase-1.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ export interface IamPolicyArtifactsOutput {
7171
export async function deploy({ acceleratorConfig, accountStacks, accounts, context, limiter, outputs }: PhaseInput) {
7272
const mandatoryAccountConfig = acceleratorConfig.getMandatoryAccountConfigs();
7373
const orgUnits = acceleratorConfig.getOrganizationalUnits();
74+
const workLoadAccountConfig = acceleratorConfig.getWorkloadAccountConfigs();
7475
const masterAccountKey = acceleratorConfig.getMandatoryAccountKey('master');
7576
const logAccountKey = acceleratorConfig.getMandatoryAccountKey('central-log');
77+
const iamConfigs = acceleratorConfig.getIamConfigs();
7678
const masterAccountId = getAccountId(accounts, masterAccountKey);
7779
if (!masterAccountId) {
7880
throw new Error(`Cannot find mandatory primary account ${masterAccountKey}`);
@@ -317,8 +319,7 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
317319
const iamPoliciesBucketName = iamPolicyArtifactOutput[0].bucketName;
318320
const iamPoliciesBucketPrefix = iamPolicyArtifactOutput[0].keyPrefix + '/';
319321

320-
for (const [accountKey, accountConfig] of mandatoryAccountConfig) {
321-
const iamConfig = accountConfig.iam;
322+
for (const { iam: iamConfig } of iamConfigs) {
322323
if (IamConfigType.is(iamConfig)) {
323324
const iamPolicies = iamConfig?.policies;
324325
for (const iamPolicy of iamPolicies || []) {
@@ -367,7 +368,7 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
367368
}
368369

369370
if (iamPoliciesDefinition) {
370-
const iamAssets = new IamAssets(accountStack, `IAM Assets-${pascalCase(accountKey)}`, {
371+
new IamAssets(accountStack, `IAM Assets-${pascalCase(accountKey)}`, {
371372
accountKey,
372373
iamConfig,
373374
iamPoliciesDefinition,
@@ -378,31 +379,36 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
378379
}
379380
};
380381

381-
const getNonMandatoryAccountsPerOu = (ouName: string, mandatoryAccKeys: string[]): Account[] => {
382-
const accountsPerOu: Account[] = [];
383-
for (const account of accounts) {
384-
if (account.ou === ouName && !mandatoryAccKeys.includes(account.key)) {
385-
accountsPerOu.push(account);
382+
const accountIamConfigs: { [accountKey: string]: IamConfig } = {};
383+
for (const { accountKey, iam: iamConfig } of iamConfigs) {
384+
if (accountIamConfigs[accountKey]) {
385+
if (accountIamConfigs[accountKey].policies) {
386+
accountIamConfigs[accountKey].policies?.push(...(iamConfig.policies || []));
387+
} else {
388+
accountIamConfigs[accountKey].policies = iamConfig.policies;
386389
}
387-
}
388-
return accountsPerOu;
389-
};
390390

391-
const mandatoryAccountKeys: string[] = [];
392-
// creating assets for default account settings
393-
for (const [accountKey, accountConfig] of mandatoryAccountConfig) {
394-
mandatoryAccountKeys.push(accountKey);
395-
await createIamAssets(accountKey, accountConfig.iam);
396-
}
391+
if (accountIamConfigs[accountKey].roles) {
392+
accountIamConfigs[accountKey].roles?.push(...(iamConfig.roles || []));
393+
} else {
394+
accountIamConfigs[accountKey].roles = iamConfig.roles;
395+
}
397396

398-
// creating assets for org unit accounts
399-
for (const [orgName, orgConfig] of orgUnits) {
400-
const orgAccounts = getNonMandatoryAccountsPerOu(orgName, mandatoryAccountKeys);
401-
for (const orgAccount of orgAccounts) {
402-
await createIamAssets(orgAccount.key, orgConfig.iam);
397+
if (accountIamConfigs[accountKey].users) {
398+
accountIamConfigs[accountKey].users?.push(...(iamConfig.users || []));
399+
} else {
400+
accountIamConfigs[accountKey].users = iamConfig.users;
401+
}
402+
} else {
403+
accountIamConfigs[accountKey] = iamConfig;
403404
}
404405
}
405406

407+
// creating assets for default account settings
408+
for (const [accountKey, iamConfig] of Object.entries(accountIamConfigs)) {
409+
await createIamAssets(accountKey, iamConfig);
410+
}
411+
406412
// Budget creation step 2
407413
await budget.step2({
408414
accountStacks,

src/deployments/cdk/src/common/iam-assets.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class IamAssets extends cdk.Construct {
2929
const customerManagedPolicies: { [policyName: string]: iam.ManagedPolicy } = {};
3030
// method to create IAM Policy
3131
const createIamPolicy = (policyName: string, policy: string): void => {
32+
console.log(`Creating Policy "${policyName}" in account "${accountKey}"`);
3233
const iamPolicyDef = iamPoliciesDefinition[policyName];
3334
const iamPolicyJson = JSON.parse(iamPolicyDef);
3435
const statementArray = iamPolicyJson.Statement;

0 commit comments

Comments
 (0)