Repository navigation
Description
Right now callers can pass their environment variables to modules by just reading them and setting them to function arg values, e.g. w/ the cli:
dagger call my-function --some-arg $SOME_ARG_VALUE --other-arg env:OTHER_VALUE
This suffices for straightforward use cases but multiple users have found it to be tedious + clunky once you need to pass a lot of env vars. It also isn't ideal for some use cases where the caller may not know what they are even supposed to pass.
This issue is to discuss any possible improvements we can make.
One very obvious potential solution would be to just give module code an api to read these from the caller. We intentionally didn't go that route because it pokes a really big whole in the idea of modules being sandboxed. This is bad both from a security and portability perspective.
Some ideas around potential solutions (not mutually exclusive):
- Do give modules a way of reading env vars from the caller, but wrap it up in a "trust model" where callers have to (one way or another) explicitly allow a given module to do so (and ideally scope it to certain env vars).
- Formalize the ideas of "main" and "library" modules, make reading env vars an exclusive feature of "main" modules
- We've talked vaguely about how there are certain modules that are meant to be invoked directly (what I called a "main" module here) and those that are just providing re-usable functionality for other modules ("library" modules)
- The idea here would be to actually encode that idea in the module API. Main modules have to be invoked directly by a caller (i.e. either the CLI or an SDK running outside of a module). They are still sandboxed but are given extra abilities for more directly interacting with the caller.
- We'd still want the trust model idea, but this would at least avoid the possibility of some transitive dependency module 10 deps deep trying to encode a bunch of logic that relies on the caller having variables being set, which feels like a potential massive footgun.
- To be clear, I don't love this idea from a simplicity perspective (the less concepts the better), but I think it should be on the table, especially if other use cases for the distinction between main+library mods come up outside this
Activity
shykes commentedon Nov 15, 2023
My preference would be to start with something more lightweight, like adding a convenience in the CLI, rather than making irreversible changes in the core... We have a lot to lose if we get this wrong: part of the appeal of the Zenith design is the encapsulation. It's easy to understand what the inputs and outputs of a function are. We should be very cautious about anything that risks breaking that.
Here's what a CLI convenience might look like:
stringorsecretare omitted in the CLI, a new default behavior kicks infoo, is to look up the environment variableFOO, and use its value.The end result of this CLI convenience is that, in the most common cases, one could set env variables in advance, and omit the bulk of repetitive arguments. This would solve the UX problem that is causing this feature request in the first place. All without compromising the hermetic nature of functions.
nipuna-perera commentedon Nov 15, 2023
When designing this, please consider the Enterprise use cases too. For example, I have to pass-through my company proxy (sometimes authed) to multiple areas for us to be able to use dagger. Because anything that goes out of our network has to go through our egress proxy. This is kinda? related - #6077
sipsma commentedon Nov 15, 2023
@shykes I like the general idea of your proposal in that we could make this a CLI-exclusive piece of functionality to start.
The auto-matching of arg name to env var would be the most magical part and I can definitely think of various corner cases or situations it might not handle super well. But that's obviously fine provided we at least improve the situation significantly.
I'm thinking about:
path,lang, etc.) but are never intended to be read from the caller envBut again, entirely possible those cases don't matter, so I'm curious from users who have run into a need for this: would the idea Solomon outlined work for you? I'm especially interested in the env vars you want from the caller and whether those translate well into required args w/ matching names.
sagikazarmark commentedon Nov 15, 2023
I have two use cases in mind where passing environment variables through would be beneficial:
I understand using GHA's or Jenkins' secret management solution is not ideal for many reasons, but it's the reality today, especially for companies just getting started. Being able to introduce Dagger to an existing pipeline step-by-step is crucial for adoption.
On the DX side: I realized I just don't enjoy writing a function with dozens of parameter. Everything in me tells me not to do it. If I have to give up those principles in Dagger...that's not a great experience.
On the UX side: I don't enjoy passing an endless number of arguments to commands either. Regardless if it's in the GHA yaml hell or in my Makefile.
I'm not sure adding new behavior on top of function arguments is going to improve that DX/UX for me. I would have to keep in mind how it works, that behavior is not immediately obvious from the code. Also, having to match parameter names with env var names....is a deep rabbit hole I'd like to stay away from (learned my lesson with Viper).
Combining this issue with another one (too many function arguments), one potential solution is being able to pass in structs to functions:
Keeping security in mind, I agree that this should probably be limited to main modules (though it may become an inconvenience at some point if you have to pass a GitHub Token to every single module).
I could also imagine adding an explicit list of env vars to dagger.json to pass through.
I wouldn't mind using some sort of experimental flag/config for this while the final details are being worked out (eg. dagger.experimental.json), although that's another incredibly deep rabbit hole.
I don't think the case for env vars is going away anytime soon and changing the semantics drastically and implicitly is going to make it harder for people to understand what's going on.
sipsma commentedon Nov 15, 2023
Thanks for the thoughts @sagikazarmark, still digesting fully but want to leave some immediate thoughts before I forget:
But we haven't fully fleshed it out and there's a handful of problems to figure out still. Support for custom struct tags (which could encompass the idea of the
envtag you used) is also something that comes up along with it.Just letting you know that it's on our radar.
The idea being that we bucket the sorts of use cases that call for "complex interaction between the caller host and modules" into "advanced use cases" and thus require that rather than invoking the CLI directly users write Dagger SDK code that implements the logic (reading the right env vars or other host resources and passing them as the right arguments).
So with #5993 you'd be able to write a short dagger script that runs on the host but can still easily call modules. You'd just use
dagger runrather thandagger call.I think approach strongly requires that the majority of use cases calling for this behavior are those that run in CI or other "non-human" environments. If, however, many users want this "auto-read env var" support for use cases that involve direct human interaction, it breaks down and would at minimum require tons more polish or just be non-viable.
The most compelling argument for this is that Dagger is already scriptable and runnable outside of modules, so why not use that existing functionality to support these sorts of use cases. It also makes sense in that it's may be a sisyphean task to really support every possible variation on how users could want to map caller env vars (and other host resources) to module function args.
I could keep going but curious what others' immediate reactions are.
nipuna-perera commentedon Nov 15, 2023
This is a MAJOR part of the appeal of dagger for us. Simply being able to run the same pipeline in local and in CI is a top feature of dagger for us. We simply can't drop our existing CI systems and with dagger, having to maintain dagger code + CI system DSL will reduce the appeal of dagger within my company.
Can I add another suggestion? Tell me if I'm crazy and this is a no-no.
How about using dagger.json to have a
passthrough_env_argswhere the module developer defines the env vars that are used by the module (required and optional). Then that can be documented (even printed out in the module CLI reference) by the module author and the consumer will have a clear idea of what ENV vars are passed through. Dagger CLI can put safeguards to prevent (or have a flag to opt-in) the passthrough ENV.We can even give the choice to the module developer on when they can use the ENVs. They are exposed as internal ENV to every function within the module but the function dev will have to do something like
os.GetEnvto actually use them. They can unset ENVs as they please as well if they absolutely don't want an ENV present within the function scope.I don't know if there are any architectural/technical limitations to something like that. But I can see a few benefits
Pros
dagger callMYDAGGERMOD_HTTP_PROXY.Cons
withFunctionpattern or similar.sagikazarmark commentedon Nov 15, 2023
Thanks @sipsma!
Yeah, I'm aware of that feature, but it falls in the bucket of "I don't enjoy writing that code".
For that reason I avoid using it entirely, even temporarily.
I haven't decided if it's the right way to think about this, but one of the reasons I liked switching to Zenith is the ability to drop Mage (and even Go) as a dependency for running my pipelines. If I understand correctly, this would essentially bring those back as dependencies.
I can't really imagine how this would look like, particularly the env var pass through, but I probably wouldn't want some additional step or code in my pipeline just to read env vars.
My immediate reaction to your second point: I just want env vars to work. 😄 I'd prefer if I it would require minimal work from me to achieve that. And I also want it to be obvious to whoever reads my pipeline code how it works.
Doesn't mean I'm right though. 😄
Mjb141 commentedon Nov 15, 2023
This is essentially what I'm looking for as well. Env vars 'just work' in almost every tool out there, I don't want to have to explain to colleagues that they now need to do something else with env vars.
I like this idea, but I'm not sure how well it would work in practice. The config side is simple andeasy to understand,
dagger.jsonis already a configuration file so it's where I'd expect to find this information, and it's self-documenting for users - "here's the env vars this file will look for if values aren't provided".Some form of explicit fetch would be required to avoid breaking the above mentioned workflow of "if a parameter value is provided use it, if none is provided use a default value I set here" by injecting a value in the middle of those conditions, but that also introduces another condition a module might have to check...
Also fine with an implementation like this, except the interactive prompt bit. No need for that if it can be explicitly enabled/disabled. As long as it's quick and simple to 1) see if this behaviour is on/off and 2) change the behaviour to on/off I'm happy. A really clear way of displaying this to users is also going to be super helpful - perhaps something in the
call -m <module> functionsresponse?shykes commentedon Nov 16, 2023
Unfortunately, the reason env vars "just work" in other tools is also the reason devops is such a mess, and Dagger is needed to clean it up: the composition model is 40 years old, and it's just not good enough. If duct-taping shell scripts together with env variables and yaml actually scaled, we wouldn't be here having this conversation. A big part of the Dagger "magic" is balancing a) a better composition system, with b) a familiar user experience that fits into your existing stack.
So that's our dilemma: to deliver on a), Dagger must break free of the traditional use of env vars for composition (because they are basically a worse version of arguments). But to deliver on b), we need env vars to be a first-class citizen in our UX.
I just want to manage expectations: we will bend over backwards to make the UX better. But we will not, cannot compromise on the scalable composition model.
A good rule of thumb for evaluating whether a proposal "breaks the composition model": does it create a system of "shadow arguments"? If the answer is yes, then it breaks the composition model and we shouldn't do it.
IMO this is a shadow argument system. Just use arguments + UX convenience.
IMO there's no benefit to asking module developers to think about env variable names. They already chose a name for their argument. Why make them choose a different name? That's just a step towards a shadow argument system.
It's a UX detail, but I think it will need for security reasons. Picture an untrusted module with an argument called
netlifyToken. You run it without arguments, not knowing the intricacies of default env lookup. Boom, yourNETLIFY_TOKENenv variable was collected and leaked. A warning prompt (or other implementation of a trust management system) would protect against that. This is the only reason (to my knowledge) for an interactive prompt.Yes agreed.
nipuna-perera commentedon Nov 16, 2023
I see what you mean here and I agree the
dagger.jsonmethod won't be good for composition.I'll give an example where just arguments would be messy and repetitive. Within my company, we have to utilize egress proxies for a lot of stuff we do, so if we pass the proxies as args,
http_proxy,https_proxyandno_proxyat a minimum would have to be passed into every function call. I could have awithsetup function for the module and set them up once, but again, every call to a function the consumer of the module has to usewithProxies. Besides, if I name the argument "http_proxy" that breaks certain language conventions. That's not a nice argument name in Go for example. I am hopeful the proxy problem will be solved in another way as part of something #6077 but you get the idea.Don't get me wrong, I really like the isolation and composition nature of what zenith is aspiring to do. But, having to jump through hoops to set up a ubiquitous env like the proxies might become too tedious.
No matter how hard we try, we can't have dagger modules behave exactly the same way in CI systems like they behave on local machines. There are restrictions that make it necessary to tweak the behavior of a module based on where it's running. Environment variables play a big part in CI systems and having a easy way to pass them through will help adoption of Dagger. Less verbose we can make our existing DSL (which is still needed in some capacity to run dagger) the better chance people will like/use it.
Being in the face of the module consumer (warnings/documentation etc.) about env var consumption of the module could alleviate this (could also get annoying). But yeah this is an issue inherent to most CLIs today and I haven't come across many that actually warn/inform users of ENV var usage. I am glad Dagger is trying to break away from this.
Don't module developers still have to think about env vars like proxies? Without them, their module won't work.
An interactive prompt won't work in CI though. So there will have to be some way to tell a module it's running in CI and run non-interactively. Today with non-zenith dagger I do this by looking at the CI system env vars.
I apologize It's a long winded response without any new proposals for a solution. But I am trying my best to represent the big Enterprise landscape from my experience and help Dagger run virtually anywhere.
sagikazarmark commentedon Nov 16, 2023
The only place where using env vars are beneficial is where you execute a pipeline using ‘dagger call’. I’d probably focus on those cases.
I agree that env vars break composition in every other scenario (they are like global vars).
But for pipeline entrypoints called from CLI, I’d argue they are actually part of the interface.
Here is what I would really like to avoid: passing countless arguments to commands just to avoid using env vars AND passing countless arguments to functions. As I said before: I don’t enjoy writing and maintaining those at all.
Mjb141 commentedon Nov 16, 2023
All good points - I didn't properly consider how some automagic around env vars might compromise the composition model. Thanks for explaining that! If the end result of improving the env vars experience is a CLI convenience and more explicitness in the CLI that's fine - I'd be quite happy with that.
Interactive prompts (and digging through what is often several wrong answers before finding the right way to disable) are often a pain point, alleviating that is going to be important. To reply to your example, other marketplaces typically put the onus on the user to be responsible for knowing what they're downloading/running. It's trivially simple to install and run a malicious package from npmjs/pypi for example, and so the process of using those package repositories is super quick with no/minimal barriers. Not saying that's necessarily the right idea, just outlining what I think developers/engineers are used to.
This does put Dagger a step ahead of other marketplaces in security posture, which if implemented I'd definitely highlight when talking about it!
vito commentedon Nov 16, 2023
@nipuna-perera Proxy env vars are a great point to bring up, but I think they should be treated as an exceptional case. I definitely don't think module authors should ever have to think about them, because that's tedious repetitive work across the entire ecosystem. Like you said, proxy env config applies to everything. If every module author had to remember to add params for it, anyone using a proxy would end up constantly blocked.
In Concourse we decided that this was an infra-level concern. Workers would pick up the "standard"
http_proxy(etc) env vars, and Concourse would inject those same values for all containers on the worker. I suppose we could try a similar approach for Dagger, but I'm sure there are gotchas. IMO this rabbit hole should be explored in a separate issue.Aside from proxies, I'm in full agreement with #6112 (comment)
13 remaining items
bitgandtter commentedon Mar 26, 2024
@shykes when you said
So in your example, that potentially insecure command will not work. Instead you would use --my-secret=env:MY_SECRET.
I'm not sure. I'm using it right now sort of like that, I mean
--my-secret=${{ secrets.GH_TOKEN }}on both local and GHA
helderco commentedon Mar 26, 2024
@bitgandtter you must be using an old
dagger version. Secrets are no longer passed by value in the CLI for a while.bitgandtter commentedon Mar 26, 2024
using locally
dagger v0.10.2 (registry.dagger.io/engine) darwin/arm64and on gha
shykes commentedon Mar 26, 2024
@bitgandtter that is very surprising. Definitely not expected behavior. Possible explanations:
stringrather thanSecret🤷
bitgandtter commentedon Mar 26, 2024
option 4 for sure ;) that said and as I mention before its a DX the need of passing the values put developers in place of either:
this can be solve by
yes this can be naive but I just wanna highlight the point of developers been allowed to expose secret values
shykes commentedon Mar 26, 2024
You're right that there will always be a way to leak secrets. Dagger doesn't aim to solve every security issue. If we 1) don't make security worse, and 2) make security better on average; we are in a good place.
rawkode commentedon Sep 13, 2024
Now that contextual modules are out of the way, it would be nice to tackle this and make it easier to not just build projects; but deploy them to production.
I have a module, OnePassword, that currently needs me to pass it the secret token and a secret reference to fetch a secret value from 1Password.
This means that any function that wants to use it, needs to wire up the secret environment variable and this is now rather cumbersome.
Now I don't think all modules should get access to environment variables, but perhaps we can bless a few?
flomedja commentedon Nov 26, 2024
I am new to
daggerand doing a poc on the usage of dagger to standardize the CI for Go projects.I just spend few hours trying to debug a module to build a Go programs, that wasn't working because I assume that
daggerhave access to environment variables of the host.Surprisingly even the
WithEnvVariable()didn't seem to set the environment variable adequately to be use in the subsequent task execution of the module.os.Getenv("Env_Name" )can't retrieve the environment variable after It was set withWithEnvVariable("Env_Name", "env_value")I have a use case similar to one previously mentionned by @skycaptain when a token is needed to retrieve private Go modules from Gitlab.
It would be interesting to have a mechanism to inject the host environment variables to
dagger call.Even If they need to be explicitely passed it is still more friendly than having to pass them through the functions input arguments as it is required now.
rawkode commentedon Nov 26, 2024
Most of the issues in this will hopefully be resolved by #8730