From d2648f0698c1859e6ca6dbceba406c0ebdbd2b2b Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Wed, 28 Aug 2024 14:52:17 -0700 Subject: [PATCH] Revert "feat: Improve `AuthClient` Compatibility (#1641)" (#1648) * Revert "feat: Improve `AuthClient` Compatibility (#1641)" This reverts commit 4edd33db2670eb081f5744907b879d6669d4fad2. * chore: keep `cheerio` --- gax/src/clientInterface.ts | 4 ++-- gax/src/fallback.ts | 48 ++++++++++++++++++++++++------------- gax/src/grpc.ts | 8 +++---- gax/src/iamService.ts | 4 ++-- gax/src/operationsClient.ts | 4 ++-- gax/test/unit/regapic.ts | 19 +++++++++++++-- 6 files changed, 58 insertions(+), 29 deletions(-) diff --git a/gax/src/clientInterface.ts b/gax/src/clientInterface.ts index 17d671642..810892762 100644 --- a/gax/src/clientInterface.ts +++ b/gax/src/clientInterface.ts @@ -18,7 +18,7 @@ import {GrpcClientOptions, ClientStubOptions} from './grpc'; import * as gax from './gax'; -import {AuthClient, GoogleAuthOptions} from 'google-auth-library'; +import {GoogleAuthOptions} from 'google-auth-library'; import { BundleDescriptor, LongrunningDescriptor, @@ -30,7 +30,7 @@ import * as operationProtos from '../protos/operations'; export interface ClientOptions extends GrpcClientOptions, - GoogleAuthOptions, + GoogleAuthOptions, ClientStubOptions { libName?: string; libVersion?: string; diff --git a/gax/src/fallback.ts b/gax/src/fallback.ts index cfc447a59..e493bbb47 100644 --- a/gax/src/fallback.ts +++ b/gax/src/fallback.ts @@ -20,7 +20,15 @@ import * as protobuf from 'protobufjs'; import * as gax from './gax'; import * as routingHeader from './routingHeader'; import {Status} from './status'; -import {GoogleAuth, AuthClient, AnyAuthClient} from 'google-auth-library'; +import { + GoogleAuth, + OAuth2Client, + Compute, + JWT, + UserRefreshClient, + GoogleAuthOptions, + BaseExternalAccountClient, +} from 'google-auth-library'; import {OperationsClientBuilder} from './operationsClient'; import type {GrpcClientOptions, ClientStubOptions} from './grpc'; import {GaxCall, GRPCCall} from './apitypes'; @@ -37,7 +45,6 @@ import * as IamProtos from '../protos/iam_service'; import * as LocationProtos from '../protos/locations'; import * as operationsProtos from '../protos/operations'; -export {AnyAuthClient as AuthClient}; export {FallbackServiceError}; export {PathTemplate} from './pathTemplate'; export {routingHeader}; @@ -78,8 +85,15 @@ export interface ServiceMethods { [name: string]: protobuf.Method; } +export type AuthClient = + | OAuth2Client + | Compute + | JWT + | UserRefreshClient + | BaseExternalAccountClient; + export class GrpcClient { - auth?: AuthClient | GoogleAuth; + auth?: OAuth2Client | GoogleAuth; authClient?: AuthClient; fallback: boolean; grpcVersion: string; @@ -99,13 +113,13 @@ export class GrpcClient { * gRPC-fallback version of GrpcClient * Implements GrpcClient API for a browser using grpc-fallback protocol (sends serialized protobuf to HTTP/1 $rpc endpoint). * - * @param {Object=} options.auth - An instance of AuthClient to use in browser, or an instance of GoogleAuth from google-auth-library + * @param {Object=} options.auth - An instance of OAuth2Client to use in browser, or an instance of GoogleAuth from google-auth-library * to use in Node.js. Required for browser, optional for Node.js. * @constructor */ constructor( - options: (GrpcClientOptions | {auth: AuthClient}) & { + options: (GrpcClientOptions | {auth: OAuth2Client}) & { /** * Fallback mode to use instead of gRPC. * A string is accepted for compatibility, all non-empty string values enable the HTTP REST fallback. @@ -113,19 +127,19 @@ export class GrpcClient { fallback?: boolean | string; } = {} ) { - if (options.auth) { - this.auth = options.auth; - } else if ('authClient' in options) { - this.auth = options.authClient; - } else if (!isNodeJS()) { - throw new Error( - JSON.stringify(options) + - 'You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments. Provide a `GoogleAuth` or `AuthClient` instance from `google-auth-library`.' - ); + if (!isNodeJS()) { + if (!options.auth) { + throw new Error( + JSON.stringify(options) + + 'You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments. Use OAuth2Client from google-auth-library.' + ); + } + this.auth = options.auth as OAuth2Client; } else { - this.auth = new GoogleAuth(options as GrpcClientOptions); + this.auth = + (options.auth as GoogleAuth) || + new GoogleAuth(options as GoogleAuthOptions); } - this.fallback = options.fallback ? true : false; this.grpcVersion = require('../../package.json').version; this.httpRules = (options as GrpcClientOptions).httpRules; @@ -250,7 +264,7 @@ export class GrpcClient { /** * gRPC-fallback version of createStub - * Creates a gRPC-fallback stub with authentication headers built from supplied AuthClient instance + * Creates a gRPC-fallback stub with authentication headers built from supplied OAuth2Client instance * * @param {function} CreateStub - The constructor function of the stub. * @param {Object} service - A protobufjs Service object (as returned by lookupService) diff --git a/gax/src/grpc.ts b/gax/src/grpc.ts index 375a923a4..f3db7b735 100644 --- a/gax/src/grpc.ts +++ b/gax/src/grpc.ts @@ -17,7 +17,7 @@ import * as grpcProtoLoader from '@grpc/proto-loader'; import {execFile} from 'child_process'; import * as fs from 'fs'; -import {GoogleAuth, GoogleAuthOptions, AuthClient} from 'google-auth-library'; +import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library'; import * as grpc from '@grpc/grpc-js'; import * as os from 'os'; import {join} from 'path'; @@ -44,8 +44,8 @@ const COMMON_PROTO_FILES: string[] = commonProtoFiles.map(file => file.replace(/[/\\]/g, path.sep) ); -export interface GrpcClientOptions extends GoogleAuthOptions { - auth?: GoogleAuth; +export interface GrpcClientOptions extends GoogleAuthOptions { + auth?: GoogleAuth; grpc?: GrpcModule; protoJson?: protobuf.Root; httpRules?: Array; @@ -113,7 +113,7 @@ export class ClientStub extends grpc.Client { } export class GrpcClient { - auth: GoogleAuth; + auth: GoogleAuth; grpc: GrpcModule; grpcVersion: string; fallback: boolean | 'rest' | 'proto'; diff --git a/gax/src/iamService.ts b/gax/src/iamService.ts index 87e096e79..758536775 100644 --- a/gax/src/iamService.ts +++ b/gax/src/iamService.ts @@ -20,7 +20,7 @@ import * as gax from './gax'; import type {GrpcClient, ClientStubOptions} from './grpc'; import type {GrpcClient as FallbackGrpcClient} from './fallback'; import {createApiCall} from './createApiCall'; -import {GoogleAuth, AuthClient} from 'google-auth-library'; +import {GoogleAuth, OAuth2Client} from 'google-auth-library'; import {ProjectIdCallback} from 'google-auth-library/build/src/auth/googleauth'; import * as routingHeader from './routingHeader'; import * as gapicConfig from './iam_policy_service_client_config.json'; @@ -40,7 +40,7 @@ export class IamClient { private _defaults: {[method: string]: gax.CallSettings}; // eslint-disable-next-line @typescript-eslint/no-explicit-any private _protos: any; - auth?: GoogleAuth | AuthClient; + auth?: GoogleAuth | OAuth2Client; descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}}; innerApiCalls: {[name: string]: Function} = {}; iamPolicyStub?: Promise<{[name: string]: Function}>; diff --git a/gax/src/operationsClient.ts b/gax/src/operationsClient.ts index b821b5c01..2941ff436 100644 --- a/gax/src/operationsClient.ts +++ b/gax/src/operationsClient.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {GoogleAuth, AuthClient} from 'google-auth-library'; +import type {GoogleAuth, OAuth2Client} from 'google-auth-library'; import {ProjectIdCallback} from 'google-auth-library/build/src/auth/googleauth'; import type {ClientOptions, Callback} from './clientInterface'; @@ -62,7 +62,7 @@ export const ALL_SCOPES: string[] = []; * @class */ export class OperationsClient { - auth?: GoogleAuth | AuthClient; + auth?: GoogleAuth | OAuth2Client; innerApiCalls: {[name: string]: Function}; descriptor: {[method: string]: PageDescriptor}; operationsStub: Promise<{[method: string]: Function}>; diff --git a/gax/test/unit/regapic.ts b/gax/test/unit/regapic.ts index 0569d21fe..9ebf08019 100644 --- a/gax/test/unit/regapic.ts +++ b/gax/test/unit/regapic.ts @@ -24,12 +24,27 @@ import * as stream from 'stream'; import echoProtoJson = require('../fixtures/echo.json'); import {GrpcClient} from '../../src/fallback'; import * as transcoding from '../../src/transcoding'; -import {PassThroughClient} from 'google-auth-library'; +import {OAuth2Client} from 'google-auth-library'; +import {GrpcClientOptions} from '../../src'; import {StreamArrayParser} from '../../src/streamArrayParser'; +const authClient = { + async getRequestHeaders() { + return {Authorization: 'Bearer SOME_TOKEN'}; + }, +}; + +const authStub = { + async getClient() { + return authClient; + }, +}; + const opts = { - authClient: new PassThroughClient(), + auth: authStub, fallback: 'rest', // enabling REGAPIC +} as unknown as (GrpcClientOptions | {auth: OAuth2Client}) & { + fallback?: boolean | 'rest' | 'proto'; }; describe('REGAPIC', () => {