Skip to content

Commit

Permalink
Fix treatment service and plugin's method of initiating google client
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlycoconuts committed Jan 21, 2025
1 parent 649e5f6 commit 97b3a7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
18 changes: 11 additions & 7 deletions plugins/turing/manager/experiment_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"

Expand Down Expand Up @@ -165,17 +166,20 @@ func NewExperimentManager(configData json.RawMessage) (manager.CustomExperimentM
if err != nil {
return nil, fmt.Errorf("failed to create XP experiment manager: %s", err)
}

// Create Google Client
googleClient, err := auth.InitGoogleClient(context.Background())
if err != nil {
return nil, err
httpClient := http.DefaultClient
if config.TreatmentServicePluginConfig.ManagementService.AuthorizationEnabled {
// Create Google Client
httpClient, err = auth.InitGoogleClient(context.Background())
if err != nil {
return nil, err
}
httpClient.Timeout = defaultRequestTimeout
}
googleClient.Timeout = defaultRequestTimeout

// Create XP client
client, err := xpclient.NewClientWithResponses(
config.BaseURL,
xpclient.WithHTTPClient(googleClient),
xpclient.WithHTTPClient(httpClient),
)
if err != nil {
return nil, fmt.Errorf("Unable to create XP management client: %s", err.Error())
Expand Down
15 changes: 7 additions & 8 deletions treatment-service/models/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,26 +566,25 @@ func NewLocalStorage(
) (*LocalStorage, error) {
// Set up Request Modifiers
clientOptions := []managementClient.ClientOption{}
httpClient := http.DefaultClient
if authzEnabled {
var googleClient *http.Client
var err error
// Init Google client for Authz. When using a non-empty googleApplicationCredentialsEnvVar that contains a file
// path to a credentials file, the credentials file MUST contain a Google SERVICE ACCOUNT for authentication to
// work correctly
if filepath := os.Getenv(googleApplicationCredentialsEnvVar); filepath != "" {
googleClient, err = auth.InitGoogleClientFromCredentialsFile(context.Background(), filepath)
httpClient, err = auth.InitGoogleClientFromCredentialsFile(context.Background(), filepath)
} else {
googleClient, err = auth.InitGoogleClient(context.Background())
httpClient, err = auth.InitGoogleClient(context.Background())
}
if err != nil {
return nil, err
}

clientOptions = append(
clientOptions,
managementClient.WithHTTPClient(googleClient),
)
}
clientOptions = append(
clientOptions,
managementClient.WithHTTPClient(httpClient),
)
xpClient, err := managementClient.NewClientWithResponses(xpServer, clientOptions...)
if err != nil {
return nil, err
Expand Down

0 comments on commit 97b3a7e

Please sign in to comment.