feat(notifications): Add SNS msg attributes for service notification filtering (#2412); r=philbooth

This commit is contained in:
Ryan Kelly 2018-04-27 19:46:38 +10:00 коммит произвёл GitHub
Родитель 0204096257
Коммит 0cf1bc402d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 106 добавлений и 25 удалений

Просмотреть файл

@ -28,6 +28,21 @@ if (notifierSnsTopicArn !== 'disabled') {
sns = new AWS.SNS({endpoint: notifierSnsTopicEndpoint, region: region}) sns = new AWS.SNS({endpoint: notifierSnsTopicEndpoint, region: region})
} }
function formatMessageAttributes(msg) {
const attrs = {}
attrs.event_type = {
DataType: 'String',
StringValue: msg.event
}
if (msg.email) {
attrs.email_domain = {
DataType: 'String',
StringValue: msg.email.split('@')[1]
}
}
return attrs
}
module.exports = function notifierLog(log) { module.exports = function notifierLog(log) {
return { return {
send: (event, callback) => { send: (event, callback) => {
@ -36,7 +51,8 @@ module.exports = function notifierLog(log) {
sns.publish({ sns.publish({
TopicArn: notifierSnsTopicArn, TopicArn: notifierSnsTopicArn,
Message: JSON.stringify(msg) Message: JSON.stringify(msg),
MessageAttributes: formatMessageAttributes(msg)
}, (err, data) => { }, (err, data) => {
if (err) { if (err) {
log.error({op: 'Notifier.publish', err: err}) log.error({op: 'Notifier.publish', err: err})

Просмотреть файл

@ -21,38 +21,103 @@ describe('notifier', () => {
log.trace.reset() log.trace.reset()
}) })
it('works with sns configuration', () => { describe('with sns configuration', () => {
const config = { let config, notifier
get: (key) => {
if (key === 'snsTopicArn') { beforeEach(() => {
return 'arn:aws:sns:us-west-2:927034868275:foo' config = {
get: (key) => {
if (key === 'snsTopicArn') {
return 'arn:aws:sns:us-west-2:927034868275:foo'
}
} }
} }
}
const notifier = proxyquire(`${ROOT_DIR}/lib/notifier`, { notifier = proxyquire(`${ROOT_DIR}/lib/notifier`, {
'../config': config '../config': config
})(log) })(log)
notifier.__sns.publish = sinon.spy((event, cb) => { notifier.__sns.publish = sinon.spy((event, cb) => {
cb(null, event) cb(null, event)
})
}) })
notifier.send({ it('publishes a correctly-formatted message', () => {
event: { notifier.send({
stuff: true event: 'stuff'
} })
assert.deepEqual(log.trace.args[0][0], {
op: 'Notifier.publish',
data: {
TopicArn: 'arn:aws:sns:us-west-2:927034868275:foo',
Message: '{\"event\":\"stuff\"}',
MessageAttributes: {
event_type: {
DataType: 'String',
StringValue: 'stuff'
}
}
},
success: true
})
assert.equal(log.error.called, false)
}) })
assert.deepEqual(log.trace.args[0][0], { it('flattens additional data into the message body', () => {
op: 'Notifier.publish', notifier.send({
data: { event: 'stuff-with-data',
TopicArn: 'arn:aws:sns:us-west-2:927034868275:foo', data: {
Message: '{\"event\":{\"stuff\":true}}' cool: 'stuff',
}, more: 'stuff'
success: true }
})
assert.deepEqual(log.trace.args[0][0], {
op: 'Notifier.publish',
data: {
TopicArn: 'arn:aws:sns:us-west-2:927034868275:foo',
Message: '{\"cool\":\"stuff\",\"more\":\"stuff\",\"event\":\"stuff-with-data\"}',
MessageAttributes: {
event_type: {
DataType: 'String',
StringValue: 'stuff-with-data'
}
}
},
success: true
})
assert.equal(log.error.called, false)
})
it('includes email domain in message attributes', () => {
notifier.send({
event: 'email-change',
data: {
email: 'testme@example.com'
}
})
assert.deepEqual(log.trace.args[0][0], {
op: 'Notifier.publish',
data: {
TopicArn: 'arn:aws:sns:us-west-2:927034868275:foo',
Message: '{\"email\":\"testme@example.com\",\"event\":\"email-change\"}',
MessageAttributes: {
email_domain: {
DataType: 'String',
StringValue: 'example.com'
},
event_type: {
DataType: 'String',
StringValue: 'email-change'
}
}
},
success: true
})
assert.equal(log.error.called, false)
}) })
assert.equal(log.error.called, false)
}) })
it('works with disabled configuration', () => { it('works with disabled configuration', () => {
@ -68,7 +133,7 @@ describe('notifier', () => {
})(log) })(log)
notifier.send({ notifier.send({
stuff: true event: 'stuff'
}, () => { }, () => {
assert.deepEqual(log.trace.args[0][0], { assert.deepEqual(log.trace.args[0][0], {
op: 'Notifier.publish', op: 'Notifier.publish',