Skip to content

Commit 0163d88

Browse files
author
sachin-maheshwari
authored
Merge pull request #42 from topcoder-platform/feature/m2mtoken
Use dynamic m2m token instead static BUS token.
2 parents 7bfd235 + d214d8d commit 0163d88

File tree

6 files changed

+28
-26
lines changed

6 files changed

+28
-26
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ The following parameters can be set in config files or in env variables:
2828
if not provided, then SSL connection is not used, direct insecure connection is used;
2929
if provided, it can be either path to private key file or private key content
3030
- BUS_API_BASE_URL: Bus API url
31-
- BUS_API_AUTH_TOKEN: Bus API auth token
3231
- REPLY_EMAIL_PREFIX: prefix of the genereated reply email address
3332
- REPLY_EMAIL_DOMAIN: email domain
3433
- DEFAULT_REPLY_EMAIL: default reply to email address, for example no-reply@topcoder.com

config/default.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ module.exports = {
2727
KAFKA_CLIENT_CERT_KEY: process.env.KAFKA_CLIENT_CERT_KEY ?
2828
process.env.KAFKA_CLIENT_CERT_KEY.replace('\\n', '\n') : null,
2929

30-
BUS_API_AUTH_TOKEN: process.env.BUS_API_AUTH_TOKEN,
3130
MENTION_EMAIL: process.env.MENTION_EMAIL,
3231
REPLY_EMAIL_PREFIX: process.env.REPLY_EMAIL_PREFIX,
3332
REPLY_EMAIL_DOMAIN: process.env.REPLY_EMAIL_DOMAIN,
@@ -37,7 +36,7 @@ module.exports = {
3736
TC_API_V3_BASE_URL: process.env.TC_API_V3_BASE_URL || 'https://api.topcoder-dev.com/v3',
3837
TC_API_V4_BASE_URL: process.env.TC_API_V4_BASE_URL || 'https://api.topcoder-dev.com/v4',
3938
TC_API_V5_BASE_URL: process.env.TC_API_V5_BASE_URL || 'https://api.topcoder-dev.com/v5',
40-
MESSAGE_API_BASE_URL: process.env.MESSAGE_API_BASE_URL || 'https://api.topcoder-dev.com/v4',
39+
MESSAGE_API_BASE_URL: process.env.MESSAGE_API_BASE_URL || 'https://api.topcoder-dev.com/v5',
4140
ENABLE_EMAILS: process.env.ENABLE_EMAILS || true,
4241
ENABLE_DEV_MODE: process.env.ENABLE_DEV_MODE || true,
4342
DEV_MODE_EMAIL: process.env.DEV_MODE_EMAIL,

connect/service.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
const request = require('superagent');
55
const config = require('config');
66
const _ = require('lodash');
7-
const tcCoreLibAuth = require('tc-core-library-js').auth;
8-
const m2m = tcCoreLibAuth.m2m(config);
97

108
/**
119
* Get project details
@@ -70,7 +68,7 @@ const getRoleMembers = (roleId) => request
7068
*/
7169
const getUsersById = (ids) => {
7270
const query = _.map(ids, (id) => 'userId:' + id).join(' OR ');
73-
return m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
71+
return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
7472
.then((token) => {
7573
if (!token && config.TC_ADMIN_TOKEN) token = config.TC_ADMIN_TOKEN;
7674

@@ -108,7 +106,7 @@ const getUsersById = (ids) => {
108106
*/
109107
const getUsersByHandle = (handles) => {
110108
const query = _.map(handles, (handle) => 'handle:' + handle).join(' OR ');
111-
return m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
109+
return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
112110
.then((token) => {
113111
if (!token && config.TC_ADMIN_TOKEN) token = config.TC_ADMIN_TOKEN;
114112

deploy.sh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ PORT=$(eval "echo \$${ENV}_PORT")
4747

4848
# email notifications config
4949
ENABLE_EMAILS=$(eval "echo \$${ENV}_ENABLE_EMAILS")
50-
BUS_API_AUTH_TOKEN=$(eval "echo \$${ENV}_BUS_API_AUTH_TOKEN")
5150
MENTION_EMAIL=$(eval "echo \$${ENV}_MENTION_EMAIL")
5251
REPLY_EMAIL_PREFIX=$(eval "echo \$${ENV}_REPLY_EMAIL_PREFIX")
5352
REPLY_EMAIL_DOMAIN=$(eval "echo \$${ENV}_REPLY_EMAIL_DOMAIN")
@@ -212,10 +211,6 @@ make_task_def(){
212211
"name": "DEV_MODE_EMAIL",
213212
"value": "%s"
214213
},
215-
{
216-
"name": "BUS_API_AUTH_TOKEN",
217-
"value": "%s"
218-
},
219214
{
220215
"name": "LOG_LEVEL",
221216
"value": "%s"
@@ -271,7 +266,7 @@ make_task_def(){
271266
}
272267
]'
273268

274-
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)
269+
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)
275270
}
276271

277272
register_definition() {

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
const config = require('config');
77
const _ = require('lodash');
88
const errors = require('./src/common/errors');
9+
const tcCoreLibAuth = require('tc-core-library-js').auth;
10+
global.M2m = tcCoreLibAuth.m2m(config);
911

1012
// key is topic name, e.g. 'notifications.connect.project.created';
1113
// value is handler for the topic to find user ids that should receive notifications for a message,

src/services/BusAPI.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,28 @@ const _ = require('lodash');
99
*
1010
* @return {Promise} promise resolved to post event
1111
*/
12-
const postEvent = (event) => request
13-
.post(`${config.TC_API_V5_BASE_URL}/bus/events`)
14-
.set('Content-Type', 'application/json')
15-
.set('Authorization', `Bearer ${config.BUS_API_AUTH_TOKEN}`)
16-
.send(event)
17-
.then(() => '')
18-
.catch((err) => {
19-
const errorDetails = _.get(err, 'message');
20-
throw new Error(
21-
`Failed to post event ${event}.` +
22-
(errorDetails ? ' Server response: ' + errorDetails : '')
23-
);
24-
});
12+
const postEvent = (event) => {
13+
return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
14+
.then((token) => {
15+
return request
16+
.post(`${config.TC_API_V5_BASE_URL}/bus/events`)
17+
.set('Content-Type', 'application/json')
18+
.set('Authorization', `Bearer ${token}`)
19+
.send(event)
20+
.then(() => '')
21+
.catch((err) => {
22+
const errorDetails = _.get(err, 'message');
23+
throw new Error(
24+
`Failed to post event ${event}.` +
25+
(errorDetails ? ' Server response: ' + errorDetails : '')
26+
);
27+
});
28+
})
29+
.catch((err) => {
30+
err.message = 'Error generating m2m token: ' + err.message;
31+
throw err;
32+
});
33+
}
2534

2635
module.exports = {
2736
postEvent,

0 commit comments

Comments
 (0)