apex / up

Deploy infinitely scalable serverless apps, apis, and sites in seconds to AWS.
https://up.docs.apex.sh
MIT License
8.8k stars 380 forks source link

How to store secrets longer than 4096 chars? #537

Open aknuds1 opened 6 years ago

aknuds1 commented 6 years ago

Prerequisites

Description

I am trying to provide certificates to my service, but up's method for storing secrets (encrypted environment variables) doesn't support strings of this length (> 4096 chars). How should I store lengthy secrets?

komuw commented 6 years ago

@aknuds1 how about compressing the certificate into a string(with something like gzip) and then uncompress it when you need it.

As an example, given this certificate(python code):

import zlib

cert = """-----BEGIN CERTIFICATE-----
MIIDuzCCAqOgAwIBAgIIU4Px/idbvgswDQYJKoZIhvcNAQELBQAwSTELMAkGA1UE
BhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMxJTAjBgNVBAMTHEdvb2dsZSBJbnRl
cm5ldCBBdXRob3JpdHkgRzIwHhcNMTcxMjEzMTMwOTQxWhcNMTgwMzA3MTMwMTAw
WjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwN
TW91bnRhaW4gVmlldzETMBEGA1UECgwKR29vZ2xlIEluYzEXMBUGA1UEAwwOd3d3
Lmdvb2dsZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAARKr5OUrAwZmnRD
Ob9Fxi3sskR9wwCsBYuE05D9+Dq5hD7UKi6o2mBDJ0q1Qicn0rg7tOHezOOGo1MR
UlwA+qnvo4IBUTCCAU0wEwYDVR0lBAwwCgYIKwYBBQUHAwEwDgYDVR0PAQH/BAQD
AgeAMBkGA1UdEQQSMBCCDnd3dy5nb29nbGUuY29tMGgGCCsGAQUFBwEBBFwwWjAr
BggrBgEFBQcwAoYfaHR0cDovL3BraS5nb29nbGUuY29tL0dJQUcyLmNydDArBggr
BgEFBQcwAYYfaHR0cDovL2NsaWVudHMxLmdvb2dsZS5jb20vb2NzcDAdBgNVHQ4E
FgQUcgxgaHW9xdqNbo1vh9z5Ln9E+UIwDAYDVR0TAQH/B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qvmlKXBx4Ot2/Un
hw4EbNX/3aBd7YdStysVAq45pmp06drE57xNNB6pXE0zX5IJL4hmXXeXxx12E6nV
5fEWCRE11azbJHFwLJhWC9kXtNHjUStedejV0NxPNO3CBWaAocvmMw==
-----END CERTIFICATE-----"""

print len(cert) # 5287 which is larger than 4096 chars
compressedCertificate = zlib.compress(cert)
print len(compressedCertificate) # 2228 which is less  than 4096 chars

So you would use the value of compressedCertificate as your environment variable and then in your app, when you read that variable, remember to uncompress [zlib.decompress(compressedCertificate) for python's case] it to get the real value.

aknuds1 commented 6 years ago

@komuw I thought of the same, but for now I'm trying to use encrypted parameters stored in AWS Systems Manager instead. I wonder if Up also uses this?

aknuds1 commented 6 years ago

@komuw Ugh, Systems Manager imposes the same restriction as up, so I'm out of luck in that regard :/ I'll see if I can make your solution work.

aknuds1 commented 6 years ago

@komuw I'm stuck on adding the gzip compressed string with up env add without corrupting it. Somehow it gets corrupted in the process, and I can't decompress it.

komuw commented 6 years ago

I'm guessing that up uses AWS parameter store behind the scenes. And param store has a limit[1] of 4096 chars on the value of the parameter. @aknuds1 you could base64 encode the gzipped string, that way the output would have safe chars to use in up.json;

import base64
encodedcertificate = base64.urlsafe_b64encode(zlib.compress(cert))
print len(encodedcertificate) # 3496 chars
  1. https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_Parameter.html
aknuds1 commented 6 years ago

@komuw Thanks, I solved it in the meantime, indeed by base64 encoding the compressed string.

tj commented 6 years ago

Up does use Parameter Store at the moment, subject to the same limitations for now :'( if this becomes common I can look into using S3 or something but 4096 generally seems to be plenty for env vars

aknuds1 commented 6 years ago

@tj Is my requirement to store a certificate so unusual?

tj commented 6 years ago

@aknuds1 I wouldn't say so but I don't think anyone else has hit the limit yet, that I'm aware of at least

aknuds1 commented 6 years ago

To me it seems there should be a way of storing longer secrets than 4096 chars.

komuw commented 6 years ago

param store has a max of 4096, I think up should continue using what is the default in param store. for storing certificates in AWS, the right place is in certificate manager[1] aws iam upload-server-certificate help
maybe up can have a config where people can input certificates and then it uploads to cert-manager, but that seems like complicating things

  1. https://aws.amazon.com/certificate-manager/
aknuds1 commented 6 years ago

@komuw Can I fetch a certificate from the certificate manager as a string in my service? I just use it to configure the RethinKDB driver.

komuw commented 6 years ago

@aknuds1 if you upload the certificate beforehand using your favorite AWS client, then in your application you should be able to query for it[1]

  1. https://docs.aws.amazon.com/cli/latest/reference/iam/get-server-certificate.html
aknuds1 commented 6 years ago

@komuw OK, but not sure it's worth the hassle as I was able to squeeze it into an environment variable.

komuw commented 6 years ago

@aknuds1 you could add an up hook[1] that uploads the certificate

{
  "name": "app",
  "hooks": {
    "predeploy": [
      "echo 'uploading ssl certificate to AWS IAM'",
      "aws iam upload-server-certificate --server-certificate-name mycert --certificate-body file://cert.pem --private-key file://private_key.key --certificate-chain file://cert_chain.pem",
      "echo 'done'"
    ]
  }
}

you would need to have the aws cli installed locally

  1. https://up.docs.apex.sh/#configuration.hook_scripts
sorenbs commented 6 years ago

Just want to add that I had a related issue today when I was trying to deploy multiple long environment variables using the OSS version of up: https://github.com/motdotla/node-lambda/issues/212

Lambda has a 4kb limit on env variables.

As my env vars are all individually below the parameter store limit, upgrading to up pro and using encrypted variables solved my issue :-)

sreekanth3107 commented 9 months ago

@tj we are storing the Account id that excessed the limit of 4096 characters now. Is there any work around to increase the length ?