Skip to content

Commit 6f7f005

Browse files
committed
try to no break existing code
1 parent c40ab2f commit 6f7f005

File tree

8 files changed

+70
-28
lines changed

8 files changed

+70
-28
lines changed

core/auth/auth.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,27 @@ 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,
108116
}
109117
}
110118

111-
if err := noAuthRoundTripper.Init(noAuthConfig, cfg.HTTPClient.Transport); err != nil {
119+
noAuthConfig.HTTPTransport = cfg.HTTPClient.Transport
120+
121+
if err := noAuthRoundTripper.Init(noAuthConfig); err != nil {
112122
return nil, fmt.Errorf("initializing client: %w", err)
113123
}
114124
return noAuthRoundTripper, nil
@@ -143,8 +153,10 @@ func TokenAuth(cfg *config.Configuration) (http.RoundTripper, error) {
143153
}
144154
}
145155

156+
tokenCfg.HTTPTransport = cfg.HTTPClient.Transport
157+
146158
client := &clients.TokenFlow{}
147-
if err := client.Init(&tokenCfg, cfg.HTTPClient.Transport); err != nil {
159+
if err := client.Init(&tokenCfg); err != nil {
148160
return nil, fmt.Errorf("error initializing client: %w", err)
149161
}
150162

@@ -206,8 +218,10 @@ func KeyAuth(cfg *config.Configuration) (http.RoundTripper, error) {
206218
}
207219
}
208220

221+
keyCfg.HTTPTransport = cfg.HTTPClient.Transport
222+
209223
client := &clients.KeyFlow{}
210-
if err := client.Init(&keyCfg, cfg.HTTPClient.Transport); err != nil {
224+
if err := client.Init(&keyCfg); err != nil {
211225
return nil, fmt.Errorf("error initializing client: %w", err)
212226
}
213227

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: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type KeyFlowConfig struct {
5353
ClientRetry *RetryConfig
5454
TokenUrl string
5555
BackgroundTokenRefreshContext context.Context // Functionality is enabled if this isn't nil
56+
HTTPTransport http.RoundTripper
57+
AuthHTTPClient *http.Client
5658
}
5759

5860
// TokenResponseBody is the API response
@@ -116,7 +118,7 @@ func (c *KeyFlow) GetToken() TokenResponseBody {
116118
return *c.token
117119
}
118120

119-
func (c *KeyFlow) Init(cfg *KeyFlowConfig, rt http.RoundTripper) error {
121+
func (c *KeyFlow) Init(cfg *KeyFlowConfig) error {
120122
// No concurrency at this point, so no mutex check needed
121123
c.token = &TokenResponseBody{}
122124
c.config = cfg
@@ -125,14 +127,15 @@ func (c *KeyFlow) Init(cfg *KeyFlowConfig, rt http.RoundTripper) error {
125127
c.config.TokenUrl = tokenAPI
126128
}
127129

128-
if rt == nil {
129-
rt = http.DefaultTransport
130+
if c.rt = cfg.HTTPTransport; c.rt == nil {
131+
c.rt = http.DefaultTransport
130132
}
131133

132-
c.rt = rt
133-
c.authClient = &http.Client{
134-
Transport: rt,
135-
Timeout: DefaultClientTimeout,
134+
if c.authClient = cfg.AuthHTTPClient; cfg.AuthHTTPClient == nil {
135+
c.authClient = &http.Client{
136+
Transport: c.rt,
137+
Timeout: DefaultClientTimeout,
138+
}
136139
}
137140

138141
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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ type NoAuthFlow struct {
1313
// NoAuthFlowConfig holds the configuration for the unauthenticated flow
1414
type NoAuthFlowConfig struct {
1515
// Deprecated: retry options were removed to reduce complexity of the client. If this functionality is needed, you can provide your own custom HTTP client.
16-
ClientRetry *RetryConfig
16+
ClientRetry *RetryConfig
17+
HTTPTransport http.RoundTripper
1718
}
1819

1920
// GetConfig returns the flow configuration
@@ -24,19 +25,17 @@ func (c *NoAuthFlow) GetConfig() NoAuthFlowConfig {
2425
return *c.config
2526
}
2627

27-
func (c *NoAuthFlow) Init(_ NoAuthFlowConfig, rt http.RoundTripper) error {
28+
func (c *NoAuthFlow) Init(cfg NoAuthFlowConfig) error {
2829
c.config = &NoAuthFlowConfig{}
2930

30-
if rt == nil {
31-
rt = http.DefaultTransport
31+
if c.rt = cfg.HTTPTransport; c.rt == nil {
32+
c.rt = http.DefaultTransport
3233
}
3334

34-
c.rt = rt
35-
3635
return nil
3736
}
3837

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

core/clients/no_auth_flow_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ func TestNoAuthFlow_Init(t *testing.T) {
2121
wantErr bool
2222
}{
2323
{"ok", args{context.Background(), NoAuthFlowConfig{}}, false},
24+
{"with transport", args{context.Background(), NoAuthFlowConfig{HTTPTransport: http.DefaultTransport}}, false},
2425
}
2526
for _, tt := range tests {
2627
t.Run(tt.name, func(t *testing.T) {
2728
c := &NoAuthFlow{}
28-
if err := c.Init(tt.args.cfg, http.DefaultTransport); (err != nil) != tt.wantErr {
29+
if err := c.Init(tt.args.cfg); (err != nil) != tt.wantErr {
2930
t.Errorf("NoAuthFlow.Init() error = %v, wantErr %v", err, tt.wantErr)
3031
}
3132
})

core/clients/token_flow.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ type TokenFlowConfig struct {
2323
ServiceAccountEmail string
2424
ServiceAccountToken string
2525
// Deprecated: retry options were removed to reduce complexity of the client. If this functionality is needed, you can provide your own custom HTTP client.
26-
ClientRetry *RetryConfig
26+
ClientRetry *RetryConfig
27+
HTTPTransport http.RoundTripper
2728
}
2829

2930
// GetConfig returns the flow configuration
@@ -34,15 +35,13 @@ func (c *TokenFlow) GetConfig() TokenFlowConfig {
3435
return *c.config
3536
}
3637

37-
func (c *TokenFlow) Init(cfg *TokenFlowConfig, rt http.RoundTripper) error {
38+
func (c *TokenFlow) Init(cfg *TokenFlowConfig) error {
3839
c.config = cfg
3940

40-
if rt == nil {
41-
rt = http.DefaultTransport
41+
if c.rt = cfg.HTTPTransport; c.rt == nil {
42+
c.rt = http.DefaultTransport
4243
}
4344

44-
c.rt = rt
45-
4645
return c.validate()
4746
}
4847

@@ -54,7 +53,7 @@ func (c *TokenFlow) validate() error {
5453
return nil
5554
}
5655

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

core/clients/token_flow_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func TestTokenFlow_Init(t *testing.T) {
2222
{"ok", args{&TokenFlowConfig{
2323
ServiceAccountToken: "efg",
2424
}}, false},
25+
{"with transport", args{&TokenFlowConfig{
26+
ServiceAccountToken: "efg",
27+
HTTPTransport: http.DefaultTransport,
28+
}}, false},
2529
{"error 1", args{&TokenFlowConfig{
2630
ServiceAccountToken: "",
2731
}}, true},
@@ -35,7 +39,7 @@ func TestTokenFlow_Init(t *testing.T) {
3539
if err != nil {
3640
t.Fatalf("Setting service account token: %s", err)
3741
}
38-
if err := c.Init(tt.args.cfg, http.DefaultTransport); (err != nil) != tt.wantErr {
42+
if err := c.Init(tt.args.cfg); (err != nil) != tt.wantErr {
3943
t.Errorf("TokenFlow.Init() error = %v, wantErr %v", err, tt.wantErr)
4044
}
4145
err = os.Setenv(ServiceAccountToken, b)

0 commit comments

Comments
 (0)