Skip to content

Commit

Permalink
Adds reserved length for pulumi suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSeptimus committed Jan 30, 2023
1 parent 324cb17 commit 1207cab
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pkg/infra/pulumi_aws/deploylib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as crypto from 'crypto'
import { setupElasticacheCluster } from './iac/elasticache'
import * as analytics from './iac/analytics'

import { h, sanitized, validate } from './iac/sanitization/sanitizer'
import { hash as h, sanitized, validate } from './iac/sanitization/sanitizer'
import { LoadBalancerPlugin } from './iac/load_balancing'
import { DefaultEksClusterOptions, Eks, EksExecUnit, HelmChart } from './iac/eks'
import { setupMemoryDbCluster } from './iac/memorydb'
Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/pulumi_aws/iac/load_balancing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TargetGroupAttachmentArgs,
} from '@pulumi/aws/lb'
import { ListenerRuleArgs } from '@pulumi/aws/alb'
import { h, sanitized } from './sanitization/sanitizer'
import { hash as h, sanitized } from './sanitization/sanitizer'
import { CloudCCLib } from '../deploylib'

export interface Route {
Expand Down
36 changes: 17 additions & 19 deletions pkg/infra/pulumi_aws/iac/sanitization/sanitizer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Multimap = require('multimap')
import * as sha256 from 'simple-sha256'

// Resource name length offset required to account for IAC-added suffixes
const reservedCharCount = 8

export interface SanitizationOptions {
maxLength?: number
minLength?: number
Expand Down Expand Up @@ -36,8 +39,8 @@ export function sanitize(s: string, options: Partial<SanitizationOptions>): Sani
`The sanitized value, "${result}", is shorter than minLength: ${options.minLength}`
)
}
if (options.maxLength != null && result.length > options.maxLength) {
result = result.substring(0, options.maxLength)
if (options.maxLength != null && result.length > options.maxLength - reservedCharCount) {
result = result.substring(0, options.maxLength - reservedCharCount)
}
if (failedRules?.length === 0) {
return { result: result, violations: [] }
Expand All @@ -64,8 +67,12 @@ function doValidate(input: string, options: Partial<SanitizationOptions>): Array
if (options.minLength != null && input.length < options.minLength) {
violations.push(`Invalid input: "${input}": length < minLength (${options.minLength})`)
}
if (options.maxLength != null && input.length > options.maxLength) {
violations.push(`Invalid input: "${input}": length > maxLength (${options.maxLength})`)
if (options.maxLength != null && input.length > options.maxLength - reservedCharCount) {
violations.push(
`Invalid input: "${input}": length > maxLength (${
options.maxLength - reservedCharCount
})`
)
}

violations.push(
Expand Down Expand Up @@ -135,20 +142,19 @@ function shortenString(
const sorted = [...componentsByPriority.keys()].sort()

if (options.maxLength != null) {
const maxLength =
options.maxLength - reservedCharCount < 1 ? 1 : options.maxLength - reservedCharCount
for (const p of sorted) {
if (length <= options.maxLength) {
if (length <= maxLength) {
break
}
for (const c of componentsByPriority.get(p)) {
if (length <= options.maxLength) {
if (length <= maxLength) {
break
}
const oldCLen = c.content.length
c.content = c.shorteningStrategy
? c.shorteningStrategy(
c.content,
options.maxLength - (length - c.content.length)
)
? c.shorteningStrategy(c.content, maxLength - (length - c.content.length))
: c.content
length -= oldCLen - c.content.length
}
Expand Down Expand Up @@ -180,18 +186,10 @@ function arrTextLen<T>(arr: Array<T>, lenFunc: (arg0: T) => number): number {
return arr.reduce((p, c, i) => p + lenFunc(c), 0)
}

export function h(content: string, priority: number | undefined = undefined): Component {
export function hash(content: string, priority: number | undefined = undefined): Component {
return {
content,
priority,
shorteningStrategy: hashComponent,
}
}

export function t(content: string, priority: number | undefined = undefined): Component {
return {
content,
priority,
shorteningStrategy: (t, m) => t.substring(0, m),
}
}

0 comments on commit 1207cab

Please sign in to comment.