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

Convert CLI handling to use Kong #113

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8ac8c13
Remove unused scope
punmechanic Dec 3, 2024
34e2b8b
Use oauth2 package to generate verifier
punmechanic Dec 3, 2024
1dab84a
Exchange the code for a token inside of the callback
punmechanic Dec 3, 2024
56dff97
Get Kong
punmechanic Nov 12, 2024
f0bfb01
Remove Config and Args from the struct
punmechanic Nov 12, 2024
adc4deb
WIP: convert to Kong
punmechanic Nov 12, 2024
4fd5981
Convert CLI to a struct so we can add hooks to it
punmechanic Nov 12, 2024
cec1a30
Ignore function args
punmechanic Nov 12, 2024
ef83ff4
Add RunContext() because Kong doesn't support binding context well to
punmechanic Nov 12, 2024
fb8504f
Add Config parsing
punmechanic Nov 12, 2024
ac68d5f
Add constants for OIDC Domain, Server Address and Client ID
punmechanic Nov 12, 2024
fd92052
Annotate the get command
punmechanic Nov 12, 2024
781acae
Add version flag
punmechanic Nov 12, 2024
c18db17
Remove rootCmd
punmechanic Nov 12, 2024
25e2bf7
Add account dumping
punmechanic Nov 12, 2024
d6c61d0
Remove Cobra init blocks
punmechanic Nov 12, 2024
ddb4a26
Convert RolesCommand to Kong
punmechanic Nov 12, 2024
d4576ec
Migrate alias
punmechanic Nov 13, 2024
0477eba
Add set commands
punmechanic Nov 13, 2024
6b59dbd
Remove Cobra
punmechanic Nov 13, 2024
e154a57
Add TTL and Time Remaining cmd tags
punmechanic Nov 14, 2024
3be4301
Generate state and verifier inside of the handler
punmechanic Dec 4, 2024
384ffc9
Ensure the server is closed after receiving a request
punmechanic Dec 4, 2024
c5dad36
Use channels to clean up AuthorizationCodeHandler
punmechanic Dec 6, 2024
30cbd3c
Reduce public API surface
punmechanic Dec 6, 2024
18a4424
Add a dedicated Okta package
punmechanic Dec 6, 2024
50f9d4d
Don't access oauth2 context directly
punmechanic Dec 6, 2024
0059623
Use os.UserConfigDir() for KeyConjurer configuration
punmechanic Dec 6, 2024
a735c08
Fallback to UserHomeDir()
punmechanic Dec 6, 2024
1753b20
Fix a bug where malformed ~/.aws/config would prevent loading
punmechanic Dec 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 39 additions & 44 deletions command/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,60 @@ import (
"io"
"net/http"
"net/url"
"os"

"github.com/riotgames/key-conjurer/internal/api"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
)

var (
FlagNoRefresh = "no-refresh"
FlagServerAddress = "server-address"
var ErrSessionExpired = errors.New("session expired")

ErrSessionExpired = errors.New("session expired")
)
type AccountsCommand struct {
Refresh bool `help:"Refresh the list of accounts." default:"true" negatable:""`
ServerAddress string `help:"The address of the account server. This does not usually need to be changed or specified." hidden:"" env:"KEYCONJURER_SERVER_ADDRESS" default:"${server_address}"`
}

func init() {
accountsCmd.Flags().Bool(FlagNoRefresh, false, "Indicate that the account list should not be refreshed when executing this command. This is useful if you're not able to reach the account server.")
accountsCmd.Flags().String(FlagServerAddress, ServerAddress, "The address of the account server. This does not usually need to be changed or specified.")
func (a AccountsCommand) Help() string {
return "Prints and optionally refreshes the list of accounts you have access to."
}

var accountsCmd = &cobra.Command{
Use: "accounts",
Short: "Prints and optionally refreshes the list of accounts you have access to.",
RunE: func(cmd *cobra.Command, args []string) error {
config := ConfigFromCommand(cmd)
stdOut := cmd.OutOrStdout()
noRefresh, _ := cmd.Flags().GetBool(FlagNoRefresh)
loud := !ShouldUseMachineOutput(cmd.Flags())
if noRefresh {
config.DumpAccounts(stdOut, loud)

if loud {
// intentionally uses PrintErrf was a warning
cmd.PrintErrf("--%s was specified - these results may be out of date, and you may not have access to accounts in this list.\n", FlagNoRefresh)
}

return nil
}
func (a AccountsCommand) RunContext(ctx context.Context, globals *Globals, config *Config) error {
loud := isPiped() || globals.Quiet
if !a.Refresh {
config.DumpAccounts(os.Stdout, loud)

serverAddr, _ := cmd.Flags().GetString(FlagServerAddress)
serverAddrURI, err := url.Parse(serverAddr)
if err != nil {
return genericError{
ExitCode: ExitCodeValueError,
Message: fmt.Sprintf("--%s had an invalid value: %s\n", FlagServerAddress, err),
}
if loud {
// intentionally uses Fprintf was a warning
fmt.Fprintf(os.Stderr, "--no-refresh was specified - these results may be out of date, and you may not have access to accounts in this list.\n")
}

if HasTokenExpired(config.Tokens) {
return ErrTokensExpiredOrAbsent
}
return nil
}

accounts, err := refreshAccounts(cmd.Context(), serverAddrURI, config.Tokens)
if err != nil {
return fmt.Errorf("error refreshing accounts: %w", err)
serverAddrURI, err := url.Parse(a.ServerAddress)
if err != nil {
return genericError{
ExitCode: ExitCodeValueError,
Message: fmt.Sprintf("server-address had an invalid value: %s\n", err),
}
}

config.UpdateAccounts(accounts)
config.DumpAccounts(stdOut, loud)
return nil
},
if HasTokenExpired(config.Tokens) {
return ErrTokensExpiredOrAbsent
}

accounts, err := refreshAccounts(ctx, serverAddrURI, config.Tokens)
if err != nil {
return fmt.Errorf("error refreshing accounts: %w", err)
}

config.UpdateAccounts(accounts)
config.DumpAccounts(os.Stdout, loud)
return nil
}

func (a AccountsCommand) Run(globals *Globals, config *Config) error {
return a.RunContext(context.Background(), globals, config)
}

func refreshAccounts(ctx context.Context, serverAddr *url.URL, ts oauth2.TokenSource) ([]Account, error) {
Expand Down
40 changes: 27 additions & 13 deletions command/alias.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package command

import (
"github.com/spf13/cobra"
)
import "context"

var aliasCmd = cobra.Command{
Use: "alias <accountName> <alias>",
Short: "Give an account a nickname.",
Long: "Alias an account to a nickname so you can refer to the account by the nickname.",
Args: cobra.ExactArgs(2),
Example: "keyconjurer alias FooAccount Bar",
Run: func(cmd *cobra.Command, args []string) {
config := ConfigFromCommand(cmd)
config.Alias(args[0], args[1])
}}
type AliasCommand struct {
AccountName string `arg:""`
Alias string `arg:""`
}

func (a AliasCommand) Run(globals *Globals, config *Config) error {
return a.RunContext(context.Background(), globals, config)
}

func (a AliasCommand) RunContext(ctx context.Context, _ *Globals, config *Config) error {
config.Alias(a.AccountName, a.Alias)
return nil
}

type UnaliasCommand struct {
Alias string `arg:""`
}

func (a UnaliasCommand) Run(globals *Globals, config *Config) error {
return a.RunContext(context.Background(), globals, config)
}

func (a UnaliasCommand) RunContext(ctx context.Context, _ *Globals, config *Config) error {
config.Unalias(a.Alias)
return nil
}
26 changes: 0 additions & 26 deletions command/context.go

This file was deleted.

Loading
Loading