Skip to content

Commit 9f08355

Browse files
author
sachin-maheshwari
authored
Merge pull request #19 from topcoder-platform/feature/deprecateOldFormat
deprecate old bus api format code clean. Need to reimplement the placeholder logic to validate the email template.
2 parents 8a89a98 + 42e5272 commit 9f08355

File tree

5 files changed

+4
-89
lines changed

5 files changed

+4
-89
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ The other configurations can be changed in `config/default.js` or by setting env
8383
- `ALLOWED_SERVICES` the allowed calling services
8484
- `JWT_TOKEN_SECRET` the secret to sign JWT tokens
8585
- `JWT_TOKEN_EXPIRES_IN` the JWT token expiration
86-
- `KAFKA_TOPIC_PREFIX` the prefix of all topics in Kafka
8786
- `TC_EMAIL_URL` the email service URL (http://localhost:4001, if deployed locally)
8887
- `TC_EMAIL_TOKEN` the email service authentication token (see tc-email README for details **link should be added later**)
8988
- `TC_EMAIL_CACHE_PERIOD` the period to cache template placeholders from email service (60 min default)

common/helper.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -126,42 +126,6 @@ function signJwtToken (payload) {
126126
return jwt.sign(payload, config.JWT_TOKEN_SECRET, {expiresIn: config.JWT_TOKEN_EXPIRES_IN})
127127
}
128128

129-
/**
130-
* Validate the event based on the source service, type, and message.
131-
*
132-
* @param {Object} event the event
133-
*/
134-
function validateEvent (event) {
135-
const schema = Joi.object().keys({
136-
event: Joi.object().keys({
137-
type: Joi
138-
.string()
139-
.regex(/^([a-zA-Z0-9]+\.)+[a-zA-Z0-9]+$/)
140-
.error(createError.BadRequest(
141-
'"type" must be a fully qualified name - dot separated string'))
142-
.required(),
143-
message: Joi.string().required()
144-
})
145-
})
146-
147-
const { error } = Joi.validate({event}, schema)
148-
if (error) {
149-
throw error
150-
}
151-
152-
// The message should be a JSON-formatted string
153-
let message
154-
try {
155-
message = JSON.parse(event.message)
156-
} catch (err) {
157-
logger.error(err)
158-
throw createError.BadRequest(
159-
`"message" is not a valid JSON-formatted string: ${err.message}`)
160-
}
161-
162-
return message
163-
}
164-
165129
/**
166130
* Validate the event payload
167131
*
@@ -201,7 +165,6 @@ module.exports = {
201165
buildService,
202166
verifyJwtToken,
203167
signJwtToken,
204-
validateEvent,
205168
validateEventPayload,
206169
verifyTokenScope
207170
}

config/default.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module.exports = {
1010
VALID_ISSUERS: process.env.VALID_ISSUERS ? process.env.VALID_ISSUERS.replace(/\\"/g, '') : null,
1111
JWT_TOKEN_SECRET: process.env.JWT_TOKEN_SECRET || '',
1212
JWT_TOKEN_EXPIRES_IN: process.env.JWT_TOKEN_EXPIRES_IN || '100 days',
13-
KAFKA_TOPIC_PREFIX: process.env.KAFKA_TOPIC_PREFIX || '',
1413
ALLOWED_SERVICES: process.env.ALLOWED_SERVICES || ['project-service', 'message-service'],
1514
TC_EMAIL_SERVICE_URL: process.env.TC_EMAIL_SERVICE_URL,
1615
TC_EMAIL_SERVICE_CACHE_PERIOD: process.env.TC_EMAIL_SERVICE_CACHE_PERIOD || (3600 * 1000),

deploy.sh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ AWS_ECS_CONTAINER_NAME=$(eval "echo \$${ENV}_AWS_ECS_CONTAINER_NAME")
3535

3636
LOG_LEVEL=$(eval "echo \$${ENV}_LOG_LEVEL")
3737
JWT_TOKEN_SECRET=$(eval "echo \$${ENV}_JWT_TOKEN_SECRET")
38-
KAFKA_TOPIC_PREFIX=$(eval "echo \$${ENV}_KAFKA_TOPIC_PREFIX")
3938
API_VERSION=$(eval "echo \$${ENV}_API_VERSION")
4039
ALLOWED_SERVICES=$(eval "echo \$${ENV}_ALLOWED_SERVICES")
4140
JWT_TOKEN_EXPIRES_IN=$(eval "echo \$${ENV}_JWT_TOKEN_EXPIRES_IN")
@@ -124,10 +123,6 @@ make_task_def(){
124123
"name": "JWT_TOKEN_SECRET",
125124
"value": "%s"
126125
},
127-
{
128-
"name": "KAFKA_TOPIC_PREFIX",
129-
"value": "%s"
130-
},
131126
{
132127
"name": "ALLOWED_SERVICES",
133128
"value": "%s"
@@ -195,7 +190,7 @@ make_task_def(){
195190
}
196191
]'
197192

198-
task_def=$(printf "$task_template" $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV $KAFKA_URL "$KAFKA_CLIENT_CERT" "$KAFKA_CLIENT_CERT_KEY" $LOG_LEVEL $JWT_TOKEN_SECRET "$KAFKA_TOPIC_PREFIX" "$ALLOWED_SERVICES" $JWT_TOKEN_EXPIRES_IN "$API_VERSION" $PORT "$AUTH_DOMAIN" "$VALID_ISSUERS" $TC_EMAIL_SERVICE_URL "$AUTH0_URL" "$AUTH0_AUDIENCE" $AUTH0_CLIENT_ID "$AUTH0_CLIENT_SECRET" $TOKEN_CACHE_TIME $AWS_ECS_CLUSTER $AWS_REGION $AWS_ECS_CLUSTER $ENV)
193+
task_def=$(printf "$task_template" $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV $KAFKA_URL "$KAFKA_CLIENT_CERT" "$KAFKA_CLIENT_CERT_KEY" $LOG_LEVEL $JWT_TOKEN_SECRET "$ALLOWED_SERVICES" $JWT_TOKEN_EXPIRES_IN "$API_VERSION" $PORT "$AUTH_DOMAIN" "$VALID_ISSUERS" $TC_EMAIL_SERVICE_URL "$AUTH0_URL" "$AUTH0_AUDIENCE" $AUTH0_CLIENT_ID "$AUTH0_CLIENT_SECRET" $TOKEN_CACHE_TIME $AWS_ECS_CLUSTER $AWS_REGION $AWS_ECS_CLUSTER $ENV)
199194
}
200195

201196
register_definition() {

service/MessageBusService.js

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
* The Message Bus service provides operations to the remote Kafka.
33
*/
44
const createError = require('http-errors')
5-
const Joi = require('joi')
65
const _ = require('lodash')
7-
const config = require('config')
86
const Kafka = require('no-kafka')
97

108
const helper = require('../common/helper')
119

12-
const PlaceholderService = require('./PlaceholdersService')
13-
1410
// Create a new producer instance with KAFKA_URL, KAFKA_CLIENT_CERT, and
1511
// KAFKA_CLIENT_CERT_KEY environment variables
1612
const producer = new Kafka.Producer()
@@ -28,46 +24,9 @@ async function init () {
2824
* @param {Object} event the event to post
2925
*/
3026
async function postEvent (event) {
31-
if (_.has(event, 'message')) {
32-
const message = helper.validateEvent(event)
33-
34-
if (event.type.startsWith('email.')) {
35-
let placeholders
36-
try {
37-
placeholders = await PlaceholderService.getAllPlaceholders(event.type)
38-
} catch (err) {
39-
throw createError.InternalServerError()
40-
}
41-
42-
const keys = _.fromPairs(_.map(placeholders, o => [o, Joi.string().required().min(1)]))
43-
const schema = Joi.object().keys({
44-
data: Joi.object().keys(keys).required(),
45-
recipients: Joi.array().items(Joi.string().email()).min(1).required(),
46-
replyTo: Joi.string().email()
47-
})
48-
const { error } = Joi.validate(message, schema)
49-
if (error) {
50-
throw error
51-
}
52-
}
53-
54-
// Post old structure
55-
const result = await producer.send({
56-
topic: `${config.KAFKA_TOPIC_PREFIX}${event.type}`,
57-
message: {
58-
value: event.message
59-
}
60-
})
61-
// Check if there is any error
62-
const error = _.get(result, '[0].error')
63-
if (error) {
64-
if (error.code === 'UnknownTopicOrPartition') {
65-
throw createError.BadRequest(`Unknown event type "${event.type}"`)
66-
}
27+
// var result
6728

68-
throw createError.InternalServerError()
69-
}
70-
} else if (_.has(event, 'payload')) {
29+
if (_.has(event, 'payload')) {
7130
helper.validateEventPayload(event)
7231

7332
// Post new structure
@@ -86,7 +45,7 @@ async function postEvent (event) {
8645
throw createError.InternalServerError()
8746
}
8847
} else {
89-
throw createError.BadRequest(`Expecting either old (type-message) structure or new (mimetype-payload)`)
48+
throw createError.BadRequest(`Expecting new (mimetype-payload) structure`)
9049
}
9150
}
9251

0 commit comments

Comments
 (0)