Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running shell scripts that wrapped CLIs #120

Open
martijnarts opened this issue Dec 19, 2022 · 13 comments
Open

Running shell scripts that wrapped CLIs #120

martijnarts opened this issue Dec 19, 2022 · 13 comments
Labels
op-cli Functionality to be implemented in 1Password CLI. Needs to be done by 1Password Developers.

Comments

@martijnarts
Copy link

op CLI version

2.10.0

Goal or desired behavior

I have a shell script that runs doctl, something like:

#!/usr/bin/env bash

doctl account get

Current behavior

It currently will output:

$ ./bin/test.sh
Error: GET https://api.digitalocean.com/v2/account: 401 (request "...") Unable to authenticate you

It should just work, though.

Relevant log output

No response

@martijnarts martijnarts added the op-cli Functionality to be implemented in 1Password CLI. Needs to be done by 1Password Developers. label Dec 19, 2022
@martijnarts
Copy link
Author

Workaround is replacing the line with op plugin run doctl account get

@mrjones2014
Copy link
Member

mrjones2014 commented Dec 19, 2022

I think this is because the #!/usr/bin/env bash shebang line causes it to run in a subshell in which ~/.config/op/plugins.sh would not have been sourced.

As far as I know there isn't really any way around this other than having your script check if ~/.config/op/plugins.sh exists, and if so, source it again.

@mrjones2014
Copy link
Member

mrjones2014 commented Dec 19, 2022

Alternatively, you could write it as a shell function in your ~/.bashrc/~/.zshrc/~/.config/fish/config.fish instead of having it be a script.

e.g. for bash/zsh

mydoctlalias() {
  doctl account get
}

or for Fish shell:

function mydoctlalias
  doctl account get
end

This way it would run in the current shell instead of a subshell and therefore your aliases from ~/.config/op/plugins.sh would be respected.

@martijnarts
Copy link
Author

Yeah, I figured that would be the problem. It might be worth adding something like this to the dev documentation.

@cussiol
Copy link

cussiol commented Dec 23, 2022

Those workarounds only work when you can modify the script. As 1Password Shell Plugins are based on alias, it seems a significant limitation for actual usage.

It is worth to think an alternative official way to make it work.

One way I thought is something like op plugins run <plugins> -- ./script

@SimonBarendse
Copy link
Member

I think this is because the #!/usr/bin/env bash shebang line causes it to run in a subshell in which ~/.config/op/plugins.sh would not have been sourced.

This is correct. Furthermore, 1Password Shell Plugins are (currently) build to be used in interactive shells. Besides the problem you're now running into of the aliases not having been sourced, the plugin might also need to prompt (e.g. when you don't have a default credential configured), which is only possible in an interactive terminal.


I agree with both these comments:

It might be worth adding something like this to the dev documentation.

Those workarounds only work when you can modify the script. As 1Password Shell Plugins are based on alias, it seems a significant limitation for actual usage.
It is worth to think an alternative official way to make it work.

@martijnarts
Copy link
Author

I start all my scripts with this now:

if ! [ -x "$(command -v op)" ]; then
    echo >&2 "Error: 1Password CLI not installed, find it at: https://developer.1password.com/docs/cli/get-started#install"
    exit 1
fi

export OP_ACCOUNT=$(op account list | grep '<SOMETHING>.1password.com' | awk '{print $3}')

@SimonBarendse
Copy link
Member

As an alternative workaround for now that doesn't require you to change your scripts, you could use a shim: create a file in your $PATH named doctl with the following contents:

op plugin run /usr/local/bin/doctl

It needs to have exec permissions (chmod +x) and it needs to take precedence in your path over the actual doctl binary

@cussiol
Copy link

cussiol commented Feb 9, 2023

@SimonBarendse I tried that workaround but got an error:

$ op plugin run -- /usr/local/bin/aws
[ERROR] 2023/02/09 01:58:06 unknown plugin: /usr/local/bin/aws
$ op plugin run /usr/local/bin/aws
[ERROR] 2023/02/09 01:58:09 unknown plugin: /usr/local/bin/aws

@lypanov
Copy link

lypanov commented Mar 30, 2023

All I'm looking for is a way to execute a command (in my case terraform plan) and have op plugin inject the AWS_ environment variables into that command. Is there any way to do this currently or is this or a related ticket blocking that?

@kjvalencik
Copy link
Member

My workaround for terraform is a wrapper script that calls aws configure export-credentials --format env before executing the terraform command.

#!/bin/bash

eval "$(op plugin run -- aws configure export-credentials --format env)"

/usr/local/bin/terraform "${@}"

I have an alias to this script set to terraform in my shell config.

@AndyTitu
Copy link
Contributor

@lypanov We have just released our new beta solution for the Terraform shell plugin. This allows AWS to work with Terraform out of the box, and also comes up with a general solution for authenticating terraform providers. Would be awesome if you could give it a try and let us know how/if this simplifies your workflows by a bunch.

@lypanov
Copy link

lypanov commented Jan 8, 2024

@lypanov We have just released our new beta solution for the Terraform shell plugin. This allows AWS to work with Terraform out of the box, and also comes up with a general solution for authenticating terraform providers. Would be awesome if you could give it a try and let us know how/if this simplifies your workflows by a bunch.

@AndyTitu Sorry for the delay in response, was recovering from illness. This works really well now thank you for the solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
op-cli Functionality to be implemented in 1Password CLI. Needs to be done by 1Password Developers.
Projects
None yet
Development

No branches or pull requests

7 participants