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: add support for IBM Cloud Secrets Manager backend (#656)
- Loading branch information
Showing
9 changed files
with
1,660 additions
and
420 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
// IBM Cloud automatically picks up the following credentials so they don't have to be passed in the config | ||
// - SECRETS_MANAGER_API_AUTH_TYPE=iam | ||
// - SECRETS_MANAGER_API_APIKEY=<apikey> | ||
// - SECRETS_MANAGER_API_ENDPOINT= endpoint URL https://{instance-id}.{region}.secrets-manager.appdomain.cloud | ||
|
||
module.exports = { | ||
credential: { | ||
apikey: process.env.IBM_CLOUD_SECRETS_MANAGER_API_APIKEY, | ||
endpoint: process.env.IBM_CLOUD_SECRETS_MANAGER_API_ENDPOINT, | ||
type: process.env.IBM_CLOUD_SECRETS_MANAGER_API_AUTH_TYPE | ||
} | ||
} |
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,11 @@ | ||
apiVersion: kubernetes-client.io/v1 | ||
kind: ExternalSecret | ||
metadata: | ||
name: ibmcloud-secrets-manager-example | ||
spec: | ||
backendType: ibmcloudSecretsManager | ||
data: | ||
# The guid id of the secret | ||
- key: guid | ||
name: username_password | ||
secretType: username_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict' | ||
|
||
const SecretsManager = require('@ibm-cloud/secrets-manager/secrets-manager/v1') | ||
const { getAuthenticatorFromEnvironment, IamAuthenticator } = require('@ibm-cloud/secrets-manager/auth') | ||
|
||
const KVBackend = require('./kv-backend') | ||
|
||
/** Secrets Manager backend class. */ | ||
class IbmCloudSecretsManagerBackend extends KVBackend { | ||
/** | ||
* Create Key Vault backend. | ||
* @param {Object} credential - Credentials for authenticating with IBM Secrets Manager. | ||
* @param {Object} logger - Logger for logging stuff. | ||
*/ | ||
constructor ({ credential, logger }) { | ||
super({ logger }) | ||
this._credential = credential | ||
} | ||
|
||
_secretsManagerClient () { | ||
let authenticator | ||
if (process.env.IBM_CLOUD_SECRETS_MANAGER_API_AUTH_TYPE && process.env.IBM_CLOUD_SECRETS_MANAGER_API_APIKEY) { | ||
authenticator = getAuthenticatorFromEnvironment('IBM_CLOUD_SECRETS_MANAGER_API') | ||
} else { | ||
authenticator = new IamAuthenticator({ | ||
apikey: this._credential.apikey | ||
}) | ||
} | ||
const client = new SecretsManager({ | ||
authenticator: authenticator, | ||
serviceUrl: this._credential.endpoint | ||
}) | ||
return client | ||
} | ||
|
||
/** | ||
* Get secret_data property value from IBM Cloud Secrets Manager | ||
* @param {string} key - Key used to store secret property value. | ||
* @param {object} specOptions - Options for this external secret, eg role | ||
* @param {string} specOptions.secretType - Type of secret - one of username_password, iam_credentials or arbitrary | ||
* @returns {Promise} Promise object representing secret property value. | ||
*/ | ||
async _get ({ key, keyOptions: { secretType } }) { | ||
const client = this._secretsManagerClient() | ||
this._logger.info(`fetching secret ${key} from IBM Cloud Secrets Manager ${this._credential.endpoint}`) | ||
const secret = await client.getSecret({ | ||
secretType: secretType, | ||
id: key | ||
}) | ||
return JSON.stringify(secret.result.resources[0].secret_data) | ||
} | ||
} | ||
|
||
module.exports = IbmCloudSecretsManagerBackend |
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,48 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
process.env.IBM_CLOUD_SECRETS_MANAGER_API_AUTH_TYPE = 'noauth' | ||
process.env.IBM_CLOUD_SECRETS_MANAGER_API_APIKEY = 'iamkey' | ||
|
||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
|
||
const IbmCloudSecretsManagerBackend = require('./ibmcloud-secrets-manager-backend') | ||
|
||
describe('IbmCloudSecretsManagerBackend', () => { | ||
let loggerMock | ||
let clientMock | ||
let ibmCloudSecretsManagerBackend | ||
|
||
const username = 'fakeUserName' | ||
const password = 'fakeSecretPropertyValue' | ||
const secret = { result: { resources: [{ secret_data: { password: password, username: username } }] } } | ||
const returnsecret = JSON.stringify({ password: password, username: username }) | ||
const key = 'username_password' | ||
|
||
beforeEach(() => { | ||
loggerMock = sinon.mock() | ||
loggerMock.info = sinon.stub() | ||
clientMock = sinon.mock() | ||
clientMock.getSecret = sinon.stub().returns(secret) | ||
|
||
ibmCloudSecretsManagerBackend = new IbmCloudSecretsManagerBackend({ | ||
credential: { endpoint: 'https//sampleendpoint' }, | ||
logger: loggerMock | ||
}) | ||
ibmCloudSecretsManagerBackend._secretsManagerClient = sinon.stub().returns(clientMock) | ||
}) | ||
|
||
describe('_get', () => { | ||
it('returns secret property value', async () => { | ||
const specOptions = {} | ||
const keyOptions = { secretType: 'password' } | ||
const secretPropertyValue = await ibmCloudSecretsManagerBackend._get({ | ||
key: key, | ||
specOptions, | ||
keyOptions | ||
}) | ||
expect(secretPropertyValue).equals(returnsecret) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.