diff --git a/README.md b/README.md index cbc2a15..73df890 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,6 @@ The following parameters can be set in config files or in env variables: if not provided, then SSL connection is not used, direct insecure connection is used; if provided, it can be either path to private key file or private key content - BUS_API_BASE_URL: Bus API url -- BUS_API_AUTH_TOKEN: Bus API auth token - REPLY_EMAIL_PREFIX: prefix of the genereated reply email address - REPLY_EMAIL_DOMAIN: email domain - DEFAULT_REPLY_EMAIL: default reply to email address, for example no-reply@topcoder.com diff --git a/config/default.js b/config/default.js index f93de3a..da9f5a3 100644 --- a/config/default.js +++ b/config/default.js @@ -27,7 +27,6 @@ module.exports = { KAFKA_CLIENT_CERT_KEY: process.env.KAFKA_CLIENT_CERT_KEY ? process.env.KAFKA_CLIENT_CERT_KEY.replace('\\n', '\n') : null, - BUS_API_AUTH_TOKEN: process.env.BUS_API_AUTH_TOKEN, MENTION_EMAIL: process.env.MENTION_EMAIL, REPLY_EMAIL_PREFIX: process.env.REPLY_EMAIL_PREFIX, REPLY_EMAIL_DOMAIN: process.env.REPLY_EMAIL_DOMAIN, @@ -37,7 +36,7 @@ module.exports = { TC_API_V3_BASE_URL: process.env.TC_API_V3_BASE_URL || 'https://api.topcoder-dev.com/v3', TC_API_V4_BASE_URL: process.env.TC_API_V4_BASE_URL || 'https://api.topcoder-dev.com/v4', TC_API_V5_BASE_URL: process.env.TC_API_V5_BASE_URL || 'https://api.topcoder-dev.com/v5', - MESSAGE_API_BASE_URL: process.env.MESSAGE_API_BASE_URL || 'https://api.topcoder-dev.com/v4', + MESSAGE_API_BASE_URL: process.env.MESSAGE_API_BASE_URL || 'https://api.topcoder-dev.com/v5', ENABLE_EMAILS: process.env.ENABLE_EMAILS || true, ENABLE_DEV_MODE: process.env.ENABLE_DEV_MODE || true, DEV_MODE_EMAIL: process.env.DEV_MODE_EMAIL, diff --git a/connect/service.js b/connect/service.js index 5efe9f3..03fffab 100644 --- a/connect/service.js +++ b/connect/service.js @@ -4,8 +4,6 @@ const request = require('superagent'); const config = require('config'); const _ = require('lodash'); -const tcCoreLibAuth = require('tc-core-library-js').auth; -const m2m = tcCoreLibAuth.m2m(config); /** * Get project details @@ -70,7 +68,7 @@ const getRoleMembers = (roleId) => request */ const getUsersById = (ids) => { const query = _.map(ids, (id) => 'userId:' + id).join(' OR '); - return m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET) + return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET) .then((token) => { if (!token && config.TC_ADMIN_TOKEN) token = config.TC_ADMIN_TOKEN; @@ -108,7 +106,7 @@ const getUsersById = (ids) => { */ const getUsersByHandle = (handles) => { const query = _.map(handles, (handle) => 'handle:' + handle).join(' OR '); - return m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET) + return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET) .then((token) => { if (!token && config.TC_ADMIN_TOKEN) token = config.TC_ADMIN_TOKEN; diff --git a/deploy.sh b/deploy.sh index 63c5aa4..393de4a 100755 --- a/deploy.sh +++ b/deploy.sh @@ -47,7 +47,6 @@ PORT=$(eval "echo \$${ENV}_PORT") # email notifications config ENABLE_EMAILS=$(eval "echo \$${ENV}_ENABLE_EMAILS") -BUS_API_AUTH_TOKEN=$(eval "echo \$${ENV}_BUS_API_AUTH_TOKEN") MENTION_EMAIL=$(eval "echo \$${ENV}_MENTION_EMAIL") REPLY_EMAIL_PREFIX=$(eval "echo \$${ENV}_REPLY_EMAIL_PREFIX") REPLY_EMAIL_DOMAIN=$(eval "echo \$${ENV}_REPLY_EMAIL_DOMAIN") @@ -212,10 +211,6 @@ make_task_def(){ "name": "DEV_MODE_EMAIL", "value": "%s" }, - { - "name": "BUS_API_AUTH_TOKEN", - "value": "%s" - }, { "name": "LOG_LEVEL", "value": "%s" @@ -271,7 +266,7 @@ make_task_def(){ } ]' - task_def=$(printf "$task_template" $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV "$KAFKA_CLIENT_CERT" "$KAFKA_CLIENT_CERT_KEY" $KAFKA_GROUP_ID "$KAFKA_TOPIC_IGNORE_PREFIX" $KAFKA_URL $DATABASE_URL $AUTHSECRET "$AUTHDOMAIN" "$JWKSURI" $TC_API_BASE_URL $TC_API_V3_BASE_URL $TC_API_V4_BASE_URL $TC_API_V5_BASE_URL $MESSAGE_API_BASE_URL $TC_ADMIN_TOKEN $ENABLE_EMAILS $MENTION_EMAIL $REPLY_EMAIL_PREFIX $REPLY_EMAIL_DOMAIN $ENABLE_DEV_MODE $DEV_MODE_EMAIL $BUS_API_AUTH_TOKEN $LOG_LEVEL $VALID_ISSUERS $PORT "$API_CONTEXT_PATH" "$AUTH0_URL" "$AUTH0_AUDIENCE" $AUTH0_CLIENT_ID "$AUTH0_CLIENT_SECRET" $TOKEN_CACHE_TIME $AWS_ECS_CLUSTER $AWS_REGION $AWS_ECS_CLUSTER $ENV) + task_def=$(printf "$task_template" $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV "$KAFKA_CLIENT_CERT" "$KAFKA_CLIENT_CERT_KEY" $KAFKA_GROUP_ID "$KAFKA_TOPIC_IGNORE_PREFIX" $KAFKA_URL $DATABASE_URL $AUTHSECRET "$AUTHDOMAIN" "$JWKSURI" $TC_API_BASE_URL $TC_API_V3_BASE_URL $TC_API_V4_BASE_URL $TC_API_V5_BASE_URL $MESSAGE_API_BASE_URL $TC_ADMIN_TOKEN $ENABLE_EMAILS $MENTION_EMAIL $REPLY_EMAIL_PREFIX $REPLY_EMAIL_DOMAIN $ENABLE_DEV_MODE $DEV_MODE_EMAIL $LOG_LEVEL $VALID_ISSUERS $PORT "$API_CONTEXT_PATH" "$AUTH0_URL" "$AUTH0_AUDIENCE" $AUTH0_CLIENT_ID "$AUTH0_CLIENT_SECRET" $TOKEN_CACHE_TIME $AWS_ECS_CLUSTER $AWS_REGION $AWS_ECS_CLUSTER $ENV) } register_definition() { diff --git a/index.js b/index.js index 94dd81b..926484f 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,8 @@ const config = require('config'); const _ = require('lodash'); const errors = require('./src/common/errors'); +const tcCoreLibAuth = require('tc-core-library-js').auth; +global.M2m = tcCoreLibAuth.m2m(config); // key is topic name, e.g. 'notifications.connect.project.created'; // value is handler for the topic to find user ids that should receive notifications for a message, diff --git a/src/services/BusAPI.js b/src/services/BusAPI.js index a72d24e..e07eecd 100644 --- a/src/services/BusAPI.js +++ b/src/services/BusAPI.js @@ -9,19 +9,28 @@ const _ = require('lodash'); * * @return {Promise} promise resolved to post event */ -const postEvent = (event) => request - .post(`${config.TC_API_V5_BASE_URL}/bus/events`) - .set('Content-Type', 'application/json') - .set('Authorization', `Bearer ${config.BUS_API_AUTH_TOKEN}`) - .send(event) - .then(() => '') - .catch((err) => { - const errorDetails = _.get(err, 'message'); - throw new Error( - `Failed to post event ${event}.` + - (errorDetails ? ' Server response: ' + errorDetails : '') - ); - }); +const postEvent = (event) => { + return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET) + .then((token) => { + return request + .post(`${config.TC_API_V5_BASE_URL}/bus/events`) + .set('Content-Type', 'application/json') + .set('Authorization', `Bearer ${token}`) + .send(event) + .then(() => '') + .catch((err) => { + const errorDetails = _.get(err, 'message'); + throw new Error( + `Failed to post event ${event}.` + + (errorDetails ? ' Server response: ' + errorDetails : '') + ); + }); + }) + .catch((err) => { + err.message = 'Error generating m2m token: ' + err.message; + throw err; + }); +} module.exports = { postEvent,