Skip to content

Commit 15647d7

Browse files
fix(core): Moving zones configuration to Vpc Config (#528)
* fix(core): Moving zones configuration to Vpc Config * remove zones from global-options * Update example config files * Fix association of private hosted zones to VPC * Fix adding zones to multiple vpcs in same account Co-authored-by: Brian969 <56414362+Brian969@users.noreply.github.com>
1 parent e76f581 commit 15647d7

File tree

13 files changed

+296
-309
lines changed

13 files changed

+296
-309
lines changed

reference-artifacts/config.example.json

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,6 @@
8686
"report-versioning": "OVERWRITE_REPORT"
8787
}
8888
},
89-
"zones": [
90-
{
91-
"account": "shared-network",
92-
"resolver-vpc": "Endpoint",
93-
"region": "ca-central-1",
94-
"names": {
95-
"public": ["cloud-hosted-publicdomain.example.ca"],
96-
"private": ["cloud-hosted-privatedomain.example.ca"]
97-
}
98-
}
99-
],
10089
"vpc-flow-logs": {
10190
"filter": "ALL",
10291
"interval": 60,
@@ -734,7 +723,12 @@
734723
"zone": "on-premise-privatedomain2.example.ca",
735724
"outbound-ips": ["10.254.254.1", "10.254.253.1"]
736725
}
737-
]
726+
],
727+
"zones": {
728+
"public": ["cloud-hosted-publicdomain.example.ca"],
729+
"private": ["cloud-hosted-privatedomain.example.ca"]
730+
},
731+
"central-endpoint": true
738732
}
739733
],
740734
"deployments": {

reference-artifacts/config.lite-example.json

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,6 @@
8686
"report-versioning": "OVERWRITE_REPORT"
8787
}
8888
},
89-
"zones": [
90-
{
91-
"account": "shared-network",
92-
"resolver-vpc": "Endpoint",
93-
"region": "ca-central-1",
94-
"names": {
95-
"public": ["cloud-hosted-publicdomain.example.ca"],
96-
"private": ["cloud-hosted-privatedomain.example.ca"]
97-
}
98-
}
99-
],
10089
"vpc-flow-logs": {
10190
"filter": "ALL",
10291
"interval": 60,
@@ -674,7 +663,12 @@
674663
"zone": "on-premise-privatedomain2.example.ca",
675664
"outbound-ips": ["10.254.254.1", "10.254.253.1"]
676665
}
677-
]
666+
],
667+
"zones": {
668+
"public": ["cloud-hosted-publicdomain.example.ca"],
669+
"private": ["cloud-hosted-privatedomain.example.ca"]
670+
},
671+
"central-endpoint": true
678672
}
679673
],
680674
"deployments": {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import * as cleanup from '../deployments/cleanup';
4040
* - create firewalls (step 1);
4141
* - create budgets (step 1);
4242
* - create transit gateways (step 1);
43-
* - create Route53 DNS logging log group;
4443
* - enable Macie (step 1);
4544
* - enable GuardDuty;
4645
* - enable Access Analyzer;

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

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ import { pascalCase } from 'pascal-case';
55
import { getAccountId, Account } from '../utils/accounts';
66
import { VpcProps, VpcStack, Vpc } from '../common/vpc';
77
import { Limit } from '../utils/limits';
8-
import { NestedStack } from '@aws-cdk/aws-cloudformation';
98
import {
10-
InterfaceEndpointConfig,
119
PeeringConnectionConfig,
1210
IamConfig,
1311
IamConfigType,
1412
IamPolicyConfigType,
1513
VpcConfig,
1614
} from '@aws-accelerator/common-config';
17-
import { InterfaceEndpoint } from '../common/interface-endpoints';
1815
import { IamAssets } from '../common/iam-assets';
1916
import { STS } from '@aws-accelerator/common/src/aws/sts';
2017
import { S3 } from '@aws-accelerator/common/src/aws/s3';
21-
import { createRoleName, createName } from '@aws-accelerator/cdk-accelerator/src/core/accelerator-name-generator';
18+
import { createRoleName } from '@aws-accelerator/cdk-accelerator/src/core/accelerator-name-generator';
2219
import { CentralBucketOutput, LogBucketOutput } from '../deployments/defaults/outputs';
2320
import * as budget from '../deployments/billing/budget';
2421
import * as certificates from '../deployments/certificates';
@@ -34,10 +31,6 @@ import { getIamUserPasswordSecretValue } from '../deployments/iam';
3431
import * as cwlCentralLoggingToS3 from '../deployments/central-services/central-logging-s3';
3532
import * as vpcDeployment from '../deployments/vpc';
3633
import * as transitGateway from '../deployments/transit-gateway';
37-
import { DNS_LOGGING_LOG_GROUP_REGION } from '@aws-accelerator/common/src/util/constants';
38-
import { LogGroup } from '@aws-accelerator/custom-resource-logs-log-group';
39-
import { LogResourcePolicy } from '@aws-accelerator/custom-resource-logs-resource-policy';
40-
import { IamRoleOutputFinder } from '@aws-accelerator/common-outputs/src/iam-role';
4134
import * as centralEndpoints from '../deployments/central-endpoints';
4235
import { VpcOutputFinder, VpcSubnetOutput } from '@aws-accelerator/common-outputs/src/vpc';
4336

@@ -197,8 +190,10 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
197190
};
198191

199192
const subscriptionCheckDone: string[] = [];
193+
const dnsLogGroupsAccountAndRegion: { [accoutKey: string]: boolean } = {};
200194
// Create all the VPCs for accounts and organizational units
201195
for (const { ouKey, accountKey, vpcConfig, deployments } of acceleratorConfig.getVpcConfigs()) {
196+
let createPolicy = false;
202197
if (!limiter.create(accountKey, Limit.VpcPerRegion, vpcConfig.region)) {
203198
console.log(
204199
`Skipping VPC "${vpcConfig.name}" deployment. Reached maximum VPCs per region for account "${accountKey}" and region "${vpcConfig.region}`,
@@ -272,6 +267,22 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
272267
vpcConfig,
273268
vpcId: vpc!.id,
274269
});
270+
271+
// Create DNS Query Logging Log Group
272+
if (vpcConfig.zones && vpcConfig.zones.public.length > 0) {
273+
if (!dnsLogGroupsAccountAndRegion[accountKey]) {
274+
createPolicy = true;
275+
dnsLogGroupsAccountAndRegion[accountKey] = true;
276+
}
277+
await centralEndpoints.createDnsQueryLogGroup({
278+
acceleratorPrefix: context.acceleratorPrefix,
279+
accountKey,
280+
accountStacks,
281+
outputs,
282+
vpcConfig,
283+
createPolicy,
284+
});
285+
}
275286
}
276287

277288
// Create the firewall
@@ -471,57 +482,6 @@ export async function deploy({ acceleratorConfig, accountStacks, accounts, conte
471482
outputs,
472483
});
473484

474-
/**
475-
* Code to create LogGroups required for DNS Logging
476-
*/
477-
const globalOptionsConfig = acceleratorConfig['global-options'];
478-
const zoneConfig = globalOptionsConfig.zones.find(zc => zc.names);
479-
const zonesAccountKey = zoneConfig?.account!;
480-
481-
const zonesStack = accountStacks.getOrCreateAccountStack(zonesAccountKey, DNS_LOGGING_LOG_GROUP_REGION);
482-
const logGroupLambdaRoleOutput = IamRoleOutputFinder.tryFindOneByName({
483-
outputs,
484-
accountKey: zonesAccountKey,
485-
roleKey: 'LogGroupRole',
486-
});
487-
if (logGroupLambdaRoleOutput) {
488-
const logGroups =
489-
zoneConfig?.names?.public.map(phz => {
490-
const logGroupName = centralEndpoints.createR53LogGroupName({
491-
acceleratorPrefix: context.acceleratorPrefix,
492-
domain: phz,
493-
});
494-
return new LogGroup(zonesStack, `Route53HostedZoneLogGroup${pascalCase(phz)}`, {
495-
logGroupName,
496-
roleArn: logGroupLambdaRoleOutput.roleArn,
497-
});
498-
}) || [];
499-
500-
if (logGroups.length > 0) {
501-
const wildcardLogGroupName = centralEndpoints.createR53LogGroupName({
502-
acceleratorPrefix: context.acceleratorPrefix,
503-
domain: '*',
504-
});
505-
506-
// Allow r53 services to write to the log group
507-
const logGroupPolicy = new LogResourcePolicy(zonesStack, 'R53LogGroupPolicy', {
508-
policyName: createName({
509-
name: 'query-logging-pol',
510-
}),
511-
policyStatements: [
512-
new iam.PolicyStatement({
513-
actions: ['logs:CreateLogStream', 'logs:PutLogEvents'],
514-
principals: [new iam.ServicePrincipal('route53.amazonaws.com')],
515-
resources: [`arn:aws:logs:${cdk.Aws.REGION}:${cdk.Aws.ACCOUNT_ID}:log-group:${wildcardLogGroupName}`],
516-
}),
517-
],
518-
});
519-
for (const logGroup of logGroups) {
520-
logGroupPolicy.node.addDependency(logGroup);
521-
}
522-
}
523-
}
524-
525485
/**
526486
* DisAssociate HostedZone to VPC
527487
* - On Adding of InterfaceEndpoint in local VPC whose use-central-endpoint: true and Endpoint also esists in Central VPC
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import * as iam from '@aws-cdk/aws-iam';
2+
import * as cdk from '@aws-cdk/core';
3+
4+
import { VpcConfig } from '@aws-accelerator/common-config';
5+
import { IamRoleOutputFinder } from '@aws-accelerator/common-outputs/src/iam-role';
6+
import { StackOutput } from '@aws-accelerator/common-outputs/src/stack-output';
7+
import { DNS_LOGGING_LOG_GROUP_REGION } from '@aws-accelerator/common/src/util/constants';
8+
import { pascalCase } from 'pascal-case';
9+
import { AccountStacks } from '../../common/account-stacks';
10+
import { createR53LogGroupName } from './step-1';
11+
import { LogGroup } from '@aws-accelerator/custom-resource-logs-log-group';
12+
import { LogResourcePolicy } from '@aws-accelerator/custom-resource-logs-resource-policy';
13+
import { createName } from '@aws-accelerator/cdk-accelerator/src/core/accelerator-name-generator';
14+
15+
export interface CreateDnsQueryLogGroupProps {
16+
vpcConfig: VpcConfig;
17+
accountKey: string;
18+
accountStacks: AccountStacks;
19+
outputs: StackOutput[];
20+
acceleratorPrefix: string;
21+
createPolicy: boolean;
22+
}
23+
24+
export async function createDnsQueryLogGroup(props: CreateDnsQueryLogGroupProps) {
25+
const { acceleratorPrefix, accountKey, vpcConfig, accountStacks, outputs, createPolicy } = props;
26+
if (!vpcConfig.zones || !vpcConfig.zones.public) {
27+
return;
28+
}
29+
const zonesStack = accountStacks.getOrCreateAccountStack(accountKey, DNS_LOGGING_LOG_GROUP_REGION);
30+
const logGroupLambdaRoleOutput = IamRoleOutputFinder.tryFindOneByName({
31+
outputs,
32+
accountKey,
33+
roleKey: 'LogGroupRole',
34+
});
35+
36+
if (!logGroupLambdaRoleOutput) {
37+
console.warn(`LogGroupRole not found in account "${accountKey}"`);
38+
return;
39+
}
40+
const logGroups =
41+
vpcConfig.zones.public.map(phz => {
42+
const logGroupName = createR53LogGroupName({
43+
acceleratorPrefix,
44+
domain: phz,
45+
});
46+
return new LogGroup(zonesStack, `Route53HostedZoneLogGroup${pascalCase(phz)}`, {
47+
logGroupName,
48+
roleArn: logGroupLambdaRoleOutput.roleArn,
49+
});
50+
}) || [];
51+
if (logGroups.length > 0 && createPolicy) {
52+
const wildcardLogGroupName = createR53LogGroupName({
53+
acceleratorPrefix,
54+
domain: '*',
55+
});
56+
57+
// Allow r53 services to write to the log group
58+
const logGroupPolicy = new LogResourcePolicy(zonesStack, 'R53LogGroupPolicy', {
59+
policyName: createName({
60+
name: 'query-logging-pol',
61+
}),
62+
policyStatements: [
63+
new iam.PolicyStatement({
64+
actions: ['logs:CreateLogStream', 'logs:PutLogEvents'],
65+
principals: [new iam.ServicePrincipal('route53.amazonaws.com')],
66+
resources: [`arn:aws:logs:${cdk.Aws.REGION}:${cdk.Aws.ACCOUNT_ID}:log-group:${wildcardLogGroupName}`],
67+
}),
68+
],
69+
});
70+
for (const logGroup of logGroups) {
71+
logGroupPolicy.node.addDependency(logGroup);
72+
}
73+
}
74+
}

src/deployments/cdk/src/deployments/central-endpoints/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './step-2';
44
export * from './step-3';
55
export * from './step-4';
66
export * from './step-5';
7+
export * from './dns-query-log-group';

0 commit comments

Comments
 (0)