Struggles with integrating 1Password's op #3542
Replies: 19 comments 2 replies
-
I'm also using I'm curious if there is a way to opt of out having the |
Beta Was this translation helpful? Give feedback.
-
you could experiment by running |
Beta Was this translation helpful? Give feedback.
-
I'm not totally sure how we could resolve this since that would only work for zsh. Maybe what we could do is reuse previously resolved env vars if they've already been resolved and maybe offer a Gratefully, this would be feasible now that the |
Beta Was this translation helpful? Give feedback.
-
This is exactly how it works (for me anyway) with direnv. If I have to adjust env vars after changing directories into the project, then I run I'm assuming this would apply to scripts too? If so, I could see the possibility that some scripts people are running now are intended to be ran on every prompt.
:) |
Beta Was this translation helpful? Give feedback.
-
Because these are secrets, I wouldn't want them cached to the file system. That means they'd have to stay in the shell. That would be a pretty big shift in architecture to what mise does now? |
Beta Was this translation helpful? Give feedback.
-
I'm not talking about caching to the file system, this would go into a different internal environment var like __MISE_DIFF. I think we'd need to track all of the current exported variables and what their source is—since if 2 config files export the same env var key that would require invalidation.
Good to know, it's probably safe for us to do similar logic. |
Beta Was this translation helpful? Give feedback.
-
There shouldn't be any security risk here since the env vars already contain the secret's value. In fact we don't even need to track the value of the keys I don't think since they're already exported, we just need to track their source |
Beta Was this translation helpful? Give feedback.
-
Good to know. I didn't realize the shell environment variables were persistent between prompts. |
Beta Was this translation helpful? Give feedback.
-
@rsyring - Correct me if I'm wrong, but wasn't your main concern around invoking a script that in turn triggers a 1password secret dump on each prompt/cmd? I'm currently using Even on entry to the root can be a bit much and I've experimented and often just use a shell function ondemand when I need the secrets loaded, since many commands don't require them. However, my point of this is that if we rely on the current So, I think the only reasonable approach would be to only do this on cd change into the project root. Back to my question about only running the |
Beta Was this translation helpful? Give feedback.
-
@andrewthauer I may not be fully understanding your comments. The point of this issue was to address being able to run a script once (presumably on changing into a project directory) instead of at every prompt. I think you are saying the same thing. We've also discussed just above being able to load on demand (e.g. I guess another consideration would be to have the option to ONLY load on demand. For some of my projects, where I'm managing infrastructure, I always need secrets. For a others, I almost never do because I can do most of the development locally and only occasionally want to run against a live service. e.g. |
Beta Was this translation helpful? Give feedback.
-
Essentially, yes, I'm saying the same thing, but I guess I'm getting into the the technical details of how So not activating and manually calling What I was ultimately trying to say, which probably did a horrible job at, was that you can't really get your cake and eat it while using the mise activate in your zshrc, bashprofile without getting the performance hit on each prompt. Which isn't noticeable normally but would be for this sort of use case. However, if there was a way to say I only want to use the cd change hook (chpwd in zsh speak), then this hit would only occur on a cd change. I'm not sure how mise would selectively only run the |
Beta Was this translation helpful? Give feedback.
-
while accurate there is some short circuiting logic that might not be working correctly here. This should be preventing |
Beta Was this translation helpful? Give feedback.
-
TBH, I haven't tested this particular use case with |
Beta Was this translation helpful? Give feedback.
-
So I had a go at porting my current First I'll document my 2 current approaches for completeness. Method 1: Direnv library helper This combines a # ~/.config/direnv/lib/op.sh
use_op_env() {
# implementation omitted to be brief, but essentially does a `direnv_load op run -- direnv dump`
}
# Then in a projects .envrc file
# ~/src/some-project/.envrc
...
use op_env some-account.1password.com Method 2: On demand shell alias This works well for either some shared global secrets and does not rely on direnv at all. It' also avoids getting the potentially annoying upfront prompt from 1password for auth or startup hit. # ~/.local/bin/op-dotenv
# this essentially replaces the direnv_load part and does:
# `cat <"$dotenv_file" | op inject` and then spits out the resulting resolved variables
# ~/.zshrc
alias ope='eval $(op-dotenv "$HOME"/.config/.global-env --export)'
# then at any point run this alias to get those secrets into your shell. op will prompt for auth the first time in the shell
ope Usage in In First the file to source which in turn calls the # ~/src/some-project/.config/scripts/op-env.sh
op-dotenv "$PWD"/.env --export Then in the mise config # ~/src/some-project/.mise.toml
[env]
_.source = "./config/scripts/op_env.sh" # file above NOTE: My setup could be adapted likely to work with variables defined in This works as expected, however, 2 issues I ran into:
So, in the end it seems definitely doable, but the 2 points above would need to be addressed before it can be widely used I think. The other thing that would be nice is a way to used "share" helpers like direnv through some common place to look up plugins (e.g. |
Beta Was this translation helpful? Give feedback.
-
I know this wouldn't be perfect but I wonder if somehow using the enter hook would solve this problem. You could have that in your global mise.toml which would effectively only run once. That can't export env vars though so idk how helpful it would be. Perhaps we could extend that somehow to allow for this. Actually this made me realize that there is a bug using the global config, it never gets run for enter, but that's a seperate problem. |
Beta Was this translation helpful? Give feedback.
-
the specific issue where mise doesn't run if config files didn't change should be fixed now that hooks are out. As part of that I needed to make hook-env run anytime the directory changes. |
Beta Was this translation helpful? Give feedback.
-
I tried revisiting this setup and got a little further. I tried 2 approaches:
However, both of these solutions currently suffer from the fact they get executed every single prompt (even non mise ones). I think twice actually, but not sure. This means it's a no go since the secrets resolution can take a few seconds. With my current @jdx - Am I missing anything currently built in that would help here? Or is this something new that would need to be incorporated into |
Beta Was this translation helpful? Give feedback.
-
this is likely shell specific but I did recently discover when changing directories we effectively call twice (once for cd and once for the prompt). I'm not sure I actually want to fix that because that code inevitably causes unexpected bugs whenever I touch it. I did recently add support for shell hooks which would be a partial, albeit ugly way to solve this. The problem with that is this is straight up executing shell code—mise won't be able to remove env vars added there once you leave. While you could in theory write a Ultimately it sounds like what you're looking for is the ability to use [env]
_.source = {path = "some-script.sh", when="cd"} As far as implementation, this would be a bit tricky since typically re-resolve this every time so there would need to be some caching in place. This would either be in the form of caching as a file (which is probably easier-implementation wise since we do similar things) or storing previous values in a distinct env var (as serialized data) like Alternatively, we could extend this to the hooks functionality as something like this: [hooks]
enter = {source = "some-script.sh"} Though I think this would be harder, since the hooks would somehow need to communicate the env var change in a way that it gets reset later. I think I don't like this idea. |
Beta Was this translation helpful? Give feedback.
-
FWIW, I ended up realizing that setting environment variables was not always straight forward for a project. A decent number of my projects actually had groups of environment variables that needed to be set based on what I was trying to do. Different cloud environments, running Ansible vs Terraform for a project, etc. etc. That was true for more than just secrets as well, maybe API URLs changed, maybe I wanted to set I also realized I often didn't want secrets loaded whenever I was in a project's directly b/c I wasn't going to do anything that used a secret. I.e. I was just going to be doing local development and would not be deploying anything. I wanted to have the ability to define these profiles as well as integrate with secrets (we use 1Password) so I created env-config. We still use mise for a lot including static environment variables but now use env-config for secrets and vars that are context/activity dependent. |
Beta Was this translation helpful? Give feedback.
-
I've been trying to figure out how to get 1Password secrets integrated with mise (refs #1359). The biggest challenge I've encountered is that the
op
utility is rather slow, taking about 1s to return when asked for secrets. That delay on every prompt is pretty annoying.Since direnv doesn't run at every prompt, I thought I had this solved with a pretty simple setup:
# .envrc direnv_load op run --no-masking -- direnv dump watch_file .mise.toml
This setup has the advantage of being usable with
op run
directly if someone prefers not to use direnv. The problem I encountered is that there is a conflict between direnv and mise which led to the environment variables not getting removed when leaving the directory. The secrets were no longer resolved but the environment variables (TF_VAR_*
) remained in the current shell.The benefit of direnv for this setup is that
.envrc
isn't executed on every prompt. If I need to refresh the secrets its easy withdirenv reload
.Since mise has the stated intention of replacing direnv for any use case, it would be better to do:
But, in this case, for performance to be acceptable, I'd only want
op-run.sh
run the first time it was needed and/or on demand.This may also also impact #1448.
One other thought regarding this: since we are dealing with secrets, I wouldn't want to see these values cached to the filesystem.
Beta Was this translation helpful? Give feedback.
All reactions