Skip to content

Commit 80442c9

Browse files
fix(xp-treatment,plugin): Fix treatment service and plugin's method of initiating management service client (#89)
* Fix treatment service and plugin's method of initiating google client * Remove redundant argument
1 parent aca8aa1 commit 80442c9

File tree

5 files changed

+31
-26
lines changed

5 files changed

+31
-26
lines changed

plugins/turing/manager/experiment_manager.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"net/http"
78
"strconv"
89
"time"
910

@@ -167,15 +168,19 @@ func NewExperimentManager(configData json.RawMessage) (manager.CustomExperimentM
167168
}
168169

169170
// Create Google Client
171+
httpClient := http.DefaultClient
170172
googleClient, err := auth.InitGoogleClient(context.Background())
171-
if err != nil {
172-
return nil, err
173+
if err == nil {
174+
googleClient.Timeout = defaultRequestTimeout
175+
httpClient = googleClient
176+
} else {
177+
log.Infof("Google default credential not found. Fallback to HTTP default client")
173178
}
174-
googleClient.Timeout = defaultRequestTimeout
179+
175180
// Create XP client
176181
client, err := xpclient.NewClientWithResponses(
177182
config.BaseURL,
178-
xpclient.WithHTTPClient(googleClient),
183+
xpclient.WithHTTPClient(httpClient),
179184
)
180185
if err != nil {
181186
return nil, fmt.Errorf("Unable to create XP management client: %s", err.Error())

treatment-service/appcontext/appcontext.go

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func NewAppContext(cfg *config.Config) (*AppContext, error) {
3232
localStorage, err := models.NewLocalStorage(
3333
cfg.GetProjectIds(),
3434
cfg.ManagementService.URL,
35-
cfg.ManagementService.AuthorizationEnabled,
3635
cfg.DeploymentConfig.GoogleApplicationCredentialsEnvVar,
3736
)
3837
if err != nil {

treatment-service/appcontext/appcontext_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func TestContext(t *testing.T) {
7878
localStorage, err := models.NewLocalStorage(
7979
testConfig.GetProjectIds(),
8080
testConfig.ManagementService.URL,
81-
testConfig.ManagementService.AuthorizationEnabled,
8281
"",
8382
)
8483
if err != nil {

treatment-service/integration-test/fetch_treatment_it_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ func (suite *TreatmentServiceTestSuite) TestAllFiltersSwitchback() {
807807
}
808808

809809
func (suite *TreatmentServiceTestSuite) TestLocalStorage() {
810-
storage, err := models.NewLocalStorage([]models.ProjectId{1}, suite.managementServiceServer.URL, false, "")
810+
storage, err := models.NewLocalStorage([]models.ProjectId{1}, suite.managementServiceServer.URL, "")
811811
suite.Require().NoError(err)
812812
suite.Require().NotEmpty(storage)
813813

treatment-service/models/storage.go

+21-19
Original file line numberDiff line numberDiff line change
@@ -589,31 +589,33 @@ func (s *LocalStorage) getAllProjects() ([]*pubsub.ProjectSettings, error) {
589589
func NewLocalStorage(
590590
projectIds []ProjectId,
591591
xpServer string,
592-
authzEnabled bool,
593592
googleApplicationCredentialsEnvVar string,
594593
) (*LocalStorage, error) {
595594
// Set up Request Modifiers
596595
clientOptions := []managementClient.ClientOption{}
597-
if authzEnabled {
598-
var googleClient *http.Client
599-
var err error
600-
// Init Google client for Authz. When using a non-empty googleApplicationCredentialsEnvVar that contains a file
601-
// path to a credentials file, the credentials file MUST contain a Google SERVICE ACCOUNT for authentication to
602-
// work correctly
603-
if filepath := os.Getenv(googleApplicationCredentialsEnvVar); filepath != "" {
604-
googleClient, err = auth.InitGoogleClientFromCredentialsFile(context.Background(), filepath)
605-
} else {
606-
googleClient, err = auth.InitGoogleClient(context.Background())
607-
}
608-
if err != nil {
609-
return nil, err
610-
}
611596

612-
clientOptions = append(
613-
clientOptions,
614-
managementClient.WithHTTPClient(googleClient),
615-
)
597+
httpClient := http.DefaultClient
598+
var googleClient *http.Client
599+
var err error
600+
// Init Google client for Authz. When using a non-empty googleApplicationCredentialsEnvVar that contains a file
601+
// path to a credentials file, the credentials file MUST contain a Google SERVICE ACCOUNT for authentication to
602+
// work correctly
603+
if filepath := os.Getenv(googleApplicationCredentialsEnvVar); filepath != "" {
604+
googleClient, err = auth.InitGoogleClientFromCredentialsFile(context.Background(), filepath)
605+
} else {
606+
googleClient, err = auth.InitGoogleClient(context.Background())
616607
}
608+
609+
if err == nil {
610+
httpClient = googleClient
611+
} else {
612+
log.Println("Google default credential not found. Fallback to HTTP default client")
613+
}
614+
615+
clientOptions = append(
616+
clientOptions,
617+
managementClient.WithHTTPClient(httpClient),
618+
)
617619
xpClient, err := managementClient.NewClientWithResponses(xpServer, clientOptions...)
618620
if err != nil {
619621
return nil, err

0 commit comments

Comments
 (0)