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.
Co-authored-by: Joel Damata <[email protected]>
- Loading branch information
1 parent
828d0ce
commit 5b41ad0
Showing
10 changed files
with
488 additions
and
8 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,15 @@ | ||
'use strict' | ||
|
||
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager') | ||
|
||
// First, ADC checks to see if the environment variable GOOGLE_APPLICATION_CREDENTIALS is set. | ||
// If the variable is set, ADC uses the service account file that the variable points to | ||
// If the environment variable isn't set, ADC uses the default service account that the Kubernetes Engine | ||
// provides, for applications that run on those services | ||
|
||
module.exports = { | ||
gcpSecretsManager: () => { | ||
const client = new SecretManagerServiceClient() | ||
return client | ||
} | ||
} |
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,10 @@ | ||
apiVersion: kubernetes-client.io/v1 | ||
kind: ExternalSecret | ||
metadata: | ||
name: gcp-secrets-manager-example | ||
spec: | ||
backendType: gcpSecretsManager | ||
data: | ||
- key: projects/111122223333/secrets/my-secret/versions/latest | ||
name: password | ||
property: value |
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,32 @@ | ||
'use strict' | ||
|
||
const KVBackend = require('./kv-backend') | ||
|
||
/** GCP Secrets Manager backend class. */ | ||
class GCPSecretsManagerBackend extends KVBackend { | ||
/** | ||
* Create Secrets manager backend. | ||
* @param {Object} logger - Logger for logging stuff. | ||
* @param {Object} client - Secrets manager client. | ||
*/ | ||
constructor ({ logger, client }) { | ||
super({ logger }) | ||
this._client = client | ||
} | ||
|
||
/** | ||
* Get secret property value from GCP Secrets Manager. | ||
* @param {string} key - Key used to store secret property value in GCP Secrets Manager. | ||
* @returns {Promise} Promise object representing secret property value. | ||
*/ | ||
async _get ({ key }) { | ||
this._logger.info(`fetching secret ${key} from GCP Secret Manager`) | ||
const version = await this._client.accessSecretVersion({ | ||
name: key | ||
}) | ||
const secret = { value: version[0].payload.data.toString('utf8') } | ||
return JSON.stringify(secret) | ||
} | ||
} | ||
|
||
module.exports = GCPSecretsManagerBackend |
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,37 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
|
||
const GCPSecretsManagerBackend = require('./gcp-secrets-manager-backend') | ||
|
||
describe('GCPSecretsManagerBackend', () => { | ||
let loggerMock | ||
let clientMock | ||
let gcpSecretsManagerBackend | ||
const key = 'password' | ||
const version = [{ name: 'projects/111122223333/secrets/password/versions/1', payload: { data: Buffer.from('test', 'utf8') } }, null, null] | ||
const secret = '{"value":"test"}' | ||
|
||
beforeEach(() => { | ||
loggerMock = sinon.mock() | ||
loggerMock.info = sinon.stub() | ||
clientMock = sinon.mock() | ||
clientMock.accessSecretVersion = sinon.stub().returns(version) | ||
|
||
gcpSecretsManagerBackend = new GCPSecretsManagerBackend({ | ||
logger: loggerMock, | ||
client: clientMock | ||
}) | ||
}) | ||
|
||
describe('_get', () => { | ||
it('returns secret property value', async () => { | ||
const secretPropertyValue = await gcpSecretsManagerBackend._get({ | ||
key: key | ||
}) | ||
expect(secretPropertyValue).equals(secret) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.