Skip to content
This repository was archived by the owner on Nov 6, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ function init (config) {
function load (options) {
init()
const mergedOptions = Object.assign({}, secrets.options, options)
const environmentSecrets = Object.assign({}, secrets.environments.$global, secrets.environments[process.env._HANDLER.split('.')[1]])
const parameterNames = _.uniq(_.values(environmentSecrets))
const provider = getStorageProvider(mergedOptions)
const environmentSecrets = Object.assign({}, secrets.environments.$global, secrets.environments[provider.getFunctionName()])
const parameterNames = _.uniq(_.values(environmentSecrets))
return provider.getSecret(parameterNames).then(data => {
const missingParameters = parameterNames.filter(expected => !_.keys(data).some(received => expected === received))
Object.assign(process.env, _.mapValues(environmentSecrets, key => data[key]))
Expand Down
7 changes: 6 additions & 1 deletion lib/providers/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,15 @@ module.exports = function (options) {
})
}

function getFunctionName() {
return process.env.AWS_LAMBDA_FUNCTION_NAME
}

return {
getSecret,
setSecret,
deleteSecret,
listSecrets
listSecrets,
getFunctionName
}
}
25 changes: 16 additions & 9 deletions plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class ServerlessSecrets {
'secrets:list-remote:list-remote': this.listRemoteSecretNames.bind(this),
'secrets:validate:validate': this.validateSecrets.bind(this),
'before:package:setupProviderConfiguration': this.setIamPermissions.bind(this),
'before:package:initialize': this.setEnvironmentConfig.bind(this),
'before:package:createDeploymentArtifacts': this.packageSecrets.bind(this),
'after:package:createDeploymentArtifacts': this.cleanupPackageSecrets.bind(this),
'before:deploy:function:packageFunction': this.packageSecrets.bind(this),
Expand Down Expand Up @@ -215,7 +216,7 @@ class ServerlessSecrets {
}

generateConfig () {
this.serverless.cli.log('Generating Serverless Secrets Config')
this.serverless.cli.log('Generating Serverless Secrets Config options')
if (!this.serverless.service.provider.name) {
throw new Error('No provider name configured in serverless.yml')
}
Expand All @@ -235,11 +236,21 @@ class ServerlessSecrets {
}
)

// variables
return {
options
}
}

setEnvironmentConfig() {
this.config.environments = this.generateEnvironmentConfig()
}

generateEnvironmentConfig () {
this.serverless.cli.log('Generating Serverless Secrets Config environments')
const functions = this.serverless.service.functions
const environments = Object.keys(functions)
.reduce((environments, key) => {
const functionName = functions[key].handler.split('.')[1]
const functionName = functions[key].name || [this.serverless.service.service, this.serverless.processedInput.options.stage, key].join('-')
if (functions[key].environmentSecrets) {
environments[functionName] = functions[key].environmentSecrets
}
Expand All @@ -248,10 +259,7 @@ class ServerlessSecrets {

environments.$global = this.serverless.service.provider.environmentSecrets || {}

return {
options,
environments
}
return environments
}

writeConfigFile () {
Expand All @@ -265,7 +273,7 @@ class ServerlessSecrets {
const functions = this.serverless.service.functions
Object.keys(functions).forEach(functionName => {
if (!functions[functionName].environment) functions[functionName].environment = {}
Object.assign(functions[functionName].environment, this.config.environments.$global, this.config.environments[functionName])
Object.assign(functions[functionName].environment, this.config.environments.$global, this.config.environments[functions[functionName].name])
})
}

Expand All @@ -291,7 +299,6 @@ class ServerlessSecrets {
this.serverless.cli.log('Validating secrets')
const provider = this.serverless.service.provider
const functions = this.serverless.service.functions

// need to validate that all secrets exist in provider
const storageProvider = this.getStorageProvider()
const missingSecretsPromise = storageProvider.listSecrets().then(secrets => {
Expand Down