Skip to content

Commit fa4915f

Browse files
committed
fix: add conditional kill hanging process
todo: this would be better as a separate command
1 parent a7d04a5 commit fa4915f

File tree

6 files changed

+23
-17
lines changed

6 files changed

+23
-17
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ sudo mv aws-cli-auth /usr/local/bin
3131
## Usage
3232

3333
```bash
34-
CLI tool for retrieving AWS temporary credentials using OIDC or SAML providers.
35-
Stores them under the $HOME/.aws/credentials file under a specified path
34+
CLI tool for retrieving AWS temporary credentials using SAML providers.
35+
Stores them under the $HOME/.aws/credentials file under a specified path or returns the crednetial_process payload for use in config
3636

3737
Usage:
3838
aws-cli-auth [command]
@@ -45,8 +45,9 @@ Available Commands:
4545
Flags:
4646
--cfg-section string config section name in the yaml config file
4747
-h, --help help for aws-cli-auth
48+
-k, --kill-rod If aws-cli-auth exited improprely in a previous run there is a chance that there could be hanging processes left over - this will clean them up forcefully
4849
-r, --role string Set the role you want to assume when SAML or OIDC process completes
49-
-s, --store-profile By default the credentials are returned to stdout to be used by the credential_process
50+
-s, --store-profile By default the credentials are returned to stdout to be used by the credential_process. Set this flag to instead store the credentials under a named profile section
5051

5152
Use "aws-cli-auth [command] --help" for more information about a command.
5253
```

cmd/root.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
)
1111

1212
var (
13-
cfgSectionName string
14-
cfgFile string
15-
storeInProfile bool
16-
rootCmd = &cobra.Command{
13+
cfgSectionName string
14+
cfgFile string
15+
storeInProfile bool
16+
killHangingProcess bool
17+
rootCmd = &cobra.Command{
1718
Use: "aws-cli-auth",
1819
Short: "CLI tool for retrieving AWS temporary credentials using SAML providers",
1920
Long: `CLI tool for retrieving AWS temporary credentials using SAML providers.
@@ -32,6 +33,7 @@ func init() {
3233
rootCmd.PersistentFlags().StringVarP(&role, "role", "r", "", "Set the role you want to assume when SAML or OIDC process completes")
3334
rootCmd.PersistentFlags().StringVarP(&cfgSectionName, "cfg-section", "", "", "config section name in the yaml config file")
3435
rootCmd.PersistentFlags().BoolVarP(&storeInProfile, "store-profile", "s", false, "By default the credentials are returned to stdout to be used by the credential_process. Set this flag to instead store the credentials under a named profile section")
36+
rootCmd.PersistentFlags().BoolVarP(&killHangingProcess, "kill-rod", "k", false, "If aws-cli-auth exited improprely in a previous run there is a chance that there could be hanging processes left over - this will clean them up forcefully")
3537
}
3638

3739
func initConfig() {

cmd/saml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func getSaml(cmd *cobra.Command, args []string) {
3535
PrincipalArn: principalArn,
3636
Duration: duration,
3737
AcsUrl: acsUrl,
38-
BaseConfig: config.BaseConfig{StoreInProfile: storeInProfile, Role: role, CfgSectionName: cfgSectionName},
38+
BaseConfig: config.BaseConfig{StoreInProfile: storeInProfile, Role: role, CfgSectionName: cfgSectionName, DoKillHangingProcess: killHangingProcess},
3939
}
4040

4141
saml.GetSamlCreds(conf)

internal/config/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package config
33
const SELF_NAME = "aws-cli-auth"
44

55
type BaseConfig struct {
6-
Role string
7-
CfgSectionName string
8-
StoreInProfile bool
6+
Role string
7+
CfgSectionName string
8+
StoreInProfile bool
9+
DoKillHangingProcess bool
910
}
1011

1112
type SamlConfig struct {

internal/saml/saml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func GetSamlCreds(conf config.SamlConfig) {
2525

2626
if !util.IsValid(awsCreds) || err != nil {
2727

28-
t, err := web.GetSamlLogin(conf.ProviderUrl, conf.AcsUrl)
28+
t, err := web.GetSamlLogin(conf)
2929
if err != nil {
3030
fmt.Printf("Err: %v", err)
3131
}

internal/web/web.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import (
1717
ps "github.com/mitchellh/go-ps"
1818
)
1919

20-
func GetSamlLogin(loginUrl, acsUrl string) (string, error) {
20+
func GetSamlLogin(conf config.SamlConfig) (string, error) {
2121

22-
checkRodProcess()
22+
if conf.BaseConfig.DoKillHangingProcess {
23+
checkRodProcess()
24+
}
2325

2426
l := launcher.New().
2527
Headless(false).
@@ -37,12 +39,12 @@ func GetSamlLogin(loginUrl, acsUrl string) (string, error) {
3739

3840
defer browser.MustClose()
3941

40-
page := browser.MustPage(loginUrl)
42+
page := browser.MustPage(conf.ProviderUrl)
4143

4244
router := browser.HijackRequests()
4345
defer router.MustStop()
4446

45-
router.MustAdd(acsUrl, func(ctx *rod.Hijack) {
47+
router.MustAdd(conf.AcsUrl, func(ctx *rod.Hijack) {
4648
body := ctx.Request.Body()
4749
_ = ctx.LoadResponse(http.DefaultClient, true)
4850
ctx.Response.SetBody(body)
@@ -51,7 +53,7 @@ func GetSamlLogin(loginUrl, acsUrl string) (string, error) {
5153
go router.Run()
5254

5355
wait := page.EachEvent(func(e *proto.PageFrameRequestedNavigation) (stop bool) {
54-
return e.URL == acsUrl
56+
return e.URL == conf.AcsUrl
5557
})
5658
wait()
5759

0 commit comments

Comments
 (0)