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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | 1x 1x 1x 1x 1x 106x 183x 183x 183x 1x 138x 69x 36x 33x 69x 69x 25x 24x 24x 24x 3x 21x 24x 27x 27x 24x 24x 24x 44x 41x 138x 138x 107x 3x 1x 190x 190x 65x 125x 125x 61x 61x 61x 46x 51x 46x 64x 1x 193x 191x 191x 155x 49x 49x 49x 49x 57x 23x 49x 23x 106x 1x 60x 60x 33x 33x 1x 32x 32x 1x 7x 6x 20x 38x 6x 6x 6x 27x 27x 27x 27x 15x 12x 27x 27x 27x 27x 15x 27x 12x 27x 27x 6x | import { get as getByDotNotation, set as setByDotNotation } from 'dot-prop'; import { keywordReplace } from './tools/utils'; import { AssetTypes, KeywordMappings } from './types'; import { keywordReplaceArrayRegExp, keywordReplaceStringRegExp } from './tools/utils'; import { cloneDeep, isArray } from 'lodash'; import APIHandler from './tools/auth0/handlers/default'; /* RFC for Keyword Preservation: https://github.com/auth0/auth0-deploy-cli/issues/688 Original Github Issue: https://github.com/auth0/auth0-deploy-cli/issues/328 */ export const doesHaveKeywordMarker = ( string: string, keywordMappings: KeywordMappings ): boolean => { return !Object.keys(keywordMappings).every((keyword) => { const hasArrayMarker = keywordReplaceArrayRegExp(keyword).test(string); const hasStringMarker = keywordReplaceStringRegExp(keyword).test(string); return !hasArrayMarker && !hasStringMarker; }); }; export const getPreservableFieldsFromAssets = ( asset: object, keywordMappings: KeywordMappings, resourceSpecificIdentifiers: Partial<{ [key in AssetTypes]: string | string[] }>, address = '' ): string[] => { if (typeof asset === 'string') { if (doesHaveKeywordMarker(asset, keywordMappings)) { return [address]; } return []; } const shouldRenderDot = address !== ''; if (Array.isArray(asset)) { return asset .map((arrayItem) => { const resourceIdentifiers: string[] = (() => { const identifiers = resourceSpecificIdentifiers[address]; if (Array.isArray(identifiers)) { return identifiers; } return [identifiers]; })(); return resourceIdentifiers .map((resourceIdentifier) => { resourceSpecificIdentifiers[address]; if (resourceIdentifier === undefined) return []; // See if this specific resource type has an identifier const identifierFieldValue = arrayItem[resourceIdentifier]; Iif (identifierFieldValue === undefined) return []; // See if this specific array item possess the resource-specific identifier return getPreservableFieldsFromAssets( arrayItem, keywordMappings, resourceSpecificIdentifiers, `${address}${ shouldRenderDot ? '.' : '' }[${resourceIdentifier}=${identifierFieldValue}]` ); }) .flat(); }) .flat(); } if (typeof asset === 'object') { return Object.keys(asset) .map((key: string): string[] => { const value = asset[key]; if (value === undefined || value === null) return []; return getPreservableFieldsFromAssets( value, keywordMappings, resourceSpecificIdentifiers, `${address}${shouldRenderDot ? '.' : ''}${key}` ); }) .flat(); } return []; }; // getAssetsValueByAddress returns a value for an arbitrary data structure when // provided an "address" of that value. This address is similar to JS object notation // with the exception of identifying array items by a unique property instead of order. // Example: // Object: `{ actions: [ { name: "action-1", code: "..."}] }` // Address: `.actions[name=action-1].code` export const getAssetsValueByAddress = (address: string, assets: any): any => { //Look ahead and see if the address path only contains dots (ex: `tenant.friendly_name`) //if so the address is trivial and can use the dot-prop package to return the value const isTrivialAddress = address.indexOf('[') === -1; if (isTrivialAddress) { return getByDotNotation(assets, address); } // It is easier to handle an address piece-by-piece by // splitting on the period into separate "directions" const directions = address.split(/\.(?![^\[]*\])/g); // If the the next directions are the proprietary array syntax (ex: `[name=foo]`) // then perform lookup against unique array-item property if (directions[0].charAt(0) === '[') { const identifier = directions[0].substring(1, directions[0].length - 1).split('=')[0]; const identifierValue = directions[0].substring(1, directions[0].length - 1).split('=')[1]; if (!isArray(assets)) return undefined; const target = assets.find((item: any) => { return item[identifier] === identifierValue; }); return getAssetsValueByAddress(directions.slice(1).join('.'), target); } return getAssetsValueByAddress( directions.slice(1).join('.'), getByDotNotation(assets, directions[0]) ); }; // convertAddressToDotNotation will convert the proprietary address into conventional // JS object notation. Performing this conversion simplifies the process // of updating a specific property for a given asset tree using the dot-prop library // returns null if address value does not exist in asset tree export const convertAddressToDotNotation = ( assets: any, address: string, finalAddressTrail = '' ): string | null => { if (assets === null) return null; //Asset does not exist on remote const directions = address.split(/\.(?![^\[]*\])/g); if (directions[0] === '') return finalAddressTrail; if (directions[0].charAt(0) === '[') { const identifier = directions[0].substring(1, directions[0].length - 1).split('=')[0]; const identifierValue = directions[0].substring(1, directions[0].length - 1).split('=')[1]; let targetIndex = -1; assets.forEach((item: any, index: number) => { if (item[identifier] === identifierValue) { targetIndex = index; } }); if (targetIndex === -1) return null; // No object of this address exists in the assets return convertAddressToDotNotation( assets[targetIndex], directions.slice(1).join('.'), `${finalAddressTrail}.${targetIndex}` ); } return convertAddressToDotNotation( getByDotNotation(assets, directions[0]), directions.slice(1).join('.'), finalAddressTrail === '' ? directions[0] : `${finalAddressTrail}.${directions[0]}` ); }; export const updateAssetsByAddress = ( assets: object, address: string, newValue: string ): object => { const dotNotationAddress = convertAddressToDotNotation(assets, address); if (dotNotationAddress === null) return assets; const doesPropertyExist = getByDotNotation(assets, dotNotationAddress) !== undefined; if (!doesPropertyExist) { return assets; } setByDotNotation(assets, dotNotationAddress, newValue); return assets; }; // preserveKeywords is the function that ultimately gets executed during export // to attempt to preserve keywords (ex: ##KEYWORD##) in local configuration files // from getting overwritten by remote values during export. export const preserveKeywords = ({ localAssets, remoteAssets, keywordMappings, auth0Handlers, }: { localAssets: object; remoteAssets: object; keywordMappings: KeywordMappings; auth0Handlers: (Pick<APIHandler, 'id' | 'type'> & { identifiers: (string | string[])[]; })[]; }): object => { if (Object.keys(keywordMappings).length === 0) return remoteAssets; const resourceSpecificIdentifiers: Partial<{ [key in AssetTypes]: string[] }> = auth0Handlers.reduce((acc, handler): Partial<{ [key in AssetTypes]: string[] }> => { return { ...acc, [handler.type]: handler.identifiers.filter((identifiers) => { return identifiers !== handler.id; })[0], }; }, {}); const addresses = getPreservableFieldsFromAssets( localAssets, keywordMappings, resourceSpecificIdentifiers, '' ); let updatedRemoteAssets = cloneDeep(remoteAssets); addresses.forEach((address) => { const localValue = getAssetsValueByAddress(address, localAssets); const remoteAssetsAddress = (() => { const doesAddressHaveKeyword = doesHaveKeywordMarker(address, keywordMappings); if (doesAddressHaveKeyword) { return keywordReplace(address, keywordMappings); } return address; })(); const remoteValue = getAssetsValueByAddress(remoteAssetsAddress, remoteAssets); const localValueWithReplacement = keywordReplace(localValue, keywordMappings); const localAndRemoteValuesAreEqual = (() => { if (typeof remoteValue === 'string') { return localValueWithReplacement === remoteValue; } //TODO: Account for non-string replacements via @@ syntax })(); if (!localAndRemoteValuesAreEqual) { console.warn( `WARNING! The remote value with address of ${address} has value of "${remoteValue}" but will be preserved with "${localValueWithReplacement}" due to keyword preservation.` ); } // Two address possibilities are provided to account for cases when there is a keyword // in the resources's identifier field. When the resource identifier's field is preserved // on the remote assets tree, it loses its identify, so we'll need to try two addresses: // one where the identifier field has a keyword and one where the identifier field has // the literal replaced value. // Example: `customDomains.[domain=##DOMAIN].domain` and `customDomains.[domain=travel0.com].domain` updatedRemoteAssets = updateAssetsByAddress( updatedRemoteAssets, address, //Two possible addresses need to be passed, one with identifier field keyword replaced and one where it is not replaced. Ex: `customDomains.[domain=##DOMAIN].domain` and `customDomains.[domain=travel0.com].domain` localValue ); updatedRemoteAssets = updateAssetsByAddress( updatedRemoteAssets, remoteAssetsAddress, //Two possible addresses need to be passed, one with identifier field keyword replaced and one where it is not replaced. Ex: `customDomains.[domain=##DOMAIN].domain` and `customDomains.[domain=travel0.com].domain` localValue ); }); return updatedRemoteAssets; }; |