common-fate / granted

The easiest way to access your cloud.
https://granted.dev
MIT License
990 stars 93 forks source link

feature: fuzzy search #312

Open Wurstnase opened 1 year ago

Wurstnase commented 1 year ago

Hi,

when you have multiple accounts it would be extremely helpful to have some kind of fuzzy search like fzf to get the correct account.

With assume -c you have already a simple search mechanism, with fuzzy this would be a great improvement.

Nevon commented 1 year ago

Would love to see this, but fuzzy searching not just on the profile name but on more attributes of the profile. A very common use-case for me is that I'll have an ARN of some resource and I want to do something with it or open the AWS console in that same account, but all I have from that is an account number, so I end up doing something like cat ~/.aws/config | grep -B1 <account_number> to find the profile name. Would be really convenient if I could just do assume -c and have the fuzzy finder find the profile based on matching the role arn.

bilalq commented 6 months ago

I just installed and tried out granted for the first time a couple minutes ago and was immediately hit with this. Right now, I'm using a combination of aws-sso-utils and some custom aliases in my day to day.

alias aws-profile='export AWS_PROFILE=$(sed -n "s/\[profile \(.*\)\]/\1/gp" ~/.aws/config | fzf)'
alias ap="aws-profile"

The above is what I use so I can select a profile with fuzzy searching by just running ap.

Not sure what this project's stance on external deps is, but if you're against having any, maybe support an env variable to either enable fzf usage or maybe provide an optional role selector command?

thecodesmith commented 5 months ago

I was able to solve this (adding fzf selection support) with a script and an alias, without modifying the assume source code. Adding here in case it helps someone.

First, a script to parse an AWS config file and print lines in the format <account number> <profile name>. This could be enhanced to include additional fields from each profile if desired. Note that it requires GNU Awk. It could be done in Perl or some other scripting language instead if needed.

#!/usr/bin/env gawk -f

BEGIN {
    # to start, no profile found yet
    profile = "";
}

# match lines like "[profile <name>]" and set profile name in profiles array
/\[profile\s*.+]/ {
    match($2, /(.*)]/, m);
    profile = m[1];
    profiles[profile] = "";
}

# match lines like "*sso_account_id = <account id>" and set the account ID in the profiles array
/.*sso_account_id\s*=\s*\w+$/ {
    profiles[profile] = $3;
}

END {
    # print each profile in the format "<account id> <profile name>"
    for (profile in profiles) {
        printf "%-12s %s\n", profiles[profile], profile;
    }
}

Store that script in a file (I named mine list-aws-profiles), make it executable, put it on your system $PATH.

Second, create an alias that chains the script plus fzf and assume:

alias aws-profile="list-aws-profiles < ~/.aws/config | fzf | awk '{print \$NF}' | xargs assume"

Alias explained:

Check out this commit for my actual source code: https://github.com/thecodesmith/dotfiles/commit/5fb46365f25ace39a4ae8366ef45cddc718f5ef4. Happy fuzzy searching!