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

Commit 828d0ce

Browse files
authored
fix: stringify json object based secrets (#247)
1 parent f26bf2b commit 828d0ce

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

lib/backends/kv-backend.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,23 @@ class KVBackend extends AbstractBackend {
8282
throw new Error('_get not implemented')
8383
}
8484

85+
/**
86+
* Convert secret value to buffer
87+
* @param {(string|Buffer|object)} plainValue - plain secret value
88+
* @returns {Buffer} Buffer containing value
89+
*/
90+
_toBuffer (plainValue) {
91+
if (plainValue instanceof Buffer) {
92+
return plainValue
93+
}
94+
95+
if (typeof plainValue === 'object') {
96+
return Buffer.from(JSON.stringify(plainValue), 'utf-8')
97+
}
98+
99+
return Buffer.from(`${plainValue}`, 'utf8')
100+
}
101+
85102
/**
86103
* Fetch Kubernetes secret manifest data.
87104
* @param {ExternalSecretSpec} spec - Kubernetes ExternalSecret spec.
@@ -108,16 +125,10 @@ class KVBackend extends AbstractBackend {
108125
}), {})
109126

110127
const encodedEntries = Object.entries(plainValues)
111-
.map(([name, plainValue]) => {
112-
let bufferValue = plainValue
113-
if (!(plainValue instanceof Buffer)) {
114-
bufferValue = Buffer.from(`${plainValue}`, 'utf8')
115-
}
116-
return [
117-
name,
118-
bufferValue.toString('base64')
119-
]
120-
})
128+
.map(([name, plainValue]) => [
129+
name,
130+
this._toBuffer(plainValue).toString('base64')
131+
])
121132

122133
return Object.fromEntries(encodedEntries)
123134
}

lib/backends/kv-backend.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,31 @@ describe('kv-backend', () => {
403403
})
404404
})
405405
})
406+
407+
describe('base64 encoding', () => {
408+
it('handles json objects', async () => {
409+
kvBackend._get = sinon.stub()
410+
kvBackend._get
411+
.resolves(JSON.stringify({
412+
textProperty: 'text',
413+
jsonStringProperty: '{ "someKey": { "myText": "text" } }',
414+
jsonProperty: { someKey: { myText: 'text' } }
415+
}))
416+
417+
const manifestData = await kvBackend
418+
.getSecretManifestData({
419+
spec: {
420+
dataFrom: ['test-key']
421+
}
422+
})
423+
424+
expect(kvBackend._get.calledOnce).to.equal(true)
425+
426+
expect(manifestData).deep.equals({
427+
textProperty: 'dGV4dA==', // base 64 value of text
428+
jsonStringProperty: 'eyAic29tZUtleSI6IHsgIm15VGV4dCI6ICJ0ZXh0IiB9IH0=', // base 64 value of: { "someKey": { "myText": "text" } }
429+
jsonProperty: 'eyJzb21lS2V5Ijp7Im15VGV4dCI6InRleHQifX0=' // base 64 value of: {"someKey":{"myText":"text"}}
430+
})
431+
})
432+
})
406433
})

0 commit comments

Comments
 (0)