Skip to content

Support AWS Secrets Manager #5024

Closed

Description

@pharindoko

For feature proposals:
The use case is the same as for SSM Parameter Store.
Storing secrets like database connection parameters / api keys etc.

Would be nice to choose between SSM Parameters Store and the AWS Secrets Manager for storing configs and secrets.

Activity

  1. e-e-e commented on Jul 11, 2018

    @e-e-e
    Contributor

    Is there any progress on this proposal? I am currently working on a project which would benefit from a simple interface to AWS secrets manager.

  2. ronkorving commented on Jul 30, 2018

    @ronkorving
    Contributor

    Yes, this please! 💯
    If nobody else will, I can probably make a PR for this. It would be nice to get some feedback from serverless maintainers whether or not that would be wasted time, or if this feature would be welcome :)

  3. eahefnawy commented on Jul 30, 2018

    @eahefnawy
    Contributor

    @ronkorving thanks for pitching in. Could you explain how you intend to implement this? and what the experience would be like for using this feature?

  4. pharindoko commented on Jul 30, 2018

    @pharindoko
    Author

    Same behaviour and implementation as for the Parameter Store (SSM).
    Just the option to choose between one or the other....

  5. eahefnawy commented on Jul 30, 2018

    @eahefnawy
    Contributor

    @pharindoko I like that. We'd def appreciate a PR! 😊

  6. e-e-e commented on Jul 31, 2018

    @e-e-e
    Contributor

    This might not be as trivial as mirroring the SSM syntax.

    Secrets manager stores multiple key values in the secrets as text. Commonly key values are stored in text as JSON, but this is not necessarily the only way they are stored. Users can use any serialisation that they want.

    If we ignored the alternative ways of storing the key values, and assume secrets are always key/value JSON, then we could use the syntax {secret:name_of_secret.key}.

    But given users are charged each time a key is decrypted we would also probably need to cache the secret so that {secret:name_of_secret.foo} and {secret:name_of_secret.bar} don't make two requests unnecessarily.

    Just some quick thoughts.

  7. ronkorving commented on Aug 1, 2018

    @ronkorving
    Contributor

    Excellent points. To be honest, I've kinda changed strategies here. Pulling in secrets and storing them locally in a cloud formation file sort of goes against keeping these secrets really secret in the first place. I'm going to change my strategy.

  8. e-e-e commented on Aug 1, 2018

    @e-e-e
    Contributor

    @ronkorving I really agree - really SSM shouldn't be used for encrypted values either for the reasons you mentioned.

    We ended up storing env vars that point to the secret key, and then in our lambda's we fetch and cache them before executing the hander.

  9. ronkorving commented on Aug 1, 2018

    @ronkorving
    Contributor

    @e-e-e I was thinking of doing exactly that

  10. pharindoko commented on Aug 12, 2018

    @pharindoko
    Author

    I used both methods:

    • request at runtime
    • request via env vars directly

    The question is how secure or risky it is to implement this solution.

    My Assumption:
    As far as I understand a cloudfornationfile will be transmitted and uploaded having the secrets already injected, when you do serverless deploy.
    The package itself will be immediately created in .serverless folder and destroyed after successful upload.
    This upload is hopefully encrypted right ? at least with ssl.

    That would be ok for my solutions.
    If someone accesses these files on my laptop I have a bigger issue.

    I would not use this solution for a cashcow service where I make millions.
    But in my eyes then you would have a different setup (k8s, fargate, docker) even because of costs.

    So yes I still would like to have it :)
    Key:Value Pair Style...

  11. agonbina commented on Sep 13, 2018

    @agonbina

    @BethanyBerkowitz that almost works but this new feature requires a WithDecryption flag to be set when pulling secrets via SSM, which the serverless SSM integration does not do, so you end up seeing this error: WithDecryption flag must be True for retrieving a Secret Manager secret.

    EDIT: this actually works if you add the additional flag to decrypt a key. Here is an example:
    ${ssm:/aws/reference/secretsmanager/my/key~true}. This is also described here

  12. Imran99 commented on Sep 14, 2018

    @Imran99

    +1. Secrets manager also supports key rotation via a custom lambda, not sure if SSM can do that.

  13. q42jaap commented on Sep 24, 2018

    @q42jaap

    @agonbina ${ssm:/aws/reference/secretsmanager/my/key~true} doesn't work for me,
    ${ssm:/aws/reference/secretsmanager/my~true} does work, but gives back the whole secret as a json serialized thing. I need to get usernames and passwords out of the json to individual parameters.
    Can this be done in serverless.yml?

  14. 5 remaining items

  15. self-assigned this
    on Feb 4, 2019
  16. added a commit that references this issue on Feb 4, 2019
    ff82197
  17. bradennapier commented on Jul 27, 2019

    @bradennapier

    This should actually be added to support the case you want ot read many files to stop reading the secret many times (you pay per decrypt).

    It should parse it once and re-use the secret any time requested during a deployment.

  18. jmsg1990 commented on Jul 31, 2019

    @jmsg1990

    You can do this by adding a secrets section to your serverless.yml like this:

    custom:
      secrets: ${ssm:/aws/reference/secretsmanager/my-secrets~true}
    

    Then, you can reference your secrets stored in ssm anywhere in you yml using: ${self:custom.secrets.db_root_username}

    The names secrets, my-secrets and db_root_username are used here just as an example.
    Also, I like naming the secret store using variables:

    custom:
      secrets: ${ssm:/aws/reference/secretsmanager/${self:service.name}-${self:provider.stage}~true}
    
  19. raakasf commented on Dec 18, 2019

    @raakasf

    Did the above work for anyone? It did not work for me, could someone please share a complete example of the yml file they used to get it work? I keep getting A valid file to satisfy the declaration 'self:custom.secrets.db_root_username' could not be found.
    Thanks!

  20. jmsg1990 commented on Dec 18, 2019

    @jmsg1990

    @rakeshsehgal1 I think that it is weird that the error message mentions A valid file. There might be a typo in your settings, so serverless is looking for a file instead of accessing ssm.
    I've seen errors in relation to SSM such as A valid service attribute to satisfy the declaration 'self:custom.settings.ssm.test' could not be found. or A valid SSM parameter to satisfy the declaration 'ssm:/aws/reference/secretsmanager/my-secrets~true' could not be found.
    I don't have a full yml to share (I split most into several parts) but if you share yours I could help you.

  21. raakasf commented on Dec 19, 2019

    @raakasf

    Thanks @jmsg1990, here is my yml:

    service: my-checks  
    provider:
       name: aws
       runtime: nodejs12.x
       stage: prod
       custom:
         secrets: ${ssm:/aws/reference/secretsmanager/my_check_secrets~true}
    functions:
      myCheckLambda:
        handler: handler.myCheckLambda
        environment:
          myCreds: ${self:custom.secrets}
    

    The my_check_secrets is in AWS Secret manager as a json. And then in my code trying to access process.env.myCreds to get the json or the value. The error that I got was (just for ref again):
    A valid service attribute to satisfy the declaration 'self:custom.secrets' could not be found.

    Thanks for helping out!

  22. jmsg1990 commented on Dec 19, 2019

    @jmsg1990

    @rakeshsehgal1 the problem is the indentation. Your custom section ended up inside the provider section. I guess this should work (haven't tested it though)

    service: my-checks  
    provider:
       name: aws
       runtime: nodejs12.x
       stage: prod
    custom:
       secrets: ${ssm:/aws/reference/secretsmanager/my_check_secrets~true}
    functions:
      myCheckLambda:
        handler: handler.myCheckLambda
        environment:
          myCreds: ${self:custom.secrets}
    

    I hope it helps!

  23. raakasf commented on Dec 19, 2019

    @raakasf

    Thanks @jmsg1990 this works, that was the only indentation I had not tried :) .
    Just to make it easier for someone else, in the final yml I directly used my key from the secret:

        environment:
          envKey1: ${self:custom.secrets.key1}
          envKey2: ${self:custom.secrets.key2}
    

    when the AWS secret is stored as:
    {
    "key1": "value1",
    "key2": "value2"
    }
    and then in your code directly access process.env.envKey1. Not sure if it's possible to use the entire custom object or not.

  24. 300LiterPropofol commented on Nov 20, 2020

    @300LiterPropofol

    Thanks @jmsg1990 this works, that was the only indentation I had not tried :) .
    Just to make it easier for someone else, in the final yml I directly used my key from the secret:

        environment:
          envKey1: ${self:custom.secrets.key1}
          envKey2: ${self:custom.secrets.key2}
    

    when the AWS secret is stored as:
    {
    "key1": "value1",
    "key2": "value2"
    }
    and then in your code directly access process.env.envKey1. Not sure if it's possible to use the entire custom object or not.
    It is really kind of you for sharing this!! Really helps me. Thank you a lot @raakasf

  25. gazal-k commented on Dec 18, 2020

    @gazal-k

    we have a secret with JSON value like

    {
      "encrypt.key": "123",
      "encrypt.initVector": "456"
    }

    Referencing it in serverless like:

    custom:
      secrets: ${ssm:/aws/reference/secretsmanager/secret-name~true}

    In the generated serverless-state.json, I can see that the JSON value is retreived correctly

        "custom": {
          "secrets": {
            "encrypt.key": "123",
            "encrypt.initVector": "456"
          }
        }

    I've tried a few different ways to read that into the environment variables of our lambda fns

      environment:
        ENCRYPT_KEY: ${self:custom.secrets.encrypt.key}
        ENCRYPT_INIT_VECTOR: ${self:custom.secrets.encrypt.initVector}

    results in the following in the state file

          "Environment": {
            "Variables": {
              "ENCRYPT_KEY": {},
              "ENCRYPT_INIT_VECTOR": {}
            }
          }

    Also tried:

      environment:
        ENCRYPT_KEY: ${self:custom.secrets['encrypt.key']}
        ENCRYPT_INIT_VECTOR: ${self:custom.secrets['encrypt.initVector']}

    but that produces

          "Environment": {
            "Variables": {
              "ENCRYPT_KEY": "${self:custom.secrets['encrypt.key']}",
              "ENCRYPT_INIT_VECTOR": "${self:custom.secrets['encrypt.initVector']}"
            }
          }

    Any suggestions on how we can extract the secret values?

  26. gazal-k commented on Dec 21, 2020

    @gazal-k

    I think I figured it out. I used a js file (https://www.serverless.com/framework/docs/providers/aws/guide/variables/#reference-variables-in-javascript-files) to transform the secret.

    transformSecretsManagerResponse.js

    module.exports = serverless => {
    	const actualFn = serverless.variables.getValueFromSsm;
    
    	const wrapper = async variableString => {
    		const response = await actualFn.call(serverless.variables, variableString);
    
    		if (typeof response === 'object') {
    			const transformedResponse = {};
    			Object.keys(response).forEach(key => {
    				transformedResponse[key.replace('.', '_')] = response[key];
    			});
    			return transformedResponse;
    		}
    		return response;
    	};
    
    	const ssmResolver = serverless.variables.variableResolvers.find(
    		variableResolver => variableResolver.serviceName === 'SSM'
    	);
    	ssmResolver.resolver = wrapper;
    
    	return 'nothing';
    };

    and in serverless.yaml

    custom:
      transformSecrets: ${file(transformSecretsManagerResponse.js)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions