Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package auth

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -130,7 +129,7 @@ func TokenAuth(cfg *config.Configuration) (http.RoundTripper, error) {
}

client := &clients.TokenFlow{}
if err := client.Init(context.Background(), &tokenCfg); err != nil {
if err := client.Init(&tokenCfg); err != nil {
return nil, fmt.Errorf("error initializing client: %w", err)
}

Expand Down
6 changes: 4 additions & 2 deletions core/clients/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ func TestDo(t *testing.T) {
t.Error(err)
return
}
req := &http.Request{
URL: u,
req, err := http.NewRequest(http.MethodGet, u.String(), http.NoBody)
if err != nil {
t.Error(err)
return
}
c := &http.Client{}
if tt.name == "fail 2 - timeout error" && server != nil {
Expand Down
6 changes: 4 additions & 2 deletions core/clients/no_auth_flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ func TestNoAuthFlow_Do(t *testing.T) {
t.Error(err)
return
}
req := &http.Request{
URL: u,
req, err := http.NewRequest(http.MethodGet, u.String(), http.NoBody)
if err != nil {
t.Error(err)
return
}
got, err := c.RoundTrip(req)
if err == nil {
Expand Down
19 changes: 7 additions & 12 deletions core/clients/token_flow.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package clients

import (
"context"
"fmt"
"net/http"

"golang.org/x/oauth2"
)

const (
Expand Down Expand Up @@ -35,9 +32,9 @@ func (c *TokenFlow) GetConfig() TokenFlowConfig {
return *c.config
}

func (c *TokenFlow) Init(ctx context.Context, cfg *TokenFlowConfig) error {
func (c *TokenFlow) Init(cfg *TokenFlowConfig) error {
c.config = cfg
c.configureHTTPClient(ctx)
c.configureHTTPClient()
if c.config.ClientRetry == nil {
c.config.ClientRetry = NewRetryConfig()
}
Expand All @@ -56,13 +53,10 @@ func (c *TokenFlow) Clone() interface{} {
}

// configureHTTPClient configures the HTTP client
func (c *TokenFlow) configureHTTPClient(ctx context.Context) {
sts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.config.ServiceAccountToken},
)
o2nc := oauth2.NewClient(ctx, sts)
o2nc.Timeout = DefaultClientTimeout
c.client = o2nc
func (c *TokenFlow) configureHTTPClient() {
client := &http.Client{}
client.Timeout = DefaultClientTimeout
c.client = client
}

// validate the client is configured well
Expand All @@ -78,5 +72,6 @@ func (c *TokenFlow) RoundTrip(req *http.Request) (*http.Response, error) {
if c.client == nil {
return nil, fmt.Errorf("please run Init()")
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.ServiceAccountToken))
return Do(c.client, req, c.config.ClientRetry)
}
14 changes: 7 additions & 7 deletions core/clients/token_flow_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package clients

import (
"context"
"fmt"
"io"
"net/http"
Expand All @@ -14,18 +13,17 @@ import (

func TestTokenFlow_Init(t *testing.T) {
type args struct {
ctx context.Context
cfg *TokenFlowConfig
}
tests := []struct {
name string
args args
wantErr bool
}{
{"ok", args{context.Background(), &TokenFlowConfig{
{"ok", args{&TokenFlowConfig{
ServiceAccountToken: "efg",
}}, false},
{"error 1", args{context.Background(), &TokenFlowConfig{
{"error 1", args{&TokenFlowConfig{
ServiceAccountToken: "",
}}, true},
}
Expand All @@ -38,7 +36,7 @@ func TestTokenFlow_Init(t *testing.T) {
if err != nil {
t.Fatalf("Setting service account token: %s", err)
}
if err := c.Init(tt.args.ctx, tt.args.cfg); (err != nil) != tt.wantErr {
if err := c.Init(tt.args.cfg); (err != nil) != tt.wantErr {
t.Errorf("TokenFlow.Init() error = %v, wantErr %v", err, tt.wantErr)
}
err = os.Setenv(ServiceAccountToken, b)
Expand Down Expand Up @@ -86,8 +84,10 @@ func TestTokenFlow_Do(t *testing.T) {
t.Error(err)
return
}
req := &http.Request{
URL: u,
req, err := http.NewRequest(http.MethodGet, u.String(), http.NoBody)
if err != nil {
t.Error(err)
return
}
got, err := c.RoundTrip(req)
if err == nil {
Expand Down
8 changes: 0 additions & 8 deletions core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,4 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
golang.org/x/oauth2 v0.17.0
)

require (
github.com/golang/protobuf v1.5.3 // indirect
golang.org/x/net v0.21.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
22 changes: 0 additions & 22 deletions core/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,7 @@ github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqy
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/oauth2 v0.17.0 h1:6m3ZPmLEFdVxKKWnKq4VqZ60gutO35zm+zrAHVmHyDQ=
golang.org/x/oauth2 v0.17.0/go.mod h1:OzPDGQiuQMguemayvdylqddI7qcD9lnSDb+1FiwQ5HA=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=