Skip to content

Commit

Permalink
fix: Authentication and Logs (#244)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Node.js version 10 or later is required (dropped support for v6 and v8)
  • Loading branch information
rothso committed Dec 30, 2020
1 parent be817a2 commit 8a34b88
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 69 deletions.
19 changes: 9 additions & 10 deletions logs/lib/retrieveLogs.js
Expand Up @@ -12,14 +12,13 @@ module.exports = {

getLogs() {
const project = this.serverless.service.provider.project;
const region = this.options.region;
let func = this.options.function;
const pageSize = this.options.count || 10;

func = getGoogleCloudFunctionName(this.serverless.service.functions, func);

return this.provider.request('logging', 'entries', 'list', {
filter: `Function execution ${func} ${region}`,
filter: `resource.labels.function_name="${func}" AND NOT textPayload=""`,
orderBy: 'timestamp desc',
resourceNames: [`projects/${project}`],
pageSize,
Expand All @@ -29,7 +28,6 @@ module.exports = {
printLogs(logs) {
if (!logs.entries || !logs.entries.length) {
logs = {
//eslint-disable-line
entries: [
{
timestamp: new Date().toISOString().slice(0, 10),
Expand All @@ -39,10 +37,13 @@ module.exports = {
};
}

let output = logs.entries.reduce(
(p, c) => (p += `${chalk.grey(`${c.timestamp}:`)} ${c.textPayload}\n`),
''
); //eslint-disable-line
let output = logs.entries.reduce((p, c) => {
let message = '';
message += `${chalk.grey(`${c.timestamp}:`)}\t`;
message += c.labels && c.labels.execution_id ? `[${c.labels.execution_id}]\t` : '';
message += `${c.textPayload}\n`;
return p + message;
}, '');

output = `Displaying the ${logs.entries.length} most recent log(s):\n\n${output}`; // prettify output
output = output.slice(0, output.length - 1); // remove "\n---\n\n" for the last log entry
Expand All @@ -53,7 +54,6 @@ module.exports = {
},
};

// retrieve the functions name (Google uses our handler property as the function name)
const getGoogleCloudFunctionName = (serviceFunctions, func) => {
if (!serviceFunctions[func]) {
const errorMessage = [
Expand All @@ -62,6 +62,5 @@ const getGoogleCloudFunctionName = (serviceFunctions, func) => {
].join('');
throw new Error(errorMessage);
}

return serviceFunctions[func].handler;
return serviceFunctions[func].name;
};
23 changes: 16 additions & 7 deletions logs/lib/retrieveLogs.test.js
Expand Up @@ -23,6 +23,7 @@ describe('RetrieveLogs', () => {
serverless.service.functions = {
func1: {
handler: 'foo',
name: 'full-function-name',
},
};
serverless.setProvider('google', new GoogleProvider(serverless));
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('RetrieveLogs', () => {
return googleLogs.getLogs().then(() => {
expect(
requestStub.calledWithExactly('logging', 'entries', 'list', {
filter: 'Function execution foo us-central1',
filter: 'resource.labels.function_name="full-function-name" AND NOT textPayload=""',
orderBy: 'timestamp desc',
resourceNames: ['projects/my-project'],
pageSize: 10,
Expand All @@ -87,7 +88,7 @@ describe('RetrieveLogs', () => {
return googleLogs.getLogs().then(() => {
expect(
requestStub.calledWithExactly('logging', 'entries', 'list', {
filter: 'Function execution foo us-central1',
filter: 'resource.labels.function_name="full-function-name" AND NOT textPayload=""',
orderBy: 'timestamp desc',
resourceNames: ['projects/my-project'],
pageSize: googleLogs.options.count,
Expand Down Expand Up @@ -117,13 +118,21 @@ describe('RetrieveLogs', () => {
it('should print the received execution result log on the console', () => {
const logs = {
entries: [
{ timestamp: '1970-01-01 00:00', textPayload: 'Entry 1' },
{ timestamp: '1970-01-01 00:01', textPayload: 'Entry 2' },
{
timestamp: '1970-01-01 00:00',
textPayload: 'Entry 1',
labels: { execution_id: 'foo' },
},
{
timestamp: '1970-01-01 00:01',
textPayload: 'Entry 2',
labels: { execution_id: 'bar' },
},
],
};

const logEntry1 = `${chalk.grey('1970-01-01 00:00:')} Entry 1`;
const logEntry2 = `${chalk.grey('1970-01-01 00:01:')} Entry 2`;
const logEntry1 = `${chalk.grey('1970-01-01 00:00:')}\t[foo]\tEntry 1`;
const logEntry2 = `${chalk.grey('1970-01-01 00:01:')}\t[bar]\tEntry 2`;
const expectedOutput = `Displaying the 2 most recent log(s):\n\n${logEntry1}\n${logEntry2}`;

return googleLogs.printLogs(logs).then(() => {
Expand All @@ -133,7 +142,7 @@ describe('RetrieveLogs', () => {

it('should print a default message to the console when no logs were received', () => {
const date = `${new Date().toISOString().slice(0, 10)}:`;
const logEntry = `${chalk.grey(date)} There is no log data to show...`;
const logEntry = `${chalk.grey(date)}\tThere is no log data to show...`;
const expectedOutput = `Displaying the 1 most recent log(s):\n\n${logEntry}`;

return googleLogs.printLogs({}).then(() => {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -74,5 +74,8 @@
"prettify:updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier --write",
"test": "jest"
},
"engines": {
"node": ">=10.0"
},
"license": "MIT"
}
4 changes: 2 additions & 2 deletions package/lib/compileFunctions.js
Expand Up @@ -43,7 +43,7 @@ module.exports = {
funcTemplate.properties.runtime =
_.get(funcObject, 'runtime') ||
_.get(this, 'serverless.service.provider.runtime') ||
'nodejs8';
'nodejs10';
funcTemplate.properties.timeout =
_.get(funcObject, 'timeout') || _.get(this, 'serverless.service.provider.timeout') || '60s';
funcTemplate.properties.environmentVariables = _.merge(
Expand Down Expand Up @@ -165,7 +165,7 @@ const getFunctionTemplate = (funcObject, projectName, region, sourceArchiveUrl)
properties: {
parent: `projects/${projectName}/locations/${region}`,
availableMemoryMb: 256,
runtime: 'nodejs8',
runtime: 'nodejs10',
timeout: '60s',
entryPoint: funcObject.handler,
function: funcObject.name,
Expand Down
44 changes: 22 additions & 22 deletions package/lib/compileFunctions.test.js
Expand Up @@ -98,7 +98,7 @@ describe('CompileFunctions', () => {
func1: {
handler: 'func1',
memorySize: 1024,
runtime: 'nodejs8',
runtime: 'nodejs10',
events: [{ http: 'foo' }],
},
};
Expand All @@ -109,7 +109,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 1024,
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 1024,
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -220,7 +220,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -300,7 +300,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -345,7 +345,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -387,7 +387,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -429,7 +429,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -476,7 +476,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -521,7 +521,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -576,7 +576,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand All @@ -595,7 +595,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func2',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func2',
entryPoint: 'func2',
availableMemoryMb: 256,
Expand Down Expand Up @@ -623,7 +623,7 @@ describe('CompileFunctions', () => {
func1: {
handler: 'func1',
memorySize: 128,
runtime: 'nodejs8',
runtime: 'nodejs10',
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
events: [{ http: 'foo' }],
},
Expand All @@ -635,7 +635,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 128,
Expand Down Expand Up @@ -663,7 +663,7 @@ describe('CompileFunctions', () => {
func1: {
handler: 'func1',
memorySize: 128,
runtime: 'nodejs8',
runtime: 'nodejs10',
maxInstances: 10,
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
events: [{ http: 'foo' }],
Expand All @@ -676,7 +676,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 128,
Expand Down Expand Up @@ -705,14 +705,14 @@ describe('CompileFunctions', () => {
func1: {
handler: 'func1',
memorySize: 128,
runtime: 'nodejs8',
runtime: 'nodejs10',
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
events: [{ http: 'foo' }],
},
func2: {
handler: 'func2',
memorySize: 128,
runtime: 'nodejs8',
runtime: 'nodejs10',
maxInstances: 10,
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
events: [{ http: 'bar' }],
Expand All @@ -725,7 +725,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 128,
Expand All @@ -743,7 +743,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func2',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func2',
entryPoint: 'func2',
availableMemoryMb: 128,
Expand Down
32 changes: 17 additions & 15 deletions provider/googleProvider.js
@@ -1,7 +1,6 @@
'use strict';

const path = require('path');
const fs = require('fs');
const os = require('os');

const _ = require('lodash');
Expand Down Expand Up @@ -77,7 +76,7 @@ class GoogleProvider {

const authClient = this.getAuthClient();

return authClient.authorize().then(() => {
return authClient.getClient().then(() => {
const requestParams = { auth: authClient };

// merge the params from the request call into the base functionParams
Expand Down Expand Up @@ -105,21 +104,24 @@ class GoogleProvider {
}

getAuthClient() {
let credentials =
this.serverless.service.provider.credentials || process.env.GOOGLE_APPLICATION_CREDENTIALS;
const credParts = credentials.split(path.sep);

if (credParts[0] === '~') {
credParts[0] = os.homedir();
credentials = credParts.reduce((memo, part) => path.join(memo, part), '');
let credentials = this.serverless.service.provider.credentials;

if (credentials) {
const credParts = this.serverless.service.provider.credentials.split(path.sep);
if (credParts[0] === '~') {
credParts[0] = os.homedir();
credentials = credParts.reduce((memo, part) => path.join(memo, part), '');
}

return new google.auth.GoogleAuth({
keyFile: credentials.toString(),
scopes: 'https://www.googleapis.com/auth/cloud-platform',
});
}

const keyFileContent = fs.readFileSync(credentials).toString();
const key = JSON.parse(keyFileContent);

return new google.auth.JWT(key.client_email, null, key.private_key, [
'https://www.googleapis.com/auth/cloud-platform',
]);
return new google.auth.GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform',
});
}

isServiceSupported(service) {
Expand Down

0 comments on commit 8a34b88

Please sign in to comment.