This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vault): Support for Hashicorp Vault (#198)
- Loading branch information
Showing
12 changed files
with
341 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: 'kubernetes-client.io/v1' | ||
kind: ExternalSecret | ||
metadata: | ||
name: hello-service | ||
spec: | ||
backendType: vault | ||
vaultMountPoint: my-kubernetes-vault-mount-point | ||
vaultRole: my-vault-role | ||
data: | ||
- name: password | ||
key: secret/data/hello-service/password | ||
property: password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
'use strict' | ||
|
||
const KVBackend = require('./kv-backend') | ||
|
||
/** Vault backend class. */ | ||
class VaultBackend extends KVBackend { | ||
/** | ||
* Create Vault backend. | ||
* @param {Object} client - Client for interacting with Vault. | ||
* @param {Object} logger - Logger for logging stuff. | ||
*/ | ||
constructor ({ client, logger }) { | ||
super({ logger }) | ||
this._client = client | ||
} | ||
|
||
/** | ||
* Fetch Kubernetes service account token. | ||
* @returns {string} String representing the token of the service account running this pod. | ||
*/ | ||
_fetchServiceAccountToken () { | ||
if (!this._serviceAccountToken) { | ||
const fs = require('fs') | ||
this._serviceAccountToken = fs.readFileSync('/var/run/secrets/kubernetes.io/serviceaccount/token', 'utf8') | ||
} | ||
return this._serviceAccountToken | ||
} | ||
|
||
/** | ||
* Fetch Kubernetes secret property values. | ||
* @param {Object[]} secretProperties - Kubernetes secret properties. | ||
* @param {string} secretProperties[].key - Secret key in the backend. | ||
* @param {string} secretProperties[].name - Kubernetes Secret property name. | ||
* @param {string} secretProperties[].property - If the backend secret is an | ||
* object, this is the property name of the value to use. | ||
* @returns {Promise} Promise object representing secret property values. | ||
*/ | ||
_fetchSecretPropertyValues ({ vaultMountPoint, vaultRole, jwt, externalData }) { | ||
return Promise.all(externalData.map(async secretProperty => { | ||
this._logger.info(`fetching secret property ${secretProperty.key}`) | ||
const value = await this._get({ vaultMountPoint: vaultMountPoint, vaultRole: vaultRole, jwt: jwt, secretKey: secretProperty.key }) | ||
|
||
return value[secretProperty.property] | ||
})) | ||
} | ||
|
||
/** | ||
* Get secret property value from Vault. | ||
* @param {string} secretKey - Key used to store secret property value in Vault. | ||
* @returns {Promise} Promise object representing secret property value. | ||
*/ | ||
async _get ({ vaultMountPoint, vaultRole, secretKey }) { | ||
if (!this._client.token) { | ||
const jwt = this._fetchServiceAccountToken() | ||
this._logger.debug(`fetching new token from vault`) | ||
const vault = await this._client.kubernetesLogin({ | ||
mount_point: vaultMountPoint, | ||
role: vaultRole, | ||
jwt: jwt | ||
}) | ||
this._client.token = vault.auth.client_token | ||
} else { | ||
this._logger.debug(`renewing existing token from vault`) | ||
this._client.tokenRenewSelf() | ||
} | ||
|
||
this._logger.debug(`reading secret key ${secretKey} from vault`) | ||
const secretResponse = await this._client.read(secretKey) | ||
|
||
return secretResponse.data.data | ||
} | ||
|
||
/** | ||
* Fetch Kubernetes secret manifest data. | ||
* @param {ExternalSecretSpec} spec - Kubernetes ExternalSecret spec. | ||
* @returns {Promise} Promise object representing Kubernetes secret manifest data. | ||
*/ | ||
async getSecretManifestData ({ spec }) { | ||
const data = {} | ||
const vaultMountPoint = spec.vaultMountPoint | ||
const vaultRole = spec.vaultRole | ||
|
||
// Also support spec.properties to be backwards compatible. | ||
const externalData = spec.data || spec.properties | ||
const secretPropertyValues = await this._fetchSecretPropertyValues({ | ||
vaultMountPoint, | ||
vaultRole, | ||
externalData | ||
}) | ||
externalData.forEach((secret, index) => { | ||
data[secret.name] = (Buffer.from(secretPropertyValues[index], 'utf8')).toString('base64') | ||
}) | ||
return data | ||
} | ||
} | ||
|
||
module.exports = VaultBackend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const pino = require('pino') | ||
|
||
const VaultBackend = require('./vault-backend') | ||
const logger = pino({ | ||
serializers: { | ||
err: pino.stdSerializers.err | ||
} | ||
}) | ||
|
||
describe('VaultBackend', () => { | ||
let clientMock | ||
let vaultBackend | ||
|
||
beforeEach(() => { | ||
clientMock = sinon.mock() | ||
|
||
vaultBackend = new VaultBackend({ | ||
client: clientMock, | ||
logger | ||
}) | ||
}) | ||
|
||
describe('_get', () => { | ||
const mountPoint = 'fakeMountPoint' | ||
const role = 'fakeRole' | ||
const secretKey = 'fakeSecretKey' | ||
const jwt = 'this-is-a-jwt-token' | ||
|
||
beforeEach(() => { | ||
clientMock.read = sinon.stub().returns({ | ||
data: { | ||
data: 'fakeSecretPropertyValue' | ||
} | ||
}) | ||
clientMock.tokenRenewSelf = sinon.stub().returns(true) | ||
clientMock.kubernetesLogin = sinon.stub().returns({ | ||
auth: { | ||
client_token: '1234' | ||
} | ||
}) | ||
|
||
vaultBackend._fetchServiceAccountToken = sinon.stub().returns(jwt) | ||
|
||
clientMock.token = undefined | ||
}) | ||
|
||
it('logs in and returns secret property value', async () => { | ||
const secretPropertyValue = await vaultBackend._get({ | ||
vaultMountPoint: mountPoint, | ||
vaultRole: role, | ||
secretKey: secretKey | ||
}) | ||
|
||
// First, we log into Vault... | ||
sinon.assert.calledWith(clientMock.kubernetesLogin, { | ||
mount_point: 'fakeMountPoint', | ||
role: 'fakeRole', | ||
jwt: jwt | ||
}) | ||
|
||
// ... then we fetch the secret ... | ||
sinon.assert.calledWith(clientMock.read, 'fakeSecretKey') | ||
|
||
// ... and expect to get its proper value | ||
expect(secretPropertyValue).equals('fakeSecretPropertyValue') | ||
}) | ||
|
||
it('returns secret property value after renewing token if a token exists', async () => { | ||
clientMock.token = 'an-existing-token' | ||
|
||
const secretPropertyValue = await vaultBackend._get({ | ||
vaultMountPoint: mountPoint, | ||
vaultRole: role, | ||
secretKey: secretKey | ||
}) | ||
|
||
// No logging into Vault... | ||
sinon.assert.notCalled(clientMock.kubernetesLogin) | ||
|
||
// ... but renew the token instead ... | ||
sinon.assert.calledOnce(clientMock.tokenRenewSelf) | ||
|
||
// ... then we fetch the secret ... | ||
sinon.assert.calledWith(clientMock.read, 'fakeSecretKey') | ||
|
||
// ... and expect to get its proper value | ||
expect(secretPropertyValue).equals('fakeSecretPropertyValue') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.