Webtask script for pulling logs from Auth0 API and pushing them to 3rd party logging API.
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
.gitignore
README.md
contextHelper.js
index.js
logglyScreenshot.png
logsWebtask.js
package.json

README.md

auth0-logs-webtask

This webtask serves as an example of how you can pull logs from the Auth0 API and push them to a third party product. In this example in particular we will be pushing the logs to Loggly.

In order to run the sample you will need:

  • Auth0 credentials: domain, clientId and clientSecret
  • Loggly credentials: customer token

The idea is to schedule the webtask to run periodically. The first time the webtask runs, it will fetch the first available Auth0 logs (up to 200), will push logs to Loggly and will return the id of the last log retrieved. An example of a webtask execution result is:

{
  "lastLogId": "55e47e08fb9377a9748fddd6"
}

The following executions will use the lastLogId returned from the last run as the checkpointId from which to return further logs from.

This is possible because when the webtask is croned you are able to access the latest runs' results from the context:

module.exports = function(context, cb) {
	if (context.body && context.body.results && context.body.results.length > 0){
		var lastRun = context.body.results[0];
		var lastRunStatus = lastRun.statusCode;
		var lastLogId = JSON.parse(lastRun.body).lastLogId; 
	}
	//...
}

Running in a local dev environment

You have to add a .env file in the root folder containing the following settings:

AUTH0_DOMAIN=yourauth0domain
AUTH0_CLIENT_ID=yourAuth0ClientId
AUTH0_CLIENT_SECRET=yourAuth0ClientSecret
LOGGLY_TOKEN=yourLogglyToken

Install dependencies

$ npm install

Change require of auth0 module in logsWebtask.js from

var Auth0 = require("auth0@0.8.2");

to

var Auth0 = require("auth0");

You can run tests with mocha:

$ mocha test

Or execute locally running

$ node index

Deploy as a webtasks with secrets

You first need to setup the webtask-cli wt:

$ npm i -g wt-cli
$ wt init

Don't forget to change back require of auth0 module in logsWebtask.js from

var Auth0 = require("auth0");

to

var Auth0 = require("auth0@0.8.2");

After that, you can create the webtask from a local file:

$ wt create logsWebtask.js \
        --name logs \
        --secret AUTH0_DOMAIN=yourauth0domain \
		--secret AUTH0_CLIENT_ID=yourAuth0ClientId \
		--secret AUTH0_CLIENT_SECRET=yourAuth0ClientSecret \
		--secret LOGGLY_TOKEN=yourLogglyToken

or from a url:

$ wt create https://raw.githubusercontent.com/solepano/auth0-logs-webtask/master/logsWebtask.js \
        --name logs \
        --secret AUTH0_DOMAIN=yourauth0domain \
		--secret AUTH0_CLIENT_ID=yourAuth0ClientId \
		--secret AUTH0_CLIENT_SECRET=yourAuth0ClientSecret \
		--secret LOGGLY_TOKEN=yourLogglyToken

And then run it with:

$ curl 'https://webtask.it.auth0.com/api/run/<tenant>/logs?webtask_no_cache=1'

Schedule the webtask

We can use the wt cron command in order to schedule the webtask (in this example every 2 minutes):

$ wt cron schedule --name logs \
        --secret AUTH0_DOMAIN=yourauth0domain \
		--secret AUTH0_CLIENT_ID=yourAuth0ClientId \
		--secret AUTH0_CLIENT_SECRET=yourAuth0ClientSecret \
		--secret LOGGLY_SUBDOMAIN=yourLogglySubdomain \
		--secret LOGGLY_TOKEN=yourLogglyToken 
		--json \
		"*/2 * * * *" \
		logsWebtask.js

Or, to schedule it from an url:

$ wt cron schedule --name logs \
        --secret AUTH0_DOMAIN=yourauth0domain \
		--secret AUTH0_CLIENT_ID=yourAuth0ClientId \
		--secret AUTH0_CLIENT_SECRET=yourAuth0ClientSecret \
		--secret LOGGLY_SUBDOMAIN=yourLogglySubdomain \
		--secret LOGGLY_TOKEN=yourLogglyToken 
		--json \
		"*/2 * * * *" \
		https://raw.githubusercontent.com/solepano/auth0-logs-webtask/master/logsWebtask.js

You can see the log streaming of the webtask runs with:

$ wt logs

Check logs in Loggly

Next steps

  • Allow to pass 'lastLogId' and 'take' as query string params
  • Secure webtask run endpoint