Skip to content

Commit

Permalink
Merge pull request #2 from martellj/feature/streaming-credential-fix
Browse files Browse the repository at this point in the history
Streaming credentials bug fix and documentation changes
  • Loading branch information
dylanroy authored Apr 27, 2018
2 parents da8ddf5 + ea23841 commit 957925c
Show file tree
Hide file tree
Showing 9 changed files with 2,603 additions and 2,240 deletions.
9 changes: 5 additions & 4 deletions Listener.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const googleCloud = require('google-cloud');
const PubSub = require('@google-cloud/pubsub');
const ConfigUtil = require('./config/ConfigUtil');
const fetchCredentials = require('./services/fetchCredentials');
const path = require('path');
Expand All @@ -7,13 +7,14 @@ const os = require('os');
/** Class that allows you to listen to a number of Dow Jones PubSub subscriptions. This is a singleton. */
class Listener {

constructor(accountCredentials) {
constructor(accountCredentials, pubsubClient) {
this.configUtil = new ConfigUtil(accountCredentials);
this.pubsubClient = pubsubClient;
}

initialize(credentials) {
initialize(credentials, pubSub) {
this.projectId = credentials.project_id;
this.pubsubClient = googleCloud.pubsub({
this.pubsubClient = this.pubsubClient || new PubSub({
projectId: this.projectId,
credentials
});
Expand Down
62 changes: 58 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,29 @@ npm install git+https://[email protected]/dowjones/dj-dna-streams-javascript.git --

Alternatively you can simply check out this project from Git.

#### Authentication Options
There are two credential types that can be used.

Option 1. Service Account Id (service_account_id)

Option 2. Client Credentials (user_id, client_id, password)

#### Configuring The App

There are three ways to pass configuration variables to the app. They are listed in increasing order of precendence (for example, if credentials or subscription ID are set via both options 1 and 2, the credentials set in option 2 will be used and those set in option 1 will be ignored).
There are three ways to pass configuration variables to the app. Please note that environment variables (Option 1) will override values provided in the `customerConfig.json` file (Option 2).
They will not override values passed directly to the `Listener` constructor (Option 3).

Option 1. Modify the 'customerConfig.json' file. In this project's root you will find the 'customerConfig.json' file. Add your service account credentials (user_id, client_id, and password) and your subscription ID. Ensure your additions follow the JSON data format conventions.
Option 1. Set environment variables.

or
###### Service Account ID

Option 2. Set environment variables.
**SERVICE_ACCOUNT_ID**
Dow Jones provided Service Account ID.

**SUBSCRIPTION_ID**
This environment variable holds the subscription ID.

###### Client Credentials

**USER_ID**
Dow Jones provided service account user ID.
Expand All @@ -38,6 +51,30 @@ Option 2. Set environment variables.

Note that USER_ID, CLIENT_ID, and PASSWORD will all need to be set in order to pass credentials to the app in this way. If any one of these is not set, any others that are set will be ignored.

Option 2. Modify the 'customerConfig.json' file. In this project's root you will find the 'customerConfig.json' file. Add your credentials and subscription ID. Ensure your additions follow the JSON data format conventions.

###### Service Account Id

```
{
"service_account_id": "<Dow Jones provided Service Account Id>",
"subscription_id": "<Subscription ID returned upon stream creation>"
}
```

###### Client Credentials

```
{
"user_id": "<Dow Jones provided service account user ID>",
"client_id": "<Dow Jones provided service account client ID>",
"password": "<Dow Jones provided service account password>",
"subscription_id": "<Subscription ID returned upon stream creation>"
}
```

or

Option 3: Passing values as function arguments. Specifically you can pass either the service account credentials and/or subscription ID. When you start a listener you can pass the service account crendentials to the Listener constructor as an object with the fields "user_id", "client_id", and "password", like so:

~~~~
Expand All @@ -48,6 +85,13 @@ Option 3: Passing values as function arguments. Specifically you can pass either
};
const listener = new Listener({
/**
Service Account ID
*/
service_account_id: "<YOUR SERVICE ACCOUNT ID HERE>",
/**
Client Credentials
*/
user_id: "<YOUR USER ID HERE>",
client_id: "<YOUR CLIENT ID HERE>",
password: "<YOUR PASSWORD HERE>"
Expand Down Expand Up @@ -93,6 +137,16 @@ Step 1: Build the docker image. Execute the following command line:

Step 2: Run the docker image

######Service Account ID

~~~
docker run -it \
-e SERVICE_ACCOUNT_ID="<your service account ID"> \
-e SUBSCRIPTION_ID="<your subscription ID>" \
dj-dna-streaming-javascript
~~~

######Client Credentials
~~~
docker run -it \
-e USER_ID="<your user ID"> \
Expand Down
8 changes: 6 additions & 2 deletions config/ConfigUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class ConfigUtil {
SERVICE_ACCOUNT_ID: 'SERVICE_ACCOUNT_ID',
SUBSCRIPTION_ID: 'SUBSCRIPTION_ID',
CREDENTIALS_URI: 'CREDENTIALS_URI',
DEFAULT_CREDENTIALS_URI: 'https://api.dowjones.com/alpha/accounts/streaming-credentials'
DEFAULT_CREDENTIALS_URI_API_KEY_AUTH: 'https://api.dowjones.com/alpha/accounts/streaming-credentials',
DEFAULT_CREDENTIALS_URI_CLIENT_AUTH: 'https://api.dowjones.com/dna/accounts/streaming-credentials'
};
}


// depricated: going forward clients should instead use getAccountCredentials
getServiceAccountId() {
if (this.accountId) {
Expand Down Expand Up @@ -86,7 +88,9 @@ class ConfigUtil {
if (process.env[this.Constants.CREDENTIALS_URI]) {
credentialsUri = process.env[this.Constants.CREDENTIALS_URI];
} else {
credentialsUri = this.Constants.DEFAULT_CREDENTIALS_URI;
credentialsUri = this.accountId ?
this.Constants.DEFAULT_CREDENTIALS_URI_API_KEY_AUTH :
this.Constants.DEFAULT_CREDENTIALS_URI_CLIENT_AUTH;
}

return credentialsUri;
Expand Down
1 change: 1 addition & 0 deletions customerConfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"service_account_id": "",
"user_id": "",
"client_id": "",
"password": "",
Expand Down
5 changes: 2 additions & 3 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ const Listener = require('../Listener');
const quietDemo = process.env.QUIET_DEMO;

const onMessageCallback = (msg) => {
let message = JSON.stringify(msg.data);
if (quietDemo === true.toString()) {
message = `${message.substring(0, 50)} ...`;
message = `${msg.data.substring(0, 50)} ...`;
}
console.log(`Received message: ${message}\n`);
console.log(`Received message: ${msg.data}\n`);
};

const listener = new Listener();
Expand Down
Loading

0 comments on commit 957925c

Please sign in to comment.