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

Nushell assume script? #795

Open
FollowTheProcess opened this issue Nov 6, 2024 · 1 comment
Open

Nushell assume script? #795

FollowTheProcess opened this issue Nov 6, 2024 · 1 comment

Comments

@FollowTheProcess
Copy link

FollowTheProcess commented Nov 6, 2024

Hello 👋🏻

Huge fan of granted, has massively simplified my workflow, thanks for the great tool!

I'm a nushell user and like some others, ran into the "your shell is not supported" error because there is no nushell assume alias auto-setup.

There is this discussion where one has been implemented. It didn't actually work for me with a straight copy paste but I made some local tweaks and it works great now.

Click me to see the patched script 👇🏻
# nushell support for https://github.com/common-fate/granted
# Save this file to `granted.nu` in your nushell config dir, then add `use granted.nu *` in your config.nu

# assume - https://granted.dev
export def --env --wrapped assume [...args: string] {
  const var_names = [
    "AWS_ACCESS_KEY_ID",
    "AWS_SECRET_ACCESS_KEY",
    "AWS_SESSION_TOKEN",
    "AWS_PROFILE",
    "AWS_REGION",
    "AWS_SESSION_EXPIRATION",
    "GRANTED_SSO",
    "GRANTED_SSO_START_URL",
    "GRANTED_SSO_ROLE_NAME",
    "GRANTED_SSO_REGION",
    "GRANTED_SSO_ACCOUNT_ID"
  ]

  # Run assumego and collect its output
  let output = with-env {GRANTED_ALIAS_CONFIGURED: "true"} {
    assumego ...$args
  }

  let granted_output = $output | lines
  let granted_status = $env.LAST_EXIT_CODE

  # First line is the command
  let command = $granted_output | get -i 0 | default "" | str trim | split row " "

  let flag = $command | get 0
  # Collect environment variables to set
  let envvars = do {
    let values = $command | skip 1
    let vars = $var_names |
      zip $values |
      filter {|x| $x.1 != "None"} |
      reduce --fold {} {|it, acc| $acc | insert $it.0 $it.1}
    $vars
  }
  match $flag {
    "GrantedOutput" => {
      # The rest of the output (if any)
      let to_display = $granted_output | skip 1
      $to_display | str join "\n" | print
    },
    "GrantedAssume" => {
      for v in $var_names {
        hide-env -i $v
      }
      load-env $envvars
    },
    "GrantedDesume" => {
      for v in $var_names {
        hide-env -i $v
      }
    },
    "GrantedExec" => {
      let num_vars = ($var_names | length)
      let cmd = $command | range ($num_vars + 1).. | str join " "
      with-env $envvars { nu -c $"($cmd)"}
    }
    _ => {
      # most likely no output, so nothing to do
    }
  }

  if $granted_status != 0 {
    error make -u {msg: $"command failed with code ($granted_status)"}
  }
}

I was wondering if we could feasibly include this in the list of default supported shells? I'm not exactly familiar with how granted auto configures the shells but I'm guessing the process for nushell support would be something like:

  • Save the above script to $nu.default-config-dir/granted.nu
  • In $nu.default-config-dir/config.nu add the following to the bottom of the file:
# Use common fate granted with nu
use granted.nu *

I'm very familiar with Go so would be happy to see if I can add this in a PR. Is there any additional granted specific setup to do other than this "installation"? :)

Maybe even just adding a section on how to do this in nushell to the shell alias docs (https://docs.commonfate.io/granted/internals/shell-alias) is enough?

Thanks again!

@tom-on-the-internet
Copy link

Thanks for this script!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants