Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
be86a9e3
Commit
be86a9e3
authored
Sep 21, 2020
by
Vitali Tatarintev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validate PagerDuty payload using json_schemer
Allows to prevent parsing of invalid payload
parent
bbb89ddf
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
5 deletions
+80
-5
app/services/incident_management/pager_duty/process_webhook_service.rb
...incident_management/pager_duty/process_webhook_service.rb
+1
-1
lib/pager_duty/validator/schemas/incident_trigger.json
lib/pager_duty/validator/schemas/incident_trigger.json
+68
-0
lib/pager_duty/webhook_payload_parser.rb
lib/pager_duty/webhook_payload_parser.rb
+8
-2
spec/lib/pager_duty/webhook_payload_parser_spec.rb
spec/lib/pager_duty/webhook_payload_parser_spec.rb
+3
-2
No files found.
app/services/incident_management/pager_duty/process_webhook_service.rb
View file @
be86a9e3
...
...
@@ -34,7 +34,7 @@ module IncidentManagement
strong_memoize
(
:pager_duty_processable_events
)
do
::
PagerDuty
::
WebhookPayloadParser
.
call
(
params
.
to_h
)
.
filter
{
|
msg
|
msg
[
'event'
].
in?
(
PAGER_DUTY_PROCESSABLE_EVENT_TYPES
)
}
.
filter
{
|
msg
|
msg
[
'event'
].
to_s
.
in?
(
PAGER_DUTY_PROCESSABLE_EVENT_TYPES
)
}
end
end
...
...
lib/pager_duty/validator/schemas/incident_trigger.json
0 → 100644
View file @
be86a9e3
{
"type"
:
"object"
,
"required"
:
[
"messages"
],
"properties"
:
{
"messages"
:
{
"type"
:
"array"
,
"items"
:
{
"type"
:
"object"
,
"required"
:
[
"event"
,
"incident"
],
"properties"
:
{
"event"
:
{
"type"
:
"string"
,
"enum"
:
[
"incident.trigger"
,
"incident.acknowledge"
,
"incident.unacknowledge"
,
"incident.resolve"
,
"incident.assign"
,
"incident.escalate"
,
"incident.delegate"
,
"incident.annotate"
]
},
"incident"
:
{
"type"
:
"object"
,
"required"
:
[
"html_url"
,
"incident_number"
,
"title"
,
"status"
,
"created_at"
,
"urgency"
,
"incident_key"
],
"properties"
:
{
"html_url"
:
{
"type"
:
"string"
},
"incindent_number"
:
{
"type"
:
"integer"
},
"title"
:
{
"type"
:
"string"
},
"status"
:
{
"type"
:
"string"
},
"created_at"
:
{
"type"
:
"string"
},
"urgency"
:
{
"type"
:
"string"
,
"enum"
:
[
"high"
,
"low"
]
},
"incident_key"
:
{
"type"
:
[
"string"
,
"null"
]
},
"assignments"
:
{
"type"
:
"array"
,
"items"
:
{
"assignee"
:
{
"type"
:
"array"
,
"items"
:
{
"summary"
:
{
"type"
:
"string"
},
"html_url"
:
{
"type"
:
"string"
}
}
}
}
},
"impacted_services"
:
{
"type"
:
"array"
,
"items"
:
{
"summary"
:
{
"type"
:
"string"
},
"html_url"
:
{
"type"
:
"string"
}
}
}
}
}
}
}
}
}
}
lib/pager_duty/webhook_payload_parser.rb
View file @
be86a9e3
...
...
@@ -2,6 +2,8 @@
module
PagerDuty
class
WebhookPayloadParser
SCHEMA_PATH
=
File
.
join
(
'lib'
,
'pager_duty'
,
'validator'
,
'schemas'
,
'incident_trigger.json'
)
def
initialize
(
payload
)
@payload
=
payload
end
...
...
@@ -19,6 +21,8 @@ module PagerDuty
attr_reader
:payload
def
parse_message
(
message
)
return
{}
unless
valid_payload?
{
'event'
=>
message
[
'event'
],
'incident'
=>
parse_incident
(
message
[
'incident'
])
...
...
@@ -26,8 +30,6 @@ module PagerDuty
end
def
parse_incident
(
incident
)
return
{}
if
incident
.
blank?
{
'url'
=>
incident
[
'html_url'
],
'incident_number'
=>
incident
[
'incident_number'
],
...
...
@@ -62,5 +64,9 @@ module PagerDuty
def
reject_empty
(
entities
)
Array
(
entities
).
reject
{
|
e
|
e
[
'summary'
].
blank?
&&
e
[
'url'
].
blank?
}
end
def
valid_payload?
::
JSONSchemer
.
schema
(
Pathname
.
new
(
SCHEMA_PATH
)).
valid?
(
payload
)
end
end
end
spec/lib/pager_duty/webhook_payload_parser_spec.rb
View file @
be86a9e3
# frozen_string_literal: true
require
'fast_spec_helper'
require
'json_schemer'
RSpec
.
describe
PagerDuty
::
WebhookPayloadParser
do
describe
'.call'
do
...
...
@@ -69,11 +70,11 @@ RSpec.describe PagerDuty::WebhookPayloadParser do
end
end
context
'when payload
has no incidents
'
do
context
'when payload
schema is invalid
'
do
let
(
:payload
)
{
{
'messages'
=>
[{
'event'
=>
'incident.trigger'
}]
}
}
it
'returns payload with blank incidents'
do
is_expected
.
to
eq
([{
'event'
=>
'incident.trigger'
,
'incident'
=>
{}
}])
is_expected
.
to
eq
([{}])
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment