Skip to content

Create secrets from a file without leaking into the cache #4403

Closed

Description

@shykes

Problem

Dagger can create new secrets from the contents of an artifact or container (File { secret }). But the resulting secret is then leaked in the cache. For this reason, File { secret } is currently considered unsafe. The risk can be mitigated, for example by only using this method for secrets that carry an acceptable risk (ie not production credentials), rigorously scoping and rotating them, limiting cache sharing, etc. But unless you are confident that you know what you are doing, this method should not be used.

That is very inconvenient, because at the moment File { secret } is the only available method for creating secrets dynamically.

Solution

  1. Fix the implementation of File { secret }, such that it doesn't leak secrets in the cache.

  2. Better document the fact that File { secret } is unsafe.

Design considerations

There is probably no easy fix for this. But we don't know for sure, because we haven't invested the effort to explore possible solutions.

Activity

  1. shykes commented on Jan 18, 2023

    @shykes
    ContributorAuthor
  2. sipsma commented on Jan 18, 2023

    @sipsma
    Contributor

    There's only one way I can think of to possible make make File { secret } safer: we could disable cache whenever we solve pipelines that request a secret. Might be easier said than done since we'd need to retroactively update already marshalled LLB (possible but hacky), but it definitely seems technically possible since it's all lazy anyways.

    I think it might be too magical and surprising of behavior though. It also would only solve the problem when you are using an exec to generate a secret and then using File { secret } to grab it. It wouldn't solve the problem of grabbing a secret from an image or a local source, but then again I'm not sure either of those are really secure to begin with, so maybe solving it for exec would be enough.

  3. shykes commented on Jan 18, 2023

    @shykes
    ContributorAuthor

    [Fix secret leak by disabling cache on Exec which produces a secret]

    @sipsma this seems like a potentially good option. It may be surprising; but the current behavior is also surprising, in a way that is strictly worse: "surprise! your code is now insecure".

    So far I see only 2 realistic options:

    1. Selectively disable exec caching as you describe.
    2. Deprecate this API call entirely
  4. rawkode commented on Jan 18, 2023

    @rawkode
    Contributor
    1. Disabling it for exec pipelines that mark some file as secret seems ripe with errors and edge cases.

    2. Removing the API would limit Daggers ability to be used full stack, Pulumi and Terraform need this ability


    I think it's definitely important to notify the user that their build cache contains secrets, this could be detected at runtime and announced - as well as added to the documentation.

    Some people may need this and use private BuildKit instances for just this purpose. Let's assume those are the minority though.

    Perhaps Dagger could provide a directory, like /nocache/ that could be suggested and encouraged for ephemeral secret collection? It could be purged after each session.

    I think I'd also point out that this isn't the crux of Daggers secret problem, getting secrets in is. It's likely that you don't have secrets coming out if there's no secrets going it.

  5. sagikazarmark commented on Jan 18, 2023

    @sagikazarmark
    Contributor

    While this indeed sounds like the shortest path, I'm inclined to say that I'd prefer to talk to a secret store outside of Dagger (ie. use the Go client to talk to Vault).

    There could be use case for both, I don't know. But as I see things right now, I would replace a container based solution with one, that just talks directly to the secret store if that became available as an option.

  6. shykes commented on Jan 19, 2023

    @shykes
    ContributorAuthor

    While this indeed sounds like the shortest path, I'm inclined to say that I'd prefer to talk to a secret store outside of Dagger (ie. use the Go client to talk to Vault).

    There could be use case for both, I don't know. But as I see things right now, I would replace a container based solution with one, that just talks directly to the secret store if that became available as an option.

    That's good to know. May I ask why you prefer one over the other? What's the important criteria for you?

    And yes, independently from this issue, we plan on allowing dynamic injection of secret from the host directly. That is currently held up in a design discussion on where and how to inject swappable secret backends. Both need to be resolved no matter what. I think there's a reasonable chance this one is resolved first, since it does not require time-consuming API design discussions.

  7. sagikazarmark commented on Jan 19, 2023

    @sagikazarmark
    Contributor

    That's good to know. May I ask why you prefer one over the other? What's the important criteria for you?

    Not sure I have a good reason. It's probably easier to understand/explain to people (compared to running the same thing in a container). Might even be slightly easier to debug.

    But now that I think about it: packaging secret fetching into an extension may also be a good option. Both of those would result in a higher level interface for fetching secrets.

    I guess we can figure out which has the better UX once all of those options become available.

  8. marcosnils commented on Jan 19, 2023

    @marcosnils
    Contributor

    Perhaps Dagger could provide a directory, like /nocache/ that could be suggested and encouraged for ephemeral secret collection? It could be purged after each session.

    ^ I was also thinking about this as a quick solution. Having a special ephemeral dir inside each container and raise a warning if you're trying to create a secret outside that dir that will leak into the cache.

  9. changed the title [-]Secrets created by containers leak into the cache[/-] [+]Create secrets from a container without leaking into the cache[/+] on Jan 19, 2023
  10. sipsma commented on Jan 19, 2023

    @sipsma
    Contributor

    @rawkode

    Removing the API would limit Daggers ability to be used full stack, Pulumi and Terraform need this ability

    Just want to double check, is the use case you're thinking of here where you use pulumi/terraform in a dagger exec and then want to get an output but that output includes a secret? And then there's currently no to get an output from an exec that doesn't involve writing it to a file? Or are there other use cases?

    Makes sense if so. One thing I'm wondering about is if in this use case you ever actually want those sorts of outputs to be cached in the first place. I certainly get there's a lot of areas where dagger's caching can benefit execution of pulumi/terraform/etc. but specifically in the step where you retrieve outputs from a stack is there much downside to just always running the command that retrieves the outputs rather than caching it?

    I completely agree with what you said that magically disabling caching when a secret is requested from an exec is going to have a lot of surprising+unwanted behavior. But I'm still curious about this since there may be different routes to achieving the same end effect in slightly different ways that would be more explicit and less confusing.

  11. rawkode commented on Jan 19, 2023

    @rawkode
    Contributor

    Just want to double check, is the use case you're thinking of here where you use pulumi/terraform in a dagger exec and then want to get an output but that output includes a secret? And then there's currently no to get an output from an exec that doesn't involve writing it to a file? Or are there other use cases?

    Yes. Pulumi supports --show-secrets on pulumi stack output and we can get secrets to stdout; but there's no way to keep them secure, as documented with other issues.

    Makes sense if so. One thing I'm wondering about is if in this use case you ever actually want those sorts of outputs to be cached in the first place. I certainly get there's a lot of areas where dagger's caching can benefit execution of pulumi/terraform/etc. but specifically in the step where you retrieve outputs from a stack is there much downside to just always running the command that retrieves the outputs rather than caching it?

    I don't think we do want these cached, though I do need dependent layers to run when the secret has changed.

  12. changed the title [-]Create secrets from a container without leaking into the cache[/-] [+]Create secrets from a file without leaking into the cache[/+] on Jan 19, 2023
  13. dolanor commented on Mar 28, 2023

    @dolanor
    Contributor

    I do think there is a way to implement it safely.
    For now, we deprecated it because of security issue. I'm gonna investigate if it is feasible, my hunch is yes. I'll update here once I'm sure of it.

  14. dolanor commented on Mar 28, 2023

    @dolanor
    Contributor

    based on: #4822 (comment)

    1. Do we want to keep file.secret() and envVariable.secret() at all?
    2. Or since setSecret could handle both by passing the plaintext out of the env or the file into it, do we want to remove this API?

    I think it makes sense to keep it to avoid API breakage, but also it's duplicate.
    But also, it's a useful shortcut.

    I tend to avoid duplicates, unless they are really useful. It saves only a few lines of code.

    1. Another solution would be to create a top level SDK function that doesn't need dagger.File nor dagger.EnvVariable, but it would be implemented by each SDK in their own way. Less codegen, more maintenance.

    I guess I would prefer more automated function, so I'm for 1., or 2. if we want to avoid duplicating functionality.

    cc @shykes @aluzzardi

  15. shykes commented on Mar 28, 2023

    @shykes
    ContributorAuthor

    based on: #4822 (comment)

    1. Do we want to keep file.secret() and envVariable.secret() at all?

    I think we should deprecate both.

    1. Or since setSecret could handle both by passing the plaintext out of the env or the file into it, do we want to remove this API?

    Yes I believe so. There's an added benefit to the clarity: one way to do secrets, everyone tries the same thing, and can quickly give us feedback, we can quickly improve it, etc.

    I think it makes sense to keep it to avoid API breakage, but also it's duplicate. But also, it's a useful shortcut.

    We can do it gently with @deprecated. For file.Secret(), breaking is strictly better than leaving an unsafe call. For envVariable.Secret, the fix is trivial, so the sooner we do it, the better.

  16. shykes commented on Mar 28, 2023

    @shykes
    ContributorAuthor

    By the way @dolanor I wrote down my thoughts in the original "dynamic secrets" issue:

    Potentially deprecate Host.Env.Secret and Host.Directory.File.Secret
    As they would become unnecessary
    e.g. Host.Env.Secret can be written as (pseudo code) c.SetSecret("aws-access-key", os.Getenv("AWS_ACCESS_KEY"))
    Deprecate Container().File().Secret() because it's insecure (leaves secret in cache)

  17. dolanor commented on Mar 29, 2023

    @dolanor
    Contributor

    It was potentially :)
    My biggest issue was about the security. Less about the API. Keeping the API while making it safe was a way to be good everywhere.

    Talking with @vikram-dagger we had some confusion around the 3 way to set secret and how they map with the 2 way to inject secrets. With 1 way to set secret, there is no confusion anymore. It's more lines of code for the user, but less confusing in the end. That's good in my book.
    We're good.

  18. dolanor commented on Mar 30, 2023

    @dolanor
    Contributor

    I believe we can close this as the only recommended option to use secret is to use setSecret, a guide is being written on how to use it (with file and/or with env). So this should not leak anymore.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions