Skip to content

Commit c80ec19

Browse files
committed
fix: ensure OriginRequestPolicy names are valid
1 parent 9cddc58 commit c80ec19

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

pulumi/resources.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export function buildCDN(
308308
cachePolicyId: disabledCachePolicy.apply((policy) => policy.id!),
309309
targetOriginId: 'httpOrigin',
310310
},
311-
orderedCacheBehaviors: routes.map((x) => buildBehavior(x, staticHeaders)),
311+
orderedCacheBehaviors: buildBehaviors(routes, staticHeaders),
312312
restrictions: {
313313
geoRestriction: {
314314
restrictionType: 'none',
@@ -368,9 +368,28 @@ export function buildCDN(
368368
return distribution
369369
}
370370

371-
function buildBehavior(route: string, headers: string[]) {
371+
interface Behavior {
372+
pathPattern: string
373+
allowedMethods: string[]
374+
cachedMethods: string[]
375+
targetOriginId: string
376+
originRequestPolicyId: pulumi.Output<string>
377+
cachePolicyId: pulumi.Output<string>
378+
viewerProtocolPolicy: string
379+
}
380+
381+
function buildBehaviors(routes: string[], headers: string[]): Behavior[] {
382+
const behaviors: Behavior[] = []
383+
for (const [index, route] of routes.entries()) {
384+
const behavior = buildBehavior(route, headers, index)
385+
behaviors.push(behavior)
386+
}
387+
return behaviors
388+
}
389+
390+
function buildBehavior(route: string, headers: string[], index: number): Behavior {
372391
const routeRequestPolicy = new aws.cloudfront.OriginRequestPolicy(
373-
registerName(`RouteRequestPolicy (${route})`),
392+
registerName(`RouteRequestPolicy${index}`),
374393
{
375394
cookiesConfig: {
376395
cookieBehavior: 'none',

tests/tests.utils.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
describe('tests/utils.ts', () => {
4+
let utils: typeof import('./utils')
5+
6+
beforeEach(async () => {
7+
vi.resetModules()
8+
utils = await import('./utils')
9+
})
10+
11+
it.each([
12+
['dsaGS-', false],
13+
['af2-431--fdwfef', false],
14+
['32546546*-fgdasg', false],
15+
['grae0-5344-gafgar', true]
16+
])('validName(%s)', async (test, expected) => {
17+
const result = utils.validName(test)
18+
expect(result).toBe(expected)
19+
})
20+
21+
})

tests/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ import { Mocks } from '@pulumi/pulumi/runtime'
88

99
export class MyMocks implements Mocks {
1010
public resources: { [key: string]: Record<string, any> } = {}
11+
private noNameTest: string[] = [
12+
'aws:route53/record:Record',
13+
'aws:s3/bucketObject:BucketObject'
14+
]
1115
newResource(args: pulumi.runtime.MockResourceArgs): {
1216
id: string | undefined
1317
state: Record<string, any>
1418
} {
19+
console.log(args.type)
20+
21+
if (!validName(args.name) && !this.noNameTest.includes(args.type)) {
22+
throw Error(`'${args.name}' is not a valid value for the name field`)
23+
}
1524
const id = `${args.name}-id`
1625
const outputs = {
1726
id: id,
@@ -51,6 +60,13 @@ export class MyMocks implements Mocks {
5160
}
5261
}
5362

63+
export function validName(name: string): boolean {
64+
console.log(name)
65+
const regex_valid = new RegExp('^[A-Za-z0-9-]*(?<!-)$')
66+
const regex_double = new RegExp('--')
67+
return regex_valid.test(name) && !(regex_double.test(name))
68+
}
69+
5470
// Convert a pulumi.Output to a promise of the same type.
5571
export function promiseOf<T>(output: pulumi.Output<T>): Promise<T> {
5672
return new Promise((resolve) => output.apply(resolve))

0 commit comments

Comments
 (0)