Skip to content

.env support & default secrets #9584

Closed
Closed

Description

@shykes

Overview

Dagger should natively support .env files. It's a de facto standard for managing environment-specific configuration in a lightweight, portable way. It is widely supported, included by docker and docker-compose.

Proposed design

Default behavior

If .env exists in the current directory, or any parent directory, dagger should load it by default.

--env-file

An env file can be specified explicitly with dagger --env-file PATH.

Secret references

Regular env file must store a secret value in plaintext. But Dagger allows you to keep only the reference of the secret in the file, minimizing the risk of secret leaks, and making it easier to rotate credentials.

Any variable value that matches the format of a Dagger secret reference, will automatically be parsed as a secret URI, and securely resolved by the engine.

For example:

USERNAME=admin
PASSWORD=op://Private/foo/credential
PROD_AWS_TOKEN=vault://foo/bar/prod/token

CLI usage

To use the variables with the dagger CLI:

  • In dagger shell, simply reference the variable anywhere in your commands with $FOO or ${FOO}.
  • In dagger call, this also works, but remember to use single-quotes around arguments containing a variable expansion, to prevent your system shell from doing the expansion with its own variables - unless that's what you want!

In both cases, variable expansion only uses the contents of the env file. The host environment variables are never used.

Usage by Dagger Functions: default secrets

Env files are an acceptable sandbox for env variables, in the same way that the git context is an acceptable sandbox for local directories.

This means that, once we support Env files, and as long as we forbid escaping the sandbox, we can now allow functions to define a default env variable for their secrets.

For example:

// Login to Dagger Cloud
function (m *DaggerCloud) Login(
  // Dagger Cloud token for authentication  
  // +default="$DAGGER_CLOUD_TOKEN"
token *dagger.Secret,
  ) error {
    // ....
  }

Activity

  1. jedevc commented on Feb 14, 2025

    @jedevc
    Contributor

    Sounds good 🎉

    1. My main question is whether transitive dependencies should be able to use default secrets from the original caller. E.g. CLI calls module A, module A calls module B, module B has a default secret - is it allowed to access it?

      It feels like the feature is a little lacking if it can't (though you can always just explicitly pass the secret between A and B), but if you can, it feels like a huge ecosystem vulnerability waiting to happen.

      I'd propose that we limit this feature to modules that are called directly by the user - at least for a first pass, and then we could lift this restriction in the future. Much easier to lift it later, than to try and patch it out if we change our minds. Maybe there's a way to protect against this kind of supply chain attack, but if so, I'm not really seeing it.

    2. Another question is whether this might conflict with how people are already using .env files today that don't contain secrets, but instead just contain their own data. Do we need a dagger-specific filename to avoid loading this then? If not, we might need a way to mitigate accidentally loading unintended values from it.

    3. Would we want to allow the .env file to contain non secret values? e.g. so you could have a default string value with a pragma that reads from the environment. This feels like it would be very neat. E.g. in our own CI, we've got a variable for the ID of the S3 bucket we upload artifacts to - it's not a secret, but it would still be nice to upload from the environment.

  2. helderco commented on Feb 14, 2025

    @helderco
    Contributor

    I think this would be like context directories. When you have transitive dependencies with defaultPath, they're relative to the module that defines it, not the one you're calling. So I'd say if module B has a default env var, it would have to have an .env in its sources. That brings up another question though. Some .env files could be committed to git to support this from a git source, but then you may still want a .env that's specific to the caller, so how do we feel about supporting a suffix on the file name, either static or defined by the module author?

    For example, I think dotenv supports both .env and .env.local (and a bunch of other tools), so you can commit one for sharing and keep the other specific to you. Docker compose on the other hand makes you specify the filename so you can manage it however you want. I think the static .env.local merged on top of .env (if exists) is simple enough to implement, gets out of your way, and is "standardized" enough that a lot of people are used to it. The downside is if you already have a .env for other purposes, and you don't want to use it in dagger.

    There's a limitation with secret providers here. The secret references are very "caller specific" which doesn't make much sense to commit to git, but could be useful in the context of a company, where secrets are stored in the same "place" (i.e., not necessarily in a central location, but at least with the same secret reference, even if env vars).

    As for normal env vars it's of limited use because we don't have an "env var" type to pass as argument, it's just the primitive types and we already support default values for those. However, could still be useful to easily replace some defaults (via .env) on certain environments like CI vs developer machine, without having to change the code.

  3. shykes commented on Feb 14, 2025

    @shykes
    ContributorAuthor

    I don't think we should handle overrides. It's not part of the original spec, and is not universally supported. In our case it's less needed because Dagger already has a way to express sane defaults in a function. If it's safe to commit in a shared .env, it's safe to commit in the function directly.. So better to only support the basic use of .env as an uncommitted env-specific configuration.

  4. shykes commented on Feb 14, 2025

    @shykes
    ContributorAuthor

    Actually we could support passing multiple env file paths... then we could stack them for you. So overrides would be possible, but would never happen automatically- up to the caller to manage their env files any way they want.

  5. shykes commented on Feb 15, 2025

    @shykes
    ContributorAuthor
    1. My main question is whether transitive dependencies should be able to use default secrets from the original caller. E.g. CLI calls module A, module A calls module B, module B has a default secret - is it allowed to access it?

    Great question. I'm not sure.

    It feels like the feature is a little lacking if it can't (though you can always just explicitly pass the secret between A and B), but if you can, it feels like a huge ecosystem vulnerability waiting to happen.
    I'd propose that we limit this feature to modules that are called directly by the user - at least for a first pass, and then we could lift this restriction in the future. Much easier to lift it later, than to try and patch it out if we change our minds. Maybe there's a way to protect against this kind of supply chain attack, but if so, I'm not really seeing it.

    I agree.

    1. Another question is whether this might conflict with how people are already using .env files today that don't contain secrets, but instead just contain their own data. Do we need a dagger-specific filename to avoid loading this then? If not, we might need a way to mitigate accidentally loading unintended values from it.

    I think it's important that it works with existing .env files without interference.

    I don't see why it wouldn't work, because we'll be using it as intended. "Loading" a .env is a very lightweight affair, it just makes it available on the CLI side, in case the engine asks for it in a session attachable. Unless the engine asks for it (and the client agrees to send it) nothing leaves the client system. So I think we're good.

    The worst thing that could happen, is that people like the secret reference feature so much, that they start removing secret plaintext from their .env, and using op:// or vault:// instead, causing other tools to break. Then they will either pressure other toolmakers to support the standard (and us to open-source the reference implementation), or start bifurcating the .env files, or both. Maybe we can plan for this, and open-source a small utility that converts a secure .env file with secret references, to an insecure one with secret values.

    1. Would we want to allow the .env file to contain non secret values? e.g. so you could have a default string value with a pragma that reads from the environment. This feels like it would be very neat. E.g. in our own CI, we've got a variable for the ID of the S3 bucket we upload artifacts to - it's not a secret, but it would still be nice to upload from the environment.

    Yes absolutely! IMO we should support all the same types that CLI arguments support; with all the same "magic string" features:

    # Regular string
    S3_BUCKET=blablabla
    # Can be cast to a container type:
    BASE_IMAGE=index.docker.io/alpine:latest
    # Can be cast to a directory type:
    DOCS_SOURCE=https://github.com/dagger/dagger#v0.15.4:docs
    DOCS_SOURCE_LOCAL=../docs
    # Can be cast to a service type:
    DB=tcp://localhost:4242
    

    Then we could just variable expansion in all the pragmas:

    func (m Foo) Bar(
      // +default="$S3_BUCKET"
      bucket string,
      // Defaults not supported yet...
      // See https://github.com/dagger/dagger/issues/8483
      baseImage *dagger.Container,
      // Default can only be local path. Would be cool to support remote also...
      // +defaultPath="$DOCS_SOURCE"
      docsSource *dagger.Directory,
      // Default services not supported...
      db *dagger.Service,
    ) {}
  6. rawkode commented on Mar 19, 2025

    @rawkode
    Contributor

    Could this work with direnv / .envrc files, rather than Dagger itself finding environment files within a repository?

    This would allow us to use source_up and other helper functions, provided by direnv.

  7. TomChv commented on Mar 24, 2025

    @TomChv
    Member

    Could this work with direnv / .envrc files, rather than Dagger itself finding environment files within a repository?

    This would allow us to use source_up and other helper functions, provided by direnv.

    I think dagger should look for either .env or .envrc, but it shouldn't pick up values from the current host env, but yeah both sources should be supported in my opinion

  8. jasonmccallister commented on May 14, 2025

    @jasonmccallister
    Contributor

    Adding some thoughts on .env support with frameworks (e.g. Laravel, Symfony).

    1. Support for a .env file is usually for supporting local development (e.g. laptop) where the user does not have an elegant way to manage the environment (typically with a UI)
    2. When you deploy frameworks with .env support, you don't create a .env file in the production environment. Instead you manage the variables using a UI or define those in Terraform or a container spec

    I'm wondering how the experience would move from local development to putting the workflow in production (e.g. GitHub Actions). Would there be a step to create a .env file and then call the Dagger Action? That experience seems less than ideal.

    However, I wonder how people feel about this approach: #10397 (comment)

    If there is an env, maybe those can be automatically pulled from the host and you don't need to provide an argument to the function?

    I could also see this as a frustrating user experience. If you need to define the env in the dagger.json but forget to - some users might spend time troubleshooting why the environment variable is not being populated. Wondering if there is a new function (e.g. WithHostEnvVar) that way its explicit to the user that its coming from the host and if its not defined in the dagger.json it throws an error?

  9. rawkode commented on May 14, 2025

    @rawkode
    Contributor

    Now that dotenv, dotenvx, and op, infisical, etc exist; it's becoming more common for .env.production to be supported and only store references to secrets.

    The nice thing about this approach is that people can still run op run -- bun run deploy without having to go through a Dagger pipeline.

    But this still leaves the security risk that some nested module cannot have access to those values just because it happens to be passed a directory with a .env file? So won't the dagger.json step still be required?

  10. locked and limited conversation to collaborators on May 14, 2025
  11. converted this issue into a discussion #10401 on May 14, 2025
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions