Skip to content

Commit

Permalink
Autocomplete for Browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Nov 18, 2020
1 parent 0a17c1a commit cfabb8c
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ the [Contributor Covenant](https://contributor-covenant.org) code of conduct.

* Tristan [tristanmorgan](https://github.com/tristanmorgan)
* Adam Sir [AzySir](https://github.com/AzySir)
* Vito Giarrusso [thtliife](https://github.com/thtliife)

## License

Expand Down
7 changes: 7 additions & 0 deletions lib/awskeyring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module Awskeyring # rubocop:disable Metrics/ModuleLength
DEFAULT_KEY_AGE = 90
# Default Console Paths
DEFAULT_CONSOLE_LIST = %w[cloudformation ec2/v2 iam rds route53 s3 sns sqs vpc].freeze
# Default Browsers
DEFAULT_BROWSER_LIST = %w[Brave FireFox Opera Safari Vivaldi].freeze

# Retrieve the preferences
#
Expand Down Expand Up @@ -201,6 +203,11 @@ def self.list_console_path
prefs.key?('console') ? prefs['console'] : DEFAULT_CONSOLE_LIST
end

# Return a list of browserss
def self.list_browsers
prefs.key?('browser') ? prefs['browser'] : DEFAULT_BROWSER_LIST
end

# Return Key age warning number
def self.key_age
prefs.key?('keyage') ? prefs['keyage'] : DEFAULT_KEY_AGE
Expand Down
4 changes: 4 additions & 0 deletions lib/awskeyring_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ def comp_type(comp_line:, curr:, prev:)
comp_len = 4
when 'remove-token', 'rmt'
comp_len = 5
when '--browser', '-b'
comp_len = 6
end

[curr, comp_len, sub_cmd]
Expand Down Expand Up @@ -495,6 +497,8 @@ def print_auto_resp(curr, len, sub_cmd) # rubocop:disable Metrics/MethodLength,
list = Awskeyring.list_console_path
when 5
list = Awskeyring.list_token_names
when 6
list = Awskeyring.list_browsers
else
exit 1
end
Expand Down
103 changes: 101 additions & 2 deletions man/awskeyring.5
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,90 @@ The commands are as follows:
.IP
Prints the version
.
.br
.
.IP
\-r, \-\-no\-remote: Do not validate with remote api\.
.
.TP
add ACCOUNT:
.
.IP
Adds an ACCOUNT to the keyring
.
.br
.
.IP
\-k, \-\-key=KEY: AWS account key id\.
.
.br
\-s, \-\-secret=SECRET: AWS account secret\.
.
.br
\-m, \-\-mfa=MFA: AWS virtual mfa arn\.
.
.br
\-r, \-\-no\-remote: Do not validate with remote api\.
.
.TP
add\-role ROLE:
.
.IP
Adds a ROLE to the keyring
.
.br
.
.IP
\-a, \-\-arn=ARN: AWS role arn\.
.
.TP
console ACCOUNT [\-\-browser BROWSER]:
awskeyring console ACCOUNT:
.
.IP
Open the AWS Console for the ACCOUNT in BROWSER or the default browser if BROWSER not specified
Open the AWS Console for the ACCOUNT
.
.br
.
.IP
\-p, \-\-path=PATH: The service PATH to open\.
.
.br
\-b, \-\-browser=BROWSER: Specify an alternative browser\.
.
.br
\-n, \-\-no\-token: Do not use saved token\.
.
.br
\-o, \-\-no\-open: Do not open the url\.
.
.TP
env ACCOUNT:
.
.IP
Outputs bourne shell environment exports for an ACCOUNT
.
.br
.
.IP
\-n, \-\-no\-token: Do not use saved token\.
.
.br
\-u, \-\-unset, \-\-no\-unset: Unset environment variables\.
.
.TP
exec ACCOUNT command\.\.\.:
.
.IP
Execute a COMMAND with the environment set for an ACCOUNT
.
.br
.
.IP
\-n, \-\-no\-token: Do not use saved token\.
.
.br
\-b, \-\-no\-bundle: Unset Bundler environment variables\.
.
.TP
help [COMMAND]:
.
Expand All @@ -66,18 +120,33 @@ import:
.IP
Import an ACCOUNT to the keyring from ~/\.aws/credentials
.
.br
.
.IP
\-r, \-\-no\-remote: Do not validate with remote api\.
.
.TP
initialise:
.
.IP
Initialises a new KEYCHAIN
.
.br
.
.IP
\-n, \-\-keychain=KEYCHAIN: Name of KEYCHAIN to initialise\.
.
.TP
json ACCOUNT:
.
.IP
Outputs AWS CLI compatible JSON for an ACCOUNT
.
.br
.
.IP
\-n, \-\-no\-token: Do not use saved token\.
.
.TP
list:
.
Expand All @@ -90,6 +159,11 @@ list\-role:
.IP
Prints a list of roles in the keyring
.
.br
.
.IP
\-d, \-\-detail, \-\-no\-detail: Show more detail\.
.
.TP
remove ACCOUNT:
.
Expand Down Expand Up @@ -120,12 +194,34 @@ token ACCOUNT [ROLE] [MFA]:
.IP
Create an STS Token from a ROLE or an MFA code
.
.br
.
.IP
\-r, \-\-role=ROLE: The ROLE to assume\.
.
.br
\-c, \-\-code=CODE: Virtual mfa CODE\.
.
.br
\-d, \-\-duration=DURATION: Session DURATION in seconds\.
.
.TP
update ACCOUNT:
.
.IP
Updates an ACCOUNT in the keyring
.
.br
.
.IP
\-k, \-\-key=KEY: AWS account key id\.
.
.br
\-s, \-\-secret=SECRET: AWS account secret\.
.
.br
\-r, \-\-no\-remote: Do not validate with remote api\.
.
.SH "ENVIRONMENT"
The AWS_DEFAULT_REGION environment variable will be used for AWS API calls where specified or fall back to us\-east\-1 when not\.
.
Expand Down Expand Up @@ -188,6 +284,9 @@ Tristan tristanmorgan \fIhttps://github\.com/tristanmorgan\fR
.IP "\(bu" 4
Adam Sir AzySir \fIhttps://github\.com/AzySir\fR
.
.IP "\(bu" 4
Vito Giarrusso thtliife \fIhttps://github\.com/thtliife\fR
.
.IP "" 0
.
.SH "LICENSE"
Expand Down
63 changes: 50 additions & 13 deletions man/awskeyring.5.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,79 @@ The commands are as follows:

* --version, -v:

Prints the version
Prints the version<br>

-r, --no-remote: Do not validate with remote api.

* add ACCOUNT:

Adds an ACCOUNT to the keyring
Adds an ACCOUNT to the keyring<br>

-k, --key=KEY: AWS account key id.<br>
-s, --secret=SECRET: AWS account secret.<br>
-m, --mfa=MFA: AWS virtual mfa arn.<br>
-r, --no-remote: Do not validate with remote api.

* add-role ROLE:

Adds a ROLE to the keyring
Adds a ROLE to the keyring<br>

-a, --arn=ARN: AWS role arn.

* awskeyring console ACCOUNT:

* console ACCOUNT [--browser BROWSER]:
Open the AWS Console for the ACCOUNT<br>

Open the AWS Console for the ACCOUNT in BROWSER or the default browser if BROWSER not specified
-p, --path=PATH: The service PATH to open.<br>
-b, --browser=BROWSER: Specify an alternative browser.<br>
-n, --no-token: Do not use saved token.<br>
-o, --no-open: Do not open the url.

* env ACCOUNT:

Outputs bourne shell environment exports for an ACCOUNT
Outputs bourne shell environment exports for an ACCOUNT<br>

-n, --no-token: Do not use saved token.<br>
-u, --unset, --no-unset: Unset environment variables.

* exec ACCOUNT command...:

Execute a COMMAND with the environment set for an ACCOUNT
Execute a COMMAND with the environment set for an ACCOUNT<br>

-n, --no-token: Do not use saved token.<br>
-b, --no-bundle: Unset Bundler environment variables.

* help [COMMAND]:

Describe available commands or one specific command

* import:

Import an ACCOUNT to the keyring from ~/.aws/credentials
Import an ACCOUNT to the keyring from ~/.aws/credentials<br>

-r, --no-remote: Do not validate with remote api.

* initialise:

Initialises a new KEYCHAIN
Initialises a new KEYCHAIN<br>

-n, --keychain=KEYCHAIN: Name of KEYCHAIN to initialise.

* json ACCOUNT:

Outputs AWS CLI compatible JSON for an ACCOUNT
Outputs AWS CLI compatible JSON for an ACCOUNT<br>

-n, --no-token: Do not use saved token.

* list:

Prints a list of accounts in the keyring

* list-role:

Prints a list of roles in the keyring
Prints a list of roles in the keyring<br>

-d, --detail, --no-detail: Show more detail.

* remove ACCOUNT:

Expand All @@ -80,11 +108,19 @@ The commands are as follows:

* token ACCOUNT [ROLE] [MFA]:

Create an STS Token from a ROLE or an MFA code
Create an STS Token from a ROLE or an MFA code<br>

-r, --role=ROLE: The ROLE to assume.<br>
-c, --code=CODE: Virtual mfa CODE.<br>
-d, --duration=DURATION: Session DURATION in seconds.

* update ACCOUNT:

Updates an ACCOUNT in the keyring
Updates an ACCOUNT in the keyring<br>

-k, --key=KEY: AWS account key id.<br>
-s, --secret=SECRET: AWS account secret.<br>
-r, --no-remote: Do not validate with remote api.

## ENVIRONMENT

Expand Down Expand Up @@ -132,6 +168,7 @@ Tristan Morgan <tristan.morgan@servian.com> is the maintainer of Awskeyring.

* Tristan [tristanmorgan](https://github.com/tristanmorgan)
* Adam Sir [AzySir](https://github.com/AzySir)
* Vito Giarrusso [thtliife](https://github.com/thtliife)

## LICENSE

Expand Down
8 changes: 8 additions & 0 deletions spec/lib/awskeyring_command_more_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
updated: Time.parse('2011-08-01T22:20:01Z')
)
allow(Awskeyring::Awsapi).to receive(:get_login_url).and_return('login-url')
allow(Process).to receive(:spawn).exactly(1).with('open -a "Safari" "login-url"').and_return(9999)
allow(Process).to receive(:spawn).exactly(1).with('open "login-url"').and_return(9999)
allow(Process).to receive(:wait).exactly(1).with(9999)
allow(Time).to receive(:new).and_return(Time.parse('2011-07-11T19:55:29.611Z'))
Expand All @@ -36,6 +37,13 @@
expect(Process).to have_received(:spawn).exactly(1).with('open "login-url"')
end

it 'opens the AWS Console with a specified Browser' do
described_class.start(%w[console test --browser Safari])
expect(Awskeyring).to have_received(:account_exists).with('test')
expect(Awskeyring).to have_received(:get_valid_creds).with(account: 'test', no_token: false)
expect(Process).to have_received(:spawn).exactly(1).with('open -a "Safari" "login-url"')
end

it 'prints the AWS Console URL' do
expect do
described_class.start(%w[console test --no-open])
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/awskeyring_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
allow(Awskeyring).to receive(:list_role_names_plus)
.and_return(%W[admin\tarn1 minion\tarn2 readonly\tarn3])
allow(Awskeyring).to receive(:list_console_path).and_return(%w[iam cloudformation vpc])
allow(Awskeyring).to receive(:list_browsers).and_return(%w[FireFox Safari])
allow(Awskeyring).to receive(:prefs).and_return('{"awskeyring": "awskeyringtest"}')
end

Expand Down Expand Up @@ -145,6 +146,13 @@
ENV['COMP_LINE'] = nil
end

it 'lists common browsers with autocomplete' do
ENV['COMP_LINE'] = 'awskeyring console servian --browser Sa'
expect { described_class.start(%w[awskeyring Sa --browser]) }
.to output("Safari\n").to_stdout
ENV['COMP_LINE'] = nil
end

it 'lists token names with autocomplete' do
ENV['COMP_LINE'] = 'awskeyring remove-token ser'
expect { described_class.start(%w[awskeyring ser remove-token]) }
Expand Down
Loading

0 comments on commit cfabb8c

Please sign in to comment.