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 Alibaba Cloud KMS Secret Manager (#355)
* add support for Alibaba Cloud KMS Secret Manager with current version only the followings are supported: 1. 'ACSCurrent' for version stage is the default value and recommented. 2. AccessKeyId + AccessKeySecret or STS (assume a provided role name using the provided AccessKeyId and AccessKeySecret). ECS_RAM_ROLE and Kube2Ram are not supported yet. 3. secret value as plaintext - parsing secret value as a json object not yet support
- Loading branch information
Showing
11 changed files
with
358 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict' | ||
|
||
// Alibaba Cloud expects the following four environment variables: | ||
// - ALICLOUD_ENDPOINT: endpoint URL http(s)://kms.{regionID}.aliyuncs.com or http(s)://kms-vpc.{regionID}.aliyuncs.com | ||
// - ALICLOUD_ACCESS_KEY_ID: The access key ID | ||
// - ALICLOUD_ACCESS_KEY_SECRET: The access key secret | ||
|
||
module.exports = { | ||
credential: { | ||
accessKeyId: process.env.ALICLOUD_ACCESS_KEY_ID, | ||
accessKeySecret: process.env.ALICLOUD_ACCESS_KEY_SECRET, | ||
endpoint: process.env.ALICLOUD_ENDPOINT, | ||
type: 'access_key' | ||
} | ||
} |
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,9 @@ | ||
apiVersion: kubernetes-client.io/v1 | ||
kind: ExternalSecret | ||
metadata: | ||
name: secretsmanager-example | ||
spec: | ||
backendType: alicloudSecretsManager | ||
data: | ||
- key: akey | ||
name: 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,51 @@ | ||
'use strict' | ||
|
||
const KVBackend = require('./kv-backend') | ||
const { default: Client, GetSecretValueRequest } = require('@alicloud/kms20160120') | ||
/** Secrets Manager backend class. */ | ||
class AliCloudSecretsManagerBackend extends KVBackend { | ||
/** | ||
* Create Secrets manager backend. | ||
* @param {Object} logger - Logger for logging stuff. | ||
* @param {Object} credential - Secrets manager credential. | ||
*/ | ||
constructor ({ logger, credential }) { | ||
super({ logger }) | ||
this._credential = credential | ||
} | ||
|
||
_getClient ({ specOptions: { roleArn } }) { | ||
const config = { | ||
endpoint: this._credential.endpoint, | ||
accessKeyId: this._credential.accessKeyId, | ||
accessKeySecret: this._credential.accessKeySecret, | ||
type: this._credential.type | ||
} | ||
if (roleArn) { | ||
config.type = 'ram_role_arn' | ||
config.roleArn = roleArn | ||
} | ||
return new Client(config) | ||
} | ||
|
||
/** | ||
* Get secret property value from Alibaba Cloud KMS Secrets Manager. | ||
* @param {string} key - Key used to store secret property value in Alibaba Cloud KMS Secrets Manager. | ||
* @returns {Promise} Promise object representing secret property value. | ||
*/ | ||
|
||
async _get ({ key, specOptions: { roleArn }, keyOptions: { versionStage } }) { | ||
this._logger.info(`fetching secret ${key} on version stage ${versionStage} from AliCloud Secret Manager using role ${roleArn}`) | ||
const getSecretValueRequest = new GetSecretValueRequest({ | ||
secretName: key, | ||
versionStage: versionStage | ||
}) | ||
|
||
const client = this._getClient({ specOptions: { roleArn } }) | ||
const value = await client.getSecretValue(getSecretValueRequest) | ||
|
||
return value.secretData.toString('utf-8') | ||
} | ||
} | ||
|
||
module.exports = AliCloudSecretsManagerBackend |
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,45 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
|
||
const AliCloudSecretsManagerBackend = require('./alicloud-secrets-manager-backend') | ||
|
||
describe('AliCloudSecretsManagerBackend', () => { | ||
let loggerMock | ||
let clientMock | ||
let aliCloudSecretsManagerBackend | ||
|
||
const password = 'fakeSecretPropertyValue' | ||
const secret = { | ||
secretData: password | ||
} | ||
const key = 'password' | ||
|
||
beforeEach(() => { | ||
loggerMock = sinon.mock() | ||
loggerMock.info = sinon.stub() | ||
clientMock = sinon.mock() | ||
clientMock.getSecretValue = sinon.stub().returns(secret) | ||
|
||
aliCloudSecretsManagerBackend = new AliCloudSecretsManagerBackend({ | ||
credential: null, | ||
logger: loggerMock | ||
}) | ||
aliCloudSecretsManagerBackend._getClient = sinon.stub().returns(clientMock) | ||
}) | ||
|
||
describe('_get', () => { | ||
it('returns secret property value', async () => { | ||
const specOptions = {} | ||
const keyOptions = {} | ||
const secretPropertyValue = await aliCloudSecretsManagerBackend._get({ | ||
key: key, | ||
specOptions, | ||
keyOptions | ||
}) | ||
expect(secretPropertyValue).equals(password) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.