Skip to content

Commit e7c0fc4

Browse files
committed
fix: add some logging
1 parent b49d0c7 commit e7c0fc4

10 files changed

Lines changed: 155 additions & 97 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ vendor/
2828
.ignore*
2929
local/
3030
.deps/
31-
.cache/
31+
.cache/
32+
*.env

aws-cli-auth.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@ package main
22

33
import (
44
"context"
5-
"log"
65
"os"
76
"os/signal"
87
"syscall"
8+
"time"
99

1010
"github.com/DevLabFoundry/aws-cli-auth/cmd"
11+
"github.com/rs/zerolog"
1112
)
1213

1314
func main() {
1415
ctx, stop := signal.NotifyContext(context.Background(), []os.Signal{os.Interrupt, syscall.SIGTERM, os.Kill}...)
1516
defer stop()
17+
logger := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).
18+
Level(zerolog.ErrorLevel).
19+
With().Timestamp().
20+
Logger()
1621

1722
go func() {
1823
<-ctx.Done()
1924
stop()
20-
// log.Printf("\x1b[31minterrupted: %s\x1b[0m", ctx.Err())
21-
os.Exit(0)
25+
logger.Fatal().Msgf("\x1b[31minterrupted: %s\x1b[0m", ctx.Err())
2226
}()
2327

24-
c := cmd.New()
28+
c := cmd.New(logger)
2529
c.WithSubCommands(cmd.SubCommands()...)
2630

2731
if err := c.Execute(ctx); err != nil {
28-
log.Fatalf("\x1b[31m%s\x1b[0m", err)
32+
logger.Fatal().Msgf("\x1b[31m%s\x1b[0m", err)
2933
}
3034
}

cmd/awscliauth.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/DevLabFoundry/aws-cli-auth/internal/credentialexchange"
1111
"github.com/Ensono/eirctl/selfupdate"
12+
"github.com/rs/zerolog"
1213
"github.com/spf13/cobra"
1314
)
1415

@@ -23,6 +24,7 @@ type Root struct {
2324
// ChannelErr io.Writer
2425
// viperConf *viper.Viper
2526
rootFlags *RootCmdFlags
27+
logger zerolog.Logger
2628
Datadir string
2729
}
2830

@@ -35,9 +37,10 @@ type RootCmdFlags struct {
3537
CustomIniLocation string
3638
}
3739

38-
func New() *Root {
40+
func New(logger zerolog.Logger) *Root {
3941
rf := &RootCmdFlags{}
4042
r := &Root{
43+
logger: logger,
4144
rootFlags: rf,
4245
Cmd: &cobra.Command{
4346
Use: "aws-cli-auth",

cmd/awscliauth_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import (
1212
"github.com/DevLabFoundry/aws-cli-auth/cmd"
1313
"github.com/DevLabFoundry/aws-cli-auth/internal/credentialexchange"
1414
"github.com/DevLabFoundry/aws-cli-auth/internal/web"
15+
"github.com/rs/zerolog"
1516
)
1617

1718
func cmdHelperExecutor(t *testing.T, args []string) (stdOut *bytes.Buffer, errOut *bytes.Buffer, err error) {
1819
t.Helper()
1920
errOut = new(bytes.Buffer)
2021
stdOut = new(bytes.Buffer)
21-
c := cmd.New()
22+
c := cmd.New(zerolog.New(io.Discard))
2223
c.WithSubCommands(cmd.SubCommands()...)
2324
c.Cmd.SetArgs(args)
2425
c.Cmd.SetErr(errOut)

cmd/saml.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/aws/aws-sdk-go-v2/config"
1515
"github.com/aws/aws-sdk-go-v2/service/sts"
1616
validator "github.com/rezakhademix/govalidator/v2"
17+
"github.com/rs/zerolog"
1718
"github.com/spf13/cobra"
1819
"gopkg.in/ini.v1"
1920
)
@@ -66,17 +67,24 @@ func newSamlCmd(r *Root) {
6667
if err != nil {
6768
return err
6869
}
69-
70+
if r.rootFlags.Verbose {
71+
r.logger = r.logger.Level(zerolog.DebugLevel)
72+
}
73+
r.logger.Debug().Str("CustomIniLocation", r.rootFlags.CustomIniLocation).Msg("if empty using default ~/.aws-cli-auth.ini")
7074
iniFile, err := samlInitConfig(r.rootFlags.CustomIniLocation)
7175
if err != nil {
7276
return err
7377
}
7478

79+
r.logger.Debug().Msgf("iniFile: %+v", iniFile)
80+
7581
conf, err := credentialexchange.LoadCliConfig(iniFile, r.rootFlags.CfgSectionName)
7682
if err != nil {
7783
return err
7884
}
7985

86+
r.logger.Debug().Str("section", r.rootFlags.CfgSectionName).Msgf("loaded section: %+v", conf)
87+
8088
if err := ConfigFromFlags(conf, r.rootFlags, flags, user.Username); err != nil {
8189
return err
8290
}
@@ -97,25 +105,28 @@ func newSamlCmd(r *Root) {
97105
saveRole = allRoles[len(allRoles)-1]
98106
}
99107

108+
r.logger.Debug().Str("saveRole", saveRole).
109+
Str("SsoEndpoint", conf.SsoUserEndpoint).
110+
Str("SsoCredFedEndpoint", conf.SsoCredFedEndpoint).
111+
Msg("")
112+
100113
secretStore, err := credentialexchange.NewSecretStore(saveRole,
101114
fmt.Sprintf("%s-%s", credentialexchange.SELF_NAME, credentialexchange.RoleKeyConverter(saveRole)),
102115
os.TempDir(), user.Username)
103116
if err != nil {
104117
return err
105118
}
106119

107-
// we want to remove any AWS_* env vars that could interfere with the default config
108-
// for _, envVar := range []string{"AWS_PROFILE", "AWS_ACCESS_KEY_ID",
109-
// "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"} {
110-
// os.Unsetenv(envVar)
111-
// }
112-
113-
awsConf, err := config.LoadDefaultConfig(ctx)
120+
cfg, err := config.LoadDefaultConfig(ctx)
114121
if err != nil {
115122
return fmt.Errorf("failed to create session %s, %w", err, ErrUnableToCreateSession)
116123
}
117124

118-
svc := sts.NewFromConfig(awsConf)
125+
if cfg.Region == "" {
126+
return fmt.Errorf("unable to deduce AWS region, AWS_REGION, AWS_DEFAULT_REGION, ~/.aws/config default or profile level region must be set")
127+
}
128+
129+
svc := sts.NewFromConfig(cfg)
119130
webConfig := web.NewWebConf(r.Datadir).
120131
WithTimeout(flags.SamlTimeout).
121132
WithCustomExecutable(conf.BaseConfig.BrowserExecutablePath)
@@ -167,7 +178,7 @@ If this flag is specified the --sso-role must also be specified.`)
167178
// sc.cmd.MarkFlagsRequiredTogether("principal", "role")
168179
// SSO flow for SAML
169180
sc.cmd.MarkFlagsRequiredTogether("is-sso", "sso-role", "sso-region")
170-
sc.cmd.PersistentFlags().Int32VarP(&flags.SamlTimeout, "saml-timeout", "", 120, "Timeout in seconds, before the operation of waiting for a response is cancelled via the chrome driver")
181+
sc.cmd.PersistentFlags().Int32VarP(&flags.SamlTimeout, "saml-timeout", "", 120, "Timeout in seconds, before the operation of waiting for a response is cancelled via CDP (ChromeDeubgProto)")
171182
// Add subcommand to root command
172183
r.Cmd.AddCommand(sc.cmd)
173184
}

eirctl.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import:
2-
- https://raw.githubusercontent.com/Ensono/eirctl/e71dd9d66293e27e70fd0620e63a6d627579c060/shared/build/go/eirctl.yaml
2+
- https://raw.githubusercontent.com/Ensono/eirctl/refs/tags/v0.9.7/shared/build/go/eirctl.yaml
33

44
contexts:
55
unit:test:
@@ -12,14 +12,16 @@ contexts:
1212
- GO
1313

1414
pipelines:
15-
build:
15+
build:
1616
- task: build:unix
1717
- task: build:win
1818
depends_on: build:unix
1919

2020
unit:test:run:
2121
- task: unit:test:prereqs
2222
- task: unit:test
23+
env:
24+
ROOT_PKG_NAME: github.com/DevLabFoundry
2325
depends_on: unit:test:prereqs
2426

2527
bin:release:
@@ -34,7 +36,7 @@ pipelines:
3436

3537
tasks:
3638
tag:
37-
command:
39+
command:
3840
- |
3941
git tag -a ${VERSION} -m "ci tag release" ${REVISION}
4042
git push origin ${VERSION}
@@ -48,6 +50,7 @@ tasks:
4850
description: |
4951
Unit test runner needs a bit of extra care in this case to ensure we have all the dependencies
5052
command: |
53+
unset GOTOOLCHAIN
5154
export GOPATH=$PWD/.deps GOBIN=$PWD/.deps/bin
5255
CGO_ENABLED=1 go test ./... -v -coverpkg=github.com/DevLabFoundry/... -race -mod=readonly -timeout=1m -shuffle=on -buildvcs=false -coverprofile=.coverage/out -count=1 -run=$GO_TEST_RUN_ARGS | tee .coverage/test.out
5356
cat .coverage/test.out | .deps/bin/go-junit-report > .coverage/report-junit.xml
@@ -56,7 +59,7 @@ tasks:
5659
unit:test:prereqs:
5760
description: Installs coverage and junit tools
5861
context: unit:test
59-
command:
62+
command:
6063
- |
6164
mkdir -p .coverage
6265
export GOPATH="${PWD}/.deps" GOBIN="${PWD}/.deps/bin"
@@ -65,13 +68,14 @@ tasks:
6568
go install github.com/AlekSi/gocov-xml@v1.0.0
6669
6770
clean:dir:
68-
command:
71+
command:
6972
- |
7073
rm -rf dist/
7174
7275
build:win:
7376
context: go1x
7477
description: Builds Go binary
78+
reset_context: true
7579
command:
7680
- |
7781
mkdir -p .deps

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ require (
1616
)
1717

1818
require (
19+
github.com/mattn/go-colorable v0.1.14 // indirect
20+
github.com/mattn/go-isatty v0.0.20 // indirect
1921
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
2022
github.com/rivo/uniseg v0.4.7 // indirect
23+
github.com/rs/zerolog v1.34.0 // indirect
2124
github.com/schollz/progressbar/v3 v3.18.0 // indirect
2225
golang.org/x/term v0.37.0 // indirect
2326
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7m
4040
github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY=
4141
github.com/clipperhouse/uax29/v2 v2.2.0 h1:ChwIKnQN3kcZteTXMgb1wztSgaU+ZemkgWdohwgs8tY=
4242
github.com/clipperhouse/uax29/v2 v2.2.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
43+
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
4344
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
4445
github.com/danieljoos/wincred v1.2.3 h1:v7dZC2x32Ut3nEfRH+vhoZGvN72+dQ/snVXo/vMFLdQ=
4546
github.com/danieljoos/wincred v1.2.3/go.mod h1:6qqX0WNrS4RzPZ1tnroDzq9kY3fu1KwE7MRLQK4X0bs=
@@ -51,6 +52,7 @@ github.com/go-rod/rod v0.116.2 h1:A5t2Ky2A+5eD/ZJQr1EfsQSe5rms5Xof/qj296e+ZqA=
5152
github.com/go-rod/rod v0.116.2/go.mod h1:H+CMO9SCNc2TJ2WfrG+pKhITz57uGNYU43qYHh438Mg=
5253
github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=
5354
github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
55+
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
5456
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
5557
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
5658
github.com/gofrs/flock v0.13.0 h1:95JolYOvGMqeH31+FC7D2+uULf6mG61mEZ/A8dRYMzw=
@@ -65,20 +67,27 @@ github.com/invopop/jsonschema v0.13.0 h1:KvpoAJWEjR3uD9Kbm2HWJmqsEaHt8lBUpd0qHcI
6567
github.com/invopop/jsonschema v0.13.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
6668
github.com/mailru/easyjson v0.9.1 h1:LbtsOm5WAswyWbvTEOqhypdPeZzHavpZx96/n553mR8=
6769
github.com/mailru/easyjson v0.9.1/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
70+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
6871
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
6972
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
73+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
74+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
7075
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
7176
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
7277
github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw=
7378
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
7479
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
7580
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
81+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
7682
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7783
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7884
github.com/rezakhademix/govalidator/v2 v2.1.2 h1:qqCIkWC6sWr8zeW9zCkYEJxbZMt/Dn1ASXkGIQe3rDI=
7985
github.com/rezakhademix/govalidator/v2 v2.1.2/go.mod h1:be7JrYM3STiL5jYt1WrQN5ArR8xTov/DvWJ9yXtULj8=
8086
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
8187
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
88+
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
89+
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
90+
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
8291
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
8392
github.com/schollz/progressbar/v3 v3.18.0 h1:uXdoHABRFmNIjUfte/Ex7WtuyVslrw2wVPQmCN62HpA=
8493
github.com/schollz/progressbar/v3 v3.18.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec=
@@ -117,6 +126,9 @@ github.com/zalando/go-keyring v0.2.6 h1:r7Yc3+H+Ux0+M72zacZoItR3UDxeWfKTcabvkI8u
117126
github.com/zalando/go-keyring v0.2.6/go.mod h1:2TCrxYrbUNYfNS/Kgy/LSrkSQzZ5UPVH85RwfczwvcI=
118127
golang.org/x/crypto v0.44.0 h1:A97SsFvM3AIwEEmTBiaxPPTYpDC47w720rdiiUvgoAU=
119128
golang.org/x/crypto v0.44.0/go.mod h1:013i+Nw79BMiQiMsOPcVCB5ZIJbYkerPrGnOa00tvmc=
129+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
130+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
131+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
120132
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
121133
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
122134
golang.org/x/term v0.37.0 h1:8EGAD0qCmHYZg6J17DvsMy9/wJ7/D/4pV/wfnld5lTU=

internal/credentialexchange/config.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package credentialexchange
22

3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"time"
7+
)
8+
39
const (
410
SELF_NAME = "aws-cli-auth"
511
WEB_ID_TOKEN_VAR = "AWS_WEB_IDENTITY_TOKEN_FILE"
@@ -29,3 +35,43 @@ type CredentialConfig struct {
2935
SsoUserEndpoint string `ini:"is-sso-endpoint"`
3036
SsoCredFedEndpoint string
3137
}
38+
39+
// AWSRole aws role attributes
40+
type AWSRoleConfig struct {
41+
RoleARN string
42+
PrincipalARN string
43+
Name string
44+
}
45+
46+
// AWSCredentials is a representation of the returned credential
47+
type AWSCredentials struct {
48+
Version int
49+
AWSAccessKey string `json:"AccessKeyId"`
50+
AWSSecretKey string `json:"SecretAccessKey"`
51+
AWSSessionToken string `json:"SessionToken"`
52+
PrincipalARN string `json:"-"`
53+
Expires time.Time `json:"Expiration"`
54+
}
55+
56+
// roleCreds can be encapsulated in this function
57+
// never used outside of this scope for now
58+
type roleCreds struct {
59+
RoleCreds struct {
60+
AccessKey string `json:"accessKeyId"`
61+
SecretKey string `json:"secretAccessKey"`
62+
SessionToken string `json:"sessionToken"`
63+
Expiration int64 `json:"expiration"`
64+
} `json:"roleCredentials"`
65+
}
66+
67+
func (a *AWSCredentials) FromRoleCredString(cred string) (*AWSCredentials, error) {
68+
rc := &roleCreds{}
69+
if err := json.Unmarshal([]byte(cred), rc); err != nil {
70+
return nil, fmt.Errorf("%s, %w", err, ErrUnmarshalCred)
71+
}
72+
a.AWSAccessKey = rc.RoleCreds.AccessKey
73+
a.AWSSecretKey = rc.RoleCreds.SecretKey
74+
a.AWSSessionToken = rc.RoleCreds.SessionToken
75+
a.Expires = time.UnixMilli(rc.RoleCreds.Expiration)
76+
return a, nil
77+
}

0 commit comments

Comments
 (0)