Skip to content

Use dynamic m2m token instead static BUS token. #42

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

Merged
merged 1 commit into from
May 7, 2018
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions connect/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
7 changes: 1 addition & 6 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 22 additions & 13 deletions src/services/BusAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down