Skip to content

Commit 225fe72

Browse files
committed
fix: add own test container context
fix: update test eirctl tasks feat: add a fallback to the chromium browser but prefer own chrome fix: remove process killer func +semver: feature +semver: FEATURE
1 parent 0816291 commit 225fe72

File tree

7 files changed

+56
-104
lines changed

7 files changed

+56
-104
lines changed

cmd/clear.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func newClearCmd(r *Root) {
2020
cmd := &cobra.Command{
2121
Use: "clear-cache <flags>",
2222
Short: "Clears any stored credentials in the OS secret store",
23+
Long: `Clears any stored credentials in the OS secret store
24+
25+
NB: Occassionally you may encounter a hanging chromium processes if not using own browser binary, you should kill all the instances of the chromium PIDs`,
2326
RunE: func(cmd *cobra.Command, args []string) error {
2427
user, err := user.Current()
2528
if err != nil {

eirctl.yaml

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import:
44
contexts:
55
unit:test:
66
container:
7-
name: docker.io/golang:1-trixie
8-
container_args:
9-
- $PWD/.cache:/root/.cache
10-
shell: bash
7+
name: docker.io/dnitsch/aws-cli-auth-ci:0.2.0
8+
entrypoint: /usr/bin/env
9+
shell: sh
1110
shell_args:
1211
- -c
1312
envfile:
@@ -24,59 +23,21 @@ pipelines:
2423
tasks:
2524
unit:test:
2625
context: unit:test
27-
description: Unit test runner needs a bit of extra care in this case to ensure we have all the dependencies
26+
description: |
27+
Unit test runner needs a bit of extra care in this case to ensure we have all the dependencies
2828
command: |
29-
apt-get update -y
30-
apt-get install -y ca-certificates \
31-
fonts-liberation \
32-
libasound2 \
33-
libatk-bridge2.0-0 \
34-
libatk1.0-0 \
35-
libc6 \
36-
libcairo2 \
37-
libcups2 \
38-
libdbus-1-3 \
39-
libexpat1 \
40-
libfontconfig1 \
41-
libgbm1 \
42-
libgcc1 \
43-
libglib2.0-0 \
44-
libgtk-3-0 \
45-
libnspr4 \
46-
libnss3 \
47-
libpango-1.0-0 \
48-
libpangocairo-1.0-0 \
49-
libstdc++6 \
50-
libx11-6 \
51-
libx11-xcb1 \
52-
libxcb1 \
53-
libxcomposite1 \
54-
libxcursor1 \
55-
libxdamage1 \
56-
libxext6 \
57-
libxfixes3 \
58-
libxi6 \
59-
libxrandr2 \
60-
libxrender1 \
61-
libxss1 \
62-
libxtst6 \
63-
lsb-release \
64-
wget \
65-
xdg-utils
66-
mkdir -p .coverage
67-
export GOPATH=$PWD/.deps
68-
go test $(go list ./... | grep -v /local/) -v -coverpkg=./... -race -mod=readonly -timeout=1m -shuffle=on -buildvcs=false -coverprofile=.coverage/out -count=1 -run=$GO_TEST_RUN_ARGS | tee .coverage/test.out
69-
cat .coverage/test.out | go-junit-report &gt; .coverage/report-junit.xml
70-
gocov convert .coverage/out | gocov-xml &gt; .coverage/report-cobertura.xml
29+
export GOPATH=$PWD/.deps GOBIN=$PWD/.deps/bin
30+
CGO_ENABLED=1 go test $(go list ./... | grep -v /local/) -v -coverpkg=./... -race -mod=readonly -timeout=1m -shuffle=on -buildvcs=false -coverprofile=.coverage/out -count=1 -run=$GO_TEST_RUN_ARGS | tee .coverage/test.out
31+
cat .coverage/test.out | .deps/bin/go-junit-report > .coverage/report-junit.xml
32+
.deps/bin/gocov convert .coverage/out | .deps/bin/gocov-xml > .coverage/report-cobertura.xml
7133
7234
unit:test:prereqs:
7335
description: Installs coverage and junit tools
7436
context: unit:test
7537
command:
7638
- |
7739
mkdir -p .coverage
78-
export GOPATH=$PWD/.deps
79-
go install github.com/jstemmer/[email protected] && \
80-
go install github.com/axw/gocov/[email protected] && \
40+
export GOPATH=$PWD/.deps GOBIN=$PWD/.deps/bin
41+
go install github.com/jstemmer/[email protected]
42+
go install github.com/axw/gocov/[email protected]
8143
go install github.com/AlekSi/[email protected]
82-
# -run ^Test_Saml_timeout$ github.com/DevLabFoundry/aws-cli-auth/cmd

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/DevLabFoundry/aws-cli-auth
22

3-
go 1.24.6
3+
go 1.25.1
44

55
require (
66
github.com/aws/aws-sdk-go-v2 v1.38.1

hack/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM docker.io/zenika/alpine-chrome:124 as chrome
2+
3+
USER root
4+
5+
RUN wget https://dl.google.com/go/go1.25.1.linux-arm64.tar.gz && \
6+
tar -C /usr/local -xzf go1.25.1.linux-arm64.tar.gz
7+
ENV PATH=$PATH:/usr/local/go/bin
8+
9+
RUN apk add build-base

internal/cmdutils/cmdutils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func GetCredsWebUI(ctx context.Context, svc credentialexchange.AuthSamlApi, secr
5353
// upon successful auth from the IDP.
5454
// Once credentials are captured they are used in the role assumption process.
5555
func refreshAwsSsoCreds(ctx context.Context, conf credentialexchange.CredentialConfig, secretStore SecretStorageImpl, svc credentialexchange.AuthSamlApi, webConfig *web.WebConfig) error {
56-
webBrowser := web.New(webConfig)
56+
webBrowser := web.New(ctx, webConfig)
5757
capturedCreds, err := webBrowser.GetSSOCredentials(conf)
5858
if err != nil {
5959
return err
@@ -65,7 +65,7 @@ func refreshAwsSsoCreds(ctx context.Context, conf credentialexchange.CredentialC
6565

6666
func refreshSamlCreds(ctx context.Context, conf credentialexchange.CredentialConfig, secretStore SecretStorageImpl, svc credentialexchange.AuthSamlApi, webConfig *web.WebConfig) error {
6767

68-
webBrowser := web.New(webConfig)
68+
webBrowser := web.New(ctx, webConfig)
6969

7070
duration := conf.Duration
7171

internal/web/web.go

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package web
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"net/http"
@@ -12,7 +13,6 @@ import (
1213
"github.com/DevLabFoundry/aws-cli-auth/internal/credentialexchange"
1314
"github.com/go-rod/rod"
1415
"github.com/go-rod/rod/lib/launcher"
15-
ps "github.com/mitchellh/go-ps"
1616
)
1717

1818
var (
@@ -23,10 +23,11 @@ var (
2323
type WebConfig struct {
2424
datadir string
2525
// timeout value in seconds
26-
timeout int32
27-
headless bool
28-
leakless bool
29-
noSandbox bool
26+
timeout int32
27+
headless bool
28+
leakless bool
29+
noSandbox bool
30+
preferChromium bool
3031
}
3132

3233
func NewWebConf(datadir string) *WebConfig {
@@ -59,20 +60,32 @@ type Web struct {
5960
}
6061

6162
// New returns an initialised instance of Web struct
62-
func New(conf *WebConfig) *Web {
63+
func New(ctx context.Context, conf *WebConfig) *Web {
64+
var l *launcher.Launcher
6365

64-
l := launcher.New().
65-
Devtools(false).
66+
if path, found := launcher.LookPath(); (found && path != "") && !conf.preferChromium {
67+
l = launcher.New().Bin(path)
68+
} else {
69+
l = launcher.New()
70+
}
71+
72+
// common set up
73+
l.Devtools(false).
6674
Headless(conf.headless).
6775
UserDataDir(conf.datadir).
6876
NoSandbox(conf.noSandbox).
6977
Leakless(conf.leakless)
7078

7179
url := l.MustLaunch()
7280

73-
browser := rod.New().
81+
browser, cancel := rod.New().
7482
ControlURL(url).
75-
MustConnect().NoDefaultDevice()
83+
MustConnect().NoDefaultDevice().WithCancel()
84+
go func() {
85+
<-ctx.Done()
86+
fmt.Println("cancelled")
87+
cancel()
88+
}()
7689

7790
return &Web{
7891
conf: conf,
@@ -199,10 +212,6 @@ func (web *Web) MustClose() {
199212

200213
func (web *Web) ForceKill(datadir string) error {
201214
errs := []error{}
202-
203-
if err := checkRodProcess(); err != nil {
204-
errs = append(errs, err)
205-
}
206215
// once processes have been killed
207216
// we can remove the datadir
208217
if err := os.RemoveAll(datadir); err != nil {
@@ -214,28 +223,3 @@ func (web *Web) ForceKill(datadir string) error {
214223
}
215224
return nil
216225
}
217-
218-
// checkRodProcess gets a list running process
219-
// kills any hanging rod browser process from any previous improprely closed sessions
220-
func checkRodProcess() error {
221-
pids := make([]int, 0)
222-
ps, err := ps.Processes()
223-
if err != nil {
224-
return err
225-
}
226-
for _, v := range ps {
227-
// grab all chromium processes
228-
// on windows the name will be reported as `chrome.exe`
229-
if strings.Contains(strings.ToLower(v.Executable()), "chrom") {
230-
fmt.Fprintf(os.Stderr, "Found process: (%d) and its parent (%d)\n", v.Pid(), v.PPid())
231-
pids = append(pids, v.Pid())
232-
}
233-
}
234-
for _, pid := range pids {
235-
if proc, _ := os.FindProcess(pid); proc != nil {
236-
fmt.Fprintf(os.Stderr, "Process to be killed as part of clean up: %d\n", pid)
237-
proc.Kill()
238-
}
239-
}
240-
return nil
241-
}

internal/web/web_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package web_test
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"net/http"
@@ -82,7 +83,6 @@ SAMLResponse=dsicisud99u2ubf92e9euhre&RelayState=
8283
}
8384

8485
func Test_WebUI_with_succesful_saml(t *testing.T) {
85-
t.Parallel()
8686

8787
ts := httptest.NewServer(mockIdpHandler(t))
8888
defer ts.Close()
@@ -96,7 +96,7 @@ func Test_WebUI_with_succesful_saml(t *testing.T) {
9696
os.RemoveAll(tempDir)
9797
}()
9898

99-
webUi := web.New(web.NewWebConf(tempDir).WithHeadless().WithTimeout(10).WithNoSandbox())
99+
webUi := web.New(context.TODO(), web.NewWebConf(tempDir).WithHeadless().WithTimeout(10).WithNoSandbox())
100100
saml, err := webUi.GetSamlLogin(conf)
101101
if err != nil {
102102
t.Errorf("expected err to be <nil> got: %s", err)
@@ -107,7 +107,6 @@ func Test_WebUI_with_succesful_saml(t *testing.T) {
107107
}
108108

109109
func Test_WebUI_timeout_and_return_error(t *testing.T) {
110-
t.Parallel()
111110

112111
ts := httptest.NewServer(mockIdpHandler(t))
113112
defer ts.Close()
@@ -121,7 +120,7 @@ func Test_WebUI_timeout_and_return_error(t *testing.T) {
121120
os.RemoveAll(tempDir)
122121
}()
123122

124-
webUi := web.New(web.NewWebConf(tempDir).WithHeadless().WithTimeout(0).WithNoSandbox())
123+
webUi := web.New(context.TODO(), web.NewWebConf(tempDir).WithHeadless().WithTimeout(0).WithNoSandbox())
125124
_, err := webUi.GetSamlLogin(conf)
126125

127126
if !errors.Is(err, web.ErrTimedOut) {
@@ -130,8 +129,6 @@ func Test_WebUI_timeout_and_return_error(t *testing.T) {
130129
}
131130

132131
func Test_ClearCache(t *testing.T) {
133-
// t.Parallel()
134-
135132
ts := httptest.NewServer(mockIdpHandler(t))
136133
defer ts.Close()
137134
tempDir, _ := os.MkdirTemp(os.TempDir(), "web-clear-saml-tester")
@@ -140,7 +137,7 @@ func Test_ClearCache(t *testing.T) {
140137
os.RemoveAll(tempDir)
141138
}()
142139

143-
webUi := web.New(web.NewWebConf(tempDir).WithHeadless().WithTimeout(20).WithNoSandbox())
140+
webUi := web.New(context.TODO(), web.NewWebConf(tempDir).WithHeadless().WithTimeout(20).WithNoSandbox())
144141

145142
if err := webUi.ForceKill(tempDir); err != nil {
146143
t.Errorf("expected <nil>, got: %s", err)
@@ -178,7 +175,6 @@ func mockSsoHandler(t *testing.T) http.Handler {
178175
}
179176

180177
func Test_WebUI_with_succesful_ssoLogin(t *testing.T) {
181-
t.Parallel()
182178

183179
ts := httptest.NewServer(mockSsoHandler(t))
184180
defer ts.Close()
@@ -197,7 +193,7 @@ func Test_WebUI_with_succesful_ssoLogin(t *testing.T) {
197193
os.RemoveAll(tempDir)
198194
}()
199195

200-
webUi := web.New(web.NewWebConf(tempDir).WithHeadless().WithTimeout(10).WithNoSandbox())
196+
webUi := web.New(context.TODO(), web.NewWebConf(tempDir).WithHeadless().WithTimeout(10).WithNoSandbox())
201197
creds, err := webUi.GetSSOCredentials(conf)
202198
if err != nil {
203199
t.Errorf("expected err to be <nil> got: %s", err)
@@ -208,7 +204,6 @@ func Test_WebUI_with_succesful_ssoLogin(t *testing.T) {
208204
}
209205

210206
func Test_WebUI_with_timeout_ssoLogin(t *testing.T) {
211-
t.Parallel()
212207

213208
ts := httptest.NewServer(mockSsoHandler(t))
214209
defer ts.Close()
@@ -227,7 +222,7 @@ func Test_WebUI_with_timeout_ssoLogin(t *testing.T) {
227222
os.RemoveAll(tempDir)
228223
}()
229224

230-
webUi := web.New(web.NewWebConf(tempDir).WithHeadless().WithTimeout(0).WithNoSandbox())
225+
webUi := web.New(context.TODO(), web.NewWebConf(tempDir).WithHeadless().WithTimeout(0).WithNoSandbox())
231226
_, err := webUi.GetSSOCredentials(conf)
232227

233228
if !errors.Is(err, web.ErrTimedOut) {

0 commit comments

Comments
 (0)