Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
feat: allow permitted-key-name to be provided as list (#409)
Browse files Browse the repository at this point in the history
* feat: allow permitted-key-name to be provided as list

* refactor: construct regex only once

Co-authored-by: Nabil BENDAFI <[email protected]>
  • Loading branch information
nbendafi-yseop and nabilbendafi authored Jun 22, 2020
1 parent e80d83d commit 10e3991
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/poller.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,16 @@ class Poller {

const externalData = descriptor.data || descriptor.properties
const namingConvention = namespace.metadata.annotations[this._namingPermittedAnnotation]
let reNaming = new RegExp()
if (Array.isArray(namingConvention)) {
reNaming = new RegExp(namingConvention.join('|'))
} else {
reNaming = new RegExp(namingConvention)
}

// Testing data property
if (namingConvention && externalData) {
externalData.forEach((secretProperty, index) => {
const reNaming = new RegExp(namingConvention)
if (!reNaming.test(secretProperty.key)) {
allowed = false
reason = `key name ${secretProperty.key} does not match naming convention ${namingConvention}`
Expand All @@ -224,7 +229,6 @@ class Poller {
const externalDataFrom = descriptor.dataFrom
if (namingConvention && externalDataFrom) {
externalDataFrom.forEach((secretProperty, index) => {
const reNaming = new RegExp(namingConvention)
if (!reNaming.test(secretProperty)) {
allowed = false
reason = `key name ${secretProperty} does not match naming convention ${namingConvention}`
Expand Down
35 changes: 35 additions & 0 deletions lib/poller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,41 @@ describe('Poller', () => {
]
},
permitted: false
},
{
// test multiple regex data
ns: { metadata: { annotations: { [namingPermittedAnnotation]: ['dev/team-a/.*', 'common/.*'] } } },
descriptor: {
data: [
{ key: 'dev/team-a/ok-secret', name: 'somethingelse' },
{ key: 'common/generic-secret', name: 'genericsecret' }
]
},
permitted: true
},
{
// test multiple regex data
ns: { metadata: { annotations: { [namingPermittedAnnotation]: ['dev/team-a/.*', 'common/.*'] } } },
descriptor: {
data: [
{ key: 'dev/team-b/nok-secret', name: 'somethingelse' },
{ key: 'common/generic-secret', name: 'genericsecret' }
]
},
permitted: false
},
{
// test multiple regex data
ns: { metadata: { annotations: { [namingPermittedAnnotation]: ['dev/team-b/.*', 'common/.*'] } } },
descriptor: {
data: [
{ key: 'common/generic-secret', name: 'genericsecret' }
],
dataFrom: [
'common/generic-secret'
]
},
permitted: true
}
]

Expand Down

0 comments on commit 10e3991

Please sign in to comment.