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 0e5201b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
13 changes: 9 additions & 4 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 @@ -167,15 +168,19 @@ func NewExperimentManager(configData json.RawMessage) (manager.CustomExperimentM
}

// Create Google Client
httpClient := http.DefaultClient
googleClient, err := auth.InitGoogleClient(context.Background())
if err != nil {
return nil, err
if err == nil {
googleClient.Timeout = defaultRequestTimeout
httpClient = googleClient
} else {
log.Infof("Google default credential not found. Fallback to HTTP default client")
}
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
39 changes: 21 additions & 18 deletions treatment-service/models/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,26 +566,29 @@ func NewLocalStorage(
) (*LocalStorage, error) {
// Set up Request Modifiers
clientOptions := []managementClient.ClientOption{}
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)
} else {
googleClient, err = auth.InitGoogleClient(context.Background())
}
if err != nil {
return nil, err
}

clientOptions = append(
clientOptions,
managementClient.WithHTTPClient(googleClient),
)
httpClient := http.DefaultClient
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)
} else {
googleClient, err = auth.InitGoogleClient(context.Background())
}

if err == nil {
httpClient = googleClient
} else {
log.Println("Google default credential not found. Fallback to HTTP default client")
}

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

0 comments on commit 0e5201b

Please sign in to comment.