Skip to content

Commit

Permalink
Truncate names that are longer than 64 characters (#12)
Browse files Browse the repository at this point in the history
* Truncate names that are longer than 64 characters

* Fix lint errors

* Update handling for names

* Remove util.format()
  • Loading branch information
sbstjn authored Jul 29, 2017
1 parent 206bd76 commit b014bca
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"standard": "^10.0.2"
},
"dependencies": {
"lodash": "^4.17.4"
"lodash": "^4.17.4",
"md5": "^2.2.1"
},
"standard": {
"envs": [
Expand Down
7 changes: 6 additions & 1 deletion src/aws/names.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const util = require('util')
const md5 = require('md5')

function clean (input) {
return input.replace(/[^a-z0-9+]+/gi, '')
return truncate(input.replace(/[^a-z0-9+]+/gi, ''))
}

function truncate (input) {
return input.length <= 64 ? input : input.substr(0, 32) + md5(input)
}

function policyScale (table, read, index, stage) {
Expand Down
14 changes: 13 additions & 1 deletion test/aws/role.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ describe('Role', () => {
const d = j[names.role('my-table-name')]

expect(d).toHaveProperty('Type', 'AWS::IAM::Role')
expect(d).toHaveProperty('Properties.RoleName', names.role('my-table-name'))
expect(d).toHaveProperty('Properties.RoleName', 'DynamoDBAutoscaleRolemytablename')
})

it('truncates role name if needed', () => {
const r = new Role('my-table-name-with-some-extra-long-string-information-added-to-the-end')
const j = r.toJSON()

expect(j).toHaveProperty(names.role('my-table-name-with-some-extra-long-string-information-added-to-the-end'))

const d = j[names.role('my-table-name-with-some-extra-long-string-information-added-to-the-end')]

expect(d.Properties.RoleName.length).toBe(64)
expect(d).toHaveProperty('Properties.RoleName', 'DynamoDBAutoscaleRolemytablename0cde19b63d7d9f9b35cd41a979fd72a2')
})
})
18 changes: 17 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ chalk@^1.0.0, chalk@^1.1.0, chalk@^1.1.1, chalk@^1.1.3:
strip-ansi "^3.0.0"
supports-color "^2.0.0"

charenc@~0.0.1:
version "0.0.2"
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"

ci-info@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-1.0.0.tgz#dc5285f2b4e251821683681c381c3388f46ec534"
Expand Down Expand Up @@ -507,6 +511,10 @@ coveralls@^2.13.1:
minimist "1.2.0"
request "2.79.0"

crypt@~0.0.1:
version "0.0.2"
resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"

[email protected]:
version "2.0.5"
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
Expand Down Expand Up @@ -1242,7 +1250,7 @@ is-arrayish@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"

is-buffer@^1.1.5:
is-buffer@^1.1.5, is-buffer@~1.1.1:
version "1.1.5"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.5.tgz#1f3b26ef613b214b88cbca23cc6c01d87961eecc"

Expand Down Expand Up @@ -1851,6 +1859,14 @@ [email protected]:
dependencies:
tmpl "1.0.x"

md5@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/md5/-/md5-2.2.1.tgz#53ab38d5fe3c8891ba465329ea23fac0540126f9"
dependencies:
charenc "~0.0.1"
crypt "~0.0.1"
is-buffer "~1.1.1"

merge@^1.1.3:
version "1.2.0"
resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.0.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da"
Expand Down

0 comments on commit b014bca

Please sign in to comment.