Skip to content

Commit

Permalink
fix(js/plugins/google-cloud): pass projectId through to GoogleAuth cl…
Browse files Browse the repository at this point in the history
…ient
  • Loading branch information
MichaelDoyle committed Feb 10, 2025
1 parent c728c10 commit 6047fc0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
6 changes: 6 additions & 0 deletions js/plugins/firebase/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import {
} from '@genkit-ai/google-cloud';
export { defineFirestoreRetriever } from './firestoreRetriever.js';

/**
* Enables telemetry export to Firebase Genkit Monitoring, backed by the
* Google Cloud Observability suite.
*
* @param options configuration options
*/
export async function enableFirebaseTelemetry(
options?: GcpTelemetryConfigOptions
) {
Expand Down
42 changes: 28 additions & 14 deletions js/plugins/google-cloud/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
* limitations under the License.
*/
import { logger } from 'genkit/logging';
import { auth, CredentialBody, GoogleAuth } from 'google-auth-library';
import {
auth,
CredentialBody,
GoogleAuth,
GoogleAuthOptions,
} from 'google-auth-library';
import { JSONClient } from 'google-auth-library/build/src/auth/googleauth.js';
import { GcpPrincipal, GcpTelemetryConfig } from './types.js';

/**
Expand All @@ -29,34 +35,42 @@ import { GcpPrincipal, GcpTelemetryConfig } from './types.js';
* searches for credential files in standard locations, before using this
* method.
*
* See also: https://github.com/googleapis/google-auth-library-nodejs?tab=readme-ov-file#loading-credentials-from-environment-variables
* @see https://github.com/googleapis/google-auth-library-nodejs?tab=readme-ov-file#loading-credentials-from-environment-variables
*
* @param projectId if provided, will take precendence over projectId from credential
*/
export async function credentialsFromEnvironment(): Promise<
Partial<GcpTelemetryConfig>
> {
export async function credentialsFromEnvironment(
projectId?: string
): Promise<Partial<GcpTelemetryConfig>> {
const telemetryConfig: Partial<GcpTelemetryConfig> = {};
const authOptions: GoogleAuthOptions<JSONClient> = {};

// provided projectId always takes precedence
if (projectId !== undefined) {
authOptions.projectId = projectId;
}

let authClient: GoogleAuth;
let options: Partial<GcpTelemetryConfig> = {};

if (process.env.GCLOUD_SERVICE_ACCOUNT_CREDS) {
logger.debug('Retrieving credentials from GCLOUD_SERVICE_ACCOUNT_CREDS');
const serviceAccountCreds = JSON.parse(
process.env.GCLOUD_SERVICE_ACCOUNT_CREDS
);
const authOptions = { credentials: serviceAccountCreds };
authOptions.credentials = serviceAccountCreds;
authClient = new GoogleAuth(authOptions);
options.credentials = await authClient.getCredentials();
telemetryConfig.credentials = await authClient.getCredentials();
} else {
authClient = new GoogleAuth();
authClient = new GoogleAuth(authOptions);
}

try {
const projectId = await authClient.getProjectId();
if (projectId && projectId.length > 0) {
options.projectId = projectId;
}
telemetryConfig.projectId = await authClient.getProjectId();
} catch (error) {
logger.warn(error);
}
return options;

return telemetryConfig;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/plugins/google-cloud/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function enableGoogleCloudTelemetry(
async function configureGcpPlugin(
options?: GcpTelemetryConfigOptions
): Promise<GcpTelemetryConfig> {
const envOptions = await credentialsFromEnvironment();
const envOptions = await credentialsFromEnvironment(options?.projectId);
return {
projectId: options?.projectId || envOptions.projectId,
credentials: options?.credentials || envOptions.credentials,
Expand Down

0 comments on commit 6047fc0

Please sign in to comment.