You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Create secrets from a file without leaking into the cache #4403
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
Fix the implementation of File { secret }, such that it doesn't leak secrets in the cache.
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.
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.
[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".
Disabling it for exec pipelines that mark some file as secret seems ripe with errors and edge cases.
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.
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.
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.
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.
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.
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
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.
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.
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
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.
Do we want to keep file.secret() and envVariable.secret() at all?
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.
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.
Do we want to keep file.secret() and envVariable.secret() at all?
I think we should deprecate both.
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.
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)
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.
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.
Activity
shykes commentedon Jan 18, 2023
FYI @sagikazarmark @rawkode @samalba @aluzzardi @jlongtine @sipsma
sipsma commentedon Jan 18, 2023
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.shykes commentedon Jan 18, 2023
@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:
rawkode commentedon Jan 18, 2023
Disabling it for
execpipelines that mark some file as secret seems ripe with errors and edge cases.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.
sagikazarmark commentedon Jan 18, 2023
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.
shykes commentedon Jan 19, 2023
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.
sagikazarmark commentedon Jan 19, 2023
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.
marcosnils commentedon Jan 19, 2023
^ 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.
[-]Secrets created by containers leak into the cache[/-][+]Create secrets from a container without leaking into the cache[/+]sipsma commentedon Jan 19, 2023
@rawkode
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.
rawkode commentedon Jan 19, 2023
Yes. Pulumi supports
--show-secretsonpulumi stack outputand we can get secrets to stdout; but there's no way to keep them secure, as documented with other issues.I don't think we do want these cached, though I do need dependent layers to run when the secret has changed.
[-]Create secrets from a container without leaking into the cache[/-][+]Create secrets from a file without leaking into the cache[/+]dolanor commentedon Mar 28, 2023
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.
dolanor commentedon Mar 28, 2023
based on: #4822 (comment)
file.secret()andenvVariable.secret()at all?setSecretcould 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.
dagger.Filenordagger.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
shykes commentedon Mar 28, 2023
I think we should deprecate both.
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.
We can do it gently with
@deprecated. Forfile.Secret(), breaking is strictly better than leaving an unsafe call. ForenvVariable.Secret, the fix is trivial, so the sooner we do it, the better.shykes commentedon Mar 28, 2023
By the way @dolanor I wrote down my thoughts in the original "dynamic secrets" issue:
dolanor commentedon Mar 29, 2023
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.
dolanor commentedon Mar 30, 2023
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.