Skip to content

Commit 845e5b9

Browse files
committed
try to no break existing code
1 parent c40ab2f commit 845e5b9

File tree

8 files changed

+64
-12
lines changed

8 files changed

+64
-12
lines changed

core/auth/auth.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,18 @@ func DefaultAuth(cfg *config.Configuration) (rt http.RoundTripper, err error) {
9898

9999
// NoAuth configures a flow without authentication and returns an http.RoundTripper
100100
// that can be used to make unauthenticated requests
101-
func NoAuth(cfg *config.Configuration) (rt http.RoundTripper, err error) {
101+
func NoAuth(cfgs ...*config.Configuration) (rt http.RoundTripper, err error) {
102102
noAuthConfig := clients.NoAuthFlowConfig{}
103103
noAuthRoundTripper := &clients.NoAuthFlow{}
104104

105+
var cfg *config.Configuration
106+
107+
if len(cfgs) > 0 {
108+
cfg = cfgs[0]
109+
} else {
110+
cfg = &config.Configuration{}
111+
}
112+
105113
if cfg.HTTPClient == nil {
106114
cfg.HTTPClient = &http.Client{
107115
Timeout: clients.DefaultClientTimeout,

core/auth/auth_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,28 @@ func TestKeyAuth(t *testing.T) {
669669
}
670670

671671
func TestNoAuth(t *testing.T) {
672+
for _, test := range []struct {
673+
desc string
674+
}{
675+
{
676+
desc: "valid_case",
677+
},
678+
} {
679+
t.Run(test.desc, func(t *testing.T) {
680+
setTemporaryHome(t) // Get the default authentication client and ensure that it's not nil
681+
authClient, err := NoAuth()
682+
if err != nil {
683+
t.Fatalf("Test returned error on valid test case: %v", err)
684+
}
685+
686+
if authClient == nil {
687+
t.Fatalf("Client returned is nil for valid test case")
688+
}
689+
})
690+
}
691+
}
692+
693+
func TestNoAuthWithConfig(t *testing.T) {
672694
for _, test := range []struct {
673695
desc string
674696
}{

core/clients/key_flow.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type KeyFlowConfig struct {
5353
ClientRetry *RetryConfig
5454
TokenUrl string
5555
BackgroundTokenRefreshContext context.Context // Functionality is enabled if this isn't nil
56+
AuthHTTPClient *http.Client
5657
}
5758

5859
// TokenResponseBody is the API response
@@ -116,7 +117,7 @@ func (c *KeyFlow) GetToken() TokenResponseBody {
116117
return *c.token
117118
}
118119

119-
func (c *KeyFlow) Init(cfg *KeyFlowConfig, rt http.RoundTripper) error {
120+
func (c *KeyFlow) Init(cfg *KeyFlowConfig, rts ...http.RoundTripper) error {
120121
// No concurrency at this point, so no mutex check needed
121122
c.token = &TokenResponseBody{}
122123
c.config = cfg
@@ -125,14 +126,24 @@ func (c *KeyFlow) Init(cfg *KeyFlowConfig, rt http.RoundTripper) error {
125126
c.config.TokenUrl = tokenAPI
126127
}
127128

129+
var rt http.RoundTripper
130+
if len(rts) > 0 {
131+
rt = rts[0]
132+
}
133+
128134
if rt == nil {
129135
rt = http.DefaultTransport
130136
}
131137

132138
c.rt = rt
133-
c.authClient = &http.Client{
134-
Transport: rt,
135-
Timeout: DefaultClientTimeout,
139+
140+
if cfg.AuthHTTPClient != nil {
141+
c.authClient = cfg.AuthHTTPClient
142+
} else {
143+
c.authClient = &http.Client{
144+
Transport: rt,
145+
Timeout: DefaultClientTimeout,
146+
}
136147
}
137148

138149
err := c.validate()

core/clients/key_flow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestKeyFlowInit(t *testing.T) {
114114
}
115115

116116
cfg.ServiceAccountKey = tt.serviceAccountKey
117-
if err := c.Init(cfg, http.DefaultTransport); (err != nil) != tt.wantErr {
117+
if err := c.Init(cfg); (err != nil) != tt.wantErr {
118118
t.Errorf("KeyFlow.Init() error = %v, wantErr %v", err, tt.wantErr)
119119
}
120120
if c.config == nil {

core/clients/no_auth_flow.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ func (c *NoAuthFlow) GetConfig() NoAuthFlowConfig {
2424
return *c.config
2525
}
2626

27-
func (c *NoAuthFlow) Init(_ NoAuthFlowConfig, rt http.RoundTripper) error {
27+
func (c *NoAuthFlow) Init(_ NoAuthFlowConfig, rts ...http.RoundTripper) error {
2828
c.config = &NoAuthFlowConfig{}
2929

30+
var rt http.RoundTripper
31+
if len(rts) > 0 {
32+
rt = rts[0]
33+
}
34+
3035
if rt == nil {
3136
rt = http.DefaultTransport
3237
}
@@ -36,7 +41,7 @@ func (c *NoAuthFlow) Init(_ NoAuthFlowConfig, rt http.RoundTripper) error {
3641
return nil
3742
}
3843

39-
// Roundtrip performs the request
44+
// RoundTrip performs the request
4045
func (c *NoAuthFlow) RoundTrip(req *http.Request) (*http.Response, error) {
4146
if c.rt == nil {
4247
return nil, fmt.Errorf("please run Init()")

core/clients/no_auth_flow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestNoAuthFlow_Init(t *testing.T) {
2525
for _, tt := range tests {
2626
t.Run(tt.name, func(t *testing.T) {
2727
c := &NoAuthFlow{}
28-
if err := c.Init(tt.args.cfg, http.DefaultTransport); (err != nil) != tt.wantErr {
28+
if err := c.Init(tt.args.cfg); (err != nil) != tt.wantErr {
2929
t.Errorf("NoAuthFlow.Init() error = %v, wantErr %v", err, tt.wantErr)
3030
}
3131
})

core/clients/token_flow.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,15 @@ func (c *TokenFlow) GetConfig() TokenFlowConfig {
3434
return *c.config
3535
}
3636

37-
func (c *TokenFlow) Init(cfg *TokenFlowConfig, rt http.RoundTripper) error {
37+
func (c *TokenFlow) Init(cfg *TokenFlowConfig, rts ...http.RoundTripper) error {
3838
c.config = cfg
3939

40+
var rt http.RoundTripper
41+
42+
if len(rts) > 0 {
43+
rt = rts[0]
44+
}
45+
4046
if rt == nil {
4147
rt = http.DefaultTransport
4248
}
@@ -54,7 +60,7 @@ func (c *TokenFlow) validate() error {
5460
return nil
5561
}
5662

57-
// Roundtrip performs the request
63+
// RoundTrip performs the request
5864
func (c *TokenFlow) RoundTrip(req *http.Request) (*http.Response, error) {
5965
if c.rt == nil {
6066
return nil, fmt.Errorf("please run Init()")

core/clients/token_flow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestTokenFlow_Init(t *testing.T) {
3535
if err != nil {
3636
t.Fatalf("Setting service account token: %s", err)
3737
}
38-
if err := c.Init(tt.args.cfg, http.DefaultTransport); (err != nil) != tt.wantErr {
38+
if err := c.Init(tt.args.cfg); (err != nil) != tt.wantErr {
3939
t.Errorf("TokenFlow.Init() error = %v, wantErr %v", err, tt.wantErr)
4040
}
4141
err = os.Setenv(ServiceAccountToken, b)

0 commit comments

Comments
 (0)