Skip to content

Secrets: write-up best practices, do's and don'ts, roadmap #13490

Open

Description

@thaJeztah

Handling secrets (passwords, keys and related) in Docker is a recurring topic. Many pull-requests have been 'hijacked' by people wanting to (mis)use a specific feature for handling secrets.

So far, we only discourage people to use those features, because they're either provenly insecure, or not designed for handling secrets, hence "possibly" insecure. We don't offer them real alternatives, at least, not for all situations and if, then without a practical example.

I just think "secrets" is something that has been left lingering for too long. This results in users (mis)using features that are not designed for this (with the side effect that discussions get polluted with feature requests in this area) and making them jump through hoops just to be able to work with secrets.

Features / hacks that are (mis)used for secrets

This list is probably incomplete, but worth a mention

So, what's needed?

The above should be written / designed with both build-time and run-time secrets in mind

@calavera created a quick-and-dirty proof-of-concept on how the new Volume-Drivers (#13161) could be used for this; https://github.com/calavera/docker-volume-keywhiz-fs

Note: Environment variables are used as the de-facto standard to pass configuration/settings, including secrets to containers. This includes official images on Docker Hub (e.g. MySQL, WordPress, PostgreSQL). These images should adopt the new 'best practices' when written/implemented.

In good tradition, here are some older proposals for handling secrets;

Activity

  1. thaJeztah commented on May 26, 2015

    @thaJeztah
    MemberAuthor

    ping @ewindisch @diogomonica @NathanMcCauley This is just a quick write-up. Feel free to modify/update the description if you think that's nescessary :)

  2. dreamcat4 commented on May 26, 2015

    @dreamcat4

    This is useful infos:

    hashicorp/vault#165

    As is this:

    hashicorp/vault#164

  3. thaJeztah commented on May 26, 2015

    @thaJeztah
    MemberAuthor

    @dreamcat4 there are some plans to implement a generic "secrets API", which would allow you to use either Vault, or Keywiz or you-name-it with Docker, but all in the same way. It's just an early thought, so it will require additional research.

  4. dreamcat4 commented on May 27, 2015

    @dreamcat4

    @thaJeztah Yep Sorry I don't want to detract from those efforts / discussion in any way. I am more thinking maybe it's a useful exercise also (as part of that longer process and while we are waiting) to see how far we can get right now. Then it shows up more clearly to others the limits and deficiencies in current process. What underlying is missing and needed the most to be added to improve the secrets.

    Also it's worth considering about the different situations of run-time secrets VS build-time secrets. For which there is also an area overlap area.

    And perhaps also (for docker) we may also be worth to consider limitations (pros/cons) between solutions that provide a mechanism to handle the secrets "in-memory". As opposed to a more heavily file-based secrets methods or network based ones e.g. local secrets server. Which are the current hacks on the table (until proper secrets API). This can help us to understand some of the unique value (for example of stronger security) added by a docker secrets API which could not otherwise be achieved by using hacks on top of the current docker feature set. However I am not a security expert. So I cannot really comment on those things with such a great certainty.

  5. thaJeztah commented on May 27, 2015

    @thaJeztah
    MemberAuthor

    @dreamcat4 yes, you're right; for the short term, those links are indeed useful.

    Also it's worth considering about the different situations of run-time secrets VS build-time secrets. For which there is also an area overlap area.

    Thanks! I think I had that in my original description, must have gotten lost in the process. I will add a bullet

    However I am not a security expert.

    Neither am I, that's why I "pinged" the security maintainers; IMO, this should be something written by them 😇

  6. diogomonica commented on May 27, 2015

    @diogomonica
    Contributor

    @thaJeztah great summary. I'll try to poke at this whenever I find some time.

  7. thaJeztah commented on Jun 7, 2015

    @thaJeztah
    MemberAuthor

    @diogomonica although not directly related, there a long open feature request for forwarding SSH key agent during build; #6396 given the number of comments, it would be good to give that some thinking too. (If even to take a decision on it whether or not it can/should be implemented)

  8. ebuchman commented on Jun 13, 2015

    @ebuchman

    Assuming you could mount volumes as user other than root (I know it's impossible, but humour me), would that be a favourable approach to getting secrets into containers?

    If so, I'd advocate for an alternative to -v host_dir:image_dir that expects the use of a data-only container and might look like -vc host_dir:image_dir (ie. volume-copy) wherein the contents of host_dir are copied into the image_dir volume on the data-only container.

    We could then emphasize a secure-data-only containers paradigm and allow those volumes to be encrypted

  9. kepkin commented on Nov 13, 2015

    @kepkin

    I've recently read a good article about that from @jrslv where he propose to build a special docker image with secrets just to build your app, and than build another image for distribution using results from running build image.

    So you have two Dockerfiles:

    • Dockerfile.build (here you simply copy all your secrets)
    • Dockerfile.dist (this one you will push to registry)

    Now we can build our distribution like that:

    # !/bin/sh
    docker build -t hello-world-build -f Dockerfile.build .
    docker run hello-world-build >build.tar.gz 
    docker build -t hello-world -f Dockerfile.dist ^

    Your secrets are safe, as you never push hello-world-build image.

    I recommend to read @jrslv article for more details http://resources.codeship.com/ebooks/continuous-integration-continuous-delivery-with-docker

  10. lamroger commented on Nov 13, 2015

    @lamroger

    Thanks for sharing @kepkin !
    Just finished reading the article. Really concise!

    I like the idea of exporting the files and loading them in through a separate Dockerfile. It feels like squashing without the "intermediate layers being in the build cache" issue.

    However, I'm nervous that it'll complicate development and might require a third Dockerfile for simplicity.

  11. TomasTomecek commented on Nov 24, 2015

    @TomasTomecek
    Contributor

    @kepkin no offense but that doesn't make any sense. Secrets are definitely not safe, since they are in the tarball and the tarball is being ADDed to production image -- even if you remove the tarball, without squashing, it will leak in some layer.

  12. thaJeztah commented on Nov 24, 2015

    @thaJeztah
    MemberAuthor

    @TomasTomecek if I understand the example correctly, the tarball is not the image-layers, but just the binary that was built inside the build container. See for example; https://github.com/docker-library/hello-world/blob/master/update.sh (no secrets involved here, but just a simple example of a build container)

  13. 198 remaining items

  14. ypadlyak commented on Mar 5, 2021

    @ypadlyak
  15. aren55555 commented on Mar 5, 2021

    @aren55555
  16. KyeRussell commented on Mar 5, 2021

    @KyeRussell
  17. jan-hudec commented on Mar 5, 2021

    @jan-hudec

    Surely this is a problem for the operations/security teams?

    Yes. They need the tools and documentation the most. When they deploy with docker, they need to inject a lot of secrets in them. And they often version the compose files, kubernetes manifests, helm charts, terraform manifests, ansible playbooks or whatever they use to deploy the containers in git, so storing encrypted bundles there is useful to them too.

    Do they really need the application software developers handling this stuff?

    Operations don't need them to, but developers themselves do. The build and test infrastructure also involves a bunch of secrets.

  18. petef19 commented on Sep 19, 2021

    @petef19

    In our docker-compose file, we've been using shared volumes in read-only mode to pass secrets to the container that are needed for service configuration.
    Once the container has successfully started and been configured, we then remove the shared files/folders on the host side. to leave no secrets on the host or in the container.
    The container (and service running in it) has no issue with that as it only uses these secret files at start up.

    Would love to get feedback about this approach. Anything wildly wrong with that ?

    Thanks.

  19. lirantal commented on Sep 19, 2021

    @lirantal
    Contributor

    @petef19 you might benefit from the bit of knowledge on mounting secrets using the new DOCKER_BUILDKIT support in Docker: https://snyk.io/blog/10-best-practices-to-containerize-nodejs-web-applications-with-docker/

  20. petef19 commented on Sep 20, 2021

    @petef19

    @lirantal

    thanks for the link, great read.

    Regarding using secrets, whether the newer build-secrets or the docker-compose-v3-secrets, the downside with that approach is that you still leave sensitive data inside the container - and the secrets can be read and accessed just like plain text files. If an attacker manages to hijack a container, it's open season.

    With our approach ("unplugged" shared volumes) - and please chime in with opinions - there is no sensitive data left on the host or in the container (after it was build and service started). But the drawback may be scalability, as we currently only run a couple of servers, so not stress tested in that regard.

  21. lirantal commented on Sep 20, 2021

    @lirantal
    Contributor

    you still leave sensitive data inside the container

    Well yes, but the point is that you mount secrets for a build stage that you don't actually use in the resulting image, and it's a nicer way of mounting data into that layer only.

  22. petef19 commented on Sep 20, 2021

    @petef19

    you still leave sensitive data inside the container

    Well yes, but the point is that you mount secrets for a build stage that you don't actually use in the resulting image, and it's a nicer way of mounting data into that layer only.

    right, but what if you need the sensitive data to configure the service when the final container starts ?

  23. thaJeztah commented on Sep 20, 2021

    @thaJeztah
    MemberAuthor

    Correct, with the build-secrets, the secrets will not be stored in the image-layer. They will be mounted during build, and no longer there after the build-step completes (there may be an empty file afterwards, due to how Linux mount points work (they require the destination to exist), but the secret themselves are not there).

    For git operations, you can also use the --ssh option when using BuildKit, which will forward an ssh-agent into the container (https://docs.docker.com/develop/develop-images/build_enhancements/#using-ssh-to-access-private-data-in-builds)

  24. lirantal commented on Sep 20, 2021

    @lirantal
    Contributor

    It looks like @petef19 is seeking a run-time solution for when containers are running, not when they are built. To achieve that, there are the usual suspects:

    1. Put secrets in source code
    2. Pass environment variables
    3. Mount a directory for the secrets
    4. Use run-time API to fetch secrets (like say hashicorp vault and such)

    ** The above are organized from the least secure way of managing secrets to the most (my opinion only) **

  25. petef19 commented on Sep 20, 2021

    @petef19

    It looks like @petef19 is seeking a run-time solution for when containers are running, not when they are built. To achieve that, there are the usual suspects:

    1. Put secrets in source code
    2. Pass environment variables
    3. Mount a directory for the secrets
    4. Use run-time API to fetch secrets (like say hashicorp vault and such)

    ** The above are organized from the least secure way of managing secrets to the most (my opinion only) **

    yes, runtime secrets to configure services as they start up. I can extend your list...

    (5) use cmd args
    (6) if the service allows to read secrest from a file, use that option
    (7) use shell scripts to init the service

    Out of these, (3), (6) and (7) can all be used with shared volumes that can be "unplugged" after the service has started successfully. leaving nothing behind on host or container.

    Question is could an attacker that hijacks the container still get out through the "unplugged" shared volume, although the "tunnel" does not exist anymore.... ? Especially if all secret mounts are read-only and are direct file mounts (not directories), so even if the shared volume was not "unplugged", wouldn't the attacker need a shared dir in order to attempt to traverse up... ?

    Re (4): this seems to be a secure approach and easily scalable, but since the API connection creds are in the container (which are a secret itself) couldn't a hijacker use those creds to query the API for secrets.... ?

  26. Ilhamskhyi commented on Feb 2, 2022

    @Ilhamskhyi
  27. lonix1 commented on May 31, 2023

    @lonix1

    Since my issue #45642 was closed and points to this one, I want to add that since "runtime secrets" are not possible, the recommendation is to use a one-node swarm to access swarm secrets. But this is not easy to do and maintain, and no longer works if we use docker rootless (no overlay network, so no swarm mode).

    Now that compose is part of the main cli tool (docker compose), please please please give us runtime secrets. (Exactly the same as swarm runtime secrets, just without swarm.)

  28. KyNorthstar commented on May 5, 2024

    @KyNorthstar

    Commenting partially, so I get notification 5 years from now, when Docker finally decides to give us a tiny step in the direction of proper credential management

    Here 6 years after that to do the same thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions