Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support PEM and p12 when using factory #1120

Merged
merged 3 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,21 @@ export class GoogleAuth {
.on('data', chunk => (s += chunk))
.on('end', () => {
try {
const data = JSON.parse(s);
const r = this._cacheClientFromJSON(data, options);
return resolve(r);
try {
const data = JSON.parse(s);
const r = this._cacheClientFromJSON(data, options);
return resolve(r);
} catch (err) {
// If we failed parsing this.keyFileName, assume that it
// is a PEM or p12 certificate:
if (!this.keyFilename) throw err;
const client = new JWT({
...this.clientOptions,
keyFile: this.keyFilename,
});
this.cachedCredential = client;
return resolve(client);
}
} catch (err) {
return reject(err);
}
Expand Down
49 changes: 49 additions & 0 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as assert from 'assert';
import {describe, it, beforeEach, afterEach} from 'mocha';
import * as child_process from 'child_process';
import * as crypto from 'crypto';
import {CredentialRequest} from '../src/auth/credentials';
import * as fs from 'fs';
import {
BASE_PATH,
Expand Down Expand Up @@ -44,6 +45,8 @@ describe('googleauth', () => {
const instancePath = `${BASE_PATH}/instance`;
const svcAccountPath = `${instancePath}/service-accounts/?recursive=true`;
const API_KEY = 'test-123';
const PEM_PATH = './test/fixtures/private.pem';
const P12_PATH = './test/fixtures/key.p12';
const STUB_PROJECT = 'my-awesome-project';
const ENDPOINT = '/events:report';
const RESPONSE_BODY = 'RESPONSE_BODY';
Expand Down Expand Up @@ -74,6 +77,11 @@ describe('googleauth', () => {
'gcloud',
'application_default_credentials.json'
);
function createGTokenMock(body: CredentialRequest) {
return nock('https://www.googleapis.com')
.post('/oauth2/v4/token')
.reply(200, body);
}

describe('googleauth', () => {
let auth: GoogleAuth;
Expand Down Expand Up @@ -1524,4 +1532,45 @@ describe('googleauth', () => {
.reply(200, {});
}
});

// Allows a client to be instantiated from a certificate,
// See: https://github.com/googleapis/google-auth-library-nodejs/issues/808
it('allows client to be instantiated from PEM key file', async () => {
const auth = new GoogleAuth({
keyFile: PEM_PATH,
clientOptions: {
scopes: 'http://foo',
email: 'foo@serviceaccount.com',
subject: 'bar@subjectaccount.com',
},
});
const jwt = await auth.getClient();
const scope = createGTokenMock({access_token: 'initial-access-token'});
const headers = await jwt.getRequestHeaders();
assert.deepStrictEqual(
headers.Authorization,
'Bearer initial-access-token'
);
scope.done();
assert.strictEqual('http://foo', (jwt as JWT).gtoken!.scope);
});
it('allows client to be instantiated from p12 key file', async () => {
const auth = new GoogleAuth({
keyFile: P12_PATH,
clientOptions: {
scopes: 'http://foo',
email: 'foo@serviceaccount.com',
subject: 'bar@subjectaccount.com',
},
});
const jwt = await auth.getClient();
const scope = createGTokenMock({access_token: 'initial-access-token'});
const headers = await jwt.getRequestHeaders();
assert.deepStrictEqual(
headers.Authorization,
'Bearer initial-access-token'
);
scope.done();
assert.strictEqual('http://foo', (jwt as JWT).gtoken!.scope);
});
});
2 changes: 1 addition & 1 deletion test/test.jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ describe('jwt', () => {
scopes: 'http://foo',
subject: 'bar@subjectaccount.com',
});

const scope = createGTokenMock({access_token: 'initial-access-token'});

jwt.authorize(() => {
scope.done();
assert.strictEqual('http://foo', jwt.gtoken!.scope);
Expand Down