From 5384d54bcd476f078af4f10257b814a7871cab1e Mon Sep 17 00:00:00 2001 From: Elena Kolevska Date: Mon, 9 Oct 2023 16:25:09 +0100 Subject: [PATCH] Fixes GCP components failing to authenticate (#3169) Signed-off-by: Elena Kolevska --- pubsub/gcp/pubsub/pubsub.go | 7 +++++-- state/gcp/firestore/firestore.go | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pubsub/gcp/pubsub/pubsub.go b/pubsub/gcp/pubsub/pubsub.go index 43db539871..8d33fd415b 100644 --- a/pubsub/gcp/pubsub/pubsub.go +++ b/pubsub/gcp/pubsub/pubsub.go @@ -124,6 +124,9 @@ func (g *GCPPubSub) getPubSubClient(ctx context.Context, metadata *metadata) (*g var pubsubClient *gcppubsub.Client var err error + // context.Background is used here, as the context used to Dial the + // server in the gRPC DialPool. Callers should always call `Close` on the + // component to ensure all resources are released. if metadata.PrivateKeyID != "" { // TODO: validate that all auth json fields are filled authJSON := &GCPAuthJSON{ @@ -141,7 +144,7 @@ func (g *GCPPubSub) getPubSubClient(ctx context.Context, metadata *metadata) (*g gcpCompatibleJSON, _ := json.Marshal(authJSON) g.logger.Debugf("Using explicit credentials for GCP") clientOptions := option.WithCredentialsJSON(gcpCompatibleJSON) - pubsubClient, err = gcppubsub.NewClient(ctx, metadata.ProjectID, clientOptions) + pubsubClient, err = gcppubsub.NewClient(context.Background(), metadata.ProjectID, clientOptions) if err != nil { return pubsubClient, err } @@ -156,7 +159,7 @@ func (g *GCPPubSub) getPubSubClient(ctx context.Context, metadata *metadata) (*g g.logger.Debugf("setting GCP PubSub Emulator environment variable to 'PUBSUB_EMULATOR_HOST=%s'", metadata.ConnectionEndpoint) os.Setenv("PUBSUB_EMULATOR_HOST", metadata.ConnectionEndpoint) } - pubsubClient, err = gcppubsub.NewClient(ctx, metadata.ProjectID) + pubsubClient, err = gcppubsub.NewClient(context.Background(), metadata.ProjectID) if err != nil { return pubsubClient, err } diff --git a/state/gcp/firestore/firestore.go b/state/gcp/firestore/firestore.go index efda8e7d9c..f36dab4cb2 100644 --- a/state/gcp/firestore/firestore.go +++ b/state/gcp/firestore/firestore.go @@ -211,10 +211,21 @@ func getFirestoreMetadata(meta state.Metadata) (*firestoreMetadata, error) { return &m, nil } +func (f *Firestore) Close() error { + if f.client != nil { + return f.client.Close() + } + + return nil +} + func getGCPClient(ctx context.Context, metadata *firestoreMetadata, l logger.Logger) (*datastore.Client, error) { var gcpClient *datastore.Client var err error + // context.Background is used here, as the context used to Dial the + // server in the gRPC DialPool. Callers should always call `Close` on the + // component to ensure all resources are released. if metadata.PrivateKeyID != "" { var b []byte b, err = json.Marshal(metadata) @@ -223,7 +234,7 @@ func getGCPClient(ctx context.Context, metadata *firestoreMetadata, l logger.Log } opt := option.WithCredentialsJSON(b) - gcpClient, err = datastore.NewClient(ctx, metadata.ProjectID, opt) + gcpClient, err = datastore.NewClient(context.Background(), metadata.ProjectID, opt) if err != nil { return nil, err } @@ -238,7 +249,7 @@ func getGCPClient(ctx context.Context, metadata *firestoreMetadata, l logger.Log l.Debugf("setting GCP Datastore Emulator environment variable to 'DATASTORE_EMULATOR_HOST=%s'", metadata.ConnectionEndpoint) os.Setenv("DATASTORE_EMULATOR_HOST", metadata.ConnectionEndpoint) } - gcpClient, err = datastore.NewClient(ctx, metadata.ProjectID) + gcpClient, err = datastore.NewClient(context.Background(), metadata.ProjectID) if err != nil { return nil, err }