Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 4x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x | /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /* eslint-disable no-console */ import type { OnEventResponse } from "@aws-cdk/custom-resources/lib/provider-framework/types"; import * as AWS from "aws-sdk"; import { APIVersions } from "aws-sdk/lib/config"; import { ServiceConfigurationOptions } from "aws-sdk/lib/service"; import { ResourceRecords } from "aws-sdk/clients/route53"; import Route53 = require("aws-sdk/clients/route53"); import {getDNSUpdateRoleNameFromServiceRecordName} from './utils'; const AWS_API_VERSION = "2013-04-01"; /** * Assume role and return Route53 client with credentials to that role * @param roleArn * @param roleSessionName */ async function assumeRoleAndGetRoute53Client( roleArn: string, roleSessionName: string ) { const params: ServiceConfigurationOptions & Pick<APIVersions, "apiVersion"> = { apiVersion: AWS_API_VERSION, }; const sts = new AWS.STS(); params.credentials = await sts .assumeRole({ RoleArn: roleArn, RoleSessionName: roleSessionName, }) .promise() .then(({ Credentials }) => ({ accessKeyId: Credentials!.AccessKeyId, secretAccessKey: Credentials!.SecretAccessKey, sessionToken: Credentials!.SessionToken, expiration: Credentials!.Expiration, })); return new AWS.Route53(params); } enum Route53ChangeAction { UPSERT = "UPSERT", DELETE = "DELETE", } interface ChangeRecordParams { route53: AWS.Route53; targetHostedZoneId: string; recordName: string; toDelegateNameServers: string[]; changeAction: Route53ChangeAction; } /** * Change the NS record of given parameter to the given name * `changeAction` field could be either UPSERT or DELETE * @param params: ChangeRecordParam */ async function changeRecord(params: ChangeRecordParams) { const { route53, targetHostedZoneId, recordName, toDelegateNameServers, changeAction, } = params; const resourceRecords: ResourceRecords = toDelegateNameServers.map( (nameServer: string) => { return { Value: nameServer }; } ); try { const response = await route53 .changeResourceRecordSets({ HostedZoneId: targetHostedZoneId, ChangeBatch: { Changes: [ { Action: changeAction, ResourceRecordSet: { Name: recordName, Type: "NS", TTL: 300, ResourceRecords: resourceRecords, }, }, ], }, }) .promise(); console.log("response = ", response.ChangeInfo.Id); } catch (e) { throw e; } } /** * Create/Update/Delete a zone delegation record cross-account, depending on event.RequestType * Edge cases: * 1. CREATE: If the resource has already existed it will update NS to the given name * 2. UPDATE: If the resource is missing (was manually deleted), it will fail. * 3. DELETE: If the resource is missing (was manually deleted), it will fail. * @param event An event with the following ResourceProperties: targetAccount, targetRoleToAssume, targetHostedZoneId, toDelegateNameServers (string[]), recordName * @returns Returns a PhysicalResourceId corresponding to record id */ export async function onEventHandler(event: any): Promise<OnEventResponse> { console.log("Event: %j", event); const { targetAccount, targetRoleToAssume, targetHostedZoneId, toDelegateNameServers, recordName, currentAccountId } = event.ResourceProperties; const roleArn = targetAccount && targetRoleToAssume ? `arn:aws:iam::${targetAccount}:role/${targetRoleToAssume}` : await resolveRoleArn(recordName, currentAccountId); const roleSessionName = event.LogicalResourceId.substr(0, 64); const route53 = await assumeRoleAndGetRoute53Client( roleArn, roleSessionName ); const _targetHostedZoneId = targetHostedZoneId?targetHostedZoneId:await resolveParentHostedZoneId(route53, recordName); console.log("roleArn = ", roleArn); console.log("targetHostedZoneId = ", _targetHostedZoneId); console.log("toDelegateNameServers = ", toDelegateNameServers); console.log("recordName = ", recordName); const baseChangeRecordProps = { route53, targetHostedZoneId: _targetHostedZoneId, recordName, toDelegateNameServers, }; switch (event.RequestType) { case "Create": await changeRecord({ ...baseChangeRecordProps, changeAction: Route53ChangeAction.UPSERT, }); break; case "Update": await changeRecord({ ...baseChangeRecordProps, changeAction: Route53ChangeAction.DELETE, }); await changeRecord({ ...baseChangeRecordProps, changeAction: Route53ChangeAction.UPSERT, }); break; case "Delete": // Delete an existing one await changeRecord({ ...baseChangeRecordProps, changeAction: Route53ChangeAction.DELETE, }); } let physicalResourceId = `cross-account-record-${targetAccount?targetAccount:roleArn.split(':')[4]}-${recordName}`; return { PhysicalResourceId: physicalResourceId, }; } /** * A function in charge of resolving the hosted zone Id from a sub domain (i.e. if 'app1.dev.yourdomain.com' is given, dev.yourdomain.com zone id will be returned) * @param recordName The full DNS record name which will be stripped to extract the parent dns zone name used to resolved the zone id * @returns ParentHostedZoneId */ async function resolveParentHostedZoneId(route53Client: Route53, recordName: string) { const listHostedZoneByNameResult = await route53Client.listHostedZonesByName({ DNSName: recordName.split(".").splice(1).join(".") }).promise(); return listHostedZoneByNameResult.HostedZones[0].Id; } /** * A function used to resolve the role ARN capable of modifying a DNS sub zone in a remote account * @param recordName The full DNS record name which will be stripped to resolve the second part of the role name * @param currentAccountId the current account Id used to resolve the first part of the role name */ async function resolveRoleArn(recordName: string, currentAccountId: string) { try { const orgClient = new AWS.Organizations({ region: "us-east-1" }); const listAccountsResults = await orgClient.listAccounts().promise(); let targetAccountId; let targetRoleToAssume; for (const account of listAccountsResults.Accounts ? listAccountsResults.Accounts : []) { // Indentify main account which is the one hosting DNS root domain if (account.JoinedMethod === "INVITED") { targetAccountId = account.Id; } else Eif (account.Id == currentAccountId) { targetRoleToAssume = getDNSUpdateRoleNameFromServiceRecordName(recordName); } } const roleArn = `arn:aws:iam::${targetAccountId}:role/${targetRoleToAssume}`; return roleArn; } catch (error) { console.error(`Failed to resolveRoleArn due to ${error}`); throw error; } } |