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

Commit

Permalink
fix: stringify json object based secrets (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flydiverny authored Mar 27, 2020
1 parent f26bf2b commit 828d0ce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
31 changes: 21 additions & 10 deletions lib/backends/kv-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ class KVBackend extends AbstractBackend {
throw new Error('_get not implemented')
}

/**
* Convert secret value to buffer
* @param {(string|Buffer|object)} plainValue - plain secret value
* @returns {Buffer} Buffer containing value
*/
_toBuffer (plainValue) {
if (plainValue instanceof Buffer) {
return plainValue
}

if (typeof plainValue === 'object') {
return Buffer.from(JSON.stringify(plainValue), 'utf-8')
}

return Buffer.from(`${plainValue}`, 'utf8')
}

/**
* Fetch Kubernetes secret manifest data.
* @param {ExternalSecretSpec} spec - Kubernetes ExternalSecret spec.
Expand All @@ -108,16 +125,10 @@ class KVBackend extends AbstractBackend {
}), {})

const encodedEntries = Object.entries(plainValues)
.map(([name, plainValue]) => {
let bufferValue = plainValue
if (!(plainValue instanceof Buffer)) {
bufferValue = Buffer.from(`${plainValue}`, 'utf8')
}
return [
name,
bufferValue.toString('base64')
]
})
.map(([name, plainValue]) => [
name,
this._toBuffer(plainValue).toString('base64')
])

return Object.fromEntries(encodedEntries)
}
Expand Down
27 changes: 27 additions & 0 deletions lib/backends/kv-backend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,31 @@ describe('kv-backend', () => {
})
})
})

describe('base64 encoding', () => {
it('handles json objects', async () => {
kvBackend._get = sinon.stub()
kvBackend._get
.resolves(JSON.stringify({
textProperty: 'text',
jsonStringProperty: '{ "someKey": { "myText": "text" } }',
jsonProperty: { someKey: { myText: 'text' } }
}))

const manifestData = await kvBackend
.getSecretManifestData({
spec: {
dataFrom: ['test-key']
}
})

expect(kvBackend._get.calledOnce).to.equal(true)

expect(manifestData).deep.equals({
textProperty: 'dGV4dA==', // base 64 value of text
jsonStringProperty: 'eyAic29tZUtleSI6IHsgIm15VGV4dCI6ICJ0ZXh0IiB9IH0=', // base 64 value of: { "someKey": { "myText": "text" } }
jsonProperty: 'eyJzb21lS2V5Ijp7Im15VGV4dCI6InRleHQifX0=' // base 64 value of: {"someKey":{"myText":"text"}}
})
})
})
})

0 comments on commit 828d0ce

Please sign in to comment.